Download presentation
Presentation is loading. Please wait.
Published bysandeep s Modified over 2 years ago
1
Course Data Structures and Applications 18CS32
2
Text Books and Reference Books Text Books 1. Fundamentals of Data Structures in C - Ellis Horowitz and Sartaj Sahni, 2nd edition, Universities Press,2014 2.Data Structures - Seymour Lipschutz, Schaum's Outlines, Revised 1st edition, McGraw Hill, 2014 Reference Books 1. Data Structures: A Pseudo-code approach with C –Gilberg & Forouzan, 2nd edition, Cengage Learning, 2014. 2. Data Structures using c and C++ - Y.Langsam, M.J.Augenstein, A.M. Tenenbaum
3
MODULE 1 Introduction to data structures
4
OutlineOutline Introduction to Data Structures Classification of Data Structures Data structure Operations Pointers and Dynamic Memory Management Functions Review of Arrays, Structures, Self Referential Structures and Unions Representation of linear Arrays in memory Dynamically allocated Arrays Array operations and multidimensional Arrays Polynomial and sparse matrix Strings Programming Examples
5
Terminologies Used Data : Data are simply values or sets of values. Data item : Data items refer to a single unit of values. Data items may be Group items or Elementary item Information: “information” is sometimes used for data with given attributes Collection of data are organized into hierarchy of Field, Record and Files Entity : An entity is something that has certain attributes or properties which may be assigned values Entity set : Entities with similar attributes form an entity set SSNNameAgeSexSalary 101Rama32M30000 105Hari30M20000 106Geetha28F25000
6
Field is a single elementary unit of information representing an attribute of an entity. Record is the collection of field values of a given entity. File is the collection of records of the entities in a given entity set. Primary Keys and Keys : The value in a certain field may uniquely determine the record in the file. Such a field K is called a primary key and the values k1, k2, ….. in such a field are called keys or key values Fixed length Records : All the records contain the data items with the same amount of space assigned to each data item. In variable-length records file records may contain different lengths
7
Introduction What is data type? A data type is a collection of objects (data)and set of operations that act on those objects For example : if data is integer, the operations may beadd, subtract, multiply, divide and any other operations appropriate for the data What is data structures? Data structure is logical or mathematical model of particular organization of data Goal of data structures? For efficient processing, data are organized into some form of structures
8
Classification of data structures DATA STRUCTURES PRIMITIVENON PRIMITIVE NON LINEARLINEAR
9
Primitive and Non primitive DS Primitive data structures are the basic data types Examples: int, float, char, boolean Non primitive data structures are the data structures created using primitive DS Examples: arrays, structures, union, stacks, list, queue etc
10
Linear and Non linear DS Non primitive data structure can be classified into linear and non linear based on how the elements are stored If the values or elements are stored in sequential order, then they are called LINEAR DS Examples: arrays, lists, stack, queue Ifthe values or elements are stored in non sequential order then it is called NON LINEAR DS Examples: trees, graphs
11
Data structure operations Create: declaration and initialization of the data structure Insert: adding new records to the structure Delete: removing a record from the structure Search: finding the location of a particular record or all the records Sort: arranging the records in some logical order Merge: combining the records from two structures into a single structure Traversal: accessing each record exactly once
12
Pointers Pointer is a derived data type that holds the address of memory location of anothervariable That is, the actual value of a pointer type is an address of memory Pointer Concept 1. Pointer constant : It is memory address with in the computer 2. Pointer value : It is address of variable 3. Pointer variable : It is a variable that holds pointer value. Representation of variable :Pointer as variable5000179 int Quantity =179;int *p= &Quantity;50485000
13
Two important operators: & the address operator * the dereferencing (indirection) operator Pointerdeclaration: data_type*pointer_variable_name; Example :int *p; Here * tells the compiler thatp is a pointer variable It can hold the address of (only) an integer variable p needs a memory location Pointer Initilization: inti=10,*p;orint i, *p=&i p=&i; Here &i returns the address of i and assigns it as the value of p Thus p is a pointer to iand *p is value at i Print p gives address of iand *p gives value at i To assign the value to i we can write,i = 10;Or*p = 10;
14
Accessing variable through pointers Two important operators: & the address operator used to find address of varible * the dereferencing (indirection) operator is to get the value at address *p means value at address p &p means address ofp inti=10, *p; p=&i; variablevalue i10 p2000 address 2000 2048
15
Null Pointer We can set a pointer to Null ie, int *p=NULL; When we dereference a NULL pointer, we are using address zero, which is a valid address in computer. If we dereference pointer p we will most likely get run time error which depends on system that we use. The null pointer can be used in relational expression, where it is interpreted as false. Ex: if (pi = = NULL) or if (!pi) Pointers can be Dangerous: Pointer can be very dangerous if they are misused.The pointers are dangerous in following situations: 1. Pointer can be dangerous when an attempt is made to access an area of memory that is either out of range of program or that does not contain a pointer reference to a legitimate object.
16
Example: int main () { int *p; int pa = 10; p = &pa; printf(“%d”, *p); //output = 10; printf(“%d”, *(p+1)); // accessing memory which is out of range }
17
2. It is dangerous when a NULL pointer is de-referenced, because on some computer it may return 0 and permitting execution to continue, or it may return the result stored in location zero, so it may produce a serious error. 3. In some system, pointers have the same size as type int, since int is the default type specifier, some programmers omit the return type when defining a function.The return type defaults to int which can later be interpreted as a pointer.This has proven to be a dangerous practice on some computer and the programmer is made to define explicit types for functions.
18
Pointer Expressions If p1 and p2 are properly declared and initialized pointers, Then the following statements are valid Sum=sum+*p1; P1+4; P1++; Z=5* - *p2/ *p1; p2-2; --p2; Y=*p1* *p2 ; *p2=*p2+10; p1-p2; Sum+=*p2 P1>p2,p1==p2, p1!=p2are valid expressions P1/p2,p1*p2and p1/3p1+p2 are not allowed- illegal
19
Pointer increment and Scale factor When we increment a pointer, its value is increased by the length of data type in bytes it points to. This length is called as scale factor The number of bytes used to store various datatype depends on the system that can be found by using sizeof operator. In IBM PC Character1 byte Integer Float Long int Double 2 bytes 4 bytes 8 bytes Henceif p1 is an integer pointer with initial value say 5000, then p1++ or p1=p1+1 will be 5002 not 5001 and p1+2 will be 5004 if p1 is an float pointer with initial value say 5000, then p1++ or p1=p1+1 will be 5004 not 5001 and p1+2 will be 5008
20
Pointers to PointersPointers to Pointers A variable which contains address of a pointer variable is called pointer-to-pointer
21
Pointers and Function Pointers as function arguments:We can pass address of variable as an argument to function.Then the parameter receivingthe address should be pointer.This process of calling function to pass address of variable and receiving as pointer is known as call by reference Example : void change(int*); int main() { int x=10; change(&x); printf(“%d\n”,x); } void change(int* p) { *p=*P+10; }
22
Pointer to function A pointer to functionis declared asdata type (*fptr)(); Example: float mul(float,float), (*fptr)(); fptr =mul; We can call function mul as (*fptr)(x,y); ormul(x,y);
23
Memory Allocation Procedures Static memory allocation : Allocation of memory during compile time Example: int x, float y; Dynamic memory allocation : Allocation of memory during runtime There are four memory management function in C to support dynamic memory management defined in the header file 1. malloc ( ) 2. calloc ( ) 3. realloc ( ) 4.free ( )
24
Dynamic memory allocation Process Conceptual view of storage of C program in memoryis shown below when ever you need a new area of memory, you may call a function malloc () request the amount of memory you needed If the memory is available in the heap, a pointer to the start of an area of memory of the requested size is returned.If the requested memory is not available the pointer NULL is returned. If memory allocated is no longer required you can throw back to heap using a function free(). Thus size of heap shrinks and grows during program execution. Local Variable - Stack Free memory --Heap Global variable-Permanent storage area C- Program Instructions –Permanent storage area
25
malloc(size) This function allows the program to allocate single block of memoryof requested size as and when required and the exact amount needed during execution. Syntax Declaration: void * malloc (size_t size); Function call ptr=(data_type *)malloc(size); ptr->is a pointer variable of type data type. data_type->can be any basic data type or user defined data type size->no: of bytes required.
26
Example #include #include int main() { int *pi; float *pf; pi=(int *)malloc(sizeof(int)); pf=(float *)malloc(sizeof(float)); *pi=1024; *pf=3.14; printf(“an integer=%d,a float=%f\n”,*pi,*pf); free(pi); free(pf); }
27
Macro to invoke malloc Macro substitution is a process where an identifier in a program is replaced by a predefined string composed of one or more tokens # define MALLOC(P,S)\ If((P=malloc(S))==NULL){\ printf(“Insufficient memory”);\ Exit(1);\ }also May be written as # define MALLOC(P,S)\ If(!(P=malloc(S))){\ printf(“Insufficient memory”);\ Exit(1);\ } Then malloc can be invoked using code MALLOC(pi,sizeof(int));
28
calloc(n,size) Function is used to allocate multiple blocks of memory. Calloc stands for contiguous allocation of multiple blocksis mainly used to allocate memory for arrays. The number of blocks is determined by first parameter n. syntax: Prototype Declaration: void * calloc(size_t elementcount, size_t element_size ) ; Function call ptr=(data_type *)calloc(n,size); ptr->is a pointer variable of type data_type. data_type->basic/user defined data type. n->no:of blocks to be allocated. size->is the no: of bytes in each block. Example: ptr=(int *)calloc(5,sizeof(int)).
29
calloc vs malloc calloc is a function for dynamic memory allocation in C language stdlib.h header file that allocates a specific number of bytes and initializes them to zero.C language malloc is a function for dynamic memory allocation in C language stdlib.h header file that allocates a specific number of bytes. Meaning calloc stands for contiguous allocation.malloc stands for memory allocation. Syntax calloc follows a syntax similar to void *calloc(size_t_num, size_t size);malloc follows a syntax similar to void *malloc(size_t_size);. Number of Arguments calloc takes two arguments. They are a number of blocks and size of each block.malloc takes one argument. It is a number of bytes. Speed calloc takes little longer than malloc. That is because of the extra step of initializing the allocated memory by zero. malloc is faster than calloc.
30
realloc(ptr,size) Before using this function,the memory should have been allocated using malloc(),calloc(). Sometimes,the allocated memory may not be sufficient and we may requires additional memory space. Sometimes allocated memory may be much larger and we want to reduce the size of allocated memory. In both of the above situations,the size of allocated memory can be changed using realloc() and the process is called reallocation of memory. Syntax: Prototype Declaration : void * realloc( void * ptr, size_t newsize); Function call : ptr=(data_type *)realloc(ptr,size) Example: ptr=(int *)realloc(ptr,1*sizeof(int)) ptr=(int *)realloc(ptr,4);
31
free(ptr) This function is used to de-allocate or free the allocated block of memory which is allocated by using the functions calloc(),malloc() and realloc() Prototype declaration void free(void * ptr); Example: #include free(ptr); ptr=NULL;
32
Arrays Array is a collection of items of the same data types that share a common name. Each data items can be accessed using the same name but different index value. General form : data_typearray_name[size]; An array is a set of pairs,, such that each index has a value associated with it. It can be called as corresponding or a mapping Ex: list[0]=25 list[1]=15 list[2]=20 list[3]=17 list[4]=35 Here, list is the name of array. By using, list [0] to list [4] the data items in list can be accessed.
33
Arrays in C A one dimensional array in C is declared by adding brackets to the name of a variable.
34
Examples int list[5], *plist[5]; int list[5]: Means list is an array of five integers list[0], list[1], list[2], list[3], list[4] int *plist[5]: Means plist is an array of five pointers to integers plist[0], plist[1], plist[2], plist[3], plist[4] int(*plist)[5] : Means plist is pointer to array of 5 integers. implementation of 1-D array list[0] list[1] list[2] list[3] list[4] base address = +1* sizeof(int) + 2*sizeof(int) + 3*sizeof(int) + 4*size(int )
35
Implementation: When the complier encounters an array declaration, list[5], it allocates five consecutive memory locations. Each memory is enough large to hold a single integer. The address of first element of an array is called Base Address. Ex: For list[5] the address of list[0] is called the base address. If the memory address of list[i] need to compute by the compiler, then the size of the int would get by sizeof (int), then memory address of list[i] is as follows: list[i] = α + i * sizeof (int)Where, α is base address. For example, an array of 10 integer variables, with indices 0 through 9, may be stored as 10 words at memory addresses 2000, 2004, 2008, 2036, so that the element with index i has the address 2000 + 4× i.
36
Compare int *list1 and int list2[5] in C. list1 and list2 are pointers. Difference: list2 reserves five locations. Notations: list2 is a pointer to list2[0] (list2 + i) is a pointer to list2[i], that is&list2[i] *(list2 + i)is nothing butlist2[i]
37
Example: 1-dimension array addressing Let int one[ ] = {0, 1, 2, 3, 4}; To printaddress and value Function call is print1(one,5); void print1(int *ptr, int size) { /* print out a one-dimensional array using a pointer */ int i; printf(“AddressContents\n”); for (i=0; i < size; i++) printf(“%u%d\n”, ptr+i, *(ptr+i)); printf(“\n”); }
38
AddressContents 12280 12301 12322 12343 12364
39
Address Calculation in single (one) Dimension Array: An element of an array say ―A[ i ] ‖ is calculated using the following formula: Address of A [i ] = B +W * ( i – LB ) Where, B = Base address W = Storage Size of one element stored in the array (in byte) i = Subscript of element whose address is to be found LB = Lower limit / Lower Bound of subscript, if not specified assume 0 (zero)
40
Example: Given the base address of an array B[1300…..1900] as 1020 and size of each element is 2 bytes in the memory. Find the address of B[1700]. Solution: The given values are: B = 1020, LB = 1300, W = 2, I = 1700 Address of A [ I ] = B +W * ( I – LB ) = 1020 + 2 * (1700 – 1300) = 1020 + 2 * 400 = 1020 + 800 = 1820 [Ans]
41
Address Calculation in Double (Two) Dimensional Array: While storing the elements of a 2-D array in memory, these are allocated contiguous memory locations.Therefore, a 2-D array must be linearized so as to enable their storage.There are two alternatives to achieve linearization: Row-Major and Column-Major.
42
C allows for arrays of two or more dimensions. A two-dimensional (2D) array is an array of arrays. A three- dimensional (3D) array is an array of arrays of arrays. In C programming an array can have two, three, or even ten or more dimensions. The maximum dimensions a C program can have depends on which compiler is being used. More dimensions in an array means more data be held, but also means greater difficulty in managing and understanding arrays.
43
How to Declare a Multidimensional Array in C A multidimensional array is declared using the following syntax: type array_name[d1][d2][d3][d4]………[dn]; Where each d is a dimension, and dn is the size of final dimension. Examples: 1. int table[5][5][20]; 2. float arr[5][6][5][6][5]; In Example 1: int designates the array type integer. table is the name of our 3D array. Our array can hold 500 integer-type elements.This number is reached by multiplying the value of each dimension. In this case: 5x5x20=500.
44
In Example 2: Array arr is a five-dimensional array. It can hold 4500 floating-point elements (5x6x5x6x5=4500). When it comes to holding multiple values, we would need to declare several variables. But a single array can hold thousands of values. Explanation of a 3D Array A 3D array is essentially an array of arrays of arrays: it's an array or collection of 2D arrays, and a 2D array is an array of 1D array.
45
Diagram below shows a 3D array representation: 3D Array Conceptual View 3D array memory map.
46
struct Student { int Roll_No; char name[10]; char department[5[; float GPA; }; struct student s1,s2,s3,s4,s5;
47
Structures Structureis a collection of data items, where each item can be of same or different data type. Consider a Student data Data are of not same type. In such case we define data type called structure Roll_NoNameDepartmentGPA 1RaviCSE8.5
48
Three ways of defining structure and Declaring variables Structurewithtag _name Structurewith out tag _name Structure with typedef statement
49
Structurewith tag _name -Syntax with Example Syntax : struct tag_name { data-type member_1; data-type member_2;. data-type member_n; } ; Struct tag_name variable name/s; Example : struct Employee { char name[10]; int age; float salary; }; struct Employee E1,E2; Note: E1 and E2 are structure variables
50
Structurewithouttag _name - Syntax with Example Syntax : struct { data-type member_1; data-type member_2;. data-type member_n; } variable name/s; Example : struct { char name[10]; int age; float salary; } E1,E2; Note: E1 and E2 are structure variables
51
Structure with typedef statement - Syntax with Example Syntax : typedef struct { data-type member_1; data-type member_2;. data-type member_n; } NAME; NAME variable name/s; Example : typedef struct { char name[10]; int age; float salary; } EMPLOYEE; EMPLOYEE E1,E2; Note: E1 and E2 are structure variables
52
Accessing members of structures To access he members of the structure dot (.) operator is used. Syntax:variable name. member_name For the employee structure shown above E1 and E2 are structure variables and values to the members may be assigned as given below strcpy(E1.name, “ravi”); E1.age = 30; E1.salary = 50000; These values may be accessed in the program as and when required using the same syntax in the RHS of expression
53
#include struct student { int Rollno; char name[10]; char dept[5]; float GPA; }; int main() { int i; struct student s1; printf("enter student details\n"); scanf("%d%s%s%f",&s1.Rollno,s1.name,s1.dept,&s1.GPA); printf("Student information\n"); printf("Roll No->%d\nName->%s\ndept->%s\nGPA->% f", s1.Rollno, s1.name, s1.dept,s1.GPA); }
54
Array of structures It is also possible to define an array of structures that is an array in which each element is a structure. The procedure is shown in the following example: struct student { char name [80]; int roll_no ; float marks ; } s [100]; In this declaration s is a 100- element array of structures. It means each element of s represents an individual student record.
55
Structure Operation Comparison if (strcmp(E1.name,E2.name)) printf(“Employees names are not same”); Else printf(“Employees names are same”); Assignment ANSI C permits assignment of structure variables of same structures, ie, E1=E2; is valid operation if both E1 and E2 are variables of same structure Checking for Equality If(E1==E2) is not supported in C
56
Structure Operation 1. Structure Equality Check: Here, the equality or inequality check of two structure variable of same type typedef struct { char name[10]; int age; float salary; }humanBeing; humanBeing person1, person2; if (person1 = = person2) is invalid. The valid function is shownnext
57
#define FALSE 0 #define TRUE 1 if (humansEqual(person1,person2)) printf("The two human beings are the same\n"); else printf("The two human beings are not the same\n"); int humansEqual(humanBeing person1, humanBeing person2) { /* return TRUE if person1 and person2 are the same otherwise return FALSE */ if (strcmp(person1.name, person2.name)) return FALSE; if (person1.age != person2.age) return FALSE; if (person1.salary != person2.salary) return FALSE; return TRUE; }
58
Structure within structure There is possibility to embed a structure within a structure. There are 2 ways to embed structure. typedef struct { int month; int day; int year; } DATE; typedef struct { char name[10]; int age; float salary; DATE dob; } HUMAN_BEING; HUMAN_BEING person1; person1.dob.month = 2; person1.dob.day = 11; person1.dob.year = 1944;
59
Self Referential Structures One or more of its components is a pointer to itself. typedefstruct { char data; struct list *link; } list ; list item1, item2, item3; item1.data=„a ‟ ; item2.data=„b ‟ ; item3.data=„c ‟ ; item1.link = item2.link = item3.link = NULL; Construct a list with three nodes item1.link=&item2; item2.link=&item3; malloc: obtain a node abc
60
Union Union is the concept borrowed from structure. Union declaration, initialization and accessing the members is same as that of structure. The difference is only the keyword is union and the fields of the union share the memory location. That is only one field of union is active at any given time. The memory allocation for union variable is the amount of storage necessary to represent the largest component. Example : union code { int m; char c; float f; }c1 ; Allocates only 4 bytes to hold the float f value.The same location is used by the other members in different instances. It is not possible to use all the members at the same time.
61
Memory allocation procedure in union 4 bytes cmcm f
62
Application of union for saving the memory typedef struct { enum tagField {female, male} sex; union { int children; int beard; }u; }sextype; typedef struct { char name[10]; int age; sextype sexinfo; }humanBeing; humanBeing person1, person2;
63
We could assign values to person1 and person2 as follows: person1.sexinfo.sex = male; person1.sexinfo.u.beard = FALSE; And person2.sexinfo.sex = female; person2.sexinfo.u.children = 4;
64
Internal implementation of structures Consider the structure struct { int i; int j; float a; float b;}code1,code2; Values will be stored in the same way using increasing addresses in the order specified in the structure notation. But structures must begin and end on the same type of memory boundary. For an even byte boundary address is multiple of 4, 8or16. Since variable code1 takes only 12 bytes the remaining 4 bytes are padded with zeros ( holes ) so that the next variable code2 value starts from the 16th byte.
65
Pointer to Structure Example : struct Employee { char name[10]; int age; float salary; } ; structEmployee E1, *ptr ; ptr=&E1; Then ptr will point to E1. Members can be accessed using arrow operator ie, ptr->age is same as E1.age ptr->age same as that (*ptr).age Note:Operators ->,. And [] enjoys highest priority among operators.
67
Linear Arrays A linear Array is a list of a finite number n of homogeneous data elements which are referenced by an index set consisting of n consecutive numbers. The number of data elements of the array is known as length or size of array that can be obtained from the index set by the formula Length = UB-LB+1 where UB is largest index and LB is smallest index. The elements of array A may be denoted by A 1,A 2,A 3,……,A n in subscript notation, A(1),A(2),A(3),……..A(N) in parenthesis notation used in FORTRAN,PL/1 and BASIC, A[1],A[2],A[3],……..A[N] in bracket notation used in PASCAL,C and C++where A[K] is called as subscripted variable and K is subscript or index.
68
Examples to Linear Arrays Each Programming Language has its own rules for declaring the arrays. Suppose DATA is a 6-element linear array containing real values. Suppose AUTO is an Integer Array with LB=1932 and UB=1984 Programming LanguageDeclaration FORTRANREAL DATA(6) PL/1DECLARE DATA(6) FLOAT PASCALVAR DATA: ARRAY[1…6]OF REAL CFloat DATA[6] Programming LanguageDeclaration FORTRAN 77INTEGER AUTO(1932:1984) PL/1DECLARE AUTO(1932:1984) FIXED PASCALVAR AUTO: ARRAY[1932…1984] of INTEGER CNot possible directly
69
Representation of Linear Arrays in memory Let LA be the linear arrays in memory of Computer. LOC(LA[K]) is address ofelement LA[K] of Array LA Address of first element of LA is known as Base address Base(LA) and computer calculates the address of any element of LA by the formula LOC(LA[K])=Base(LA)+w(K- lower bound) where w is number of bytes per word. For example : In an array declared in C as int average[5] = {8,3,10,2,4}Array index starts at zero,The base address is & average[0] and let it be 2000 Then address of average[3]is 2000+2*(3-0) =2006 Note: Time to access LOC(LA[K]) is same for any value of K
70
Array - Operations Traverse − print all the array elements one by one. Insertion − add an element at given index. Deletion − delete an element at given index. Search − search an element using given index or by value. Update − update an element at given index. Merge – combining the elements from two arrays into a single structure Sort – arranging the records in some logical order
71
Traversing Algorithm: TRAVERSING(LA, N,LB,UB) Here LA is linear Array with N elements, UB is largest index and LB is smallest index and PROCESS is an operation we apply as indication of visited. 1.[Initialize counter] set K:=LB 2.Repeat steps 3 and 4 while K<=UB 3. [Visit element] Apply PROCESS to LA[K] 4. [Increase counter] Set K:=K+1 [End of Step 2 loop] 5.EXIT
72
Alternate form of Traversal Algorithm Algorithm: TRAVERSING(LA, N,LB,UB) Here LA is linear Array with N elements, UB is largest index and LB is smallest index and PROCESS is an operation we apply as indication of visited. 1. Repeat for K=LB to UB Apply PROCESS to LA[K] [End of loop] 2.EXIT
73
Insertion Insert operation is to insert one or more data elements into an array. New element can be added – At the beginning At the end or At any given index of array.
74
Insertion at the beginning.????? 1020304050 012345 …. 199
75
Insertion at the end.????? 1020304050 012345 …. 199
76
Insertion at a given index 012345 …..????? 10 2030 4050 let index be 2 199
77
Algorithm INSERT(LA,N,K,ITEM) Here LA is linear Array[0…N-1] with N elements and K is the position to insert the element such that K<=N and ITEM is the element to be inserted in Kth position 1. [Initialize counter] Set J:= N-1 2. Repeat steps 3 and 4 whileJ >=K 3. [Move Jth element Downwards]Set LA[ J+1 ]=LA[ J ] 4. [Decrease counter] Set J = J-1 [End of step 2 loop] 5.[Insert Element] Set LA[ K ] = ITEM 6. [Reset N] Set N = N+1 7. EXIT
78
Deletion Element can be deleted – From the beginning At the end or At any given index of array.
79
Deletion : front of the array …..????? 1020304050 012345
80
Deletion : end of the array …..????? 1020304050 012345
81
Deletion : at given index …..????? 1020304050 012345
82
Algorithm DELETE(LA,N,K,ITEM) Here LA is linear Array[0…N-1] with N elements and K is the position to delete the element such that K<=N-1 and ITEM is the element deleted from Kth position 1. Set ITEM:=LA[K] 2. Repeat for J =K to N-2 3. [Move J + 1 st element Upwards]Set LA[ J ]=LA[ J +1 ] [End of loop] 4.[Reset N] Set N = N-1 5.EXIT
83
bubble sort Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair-wise comparisons and swapping CS 307 Fundamentals of Computer Science 83 77 4235 12 101 5 123456
84
bubble sort Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair-wise comparisons and swapping CS 307 Fundamentals of Computer Science 84 5 12 35 101 123456 77 Swap 42 4277
85
bubble sort Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair-wise comparisons and swapping CS 307 Fundamentals of Computer Science 85 5 12 42 101 123456 77 Swap 35 35 77
86
bubble sort Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair-wise comparisons and swapping CS 307 Fundamentals of Computer Science 86 5 35 42 101 123456 77 Swap 12 1277
87
bubble sort Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair-wise comparisons and swapping CS 307 Fundamentals of Computer Science 87 42 3512 77101 5 123456 No need to swap
88
bubble sort Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair-wise comparisons and swapping CS 307 Fundamentals of Computer Science 88 77 1235 42 123456 101 Sw ap 5 5 101
89
bubble sort Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair-wise comparisons and swapping CS 307 Fundamentals of Computer Science 89 123456 42 3512 77 5 101 Largest value correctly placed
90
Sorting algorithm BUBBLE ( DATA, N) DATA is an array with N elements.This algorithm sorts the elements in DATA 1. Repeat step 2 and 3 for K=1 to N-1 2. Set PTR:=1 [initialize pass pointer PTR] 3. Repeat while PTR <= N-K [ execute pass] If DATA [PTR] > DATA[PTR +1], then interchange DATA[PTR] and DATA[PTR +1] [end of if structure] set PTR := PTR+1 [End of inner loop] [End of step 1 outer loop] 4.Exit
91
Sort 2010405 Initial
92
Sort 20 > 10 405 First pass
93
Sort 10 > 20 540 Second pass
94
Sort 10 >5>5 2040 Third pass
95
Complexity of Bubble sort algorithm can be found by counting number of comparison made = (n-1)+(n-2)+(n-3)+……..+2+1=n(n-1)/2 =n*n= n 2
96
Searching Let DATA be a collection of data elements in memory. Suppose a specific ITEM of information is given. Searching refers to the operation of finding the location LOC of ITEM in DATA, or printing some message that ITEM present/ does not appear there. The search is said to be successful if ITEM does appear in DATA and unsuccessful otherwise.Two approaches of searching are 1.Linear search 2.Binary search Linear Search:Suppose DATA is a linear array with n elements. One of the way to search for a given ITEM in DATA is to compare ITEM with each element of DATA one by one.That is, first test whether DATA [l] = ITEM, and then test whether DATA[2] = ITEM, and so on. This method, which traverses DATA sequentially to locate ITEM, is called linear search or sequential search.
97
Search How to search…..????? 1020304050 012345 KEY to be searched is 30 30 =
98
Linear Search LINEAR ( DATA, N, ITEM, LOC) DATA is an array with N elements. ITEM is the key to be searched. Algorithm finds location LOC of ITEM in DATA or sets LOC = -1 if the search is unsuccessful 1. [Insert ITEM at the end of DATA] Set DATA[N+1]:= ITEM 2. [initialize counter] Set LOC := 1 3. [search for ITEM] Repeat while DATA [ LOC ] ≠ ITEM Set LOC:=LOC+1 [End of loop] 4.If LOC = N + 1 Set LOC := -1 5.Return LOC
99
Complexity of the Linear Search Algorithm Worst Case:The worst case occurs when one must search through the entire array DATA, i.e., when ITEM does not appear in DATA or ITEM is last element in the given list. In this case, the algorithm requires comparisonsf(n) = n + 1 or f(n)=n respectively.Thus, in the worst case, the running time is proportional to n. Average Case:The average number of comparisons required to find the location of ITEM is approximately equal to half the number of elements in the array.f(n)= (+1)/2 Best Case :The Best case Complexity=1
100
Binary Search- Procedure Suppose DATA Array is sorted, then to find Location of ITEM in DATA array binary search is an efficient searching algorithm List DATA[LB:UB] is single sorted segment, ITEM is to be searched, Set BEG=LB and END=UB Step1:While BEG<=END find mid=(BEG+END)/2, Compare DATA[mid] with ITEM to be searched There are 3 possible cases 1. If DATA[mid] equals to ITEM then set LOC=mid and display message- Item found at LOC position and return LOC 2. IfITEM<DATA[mid] setEND=mid-1 repeat step 1 3. IfITEM>DATA[mid] set BEG=mid+1 repeat step 1 Step 2: LOC=-1,Display-Item not found and return LOC
101
Binary Search Let ITEM=31, BEG=0,END= 9
102
ITEM=31, BEG=0,END= 9 mid=(BEG+END)/2=4,
103
ITEM=31, mid=4 ITEM>a[mid]HenceBEG=mid+1=5, END= 9
104
ITEM=31,BEG=5,END= 9 mid=(BEG+END)/2=7
105
ITEM=31, mid=7 ITEM<a[mid]HenceEND=mid-1=6,BEG= 5,
106
ITEM=31,BEG=5,END= 6 mid=(BEG+END)/2=5
107
ITEM=31,mid=5 ITEM=a[mid], Key found at location 5
108
BINARY SEARCH BINARY ( DATA, LB,UB, ITEM, LOC) DATA is an sorted array with lower bound LB and upper bound UB and ITEM is the key to be searched. The variable BEG and END and MID denotes the beginning, end and middle location of segment of elements of DATA respectively. Algorithm finds location LOC of ITEM in DATA or sets LOC = NULL if the search is unsuccessful 1. [initialize segment variables] Set BEG := LB, END := UB 2. Repeat steps 3 to 5 while BEG <= END 3. Set MID := INT((BEG +END)/2) 4. if DATA[MID]= =ITEM then Set LOC=MID return LOC [End of if structure] 5.If ITEM < DATA[MID] then Set END := MID-1 else Set BEG := MID +1 [End of if structure] [End of step 2loop] 6.Set LOC:= NULL 7.Return LOC
109
Complexity of Binary search The running time for the worst case isLog 2 n One can also show that the running time for the average case is approximately equal to the running time for the worst case that is Log 2 n For Best case running time=1
110
Merge 10302040
111
DYNAMICALLY ALLOCATED ARRAYS One Dimensional Array If we cannot determine how large an array to use, we may defer this decision to run time and allocate the array dynamically. Example: intn, *list; printf(“Enter the number of numbers in array”); scanf(“%d”, &n); if((list=(int*)malloc(n*size of(int))==Null) { printf (“No memory to allocate”); exit(0); } //The programs fails only wheninsufficient memory to hold the list of numbers.
112
Two Dimensional Arrays C uses array-of-arrays representation to represent a multidimensional array. The two dimensional arrays is represented as a one-dimensional array in which each element is itself a one-dimensional array. Example: int x[3][5]; Consider Array-of-arrays representation of mXn matrix C find element x[i][j] by first accessing the pointer in x[i]. Where x[i] = α+w*n*(i-LB), which give the address of the zeroth element of row i of the array. Then adding w*(j-LB) to this pointer ( x[i] ), the address of the [j]th element of row i is determined. x[i][j] = x[i]+ w*(j-LB)
113
Creation of Two-Dimensional Array Dynamically int **myArray; myArray = make2dArray(3,5); myArray[2][3]=6; int ** make2dArray(int rows, int cols) { /* create a two dimensional rows X cols array */ int **x, i; x=malloc( rows * sizeof (*x)); /*get memory for row pointers*/ for (i= 0;i<rows; i++) /* get memory for each row */ x[i]=malloc( cols * sizeof(**x)); return x; } The second line allocates memory for a 3 by 5 two-dimensional array of integers and the third line assigns the value 6 to the [2][3] element of this array.
114
Multidimensional arrays Two-dimensional arrays A two dimensional m x n array A is a collection of m * n data elements such that each element is specified by a pair of integers, called subscripts, with the property that, 1<= J<=m and 1<=K<=n Two dimensional arrays are called matrices or tables or matrix arrays
115
Suppose A is a two dimensional m x n array, the first dimension of A contains the index set 1…….m, with lower bound 1 and upper bound m The second dimension of A contains the index set 1…..n, with lower index 1 and upper index n m X n is called the size of the array Some programming languages allow to define multidimensional arrays in which lower bound is not 1 ( non- regular arrays) Length of the dimension can be obtained from the formula Length = upper bound – lower bound +1
116
Representation of 2D arrays in memory Array will be represented in memory by a block of m.n sequential memory locations The programming language will store the array A either column-major order or row major order Storing the elements column by column is column major, Storing the elements row by row is row major order
118
Address of any element A[ J,K ] can be calculated as follows Row-major LOC( A[ J, K ]) = base(A) + w ( N( J-LB ) + ( K-LB ) ) Column major LOC( A[ J, K ]) = base(A) + w ( M( K-LB ) + ( J-LB ))
119
General multidimensional arrays An- dimensional array B is m1Xm2Xm3X……Xmn is a collection of m1.m2.m3…..mn data elements. For example B[2][4][3] is a 3 dimensional array with 2*4*3=24 data elements is shown below
121
POLYNOMIALS “A polynomial is a sum of terms, where each term has a form a i x ei, where x is the variable, a i is the coefficient and ei is the exponent an integer >=0.” Two example polynomials are: A(x) =3x 20 + 2x 5 + 4 B(x) =x 4 + 10x 3 + 3x 2 +1 The largest (or leading) exponent of a polynomial is called its degree. Coefficients that are zero are not displayed. The term with exponent equal to zero does not show the variable since x raised to a power of zero is 1. Assume there are two polynomials, A(x) = Σ a i x i B (x) =Σ b i x i then: A(x) + B(x) = Σ (a i + b i ) x i A(x).B(x) = Σ (a i x i. Σ (b i x i ))
122
Polynomial Representation A Polynomial may be sparse or dense polynomial If number of non zero coefficient is small relative to degree of Polynomial, it is called as sparse polynomial. Example : A(x) =3x 20 + 2x 5 + 4 If number of non zero coefficient is large relative to degree of Polynomial, it is called as dense polynomial. Example : B(x) =x 4 + 10x 3 + 3x 2 +1 Polynomial may be represented using structures.There are two approaches 1.#define MAX-DEGREE 101 /*Max degree of polynomial+1*/ typedef struct { int degree; float coef [MAX-DEGREE]; } polynomial; polynomial a,b; Now a and b are two polynomials of degree n < MAX_DEGREE.This representation is suitable for dense polynomial and will waste lot of space for sparse polynomial.
123
Polynomial Addition LetA(x) =3x 6 + 2x 5 +2x 2 + 4 B(x) =x 4 + 10x 3 + 3x 2 +1 ThenD(x)=A(x)+B(x)= 3x 6 + 2x 5 + x 4 + 10x 3 + 5x 2 + 5 data structure : #define MAX-DEGREE 101 /*Max degree of polynomial+1*/ typedef struct { int degree; float coef [MAX-DEGREE]; } polynomial; polynomial a,b;
124
Function to add two Polynomials Function IsZero(poly)return TRUE if Polynomial has no terms. Function Coef(poly, Lead_exp(Poly))return coefficient of Polynomial of its largest expon Function Lead_Exp(poly) return degree of polynomial Function Attach(d, coef(b,Lead_Exp(b)), Lead_exp(b)) append term of b to d Function remove(b,Lead_Exp(b)) remove specified term from poly b /* d =a + b, where a, b, and d are polynomials */ d = Zero( ) while (! IsZero(a) && ! IsZero(b)) do { switch COMPARE (Lead_Exp(a), Lead_Exp(b)) { case -1: d = Attach(d, Coef (b, Lead_Exp(b)), Lead_Exp(b)); b = Remove(b, Lead_Exp(b)); break; case 0: sum = Coef (a, Lead_Exp (a)) + Coef ( b, Lead_Exp(b)); if (sum) { Attach (d, sum, Lead_Exp(a)); a = Remove(a, Lead_Exp(a)); b = Remove(b, Lead_Exp(b)); }
125
case 1: d = Attach(d, Coef (a, Lead_Exp(a)), Lead_Exp(a)); a = Remove(a, Lead_Exp(a)); } insert any remaining terms of a or b into d advantage: easy implementation disadvantage: waste space when sparse
126
Sparse Polynomial Representation An alternate representation that uses only one global array, terms to store all polynomials That is, it uses array of structures.The C declarations needed are: #define MAX_TERMS 100 /*size of terms array*/ typedef struct { float coef; int expon; } polynomial; polynomial terms[MAX-TERMS];int avail = 0; Consider the two polynomials : A(x) = 2x l000 + 1 andB(x) = x 4 + 10x 3 + 3x 2 + 1 These Polynomials are stored in array as given below
127
A(x) = 2x l000 + 1 B(x) = x 4 + 10x 3 + 3x 2 + 1 D(x) = 2111031 10000432 Start A Finish A Start BAvail Finish B Coef Expon 012345678910111213
128
Polynomial Addition C function is written that adds two polynomials, A and B to obtain D =A + B. To produce D (x), padd( ) is used to add A (x) and B (x) term by term. Starting at position avail, attach( ) which places the terms of D into the array, terms. void padd(int startA, int finishA, int startB, int finishB, int *startD,int *finishD) { /* add A(x) and B(x) to obtain D(x) */ float coefficient; *startD = avail; while (startA <= finishA && startB <= finishB) switch(COMPARE(terms[startA].expon, terms[startB].expon)) { case -1: attach (terms [startB].coef, terms[startB].expon);// a expon < b expon start B++; break; case 1: attach (terms [startA].coef, terms[startA].expon); // a expon > b expon start A++; break;
129
case 0: coefficient = terms[startA].coef + terms[startB].coef; if (coefficient) attach (coefficient, terms[startA].expon); startA++; startB++; } /* add in remaining terms of A(x) */ for(; startA <= finishA; startA++) attach (terms[startA].coef, terms[startA].expon); /* add in remaining terms of B(x) */ for( ; startB <= finishB; startB++) attach (terms[startB].coef, terms[startB].expon); *finishD = avail-1; }
130
void attach(float coefficient, int exponent) { /* add a new term to the polynomial */ if (avail >= MAX-TERMS) { fprintf(stderr,"Too many terms in the polynomial\n"); exit(EXIT_FAILURE); } terms[avail].coef = coefficient; terms[avail++].expon = exponent; }
131
Analysis of padd( ): The number of non-zero terms in A and B is the most important factors in analyzing the time complexity. Let m and n be the number of non-zero terms in A and B, If m >0 and n > 0, the while loop is entered. Each iteration of the loop requires O(1) time. At each iteration, the value of startA or startB or both is incremented. The iteration terminates when either startA or startB exceeds finishA or finishB. The number of iterations is bounded by m + n -1 The time for the remaining two for loops is bounded by O(n + m) because we cannot iterate the first loop more than m times and the second more than n times. So, the asymptotic computing time of this algorithm is O(n +m).
132
Matrix-Representation A matrix contains m rows and n columns of elements We write m x n (read "m by n") to designate a matrix with m rows and n columns. The total number of elements in such a matrix is m.n. If m equals n, the matrix is square. Sparse matrix :A matrix in which number of non-zero elements is relatively less in a matrix. Dense matrix : A matrix in which number of zero elements is relatively less in a matrix.
133
Dense and Sparse Matrix 00 00 0 0 00 0 0 00 0 0 row0 row1 row2 row3 row4 row5 col1col2col3 1500220 15 11300 00 60 0000 910000 0028000 col4col5col6 sparse matrix Dense matrix
134
Matrix Representation In general Matrices are represented by a 2D array. But there is wastage of space if 2D array representation is used for sparse matrix. Hence sparse matrix is represented as array of triples. In triples representation, each element is characterized by The triples should be in ascending order of rows and column in the row should be in ascending order within that row. To know the number of rows, number of columns and number of non- zero elements in the matrix, thisinformationis also storedin triple representation That is if “a” is array of triples a[0].row contains the number of rows, a[0].col contains the number of columns and a[0].value contains the total number of nonzero entries.
135
Sparse Matrix Representation- Data structures #define MAX_TERMS 101 /* maximum number of terms +1*/ typedef struct { int col; int row; int value; } term; term a[MAX_TERMS];
136
Triple representation of sparse matrix 00 00 0 0 00 0 0 00 0 0 15 row0 row1 row2 row3 row4 row5 col1col2col3col4col5col6 1500220 11300 00 60 0000 910000 0028000 < rowcolumnValue>
137
Transposing a Matrix To transpose a matrix, interchange the rows and columns.This means that each element a[i][j] in the original matrix becomes element a[j][i] in the transpose matrix. A good algorithm for transposing a matrix: for each row i take element and store it as element of the transpose; If we process the original matrix by the row indices it is difficult to know exactly where to place element in the transpose matrix until we processed all the elements that precede it. This can be avoided by using the column indices to determine the placement of elements in the transpose matrix.This suggests the following algorithm: for all elements in column j place element in element The columns within each row of the transpose matrix will be arranged in ascending order.
138
Transpose of sparse matrix < rowcolumnValue> Sparse matrix stored as triple Transpose matrix stored as triple
139
void transpose (term a[], term b[]) { /* b is set to the transpose of a */ int n, i, j, currentb; n = a[0].value; /* total number of elements */ b[0].row = a[0].col; /* rows in b = columns in a */ b[0].col = a[0].row; /* columns in b = rows in a */ b[0].value = n; if (n > 0) { currentb = 1; for (i = 0; i < a[0].col; i++) for (j= 1; j<=n; j++) if (a[j].col == i) { b[currentb].row = a[j].col; b[currentb].col = a[j].row; b[currentb].value = a[j].value; currentb++; }
140
Complexity is O(Columns. Elements) =(column.row.column) Where as if we use 2D representation Complexity is O(row.columns) Hence we go for another algorithm called as fast trnspose algorithm in case of triple representation of matrix.
141
STRING String: A finite sequence of zero or more Characters Length: The number of characters in a string Empty or Null String: The string with zero characters. Concatenation: Let S1 and S2 be the strings.The string consisting of the characters of S1 followed by the character S2 is called Concatenation of S1 and S2, denoted by S1//S2 Ex:„THE ‟ // „END ‟ = „THEEND ‟ „THE ‟ // „ ‟ „// „END ‟ = „THE END ‟ The length of S1//S2 is sum of the lengths of strings S1 and S2. Substring: A stringY is called substring of a string S if there exist string X and Z such thatS = X //Y // Z If X is an empty string, thenY is called an Initial substring of S, and Z is an empty string thenY is called a terminal substring of S. Ex:„BE OR NOT ‟ is a substring of „TO BE OR NOT TO BE ‟ „THE ‟ is an initial substring and END is of a terminal substring of String „THE END ‟
142
STRINGS IN C In C, the strings are represented as character arrays terminated by the null character \0. Declaration 1: #define MAX_SIZE 100 /* maximum size of string */ char s[MAX_SIZE] = {“dog”}; char t[MAX_SIZE] = {“house”}; s[0]s[1] do s[2]s[3]t[0]t[1]t[2]t[3]t[4]t[5] g\0house\0 The above figure shows how these strings would be represented internally in memory. Declaration 2: char s[ ] = {“dog”}; char t[ ] = {“house”}; Using these declarations, the C compiler will allocate just enough space to hold each word including the null character.
143
STORING STRINGS Strings are stored in three types of structures 1.Fixed length structures 2.Variable length structures with fixed maximum 3.Linked structures 1.Record Oriented Fixed length storage: In fixed length structures each line of print is viewed as a record, where all have the same length i.e., where each record accommodates the same number of characters. Example: Suppose the input consists of the program. Using a record oriented, fixed length storage medium, the input data will appear in memory as pictured below.
145
To insert a record then it requires that all succeeding records be moved to new memory location. This disadvantage can be easily solved as shown in below figure.
146
That is, one can use a linear array POINT which gives the address of successive record, so that the records need not be stored in consecutive locations in memory. Inserting a new record will require only an updating of the array POINT. The main advantage of this method are 1.The ease of accessing data from any given record 2.The ease of updating data in any given record (as long as the length of the new data does not exceed the record length) The main disadvantages are 1.Time is wasted reading an entire record if most of the storage consists of inessential blank spaces. 2.When the correction consists of more or fewer characters than the original text, changing a misspelled word requires record to be changed.
147
2. Variable length structures: The storage of variable-length strings in memory cells can be done in two general ways 1.One can use a marker, such as two dollar signs ($$), to signal the end of the string 2.One can list the length of the string—as an additional item in the pointer array.
148
Example:
149
3.Linked storage: Strings are stored by means of linked lists. A linked list is a data structure where each element (called a node) is made up of two items: a)the data and b)a reference (or pointer), which points to the next node. A linked list is a collection of nodes where each node is connected to the next node through a pointer.
150
CHARACTER DATA TYPE The various programming languages handles character data type in different ways. Constants Many programming languages denotes string constants by placing the string in either single or double quotation marks. Ex:„THE END ‟ “THE BEGINNING” The string constants of length 7 and 13 characters respectively. Variables Each programming languages has its own rules for forming character variables. These variables fall into one of three categories 1.Static: In static character variable, whose length is defined before the program is executed and cannot change throughout the program 2.Semi-static: The length of the variable may vary during the execution of the program as long as the length does not exceed a maximum value determined by the program before the program is executed. 3.Dynamic: The length of the variable can change during the execution of the program.
151
STRING OPERATION 1.Substring: Group of consecutive elements in string is known as substring that are basic units to access in a string. Accessing a substring from a given string requires three pieces of information: (1)The name of the string or the string itself (2)The position of the first character of the substring in the given string (3)The length of the substring or the position of the last character of the substring. Syntax: SUBSTRING (string, initial, length) The syntax denote the substring of a string S beginning in a position K and having a length L. Ex: SUBSTRING ('TO BE OR NOT TO BE ‟, 4, 7) = 'BE OR N ‟ SUBSTRING ('THE END', 4, 4) = „END'
152
STRING OPERATION 2.Indexing : Indexing also called pattern matching, refers to finding the position where a string pattern P first appears in a given string text T.This operation is called INDEX Syntax: INDEX (text, pattern) If the pattern P does not appears in the text T, then INDEX is assigned the value 0. The arguments “text” and “pattern” can be either string constant or string variable. Suppose T contains the text “ HIS FATHER IS THE PROFESSOR” Then, INDEX(T, ‟ THE ‟ )has the value 7 INDEX(T, ‟ THE ‟ ) h as the value 14 INDEX(T, ‟ THEN ‟ )has the value 0
153
3.Concatenation Let S1 and S2 be string.The concatenation of S1 and S2 which is denoted by S1 // S2, is the string consisting of the characters of S1 followed by the character of S2. Ex: (a) Suppose S1 = 'MARK' and S2= „TWAIN' then S1 // S2 = „MARKTWAIN ‟ Concatenation is performed in C language using strcat function as shown below strcat (S1, S2); Concatenates string S1 and S2 and stores the result in S1 strcat ( ) function is part of the string.h header file; hence it must be included in link section
154
STRING OPERATION 4.Length : The number of characters in a string is called its length. Syntax: LENGTH (string) Ex: LENGTH („computer ‟ ) = 8 String length is determined in C language using the strlen( ) function, as shown below: X = strlen ("sunrise"); strlen function returns an integer value 7 and assigns it to the variable X Similar to strcat, strlen is also a part of string.h, hence the header file must be included at the time of pre-processing.
155
String handling Functions strlen(str) ; Returns the no. of characters in the given string strcpy(dest,src) ; Copies the given source stringto the destination string. strncpy(dest,source,n); Copies the n character from source stringto the destination string. strcmp(s1,s2) : Compares two strings and returns 0 if two strings are equal, 1 if s1>s2,-1 if s1<s2 strncmp(s1,s2,n) : Compares n characters from s1 and s2 returns 0 if they are equal, 1 ifn charecters of s1> n charecters of s2, -1 if n charecters of s1< n charecters of s2 strcat(s1,s2) : Joins two string into single string, s2 appended to s1 Strncat(s1,s2,n); Concatenates n characters from s2 to s1 strupr(str) : Converts characters of str into uppercase overwrites on str strlwr(str) : Converts characters of str into lowercase overwrites on str strrev(str) : reverse a given string str and overwrites on str Strstr(s1,s2); Checks whether substring s2 is present in s1
156
String handling Functions in c
157
String insertion Examplei=1
158
Word/Text Processing Word Processing Operations 1.Insertion: To insert a string S in a text T, so that S begins in position K We denoteINSERT(text,position,string) Example: INSERT(„ABCDEFG ‟,3, ‟ XYZ ‟ )=„ABXYZCDEFG ‟ This INSERT function can be implementedusing string operation as given below INSERT(T,K,S)= SUBSTRING(T,1,K-1)//S// SUBSTRING(T,K,LENGTH(T)-K+1) ie, Initial substring of T before position K,which has length K-1 is concatenated with string S and result is concatenated with remaining part of T which begins in position K and has LENGTH(T)-(K-1)= Length(T)-K+1
159
Deletion :To delete the sub string from a text T, that begins at position K and has length L We denoteDELETE(text,position,Length) Example: DELETE(„ABCDEFG ‟,4,2)=„ABCFG ‟ DELETE(„ABCDEFG ‟,0,2)=„ABCDEFG ‟ This DELETE function can be implementedusing string operation as given below DELETE(T,K,L)= SUBSTRING(T,1,K-1)// SUBSTRING(T,K+L,LENGTH(T)-K-L+1) ie, Initial substring of T before position K,which has length K-1 is concatenated with terminal substring of T beginning at position K+L Which has length LENGTH(T)-(K+L-1) = LENGTH(T)-K-L+1 Note: DELETE(T,K,L)=T when K=0
160
Replacement: To replace the first occurrence of pattern P1 by a pattern P2 in a given text T, We WriteREPLACE(text,Pattern1,Pattern2) For example REPLACE(„XABYABZ ‟, „AB ‟, ‟ C ‟ )=„XCYABZ ‟ REPLACE(„XABYABZ ‟, „BA ‟, ‟ C ‟ )=„XABYABZ ‟ REPLACE function can be expressed as deletion followed by an insertion as given below K: = INDEX(T,P1) T:= DELETE(T,K,LENGTH(P1)) INSERT(T,K,P2) To replace every occurrence of pattern P1 in T by patternP2 apply repeatedlyREPLACE(T,P1,P2)until INDEX(T,P)=0
161
Algorithm
162
PATTERN MATCHING ALGORITHMS Pattern matching is the problem of deciding whether or not a given string pattern P appears in a string text T.The length of P does not exceed the length of T. First Pattern Matching Algorithm The first pattern matching algorithm is one in which comparison is done by a given pattern P with each of the substrings of T, moving from left to right, until a match is found.
163
Let pattern P is 4 character string and T is 20 character string appearing as linear array ie, P=P[1],P[2],P[3],P[4] and T=T[1],T[2],T[3],……….T[20] P is compared with each of following 4- character substring of T ieWk=SUBSTRING(T,K,Length(P)) W1= T[1],T[2],T[3],T[4], W2= T[2],T[3],T[4],T[5] ……. W17=T[17],T[18],T[19],T[20] Here MAX=20-4+1=17
164
Algorithm: (Pattern Matching) P and T are strings with lengths R and S, and are stored as arrays with one character per element.This algorithm finds the INDEX of P in T. 1.[Initialize.] Set K: = 1 and MAX: = S - R + 1 2.Repeat Steps 3 to 5 while K ≤ MAX 3.Repeat for L = 1 to R: [Tests each character of P] If P[L] ≠ T[K + L – l], then: Go to Step 5 [End of inner loop.] 4.[Success.] Set INDEX = K, and Exit 5.Set K := K + 1 [End of Step 2 outer loop] 6.[Failure.] Set INDEX = O 7.Exit
165
Example
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.