Data Base Management System

Description

It is a simple ebook of Data base Management System. Easy to understand the Concepts od Data base management.

Reviews
Stats
views:
71
rating:
not rated
reviews:
0
posted:
7/10/2009
language:
English
pages:
0
S-304 Data Structure and Management By: Oza Hemant P 1 S-304 Data Structure and Management Want to get something in life Always think positive You will definitely get the thing you want By: Oza Hemant P 2 S-304 Data Structure and Management COURSE CODE: NAME OF COURSE: 1. RATIONALE: S-304 DATA STRUCTURE MANAGEMENT This course is useful for basic understanding of data. It provides enough knowledge for the application of different structures. also data 2. SCHEME OF STUDIES & EXAMINATIONS: COURSE CODE NO. S-304 NAME OF COURSE DATA STRUCTURE MANAGEMENT PRE REQ L ---3 TEACHING SCHEME P 4 C 5 100 50 25 175 EXAMINATION SCHEME TH. PRAC. TW TOTAL 3. SCHEME OF TEACHING AND ASSESSMENT: SR NO 1 2 3 4 5 6 NAME OF TOPIC BASIC CONCEPTS OF DATA REPRESENTATION : STACKS AND QUEUES LINKED LISTS SEARCHING AND SORTING TREES STRINGS TOTAL 8 8 8 8 6 42 12 12 12 12 4 56 20 20 20 15 15 100 THEORY HOURS 4 PRACTICAL HRS 4 % WEIGHTAGE 10 4.     OBJECTIVES: The student should be able to Differentiate the different types of data structures. Select the appropriate data structure for any application. Differentiate various types of sorting and searching methods. Select appropriate sorting and searching method. By: Oza Hemant P 3 S-304 Data Structure and Management 4 TOPICS AND SUB-TOPICS 1. BASIC CONCEPTS OF DATA REPRESENTATION: 1.1 Cells. 1.2 Fields. 1.3 Arrays. 1.4 Records. STACKS AND QUEUES 2.1 What is a Stack? 2.2 Operations on Stack (PUSH & POP). 2.3 Implementation of a Stack. 2.4 What is Queue? 2.5 Implementation of the Queues. 2.6 Priority Queues. 4 Hrs. 2. 8 Hrs. 3. LINKED LISTS 8 Hrs. 3.1 Pointers and Linked allocation. 3.2 Linked lists & Sequential list. 3.3 Difference between Linked & sequential List. 3.4 Operations on linear lists using singly linked & doubly Linked storage structure. 3.5 Circular linked list. 3.6 Application of linked lists. SEARCHING AND SORTING 8 Hrs. 4.1 Various sorting techniques. Selection sort - bubble sort- Quick sort Merge sorting - Tree sort - Shell sort - Radix sort. 4.2 Sequential searching. 4.3 Binary searching. 4.4 Hash tables - methods. 4.5 Hashing functions. 4.6 Collision resolution techniques. TREES 5.1 5.2 5.3 5.4 5.5 5.6 Definitions and Concepts. Binary trees. Operations on binary trees. Binary tree and tree traversal algorithms. Operations on binary trees. List representation of Tree. 6 Hrs. 8 Hrs. 4. 5. 6. STRINGS 6.1 Strings and their representations. 6.2 String Conversion. 6.3 String manipulation. 6.4 String arrays. By: Oza Hemant P 4 S-304 6. LABORATORY EXPERIENCES 1. Develop an algorithm for push and pop stock Operations and implement using array data Structure and pointer. 2. Develop an algorithm for insert and delete Operations of queue and implement using Array and pointer data structures. Develop algorithms for following ordered Singly linked list operations: Addition into the list Deletion from the list searching into the list display the list Write a complete menu driven program for the Above operations and test it. 4. Develop algorithms for following binary tree Operations:Addition new nodes into the tree Deletion nodes from the tree searching a node into the tree display current nodes of the tree Data Structure and Management 28 hrs. 2 hrs 2 Hrs. 3. 4 hrs. 4 hrs. Write complete many driven program for the above Operations and test it 5. 6. 7. Develop algorithm for sequential search, write Program for the same and test it. Develop algorithm for binary search write Program for the same and test it. Develop algorithms for following sorting Methods write programs for each and test it. Bubble sort. Insertion sort. Selection sort Shell sort Merge sort Quick sort Radix sort Develop algorithms for following string Operations and implement them Finding length of a given string copying a string into another string concatenating two strings. String comparison Finding a sub-string into a string. 2 hrs. 2 hrs. 7 hrs. 8. 5 hrs. By: Oza Hemant P 5 S-304 Data Structure and Management By: Oza Hemant P 6 S-304 1.1 What is Data Structure? Data Structure and Management  Data Structure: The logical or mathematical model of a particular organization of data is called data structure.  Operations on Data Structure: 1. 2. 3. 4. 5. 6. Traversing Inserting Deleting Searching Sorting Merging 1.2 Define following terms: (a) cell (b) field (c) record (d) Array  (a) Cell: The smallest fundamental structural unit which represents a data entity.  (b) Field: The smallest piece of information that can be referenced in programming language.  (c) Record: a record is a collection of related data items. For example :student of the class  (d) Array: array is an ordered set which consist of fixed number of objects of same type. 1.3 Array 1.3.1 Array (Definition): Array is an ordered set which consist of fixed number of objects of same type. Array is a linier data structure. 1.3.2 Types of Array: 1.3.2.1 One Dimensional Array (Storage Representation):  In one dimensional array the number of memory location is sequentially allocated to the array. So it is also known as sequential list.  If each element requires one word in memory then n elements array occupies n consecutive words in memory.  Since the size of an array is fixed it requires fixed number of memory location.  Representation of an array in memory location is shown below: L0 A[0] A[1] A[2] . L0 + C*(I - B) A[I] 2020 7 2000 2001 2002 A[N-1] By: Oza Hemant P S-304 Data Structure and Management  L0 is the starting address or address of first element in the array.  C is the number of words allocated to each element  B is the starting index of an array.  Thus location of A[I] in the array is calculated as: Loc (A [I]) = L0 + C*(I - B) If we want to find the address of A[2] then it is calculated as: Loc (A [2]) = L0 + C*(I - B) = 2000 + 1 * (2 - 0) = 2000 + 2 = 2002 1.3.2.2 Operations on One dimensional Array 1. Traversal: This operation is used to traverse one dimensional array for inserting or displaying elements of one dimensional array. Algorithm: TRAVERSE (A, LB, UB)  A is an array  LB is lower limit of an array  UB is Upper limit of an array 1. 2. [Initialize] COUNTLB [Perform Traversal and Increment counter] While COUNT ≤ UB Write A [COUNT] or Read A [COUNT] COUNTCOUNT+1 3. 2. [Finished] Return Insertion: This operation is used to insert an element into one dimensional array. Algorithm: INSERT (A, POS, NUMBER, ELEMENT)  A is an array  POS indicates position at which we want to insert element.  NUMBER indicates number of elements in an array.  ELEMENT indicates element to be inserted. 1. [Initialize] TEMPNUMBER By: Oza Hemant P 8 S-304 Data Structure and Management 2. [Move the elements one position down] Repeat While TEMP ≥ POS A [TEMP + 1]A[TEMP] TEMPTEMP-1 3. 4. 5. 3. [Insert element] A [POS] ELEMENT [Increase size of array] NUMBERNUMBER + 1 [Finished] Exit Deletion: This operation is used to delete an element from one dimensional array. Algorithm: DELETE (A, POS, NUMBER, ELEMENT)  A is an array  POS indicates position at which we want to insert element.  NUMBER indicates number of elements in an array.  ELEMENT indicates element to be inserted. 1. 2. 3. [Delete Element] ELEMENTA [POS] [Initialization] TEMPPOS [Move the elements one position up] Repeat While TEMP ≤ NUMBER-1 A [TEMP] A [TEMP + 1] TEMPTEMP+1 4. 5. 4. [Decrease size of array] NUMBERNUMBER - 1 [Finished] Exit Searching: This operation is used to search particular element in one dimensional array. Algorithm:  SEARCH(K, N, X)  These function searches the list K consist of N elements for the value of X. 1. [Initialize search] By: Oza Hemant P 9 S-304 I1 K [N+1] X 2. [Search the Vector] Repeat while K [I] ≠ X II+1 3. [Successful Search?] If I = N+1 then Write “Unsuccessful Search” Return 0 Else Write “Successful Search” Return 1 Data Structure and Management 5. Sorting: This operation is used to sort the elements of array in ascending order or descending order. Algorithm:  SORT(K,N)  These function sorts elements of array K consist of N elements.  PASS denotes the pass index.  LAST denotes the last unsorted element.  EXCHS counts the total number of exchange made during pass 1. [Initialize] LASTN 2. [Loop on pass index] Repeat thru step 5 for PASS = 1, 2, …., N-1 3. [Initialize exchange counter for this pass ] EXCHS0 4. [Perform pair wise comparisons on unsorted elements ] Repeat for I = 1, 2, …., LAST-1 If K[I] > K[I+1] then K[I] K[I+1] EXCHSEXCHS+1 5. [Exchange made on this pass?] If EXCHS = 0 then Write “Mission Complete” Else By: Oza Hemant P 10 S-304 LASTLAST-1 6. [Finished] Return (Minimum number of pass required) Data Structure and Management 6. Find Big or Small Number of Array: This operation is used to find the smallest and largest element of an array. Algorithm: BIGSMALL (A, NUMBER)  A is an array  NUMBER indicates number of elements in an array. 1. [Initialize] MAXA [0] MINA [0] 2. 3. [Scan each element in array] For I = 1 to N [Compare Element] If A [I] > MAX then MAXA [I] If A [I] < MIN then MINA [I] 4. [Output Max and Min] Write MAX Write MIN 5. [Finished] Exit 1.3.2.2 Two Dimensional Array (Storage Representation): (1) Row-major order storage for two-dimensional array:  One method of representing a two dimensional array in memory is the row major order representation.  In this representation the first row of the array occupies the first set of memory locations reserved for the array, the second row occupies the next set and so on.  An array consisting of n rows and m columns can be stored sequentially in row major order as : Row1 A[1,1] A[1,2] A[1,3] A[1,4]……………..A[1,m] Row 2 A[2,1] A[2,2] A[2,3] A[2,4]…………….A[2,m] …………………………………………………………. Row n A[n,1] A[n,2] A[n,3] A[n,4]…………….A[n,m] By: Oza Hemant P 11 S-304  Example: Data Structure and Management A two dimensional array consist of two rows and four columns is stored sequentially in row major order as: A[1][1] A[1][2] A[1][3] A[1][4] A[2][1] A[2][2] A[2][3] A[2][4] Row2 Row1  The address of element A[i, j] can be obtained by evaluating expression: Loc (A [i, j]) = L0 + (i - 1) * m + (j - 1)  Where L0 is the address of the first element in the array.  i is the row index  j is the column index  for example the address of element A[2,3] is calculated as: A[2,3] = L0 + (i - 1) * m + (j - 1) Here, m=4, n=2 , i=2, j=3 =L0 + (2 - 1) * 4 + (3 - 1) =L0 + 4 + 2 =L0 + 6 (2) Column-major order storage for two-dimensional array:  One method of representing a two dimensional array in memory is the column major order representation.  In this representation the first column of the array occupies the first set of memory locations reserved for the array, the second column occupies the next set and so on.  An array consisting of n rows and m columns can be stored sequentially in column major order as : Column1 A[1,1] A[2,1] A[3,1] A[4,1]……………..A[n,1] Column 2 A[1,2] A[2,2] A[3,2] A[4,2]…………….A[n,2] …………………………………………………………. Column m A [1,m] A[2,m] A[3,m] A[4,m]…………….A[n, m]  Example: By: Oza Hemant P 12 S-304 Data Structure and Management A two dimensional array consist of two rows and four columns is stored sequentially in column major order as: A[1][1] A[2][1] A[1][2] A[2][2] A[1][3] A[2][3] A[1][4] A[2][4] Column4 Column 3 Column 2 Column 1  The address of element A[i, j] can be obtained by evaluating expression: Loc (A [i, j]) = L0 + (j - 1) * n + (i - 1)  Where L0 is the address of the first element in the array.  i is the row index  j is the column index  for example the address of element A[2,3] is calculated as: A[2,3] = L0 + (j - 1) * n + (i - 1) Here, m=4, n=2 , i=2, j=3 =L0 + (3 - 1) * 2 + (2 - 1) =L0 + 4 + 2 =L0 + 6 1.3.2.3 “Insertion and deletion operation are difficult and time consuming” with array.  Insertion with Array:  Suppose there are N elements in an array and we want to insert an element between first and second element.  We have to move last N-1 elements down  Deletion with Array:  Suppose there are N elements in an array and we want to delete an element from array.  We have to move the elements up to take vacant space after deleting element. Thus insertion and deletion operation are very difficult and time consuming with array. 1.3.2.4 Difference between array and array of structure with example. Ans:  Array: array is a collection of variables of same data type.  Structure: structure is a collection of the variables of different data types. By: Oza Hemant P 13 S-304  We can declare the structure as: Struct student { Int rollno; Char name [20]; } Data Structure and Management  If we want to access the member of the structure then we have to declare a variable of type structure. As follow: Struct student s1;  By using this variable we can access the member of structure as follows: S1.rollno = 10; S1.name = “xyz”;  Now if we want to insert values for 60 students then we have to create 60 variables of type student but it is very difficult process. So we have to create an array of structure student using following statement: Struct student s [60];  Here s [60] is an array of structure student. Thus array is a collection of variables of same data type while array of structure is a collection of variables of type structure which is collection of variables of different data types. 1.3.2.5 Difference between structure and class By default the data member of the class are private while by default the data member of the structure are public. 1.4 Primitive and non-primitive data structure.  Primitive Data Structure: The Data structures that are directly operated by machine level instructions are known as primitive data structure. Following are the primitive data structure: (a) Integer:  A quantity representing objects which are discrete in nature i.e the number of objects are countable can be represented by an integer.  The set of integer is: {…..-(n+1), -n , ….. , -2, -1,0,1,2,………n,n+1}  The sign and magnitude method is used to represent integer numbers. In this method we place a sign symbol in front of the number. (b) Real:  The number having fractional part i.e decimal point is called real number.  Common way of representing real number is normalized floating point representation. By: Oza Hemant P 14 S-304 Data Structure and Management  In this method the real number is expressed as a combination of mantissa and exponent. (c) Character:  Character data structure is used to store nonnumeric information.  It can be letters [A-Z], [a-z], operators and special symbols.  A character is represented in memory as a sequence bits.  Two most commonly known character set supported by computer are ASCII and EBCDIC. (d) Logical:  A logical data item is a primitive data structure that can assume the values of either “true” or “false”.  Most commonly used logical operators Are AND, OR and NOT. (e) Pointer:  Pointer is a variable which points to the memory address. This memory address is the location of other variable in memory.  It provides homogeneous method of referencing any data structure, regardless of the structure‟s type or complexity.  Another characteristic is that it provides faster insertion and deletion of elements.  Non Primitive Data Structure: The data structures that are not directly operated by machine level instructions are known as non primitive data structure. Following are the non primitive data structure: (a) Array:  Array is an ordered set which consist of a fixed number of object.  No insertion and deletion operation are performed is performed on array.  We can only change the value of the element in array. (a) List:  List is an ordered set which consist of variable number of elements or object.  Insertion and deletion can be performed on list. (b) File:  A file is a large list that is stored in the external memory of computer.  A file may be used as a repository for list items commonly called records. 1.5 Linier and non linier data structure.  Data Structure: The logical or mathematical model of a particular organization of data is called data structure.  There are two types of Data Structure: 1. Linier Data Structure: By: Oza Hemant P 15 S-304 2. Non Linier Data Structure  Linier Data Structure: Data Structure and Management  In the linier data structure processing of data items is possible in linier fashion.  Data are processed one by one sequentially.  Examples of the linier data structure are: (A) Array (B) Linked List (C) Stack (D) Queue  Array: Array is an ordered set which consist of a fixed number of object.  Linked List: A linked list is a collection of nodes. Each node has two fields: (i) Information (ii) address, which contains the address of the next node. 1 2 3  Stack: A stack is a linier list in which insertion and deletion operations are performed at only one end of the list. 50 40 30 20 10 TOP BOTTOM  Queue: A queue is a linier list in which insertion is performed at one end called rear and deletion is performed at another end of the list called front. Deletion Insertion Front  Non Linier Data Structure: Rear  In the Non linier data structure processing of data items is not possible in linier fashion.  Examples of the non linier data structure are: (A) Tree (B) Graph  Tree: In tree data contains hierarchical relationship. SSPC CE By: Oza Hemant P EC IT 16 S-304 Data Structure and Management  Graph: this data structure contains relationship between pairs of elements. Delhi Bombay Culcutta 1.6 Difference between List and Array  List Madras Array (1) No of elements in an array is fixed. (1) No of elements in a list are not fixed. (2) It uses pointer variable which (2) It does not use pointer variable so it occupies an extra memory space. does not occupies extra memory space. (3) Insertion and deletion operation are (3) Insertion and deletion operation are very easy to perform (4) There are two types of List: 1. Linier List 2. Non Linier List (5) Searching is slower in case of List. very difficult to perform. (4) There are two types of array: 1. One dimensional array 2. Two dimensional array (5) Searching is faster in case of array. 1.7 Difference between Static Memory allocation and Dynamic Memory allocation.  Static Memory Allocation Dynamic Memory Allocation 1. Static Memory Allocation is Performed 1. Dynamic Memory Allocation is at Compile Time. Performed at Runtime. 2. In Static Memory Allocation Memory 2. In Dynamic Memory Allocation may be wasted. memory is allocated when it is needed. So there is no wastage of memory. 3. Array is an example of static memory 3. Linked List is an example of dynamic allocation. memory allocation. We can use malloc () function to allocate memory at runtime. 4. In static memory allocation memory is 4. allocated sequentially. In dynamic memory allocation memory is not allocated sequentially. By: Oza Hemant P 17 S-304 Data Structure and Management Exercise (1) Explain following term: (a) cell (b) field (c) array (d) record (2) Explain primitive and non-primitive data structure with example. (3) Explain raw-major order of storage for two-dimensional array. Derive the formula for finding storage address. (4) Explain column-major order of storage for two-dimensional array. Derive the formula for finding storage address. (5) Explain difference between array and array of structure with example. (6) Compare linier data structure with non linier data structure. (7) Explain Difference between List and Array (8) Explain Difference between Static Memory allocation and Dynamic Memory allocation. (9) Difference between structure and class (10) Explain “Insertion and deletion operation are difficult and time consuming” with array. By: Oza Hemant P 18 S-304 Data Structure and Management By: Oza Hemant P 19 S-304 2.1 Stack 2.1.1 Define stack Stack (Definition): Data Structure and Management  A stack is a linier list in which insertion and deletion operations are performed at only one end of the list. 2.1.2 Representation of Stack 50 40 30 20 10 Figure2.1 Stack TOP BOTTOM  The insertion operation is referred to as a push operation.  The deletion operation is referred to as a pop operation.  The most accessible element in the stack is known as a TOP element and the least accessible element in the stack is known as a BOTTOM element.  Since, the insertion and deletion operation are performed at only one end of the stack the element which is inserted last in the stack is first to delete. So stack is also known as last in First out (LIFO). Deletion Insertion ….. ….. Bottom Top Figure 2.2 Representation of the Stack  A pointer TOP keeps track of the top element in the stack. Initially when the stack is empty, TOP has a value of zero and when the stack contains a single element, TOP has a value of one and so on.  Each time a new element is inserted in the stack the TOP pointer is incremented by one and the pointer is decremented by one when element is deleted. 2.1.3 Examples of Stack (1) Railway shunting system (2) Pile of tray in cafeteria By: Oza Hemant P 20 S-304 2.1.4 Application of Stack: (1) Recursion (2) Stack Machine (3) Evaluates polish notations 2.1.5 Operation of the Stack: (1) Push: it is used to insert an element into the stack (2) Pop: it is used to delete an element from stack Data Structure and Management (3) Peep: it is used to retrieve ith element from the top of the stack (4) Change: it is used to change value of the ith element from the top of the stack. 2.1.6 Algorithms for Stack Operation (1) Algorithm for PUSH Operation. Push(S, Top, X)  This algorithm insert an element X to the top of the stack.  The Stack is represented by vector S which contains N elements.  TOP is a pointer which points to the top element of the Stack 1. [Check for stack overflow] If TOP >= N then Write („Stack Overflow‟) Return 2. [Increment TOP] TOP = TOP + 1 3. [Insert Element] S [TOP] = X 4. [Finished] Return (2) Algorithm for POP Operation. POP(S, TOP)  This algorithm removes an element from the Top of the Stack.  The Stack is represented by vector S which contains N elements.  TOP is a pointer which points to the top element of the Stack 1. [Check for the Underflow on the Stack] If TOP = 0 then Write („STACK UNDERFLOW ON POP‟) Exit 2. [Decrement Pointer] TOP = TOP - 1 By: Oza Hemant P 21 S-304 3. [Return former top element of the stack] Return(S [TOP + 1]) (3) Algorithm for PEEP Operation. PEEP(S, TOP, I) Data Structure and Management  This algorithm returns the value of the i th element from the Top of the Stack.  The Stack is represented by vector S which contains N elements.  TOP is a pointer which points to the top element of the Stack 1. [Check for the Underflow on the Stack] If TOP – I + 1 ≤ 0 then Write („STACK UNDERFLOW ON PEEP‟) Exit 2. [Return the ith element from the TOP of the Stack] Return(S [TOP – I + 1]) (4) Algorithm for CHANGE Operation. Ans: CHANGE(S, TOP, X, I)  This algorithm changes the value of the I th element from the Top of the Stack by X  The Stack is represented by vector S which contains N elements.  TOP is a pointer which points to the top element of the Stack 1. [Check for the Underflow on the Stack] If TOP – I + 1 ≤ 0 then Write („STACK UNDERFLOW ON CHANGE‟) Exit 2. [Change ith element from the TOP of the Stack] S [TOP – I + 1]  X 3. [finished] Return 2.1.7 Double stack using sequential allocation.  In certain cases the program contains more then one stack. In such a situation, it can happen that one stack has encountered an overflow condition while others are far from being full. Instead of imposing a maximum size on each stack, it would be preferable to allocate a common pull or maximum block of memory which both of the stack can use. This type of arrangement is known as double stack and it is shown below:  By: Oza Hemant P 22 S-304 Data Structure and Management Bottom 1 Top 1 Top 2 Bottom 2  In the above figure the first stack grows to the right, while the second stack grows to the left.  With this arrangement it is possible for one stack to occupy more than half of the memory allocated to both stacks.  An overflow condition will occur only if the total size of the two stacks exceeds the allocated memory space. 2.1.8 Application of Stack  There are three main application of stack:  (1) Recursion:  Recursion is the technique of defining a process in terms of itself.  Stack is widely used in recursion because of it‟s Last in First out Property.  It is used to store the return address of the function call and the parameter or local variable that arise in the function call.  There are two types of recursion: (1) Primitive Recursive function: Example Factorial Function (2) Non primitive Recursive function: Example Ackerman‟s function  (2) Evaluate polish notation:  There are basically three types of polish notation: (A) Infix (B) Prefix (C) Postfix  When the operator exists between two operands then it is known as Infix notation. For example A+B  When the operators are written before their operands then it is known as Prefix notation. For example +AB  When the operators are written after their operands then it is known as Postfix notation. For example AB+  Stack is widely used to convert infix notation into prefix or postfix notation.  (3) Stack Machine:  Stack Machines are also used to evaluate polish notations.  Two example of the stack machine are PDP-11 and B5000. These machines are well suited for stacking local variables and parameter that arise in the function call.  Stack machines provides faster execution of polish notation. By: Oza Hemant P 23 S-304 2.1.9 Recursion Data Structure and Management  Recursion: Recursion is the technique of defining a process in terms of itself.  In terms of C language the function calling itself is known as recursion.  There are two types of recursion: 1. Primitive Recursive function 2. Non primitive Recursive function  (1) Primitive Recursive function:  This is known as recursively defined function.  Example of the Primitive recursive function is the factorial function. The factorial function is defined as: 1 FACTORIAL (N) = N * FACTORIAL (N-1) otherwise  (2) Non primitive Recursive function:  This is known as recursive use of the procedure.  Example of the Non primitive recursive function is the Ackermann‟s function. The Ackerman‟s function is defined as: N + 1, if M = 0 if N = 0 A (M, N) = A (M-1, 1), if N=0 A (M-1, A (M, N-1)), otherwise  Relation Between Stack and Recursion:  Recursive procedures also have last in first out characteristics. So stack is widely used in recursion.  At each procedure call the stack is pushed to save the necessary values such as formal parameters, local variables and return address of the procedure call.  After completion of the execution of the procedure the stack values are poped to restore the saved values of the procedure call. 2.1.10 Iterative Process and Recursive Process  Iterative Process:  In iterative process there are four parts: Initialization, Decision, Computation, Update By: Oza Hemant P 24 S-304 1. Initialization: Data Structure and Management The parameters of the function and a decision parameter in this part are set to their initial value. 2. Decision: The decision parameter is used to determine whether to remain in the loop or when to exit from the loop. 3. Computation: The required computation is performed in this part. 4. Update: The decision parameter is updated and a transfer to the next iteration results. Entry Initialization Decision Return Update Computation  Recursive process:  In recursive process the process is called from within the process.  There are three parts of the recursive process: 1. Save formal parameters, local variables and return address. 2. If the base criterion has been reached then perform the final calculation and go to step 3 otherwise perform the partial computation and go to step 1. 3. Restore the most recently saved parameters, local variables and return address By: Oza Hemant P 25 S-304 Data Structure and Management Entry Save Formal Parameters and return address Test Partial Computation Final Computation Procedure Call to Itself Restore formal parameters and return address Entry 2.1.11 Linked implementation of stack (1) Algorithm for push operation  PUSH(TOP,X)  TOP is a pointer which points to the first node in the stack  X is the element which we want to insert in to stack. 1. [Check for Availability stack underflow] If AVAIL = NULL then‟ Write “Availability stack underflow” Return 2. [Check for the stack element and insert element] If TOP = NULL then NEW AVAIL AVAILLINK (AVAIL) INFO (NEW) X LINK (NEW) NULL TOPNEW Else NEWAVAIL AVAILLINK (AVAIL) By: Oza Hemant P 26 S-304 INFO (NEW) X LINK (NEW) TOP TOPNEW 3. [Finished] Return (2) Algorithm for pop operation  POP(TOP)  TOP is a pointer which points to the first node in the stack 1. [Check for empty stack] If TOP = NULL then Write “Stack is empty” Return 2. [Check for the element in the stack and delete it] If LINK (TOP) = NULL then YINFO (TOP) TOPNULL Else Data Structure and Management (Assign the address pointed by TOP pointer to TEMP pointer) TEMPTOP YINFO (TEMP) (Assign the address of next node to TOP pointer) TOPLINK (TEMP) 3. [Finished] Return 2.2 Queue 2.2.1 Define Queue  A queue is a linier list in which insertion is performed at one end called rear and deletion is performed at another end of the list called front. 2.2.2 Representation of Queue  The information in such a list is proceeds in the same order as it was received.  Since insertion is performed at one end and deletion is performed at another end the element which is inserted first is first to delete. So it is also known as First in First out (FIFO) or First Come First Serve (FCFS) data structure. By: Oza Hemant P 27 S-304 Data Structure and Management Deletion Insertion Front Rear 2.2.3 Examples of Queue (1) A raw of students at registration counter (2) Line of cars waiting to proceeds in some direction at traffic signal 2.2.4 Application of Queue  A queue is the natural data structure for a system to serve its incoming requests. Most of the process scheduling or disk scheduling algorithms in operating systems use queues.  Computer hardware like a processor or a network card also maintain buffers in the form of queues for incoming resource requests. A stack-like data structure causes starvation of the first requests, and is not applicable in such cases.  A mailbox or port to save messages to communicate between two users or processes in a system is essentially a queue-like structure.  Queue is widely used in Simulation. 2.2.5 Operation on Queue: (1) Insert an element in to queue (2) Delete an element from queue 2.2.6 Algorithms for Queue operations (1) Algorithm to insert an element in queue QINSERT (Q, F, R, N, Y)  This function insert an element into the queue  The Queue is represented by vector Q which contains N elements.  F is a pointer which points to the front end  R is a pointer which points to the rear end  Y is the element to be inserted. (1) [Overflow?] If R≥N then Write („Overflow‟) (2) [Increment rear pointer ] RR+1 By: Oza Hemant P 28 S-304 (3) [Insert element] Q[R] Y (4) [Is front pointer properly set?] If F=0 then F1 Return (2) Algorithm to delete an element from the queue QDELETE (Q, F, R) Data Structure and Management  The Queue is represented by vector Q which contains N elements.  F is a pointer which points to the front end  R is a pointer which points to the rear end (1) [Underflow?] If F = 0 then Write („UNDERFLOW‟) Return (0) (2) [Delete element] YQ [F] (3) [Queue empty?] If F =R Then FR0 Else FF+1 (4) [Return element] Return (y) 2.2.7 Linked implementation of queue (1) Algorithm to insert an element in queue.  QINSERT(FRONT,REAR,X)  FIRST is a pointer which points to the first element in the queue.  REAR is a pointer which points to the last element in the queue.  X is an element which we want to insert in to queue. 1. [Check for availability stack underflow] If AVAIL = NULL then Write “Availability stack underflow” Return 2. [Obtain address of next free node] NEWAVAIL By: Oza Hemant P 29 S-304 3. [Remove free node from availability stack] AVAILLINK (AVAIL) 4. [initialize field of new node] INFO (NEW) X LINK (NEW) NULL 5. [If queue is empty?] If REAR = NULL then REARNEW FRONTNEW 6. [initialize search for last node] SAVEREAR 7. [Search end of the list] Repeat while LINK (SAVE) ≠ NULL SAVELINK (SAVE) 8. [Set LINK field of last node to NEW ] LINK (SAVE) NEW 9. [set REAR pointer to point to the LAST element] REARNEW 10. [Finished] Return (2) Algorithm to delete an element from queue.  QDELETE(FRONT,REAR) Data Structure and Management  FIRST is a pointer which points to the first element in the queue.  REAR is a pointer which points to the last element in the queue. 1. [Check for empty Queue] If FRONT = NULL then Write “Queue is empty” Return 2. [Check for elements in Queue and delete element] If LINK (FRONT) = NULL then YINFO (FRONT) FRONTNULL REARNULL Else (Assign the address pointed by FRONT pointer to TEMP pointer) By: Oza Hemant P 30 S-304 TEMPFRONT YINFO (TEMP) Data Structure and Management (Assign the address of next node to FRONT pointer) FRONTLINK (TEMP) 3. [Finished] Return 2.3 Circular Queue  A circular queue is a queue in which elements are arranged such that the first element in the queue follows the last element in the queue.  The arrangement of circular queue is shown in figure below:  Here in the circular queue the first element q [1] follows the last element q[n].  Disadvantage of simple queue is that even if we have a free memory space in a queue we can not use that free memory space to insert element.  For example consider following operations:  Even we have a free memory space we can not use that memory space. So simple queue results in wastage of memory space. This problem can be solved by using circular queue.  For example consider the following operations: By: Oza Hemant P 31 S-304 2.3.1 Operations on Circular Queue (1) Insert new element in to circular queue (2) Delete element from circular queue 2.3.2 Algorithm for Circular queue (1) Algorithm to insert element in circular queue.  CQINSERT(Q,F,R,N,Y)  This function inserts an element in to circular queue. Data Structure and Management  The Queue is represented by vector Q which contains N elements.  F is a pointer which points to the front end  R is a pointer which points to the rear end  Y is the element to be inserted. (1) [Reset rear pointer?] If R = N then R1 Else RR + 1 (2) [Overflow?] If F = R then Write (“queue overflow”) (3) [Insert element] Q[R]Y (4) [Is front pointer properly set?] If F = 0 then F1 Return (2) Algorithm to delete element from circular queue.  CQDELETE(Q,F,R)  This function deletes an element from circular queue  The Queue is represented by vector Q which contains N elements.  F is a pointer which points to the front end  R is a pointer which points to the rear end (1) [Underflow] If F = 0 then Write (Underflow“) (2) [Delete element] By: Oza Hemant P 32 S-304 Return (Q[F]) (3) [Queue empty?] If F = R then FR0 (4) [Increment front pointer] If F = N then F1 Else FF+1 2.4 Dequeue Data Structure and Management  Dequeue is a linier list in which insertion and deletion operation are performed at either end of the queue.  Dequeue is also known as double ended queue.  Dequeue is shown below: Front Deletion Insertion  There are two types of Dequeue:   Rear Insertion Deletion Input restricted Dequeue: in input restricted Dequeue insertion operation is performed at only one end of the queue. Output restricted Dequeue: in output restricted Dequeue deletion operation is performed at only one end of the queue. 2.4.1 Operation on Dequeue (1) Insert an element in to Dequeue (2) Delete an element from Dequeue 2.4.2 Algorithms for Dequeue (1) Algorithm for Insert an element in to Dequeue  DQINSERT(DQ,F,R,N,X)  This function inserts an element in to Dequeue.  The Dequeue is represented by vector DQ which contains N elements.  F is a pointer which points to the front end  R is a pointer which points to the rear end  X is the element to be inserted. By: Oza Hemant P 33 S-304 1. [Check overflow condition] If R ≥ N and F = 0 then Write “Dequeue overflow” Return 2. [Check front pointer value] If F > 0 then FF-1 Else Return 3. [Insert element at the front end] DQ [F] X 4. [Check rear pointer value] If R < N then RR+1 Else Return 5. [Insert element at the rear end] DQ [R]  X 6. [Finished] Return (2) Algorithm for Delete an element from Dequeue.  DQDELETE(DQ,F,R)  This function deletes an element from Dequeue Data Structure and Management  The Queue is represented by vector DQ which contains N elements.  F is a pointer which points to the front end  R is a pointer which points to the rear end  [Check for underflow] If F = 0 and R = 0 then Write “Dequeue underflow” Return  [Delete element from front end] If F > 0 then Return (DQ [F])  [Check queue for empty] If F = R then By: Oza Hemant P 34 S-304 FR0 Else FF+1  [Delete element from rear end] If R > 0 then Return (DQ [R])  [Check for queue empty] If F = R then FR0 Else RR-1  [Finished] Return 2.5 Priority Queue Ans: Data Structure and Management  A queue in which we are able to insert items or remove items from any position based on some priority is known as priority queue. R1 1 R2 2 …… …… Ri-1 1 O1 2 O2 2 ……. …….. Oj-1 2 B1 3 B2 3 …… Bk-1 …… 3  Figure represents a priority queue of jobs waiting to use a computer  Priorities of 1, 2, 3 have been attached to jobs of real time, on-line and batch respectively.  Figure shows how the single priority queue can be visualized as three separate queues. Priority 1: R1 Priority 2: O1 Priority 3: B1 B2 ……… Bk-1 ….. Bk O2 ……… Oj-1 ….. Oj R2 ……… Ri-1 ….. Ri  When elements are inserted, they are always added at the end of one of the queues as determined by the priorities  Elements in the second queue are deleted only when the first queue is empty.  Elements in the third queue are deleted only when the first and second queue are empty By: Oza Hemant P 35 S-304 2.6 Comparison of LIFO and FIFO LIFO Data Structure and Management FIFO (1) In LIFO the insertion and deletion (1) In FIFO the insertion and deletion operation are performed at only one end. operation are performed at two different ends. (2) In LIFO the element which is inserted (2) In FIFO the element which is last is first to delete. inserted first is first to delete. (3) LIFO require only one pointer called (3) FIFO requires two pointers called TOP (4) Example: piles of trays in cafeteria front and rear. (4) Example: students at registration counter (5) In LIFO there is no wastage of (5) In FIFO even if we have free memory space. memory space sometimes we cannot use that space to store elements. By: Oza Hemant P 36 S-304 Data Structure and Management Exercise (1) Explain double stack using sequential allocation. (2) Convert a+b * c-d /e-h expression in prefix and postfix. (3) Write an algorithm for push operation (4) Explain priority queue (5) Explain circular queue. Write an algorithm for deletion in queue (6) Explain recursion with example. (7) Write short note on recursion. Also explain how stack data structure is used in recursion. (8) Explain stack and queue in brief. Write at least two applications for each. (9) Write an algorithm for PUSH operation on stack using linked allocation. (10) Convert into prefix: (a*b)+(c+d-e) (11) Convert into postfix: [(m+n)*(m-n)]/(m*n) (12) What is circular queue? What are the advantages of circular queue over normal queue? (13) Explain LIFO data structure. Write an algorithm for any one operation on it. (14) Short Note: Application of Stack (15) Compare circular queue and queue. (16) Write algorithm for inserting new element in circular queue (17) Write algorithm to add an item to stack and delete an item from stack. (18) List application of stack and explain recursion. (19) What is priority queue? Write algorithm to delete an element from circular queue. (20) What is priority queue? Write algorithm to delete an element from circular queue using linked representation. (21) Explain relation between recursion and stack. (22) Compare LIFO and FIFO (23) Explain: (a) queue(b) Dequeue (c) priority queue(d)circular queue (24) Write an algorithm to delete an element from queue (25) Write an algorithm to delete an element from stack using linked representation. (26) Explain various operation on LIFO (27) Write an algorithm to insert and delete element from circular queue. (28) Explain with example how iterative method differs from recursive method. By: Oza Hemant P 37 S-304 Data Structure and Management By: Oza Hemant P 38 S-304 3.1 Pointers and Linked Allocation Data Structure and Management  Pointer: A pointer is a variable which contains an address of another variable in memory.  We can create a pointer variable in C using following syntax: Data type *pointer name;  For example: Int *ptr; Here, ptr is a pointer of type integer  Suppose one variable called X having value 35 is stored at memory location 2010. 2010 35  ptr  If we want a pointer ptr to points to the memory location 2010 then we have to use following syntax: ptr = &X;  If we want to access the the value of variable X using pointer variable than we have to use the following syntax: Y = *ptr;  Thus the use of a pointer or link to refer to the element of data structure is known as linked allocation.  “Elements which are logically adjacent need not be physically adjacent in Memory. “  In sequential storage each elements in array are stored at sequential memory location.  For example: 10 20 30 40 2000 2001 2002 2003  While in linked allocation each node contains one link which points to the address of the next node in memory. So the elements in list are not stored at sequential memory location.  For example: A 2010 B 2002 C 2006 D FIRST=2000 2010 2002 2006  Thus the elements in the list are logically adjacent but physically not adjacent to each other in memory. By: Oza Hemant P 39 S-304 3.2 Comparison between Sequential list and linked list Data Structure and Management  Comparison of Sequential list and linked list is given below: Linked List Sequential List 1. In linked list number of 1. in sequential list number of elements elements in the list is not fixed. in list is fixed. 2. In linked list allocation of 2. In sequential list allocation of memory is done at runtime. memory is done at compile time. 3. Insertion and deletion operation 3. Insertion and deletion operation are are very easy and less time very lengthy and time consuming in consuming in linked list. sequential list 4. In linked list we require pointer 4. In sequential list there is no need to variable which occupies extra use pointer variable so it does not memory space. occupies extra memory space. 5. Searching in a linked list is very 5. Searching is less time consuming in time consuming because we have sequential list because we can use to traverse entire list even if all binary search method to search an the elements in the list are sorted. element which is very efficient. 6. In linked list the elements 6. In sequential list elements are which are logically adjacent need logically and physically adjacent to not be physically adjacent. each other. 7. It is very easy to join or split 7. It is very difficult to split or join two two lists. lists. 3.3 Singly linked linier list  A list is defined as an ordered set which consist of variable number of elements.  A list in which each node contains a link or pointer to next node in the list is known as singly linked linier list or one way chain.  Each node in the list consist of two parts: (1) Information (2) Link Node Info Link  Representation of Singly linked linier list is shown below: By: Oza Hemant P 40 S-304 A Data Structure and Management 2010 B 2002 C 2006 D FIRST=2000 2010 2002 2006  Here, the variable FIRST contains an address of the first node in the list.  The last node of the list does not have any successor node, so it does not contain any address in its link field. In such case a NULL value is stored as an address. NULL indicates end of the list. 3.3.1 Operations on Singly linked linier list  Followings are the operations that can be performed on singly linked linier list. 1. Insert new node at beginning of the list 2. Insert new node at end of the list 3. Insert new node into ordered list 4. Insert new node at specific location in the list. 5. Delete node from beginning of the list 6. Delete node from end of the list. 7. Delete node from any specific position in the list 8. Copy list 9. Count number of nodes in list. 3.4 Circular Linked list  A list in which last node contains a link or pointer to the first node in the list is known as circular linked list.  Representation of circular linked list is shown below: A 2010 B 2002 C 2006 D FIRST=2000 2010 2002 2006  Advantage of circular linked list over singly linked linier list: (1) In singly linked list the last node contains NULL address. So if we are at middle of the list and want to access the first node we can not go backward in the list. Thus every node in the list is not accessible from given node. While in Circular linked list last node contains an address of the first node so every node is in the list is accessible from given node. (2) In singly linked list if we want to delete node at location X, first we have to find out the predecessor of the node. To find out the predecessor of the node we have to search entire list from starting from FIRST node in the list. So in order to delete the node at Location X we also By: Oza Hemant P 41 S-304 Data Structure and Management have to give address of the FIRST node in the list. While in Circular linked list every node is accessible from given node so there is no need to give address of the first node in the list. (3) Concatenation and splitting operations are also efficient in circular linked list as compared to the singly linked list.  Disadvantage: (1) Without some care in processing it is possible to get into the infinite loop. So we must able to detect end of the list.  To detect the end of the list in circular linked list we use one special node called the HEAD node.  Such a circular linked list with HEAD node is shown below: HEAD 2010 B 2002 C 2006 D  Advantage of such a representation is that the list is never empty. 3.5 Doubly Linked Linier list  In Singly linked list we are able to traverse the list in only one direction. However in some cases it is necessary to traverse the list in both directions.  This property of linked list implies that each node must contain two link fields instead of one link field.  One link is used to denote the predecessor of the node and another link is used to denote the successor of the node.  Thus each node in the list consist of three fields: (1) Information (2) LPTR (3) RPTR NODE LPTR INFO RPTR  A list in which each node contains two links one to the predecessor of the node and one to the successor of the node is known as doubly linked linier list or two way chain.  Representation of doubly linked linier list is shown below: L A B C R  L is a pointer denotes the left most node in the list. By: Oza Hemant P 42 S-304  R is a pointer denotes the right most node in the list. Data Structure and Management  The left link of the left most node and right link of right most node are set to NULL to indicate end of the list in each direction.  Advantage: (1) In singly linked list we can traverse only in one direction while in doubly linked list we can traverse the list in both directions. (2) Deletion operation is faster in doubly linked list as compared to the singly linked list. 3.5.1 Doubly linked circular list  A doubly linked list in which left link of the left most node points to the right most node and right link of the right most node points to the left most node is known as Doubly circular linked list.  Representation of doubly circular linked list shown below: A B C  Advantage: (1) Every node is accessible from given node.  Disadvantage: (1) Without some care in processing it is possible to get into the infinite loop. So we must able to detect end of the list.  To detect the end of the list in doubly circular linked list we use one special node called the HEAD node.  Such a doubly circular linked list with HEAD node is shown below: B C 3.5.2 Operations on Doubly linked linier list  Followings are the operations that can be performed on doubly linked linier list. (1) Insert new node in doubly linked linier list (2) Delete node from doubly linked list (3) Traverse doubly linked list. By: Oza Hemant P 43 S-304 3.6 Algorithms for Singly linked linier list. 1. Algorithm to insert new node at beginning of the linked list.  INSERT(X,FIRST) Data Structure and Management  This function inserts a new element X at the beginning of the linked list.  FIRST is a pointer which contains address of first node in the list. 1. [Check for availability stack underflow] If AVAIL = NULL then Write “Availability stack underflow” Return 2. [Obtain address of next free node] NEWAVAIL 3. [Remove free node from availability stack] AVAILLINK (AVAIL) 4. [Is the list is empty?] If FIRST = NULL then INFO (NEW) X LINK (NEW) NULL Else INFO (NEW) X LINK (NEW) FIRST 5. [Assign the address of the Temporary node to the First Node ] FIRSTNEW 6. [Finished] Return (FIRST) 2. Algorithm to insert new node at end of the linked list.  INSERT(X,FIRST)  This function inserts a new element X at the end of the linked list.  FIRST is a pointer which contains address of first node in the list. 1. [Check for availability stack underflow] If AVAIL = NULL then Write “Availability stack underflow” Return 2. [Obtain address of next free node] NEWAVAIL 3. [Remove free node from availability stack] 44 By: Oza Hemant P S-304 AVAILLINK (AVAIL) 4. [initialize field of new node] INFO (NEW) X LINK (NEW) NULL 5. [If list is empty?] If FIRST = NULL then FIRSTNEW 6. [initialize search for last node] SAVEFIRST 7. [Search end of the list] Repeat while LINK (SAVE) ≠ NULL SAVELINK (SAVE) 8. [Set LINK field of last node to NEW ] LINK (SAVE) NEW 9. [Finished] Return (FIRST) 3. Algorithm to insert new node at specific location.  INSPOS(X,FIRST,N) Data Structure and Management  This function inserts a new element N into the linked list specified by address X.  FIRST is a pointer which contains address of first node in the list. 1. [Check for availability stack underflow] If AVAIL = NULL then Write “Availability stack underflow” Return 2. [Obtain address of next free node] NEWAVAIL 3. [Remove free node from availability stack] AVAILLINK (AVAIL) 4. [initialize field of new node] INFO (NEW) N 5. [If list is empty?] If FIRST = NULL then LINK (NEW) NULL FIRSTNEW 6. [Search the list until desired address found] By: Oza Hemant P 45 S-304 SAVEFIRST Data Structure and Management Repeat while LINK (SAVE) ≠ NULL and SAVE ≠ X PREDSAVE SAVELINK (SAVE) 7. [Node found] LINK (PRED) NEW 8. [Finished] Return (FIRST) 4 Algorithm to insert new node in ordered linked list.  INSORD(X,FIRST)  This function inserts a new element X into the ordered linked list.  FIRST is a pointer which contains address of first node in the list. 1. [Check for availability stack underflow] If AVAIL = NULL then Write “Availability stack underflow” Return 2. [Obtain address of next free node] NEWAVAIL 3. [Remove free node from availability stack] AVAILLINK (AVAIL) 4. [initialize field of new node] INFO (NEW) X 5. [If list is empty?] If FIRST = NULL then LINK (NEW) NULL FIRSTNEW 6. [Does the new node precede all nodes in the list?] If INFO (NEW) ≤ INFO (FIRST) then LINK (NEW) FIRST FIRSTNEW 7. [Initialize search pointer] SAVEFIRST 8. [Search for predecessor of new node] Repeat while LINK (SAVE) ≠ NULL and INFO (LINK (SAVE)) ≤ INFO (NEW) SAVELINK (SAVE) By: Oza Hemant P 46 S-304 9. [Set LINK field of new node and its predecessor ] LINK (NEW) LINK (SAVE) LINK (SAVE) NEW 10. [Finished] Return (FIRST) 5. Algorithm to delete first node of linked list  DELFIRST(FIRST)  This function deletes a first node from the list. Data Structure and Management  FIRST is a pointer which contains address of first node in the list. 1. [Check for empty list] If FIRST = NULL then Write “List is empty” Return 2. [Check for the element in the list and delete it] If LINK (FIRST) = NULL then YINFO (FIRST) FIRSTNULL Else (Assign the address pointed by FIRST pointer to TEMP pointer) TEMPFIRST YINFO (TEMP) (Assign the address of next node to FIRST pointer) FIRSTLINK (TEMP) 3. [Finished] Return (FIRST) 6. Algorithm to delete last node of linked list  DELLAST(FIRST)  This function deletes a last node from the list.  FIRST is a pointer which contains address of first node in the list. 1. [Check for empty list] If FIRST = NULL then Write “List is empty” Return 2. [Check for the element in the list and delete it] If LINK (FIRST) = NULL then By: Oza Hemant P 47 S-304 YINFO (FIRST) FIRSTNULL Else Data Structure and Management (Assign the address pointed by FIRST pointer to TEMP pointer) TEMPFIRST Repeat while LINK (TEMP) ≠ NULL PREDTEMP TEMPLINK (TEMP) 3. [Delete Last Node] YINFO (TEMP) LINK (PRED) NULL 4. [Finished] Return (FIRST) 7. Algorithm to delete specific node from linked list.  DELPOS(FIRST,X)  This function deletes a node from specific location from the list.  FIRST is a pointer which contains address of first node in the list. 1. [Check for empty list] If FIRST = NULL then Write “List is empty” Return 2. [If there is only one node?] If LINK (FIRST) = NULL then YINFO (FIRST) FIRSTNULL 3. [If list contains more than one node?] (Assign the address pointed by FIRST pointer to TEMP pointer) TEMPFIRST Repeat while LINK (TEMP) ≠ NULL and TEMP ≠ X PREDTEMP TEMPLINK (TEMP) 4. [Node found?] If TEMP ≠ X then Write “Node not found” Else By: Oza Hemant P 48 S-304 YINFO (TEMP) LINK (PRED) LINK (TEMP) 5. [Finished] Return (FIRST) 8. Count number of nodes in linked list  COUNT(FIRST)  This function counts number of nodes in the list. Data Structure and Management  FIRST is a pointer which contains address of first node in the list.  [Empty List?] If FIRST = NULL then Write “List is empty”  [Initialize] Count1 SAVEFIRST  [process the list until end of the list] Repeat while LINK (SAVE) ≠ NULL CountCount + 1 SAVELINK (SAVE)  [Finished] Return (Count) 9. Algorithm to copy linked list  COPY(FIRST)  This function copy one list into another list.  FIRST is a pointer which contains address of first node in the list.  BEGIN is a pointer which points to the address of first node in the list.  [Empty List?] If FIRST = NULL then BEGINNULL  [Copy First Node] If AVAIL = NULL then Write “Availability stack underflow” Else NEWAVAIL AVAILLINK (AVAIL) FIELD (NEW) INFO (FIRST) By: Oza Hemant P 49 S-304 BEGINNEW    [Initialize traversal] SAVEFIRST [Move to next node if not at end of the list] Repeat thru step 6 while LINK (SAVE) ≠ NULL [Update predecessor and save pointers] PREDNEW SAVELINK (SAVE)  [Copy node] If AVAIL = NULL then Write “availability stack underflow” Else NEWAVAIL AVAILLINK (AVAIL) FIELD (NEW) INFO (SAVE) PTR (PRED) NEW   [Set link of last node and return] PTR (NEW) NULL [Finished] Return 3.7 Algorithms for Doubly linked linier list 1. Algorithm to insert new node in doubly linked list  DOUBLEINS(L, R, M, X)  This function inserts an element into double linked list. Data Structure and Management  L is a pointer which contains address of left most node in the list.  R is a pointer which contains address of right most node in the list.  X contains the value to be inserted in to the list  [Create new node] If AVAIL = NULL then Write “Availability stack underflow” Else NEWAVAIL AVAILLINK (AVAIL)  [Copy information field] INFO (NEW) X By: Oza Hemant P 50 S-304  [insertion into an empty list] If R = NULL then LPTR (NEW) RPTR (NEW) NULL LRNEW  [left most insertion] If M = L then LPTR (NEW) NULL RPTR (NEW) M LPTR (M) NEW LNEW  [insert in middle] LPTR (NEW) LPTR (M) RPTR (NEW) M LPTR (M) NEW RPTR (LPTR (NEW)) NEW  [Finished] Data Structure and Management 2. Algorithm to delete node from doubly linked list.  DOUBLEDEL(L, R,OLD)  This function inserts an element into double linked list.  L is a pointer which contains address of left most node in the list.  R is a pointer which contains address of right most node in the list.  OLD contains address of node which we want to delete.  [Underflow] If R = NULL then Write “Underflow”  [Delete Node] If L = R then LRNULL Else if OLD = L then LRPTR (L) LPTR (L) NULL Else if OLD = R then RLPTR(R) RPTR(R) NULL Else By: Oza Hemant P 51 S-304 RPTR (LPTR (OLD)) RPTR (OLD) LPTR (RPTR (OLD)) LPTR (OLD)  [Return Delete node] Restore (OLD) Return 3.8 Application of Linked list Data Structure and Management  Linked lists are used as a building block for many other data structures, such as stacks, queues and their variations.  The "data" field of a node can be another linked list. By this device, one can construct many linked data structures with lists; this practice originated in the Lisp programming language, where linked lists are a primary (though by no means the only) data structure, and is now a common feature of the functional programming style.  Sometimes, linked lists are used to implement associative arrays, and are in this context called association lists. There is very little good to be said about this use of linked lists; they are easily outperformed by other data structures such as self-balancing binary search trees even on small data sets. However, sometimes a linked list is dynamically created out of a subset of nodes in such a tree, and used to more efficiently traverse that set.  Polynomial manipulation  Linked dictionary  Multiple precision arithmetic By: Oza Hemant P 52 S-304 Data Structure and Management Exercise (1) Difference between static and dynamic allocation (2) Write an algorithm to insert a new node at the end of linked list (3) Compare linked list with array (4) Write an algorithm to insert new node at the beginning of the list (5) Application of linked list (6) Explain advantage of doubly linked list over singly linked list (7) Write an algorithm to insert new node at the beginning of the doubly linked list (8) Short note: Doubly linked list (9) Write an algorithm to copy linked list (10) Write an algorithm to search node in singly linked list (11) Explain singly linked list and doubly linked list. Advantage of doubly linked list on singly linked list (12) Write an algorithm to insert an element in ordered linked list. (13) Difference between linked list and sequential list (14) Explain : (a) singly linked list (b) doubly linked list (c) circular linked list (15) Write an algorithm to delete first node in a singly linked list (16) Write an algorithm to delete last node in a singly linked list (17) Write an algorithm to delete element from circular linked list By: Oza Hemant P 53 S-304 Data Structure and Management By: Oza Hemant P 54 S-304 4.1 Sequential Search or Linier Search Data Structure and Management  This method is used to find out element in an unordered list.  Suppose the list L consist of N elements and we want to find the element from the list.  In this technique first the value of the element is compared with the value of the first element in the list, if match is found then the search is terminated.  If match is not found then next element from the list is fetched and compared with the element.  This process is repeated until match is found or all the element in the list is compared.  Consider the following example:  The list L consist of 5 elements as shown below: Index 1 2 3 4 5 Element 22 33 05 66 44  Suppose we want to find 05 in the list. So 05 is compared with the first element in the list which is 22. Index 1 2 3 4 5 Element 22 33 05 66 44 05 Match not found.  Since match is not found next element is fetched from the list and compared with 05 and this process is repeated until match found or all elements in the list is compared as shown below. Index 1 2 3 4 5 Index 1 2 3 4 5 By: Oza Hemant P Element 22 33 05 66 44 Element 22 33 05 66 44 05 Match not found 05 Match Found 55 S-304 4.1.1 Algorithm for sequential search method  LINER_SEARCH(K, N, X) Data Structure and Management  These function searches the list K consist of N elements for the value of X. 1. [Initialize search] I1 K [N+1] X 2. [Search the Vector] Repeat while K [I] ≠ X II+1 3. [Successful Search?] If I = N+1 then Write “Unsuccessful Search” Return 0 Else Write “Successful Search” Return 1 4.2 Binary Search  This method is used to find out element in an ordered list.  It is very efficient method to search element.  Let low represents lower limit of the list and high represents the higher limit of the list.  First we calculate the value of middle as: Mid = (Low + High)/2 Low Middle High 1 2 3 4 5 6 7 8 9 10 75 151 203 275 318 489 524 591 647 727  Now we compare the value of the middle element with the element to be searched.  If the value of the middle element is greater then the element to be searched then the element will exist in lower half of the list. So take high = mid – 1 and find value of the middle in this new interval.  If the value of the middle element is smaller then the element to be searched then the element will exist in upper half of the list. So take low = mid + 1 and find value of the middle in this new interval. By: Oza Hemant P 56 S-304 Data Structure and Management  This process is repeated until entire list is searched or the element is found.  Suppose we want to find the element 275 in the above list.  First, mid = (low + high)/2 = (1+10)/2 =5  Here the value of middle element is 318 which is greater then 275 so the element is in the lower half of the list. Take high = mid – 1 = 5-1=4  Now mid = (low + high)/2 = (1 + 4)/2 =2  Here the value of the middle element is 151 which is smaller then 275 so the element is in the upper half of the list. Take low = mid + 1 = 2+1 = 3  Now mid = (low + high)/2 = (3+4)/2 =3  Here the value of the middle element is 203 which is smaller then 275 so the element is in the upper half of the list. Take low = mid + 1 = 3+1 = 4  Now mid = (low + high)/2 = (4+4)/2 =4  Here the value of the middle element is 275 which we want to find. 4.2.1 Algorithm for Binary Search Method.  BINARY_SEARCH(K, N, X)  These function searches the list K consist of N elements for the value of X.  LOW, HIGH and MIDDLE denotes the lower, upper and middle limit of the list. 1. [Initialize] LOW1 HIGHN 2. [Perform Search] Repeat thru step 4 while LOW ≤ HIGH 3. [Obtain Index of midpoint interval] MIDDLE [(LOW + HIGH)/2] By: Oza Hemant P 57 S-304 4. [Compare] If X < K [MIDDLE] then HIGH  MIDDLE – 1 Else if X > K [MIDDLE] then LOWMIDDLE+1 Else Write “Successful Search” Return (MIDDLE) 5. [Unsuccessful Search] Write “Unsuccessful Search” Return 0 Data Structure and Management 4.2.2 Compare Sequential Search with Binary Search. Sequential Search Binary Search (1) Sequential Search is used to find (1) Binary Search is used to find elements from unordered list (2) This technique is less efficient elements from ordered list. (2) This technique is more efficient (3) The order of sequential search is (3) The order of Binary Search is O(N). O(Log2N) 4.3 Classification of sorting method.  Internal sort: 1. Bubble sort 2. Quick sort 3. selection sort 4. Heap sort 5. insertion sort 6. shell sort  External sort: 1. Merge sort 2. Radix sort By: Oza Hemant P 58 S-304 4.3.1 Selection sort Data Structure and Management  Beginning with the first record in the array, a search is performed to locate the element which has the smallest value. When this element is found, it is interchanged with the first element in the array. Thus the element with the smallest value is placed at first position in the array.  Now we start with second record in the array and search the element which has the second smallest value in the array. When this element is found it is interchanged with the second element in the array. Thus the element with the second smallest value is placed at second position in the array.  This process continues until all the elements in the array are arranged in ascending order.  Consider the following example: 42 23 74 11 65 58 94 36 99 87 i Ki 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 42 23 74 11 65 58 94 36 99 87 11 23 74 42 65 58 94 36 99 87 11 23 74 42 65 58 94 36 99 87 11 23 36 42 65 58 94 74 99 87 11 23 36 42 65 58 94 74 99 87 11 23 36 42 58 65 94 74 99 87 11 23 36 42 58 65 94 74 99 87 11 23 36 42 58 65 74 94 99 87 11 23 36 42 58 65 74 87 99 94 11 23 36 42 58 65 74 87 94 99 Performance:  During first pass we required n-1 comparisons. During second pass we required n-2 comparisons and during ith pass we required n-i comparisons. So total number of comparisons is given as: n-1 ∑ (n-i) = n(n-1)/2 i=1  Thus the order of comparisons is proportional to n2 i.e O(n2) Ex: sort following data using selection sort method 5 2 35 9 3 -2 52 0 By: Oza Hemant P 59 S-304 4.3.2 Bubble Sort Data Structure and Management  In bubble sort method during the first pass element R1 and R2 are compared and if they are out of order then they are interchanged. This process is repeated for elements R2 and R3 and so on.  After the end of first pass the record with the largest value is placed at nth position.  During second pass the record with next largest value is placed at n-1 position and so on.  After each pass we check for the interchange. If no interchanges occurred then the array must be sorted and no further pass is required.  Consider the following example: 42 23 74 11 65 58 94 36 99 87 i Ki 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 10 42 23 74 11 65 58 94 36 99 87 23 42 11 65 58 74 36 94 87 99 23 11 42 58 65 36 74 87 94 99 11 23 42 58 36 65 74 87 94 99 11 23 42 36 58 65 74 87 94 99 11 23 36 42 58 65 74 87 94 99 11 23 36 42 58 65 74 87 94 99 Performance:  During first pass we required n-1 comparisons. During second pass we required n-2 comparisons and during ith pass we required n-i comparisons. So total number of comparisons is given as: n-1 ∑ (n-i) = n (n-1)/2 i=1  Thus the order of comparisons is proportional to n2 i.e O(n2) Ex: sort following data using bubble sort method 5 2 35 9 3 -2 52 0 By: Oza Hemant P 60 S-304 4.3.3 Simple Merge Sort Ans: Data Structure and Management  Simple merge sort is used to merge two ordered list in a single ordered list.  Suppose we have to sorted table with name table1 and table 2.  In simple merge sort we compare the elements of both table and the element which has the smallest value is placed in a new table.  This process is repeated until all the elements from both the table are placed in a new table.  Consider the following example: Table 1: 11 Table 2: 9 23 25 42  First we compare the elements of both the table and the element which has the smallest value is placed in a new table.  So the trace is as follow: Table 1: 11 23 42 Table 2: 25 New Table: 9 Table 1: 23 42 Table 2: 25 New Table: 9 11 Table 1: 42 Table 2: 25 New Table: 9 Table 1: 42 Table 2: New Table: 9 Table 1: Table 2: New Table: 9 11 23 25 42  We store the two sorted table in a common vector. Thus two sorted tables in a common vector can be stored as follow: 11 23 42 9 25 11 23 25 11 23 First Second Third  Here First to second-1 represents the first table and second to third represent second table.  We required temporary vector of the same size to hold the result of sorting By: Oza Hemant P 61 S-304 Data Structure and Management  We can also merge k ordered list into single sorted list. Such a merging process is known as multiple merging or k- way merging. 4.3.4 Quick sort OR Partition Exchange sort  In this method we pick the first element from the array and place it in it‟s proper position in the array such that all the elements preceding the element having smaller value and all the elements following the element having larger value.  Thus the table is divided into two sub tables.  The same process is then applied to each of the sub tables and repeated until all records are placed in their final position.  Consider the following example: K1 {42 {11 11 11 11 11 11 11 11 11 4.3.5 Radix sort  In this method we use ten pockets for the digits 0 to 9.  Consider the following set of data: 42 23 74 11 65 57 94 36 99 87 70 81 61  During first pass we separate the unit digit of the number and place the number in to appropriate pocket according to its unit digit.  For example the first number is 42 so we separate the unit digit of the number 42 which is 2 so we place the number in pocket 2. Same procedure is repeated for remaining numbers.  After first pass the numbers in each pockets are as follow: 61 81 70 Pocket: 0 By: Oza Hemant P 11 1 94 87 99 8 9 62 K2 23 23 {23 23 23 23 23 23 23 23 K3 74 36} 36} {36} 36 36 36 36 36 36 K4 11 42 42 42 42 42 42 42 42 42 K5 65 {65 {65 {65 {58} 58 58 58 58 58 K6 58 58 58 58 65 65 65 65 65 65 K7 94 94 94 94 {94 {94 {87 {74} 74 74 K8 36 74 74 74 74 74 74} 87 87 87 K9 99 99 99 99 99 99 94 94 94 94 K10 87} 87} 87} 87} 87} 87} {99} {99} {99} 99 42 23 74 65 36 57 2 3 4 5 6 7 S-304 Data Structure and Management  Now arrange the number according to their pocket. The numbers after first pass are as follows: 70 11 81 61 42 23 74 94 65 36 57 87 99  During second pass we separate the next higher digit of the number and place the number in to appropriate pocket according to its digit.  After second pass the numbers in each pockets are as follow: 65 11 Pocket: 0 1 23 36 42 57 61 2 3 4 5 6 74 87 99 70 81 94 7 8 9  Now arrange the number according to their pocket. The numbers after second pass are as follows: 11 23 36 42 57 61 65 70 74 81 87 94 99  The same process is repeated until all the elements are sorted. Ex-1 Give tracing for following data using radix sort method. 42 24 40 10 64 57 93 35 98 86 70 4.3.9 Algorithms for sorting methods (1) Algorithm for selection sort method.  SELECTION_SORT(K,N)  These function sorts elements of array K consist of N elements.  PASS denotes the pass index and the position of the first element in the vector which is to be examined during particular pass.  MIN_INDEX denotes the position of the smallest element encountered thus far in a particular pass. 1. [Loop on pass index] Repeat thru step 4 for PASS = 1, 2, 3… N-1 2. [initialize minimum index] MIN_INDEX PASS 3. [Make a pass and obtain element with the smallest value] Repeat for I = PASS+1 to N If K [I] < K [MIN_INDEX] then MIN_INDEXI 4. [Exchange elements] If MIN_INDEX ≠ PASS then K [PASS] 5. [finished] Return By: Oza Hemant P 63 K [MIN_INDEX] S-304 (2) Algorithm for Bubble sort method  BUBLE_SORT(K,N) Data Structure and Management  These function sorts elements of array K consist of N elements.  PASS denotes the pass index.  LAST denotes the last unsorted element.  EXCHS counts the total number of exchange made during pass 1. [Initialize] LASTN 2. [Loop on pass index] Repeat thru step 5 for PASS = 1, 2… N-1 3. [Initialize exchange counter for this pass ] EXCHS0 4. [Perform pair wise comparisons on unsorted elements ] Repeat for I = 1, 2… LAST-1 If K [I] > K [I+1] then K [I] K [I+1] EXCHSEXCHS+1 5. [Exchange made on this pass?] If EXCHS = 0 then Write “Mission Complete” Else LASTLAST-1 6. [Finished] Return (Minimum number of pass required) (3) Algorithm for simple merge sort method.  SIMPLE_MERGE(K,FIRST,SECOND,THIRD)  This function merges two sorted table into single sorted table.  Here FIRST to SECOND-1 represents the first table and SECOND to THIRD represent second table  TEMP is the temporary vector. 1) [Initialize] IFIRST JSECOND L0 2) [Compare corresponding elements and output the smallest] By: Oza Hemant P 64 S-304 Repeat while I KEY JJ-1 If I < J then K [I] Else FLAGfalse K [LB] K [J] K [J] Data Structure and Management Call QUICK_SORT (K, LB, J-1) Call QUICK_SORT (K, J+1, UB) 3) [Finished] (5) Algorithm for Radix sort  RADIX_SORT(FIRST,N)  1. [Perform sort] Repeat thru step 4 for J= 1, 2… M 2. [Initialize the pass] Repeat for I = 0 to 9 T [I]B[I]NULL RFIRST 3. [Distribute each record in the appropriate pocket] Repeat while R ≠ NULL Dbj NEXTLINK(R) If T (D) = NULL then T [D] B [D] R Else LINK (T [D]) R By: Oza Hemant P 66 S-304 T [D]R LINK(R) NULL RNEXT 4. [Combine pockets] P0 Repeat while B [P] = NULL PP+1 FIRSTB [P] Repeat for I = P+1, P+2…9 PREVT [I-1] If T [I] ≠ NULL then LINK (PREV) B [I] Else T [I] PREV 5. [Finished] Return (6) Algorithm for Insertion sort  INSERTIONSORT(L, N)  L is an array consist of N elements 1. [Initialize] L [0]-0 2. [Loop on Array] Repeat thru step 5 for I = 1 to N 3. [Set temporary variable and pointer] TEMPL [I] PointerI-1 4. [Compare elements] While (TEMP< L [Pointer]) L [Pointer + 1]  L [Pointer] Pointer  Pointer -1 5. [Insert element to its proper place] L [Pointer]  TEMP 6. [Finished] (7) Algorithm for shell sort  SHELLSORT(K,N) By: Oza Hemant P Data Structure and Management 67 S-304  K is an array consist on N elements 1. [Initialize Gap] Gap  N / 2 2. Repeat thru step 6 while (Gap = Gap/2) 3. [Initialize Swap variable] Swap0 4. [Repeat through step 6 while(Swap)] 5. [Repeat through step 6 for I = 0 to I < (N - Gap)] 6. if K[I] > K[I + Gap] then TempK [I] K [I] K [I + Gap] K [I + Gap] Temp Swap1 7. [Finished] 4.4 Hashing Functions  Hashing Functions: Data Structure and Management  Hashing provides direct access of records from the file no matter where the record is in the file.  This technique uses the hashing function say H which maps the Key to the particular address of the record.  Thus hashing function provides key to address transformation.  A hashing function H maps the Key space K into an address space.  The hashing function generates an address or location by performing some arithmetic or logical operation on given key.  Example: suppose there are 60 students in the classroom and we want to maintain their record. We have to store the record in an array. In this case the array index ranges from 0-59. We may use this index as the registration number of student. For example if a student has a registration number S15280531 then the record is stored at address 31.  Hashing function is of two types: (1) Distribution dependent hashing function (2) Distribution independent hashing function  Some of the most widely used hashing functions are described below: (1) Division Method (2) The Mid square Method (3) The Folding Method (4) Digit Analysis By: Oza Hemant P 68 S-304 (5) The Length Dependent Method (6) Algebraic Coding (7) Multiplicative Hashing Data Structure and Management  (1) Division Method: one of the hashing function uses the division method, which is defined as: H(x) = x mod m + 1 For example: H (35) = 35 mod 11 + 1 = 2 + 1 = 3 The Division method generates a key value which belongs to the set {1, 2… m}  (2) Mid square Method: In this method a key is multiplied by itself and the address is obtained by selecting an appropriate number of digits from the middle of the square For example: consider a six digit key 123456. Squaring this key result in the value 15241383936. if a three digit address is required then position 5 to 7 could be chosen which gives 138.  (3) Folding Method: in this method a key is partitioned into parts. The length of each part is similar to the length of the address required. These parts are added together and final carry is ignored to produce the address. For example if a key 35678943 is transformed into 2 digit address then we have: 35 + 67 + 89 + 43 = 234 = 34. This method is known as fold shifting method. Another method is fold boundary method in which if a key 35678943 is transformed into 2 digit address then we have: 53 + 76 + 98 + 34 = 261 = 61. 4.5 Collision resolution techniques.  Collision Resolution: When a hashing function maps several keys to same address space then it is known as collision.  For Example: Suppose we are storing the records in an array which ranges from 0 to 99.if a hashing function is H(k) = k % 100 then this function will produces same address for the keys 15433 and 26733. in this case collision is encountered.  The techniques used to resolve this collision is known as collision resolution technique.  Some popular collision resolution techniques are as follows: 1. Linier Probing 2. Rehashing 3. Quadratic Probing 4. Chaining By: Oza Hemant P 69 S-304 Data Structure and Management  (1) Linier Probing: If a record with key x is mapped to address location d and that location is already occupied by another key then other locations in the table are examined until a free memory location is found.  For Example: Suppose we have to insert the following key values with hashing function H(k) = k % 100 50904, 78907, 68403, 86704, 72308 Index 00 01 02 03 04 05 06 07 08 Key Empty Empty Empty 68403 50904 86704 Empty 78907 72308  The array must be circular in order to search an empty location.  (2) Rehashing: If a hash function results in a collision then we use the secondary hash function to calculate the address. This is known as rehashing and this procedure is repeated until an empty location is found.  In above example the hash function H (k) = k % 100 produces collision for the key 86704 so we use secondary hash function as: H (k) = (k + constant) % 100. Let constant = 1. Then this secondary function produces the address (86704+1) % 100 = 05. This location is empty so we put the key 86704 at location 05.  (3)Quadratic Probing If there is a collision at hash address h, this method probes the table at locations h+1, h+4, h+9…., that is h + i2 (mod hash size). That is the increment function is i2. This substantially reduces clustering.  (4)Key dependent increment Rather than having the increment depend on the number of probes already made, we can let it Be some simple function of the key itself. For example, we could truncate the key to a Character and use its code as the increment. single method By: Oza Hemant P 70 S-304 Data Structure and Management A good approach, when the remainder after division is taken as the hash function, is to let the increment depend on the quotient of the same division.  (5)Random probing A final method is to use a pseudorandom number generator to obtain the increment. The generator used should be one that always generates the same sequence provided it starts with The same need. This method is excellent in avoiding clustering, but is likely to be slower than The others. Exercise (1) Write a program in C to sort N Numbers in descending order using bubble sort method (2) Explain quick sort method. Take some data and show tracing (3) Write C program for simple merge sort (4) Write a program in C to search given name from a list of name stored in array. Use binary search (5) Write an algorithm for bubble sort method (6) Write an algorithm for Quick sort method (7) Explain radix sort method. Give tracing for following data: 42 24 40 10 64 57 93 35 98 86 70 (8) Short note : hashing function (9) Write an algorithm for simple merge sort (10) Explain bubble sort method using following data: 5 2 35 9 3 -2 52 0 (11) Explain selection sort method using following data: 5 2 35 9 3 -2 52 0 (12) List collision resolution technique and explain any one (13) Short note: Quick Sort (14) Compare sequential search with binary search (15) Explain simple merge sort method with tracing (16) Short Note: Binary search (17) Short note: shell sort (18) What is collision resolution technique (19) Write an algorithm for selection sort method (20) Explain quick sort with example (21) Explain radix sort with algorithm (22) What is heap sort? Write algorithm for heap sort By: Oza Hemant P 71 S-304 Data Structure and Management By: Oza Hemant P 72 S-304 5.1 Definition and Concepts: (A) Graph: Data Structure and Management  A graph G consists of a nonempty set V called the set of nodes and a set E called the set of edges, and a mapping from the set of edges E to a set of pairs of elements of V.  Here both sets V and E of the graph are finite.  We can write graph as G = (V, E).  Every edge of the graph G can associate a pairs of nodes of the graph. If an edge x is in E then we can associate a pair of nodes (u, v) using x. (B) Adjacent Node:  Any two nodes in the graph which are connected by an edge in the graph are called an adjacent nodes. (C) Directed Edge:  In a graph G = (V, E), an edge which is directed from one node to another is called a directed edge. (D) Undirected Edge:  In a graph G = (V, E), an edge which has no specific direction from one node to another is called an undirected edge. (E) Directed Graph Or Digraph:  A graph in which every edge is directed is called directed graph or digraph. (F) Undirected Graph:  A graph in which every edge is undirected is called undirected graph. By: Oza Hemant P 73 S-304 (G) Mixed Graph: Data Structure and Management  A graph in which some of the edges are directed and some of the edges are undirected then the graph is called mixed graph. (H) Loop:  An edge of a graph which joins a node to itself is called loop. (I) Parallel edge:  If a pair of node is connected by more then one edges then such edges are known as parallel edges. (J) Multi graph:  A graph which contains some parallel edges is called a multi graph. (K) Simple Graph:  A graph in which there is no more then one edge between a pair of nodes then such a graph is called a simple graph. By: Oza Hemant P 74 S-304 (L) Weighted Graph: Data Structure and Management  A graph in which weights are assigned to every edge is called a weighted graph. 2 3 4 1 (M) Isolated Node:  In a graph a node which is not adjacent to any other node is called isolated node. (N) Null Graph:  A graph which contains only isolated node is called Null Graph. (0) Out Degree:  In a directed graph, for any node v the number of edges which have v as their initial node is called the out degree of the node v. (P) In Degree:  In a directed graph, for any node v the number of edges which have v as their terminal node is called the in degree of the node v. (Q) Total Degree:  The sum of the out degree and in degree of a node V is called the total degree of node V. (Q1) PATH:  A path is said to traverse through the nodes appearing in sequence, starting from initial node of the first edge and ending in the terminal node of the last edge in the sequence. By: Oza Hemant P 75 S-304 (Q2) Length of the Path: Data Structure and Management  The number of edges appearing in the sequence of path is called the length of the path. (Q3) Cyclic Path:  The path which originates and ends in the same node is called a cyclic path. A cycle is called elementary if it does not traverse through any nodes more than once. (Q4) Acyclic Path:  A path which originated from a node and do not end in the same node is called acyclic path. (R) Tree:  A tree is defined as a finite set of one or more nodes such that   There is a special node called the root node R. The remaining nodes are divided into n ≥ 0 disjoint sets T1, T2, …… Tn, where each of these sets are tree. T1, T2…., Tn is called the sub tree of the root R (S) Forest:  A forest is a set of n ≥ 0 disjoint trees. (T) Root Node:  In a directed tree a node which has in degree 0 is called the root node. (U) Leaf Node:  In a directed tree a node which has out degree 0 is called the leaf node or terminal node. By: Oza Hemant P 76 S-304 (V) Directed Tree: Data Structure and Management  A directed tree is an acyclic digraph which has one node called the root node with in degree 0 while all other nodes have in degree 1. R (W) m-ary Tree:  If in a directed tree the out degree of every node is less then or equal to m then the tree is called the m – ary tree. (X) Complete m - ary Tree:  if the out degree of every node is exactly equal to m or 0 and the number of nodes at level i is mi-1 then the tree is called full or complete m – ary tree. (Y) Binary Tree:  If in a directed tree the out degree of every node is less then or equal to 2 then the tree is called the binary tree. (Z) Complete Binary Tree:  if the out degree of every node is exactly equal to 2 or 0 and the number of nodes at level i is 2i-1 then the tree is called full or complete binary tree. (A1) Ordered Tree:  If in a directed tree an ordering of the node at each level is prescribed then such a tree is called an ordered tree. R V1 V0 V4 V2 By: Oza Hemant P V3 V5 V6 77 S-304 Definitions related with tree (A2) In degree: Data Structure and Management  Number of edges terminated at given node is called in degree of node. (A3) Out degree:  Number of edges emerging from given node is called out degree of node. (A4) Edge:  Two nodes are connected by edge. It is the connection of two nodes. (A5) Level:  In a tree the distance between two nodes are represent as level. (A6) Depth:  Maximum level number in a tree is called the depth of the tree. (A7) Height:  Total number of level in a tree is called the height of the tree. (A8) Weight:  Total number of leaf nodes in a tree is called the weight of the tree. (A9) Strictly Binary Tree:  If every non leaf node in a binary tree has nonempty left and right sub tree is called strictly binary tree. 5.2 Difference between root node and leaf node.  Root node has the in degree 0 while the leaf node has the out degree 0 R Root Node with in degree 0 Leaf Node Leaf Node By: Oza Hemant P 78 S-304 5.3 Comparison of Tree and Linked list  Linked List Tree Data Structure and Management (1) Linked list is an example of (1) Tree is an example of non linier linier data structure. data structure. (2) Searching time is more (2) Searching time is less required in required in linked list. Because tree because we can search an element we must have to traverse each using binary search method. node sequentially in linked list even if the list is sorted. 5.4 Representation of binary tree.  We can represent binary tree in memory by two methods: (1) Array implementation (2) Linked implementation 5.4.1 Array implementation of Binary tree  While implementing binary tree we have to consider two cases: (1) Array implementation of complete binary tree:  A complete binary tree is a tree in which there is one node at the root level , two nodes at level 2, four nodes at level 3 etc…  Example of such a tree and its array implementation is shown below: 1 A Level 1 3 2 B E Level 2 4 C Index Info 1 2 3 4 5 5 D 6 F 7 G Level 3 6 7 A B E C D F G  In this type of implementation the location of left children of node I is given by 2*I and the location of right children of node I is given by 2*I + 1.  For example: consider the node 3 in above example which is E. The location of left child is given by 2*I = 2*3 = 6 which is F. location of right child is given by 2*I + 1 = 2*3 + 1 = 7 which is G By: Oza Hemant P 79 S-304 Data Structure and Management  Similarly the location of the parent of node j is given by TRUNC (j/2). For example location of parent of node 4 and 5 is 2.  Thus to represent complete binary tree with 2n-1 node we require 2n-1 array elements. (2) Array implementation of incomplete binary tree:  Now consider the tree which has number of nodes less than or more than 2n-1  Array implementation of such a tree is shown below: A I 1 B 2 C 3 F 4 D 5 E G 8 9 10 11 H INDEX 1 INFO 2 3 4 5 6 7 8 9 10 11 12 13 14 15 A B I C F - - D E G H - - - - In this type of implementation the location of left children of node I is given by 2*I and the location of right children of node I is given by 2*I + 1.  Thus location of left children and right children of node 3 is given by 2*I = 2*3 = 6 and 2*I + 1 = 2*3 + 1 = 7. But node 3 has no left or right node so this memory location becomes wastage in array implementation.  Thus this method is not efficient for incomplete binary tree. 5.4.2 Linked implementation of Binary tree.  In binary tree there is a node called root node. This root node again has two parts left sub tree and right sub tree.  Thus a node in binary tree is represented as below: LPTR INFO RPTR  Each node have three parts: (1) Info: information of node (2) LPTR: pointer which points to left child of the node. (3) RPTR: pointer which points to right child of the node.  If a node which has no left or right child then its LPTR and RPTR fields are represented with NULL.  Example: By: Oza Hemant P 80 S-304 A Data Structure and Management B E C D F G The above tree can be represented in linked implementation as below: A B E C D F G 5.5 Binary Search Tree:  A tree in which all the elements in the left sub tree of a node N are less than the content of node N and all the elements in the right sub tree of node N are greater than the content of node N is called Binary search tree.  Example: 45 35 68 15 38 65 72 5.6 Operations on Binary Search Tree:  Following operations can be performed on Binary Search tree: 1. Traversal 2. Insertion 3. Deletion 4. Search 5. Copy 5.6.1 Traversal operation of Binary Search Tree By: Oza Hemant P 81 S-304 5.6.1.1 Traversal: Data Structure and Management  Traversal is the method of processing every nodes in the tree exactly once in a systematic manner. 5.6.1.2 Types of Traversal: There are three different types of tree traversal. 1. Preorder Traversal 2. In order Traversal 3. Post order Traversal  (1) Preorder Traversal:  The preorder traversal follows three steps: 1. Process the root node first. 2. Traverse the left sub tree in preorder. 3. Traverse the right sub tree in preorder.  Example: A B C D E F G Preorder Traversal: A B D E C F G  (2) In order Traversal:  The In order traversal follows the three steps: 1. Traverse the left sub tree in inorder. 2. Process the root node. 3. Traverse the right sub tree in inorder.  Example: A B C D E F G InOrder Traversal: D B E A F C G  (3) Post order Traversal: By: Oza Hemant P 82 S-304  The Post order traversal follows the three steps: 1. Traverse the left sub tree in Post order. 2. Traverse the right sub tree in Post order. 3. Process the root node.  Example: A Data Structure and Management B C D E F G Post Order Traversal: D E B F G C A 5.6.1.3 Examples of tree traversal Ex-1: Travers the following Tree in Pre Order, In Order, Post Order. G D C B F M E Pre Order: G D B F E C M L P In Order: B D E F G C L M P Post Order: B E F D L P M C G L P Ex-2: Travers the following Tree in Pre Order, In Order, Post Order. A B E C D G H F I By: Oza Hemant P 83 S-304 Pre Order: A B D G C E H I F In Order: D G B A H E I C F Post Order: G D B H I E F C A Data Structure and Management Ex-3: Travers the following Tree in Pre Order, In Order, Post Order. M E P B L N V A D T Z Pre Order: M E B A D L P N V T Z In Order: A B D E L M N P T V Z Post Order: A D B L E N T Z V P M 5.6.1.4 Algorithms for Binary Tree Traversal (1) Pre Order Traversal:  PREORDER(T)  This function traverses the tree in pre order.  T is a pointer which points to the root node of tree. 1. [Process the root node] If T ≠ NULL then Write (DATA (T)) Else Write “Empty Tree” 2. [Process the left sub tree] If LPTR (T) ≠ NULL then Call PREORDER (LPTR (T)) 3. [Process the right sub tree] If RPTR (T) ≠ NULL then Call PREORDER (RPTR (T)) 4. [Finished] Return By: Oza Hemant P 84 S-304 (2) In Order Traversal:  INORDER(T)  This function traverses the tree in inorder.  T is a pointer which points to the root node of tree. 1. [Check for empty tree] If T = NULL then Write “Empty Tree” 2. [Process the left sub tree] If LPTR (T) ≠ NULL then Call INORDER (LPTR (T)) 3. [Process the root node] Write (DATA (T)) 4. [Process the right sub tree] If RPTR (T) ≠ NULL then Call INORDER (RPTR (T)) 5. [Finished] Return (3) Post Order Traversal:  POSTORDER(T)  This function traverses the tree in post order.  T is a pointer which points to the root node of tree. 1. [Check for empty tree] If T = NULL then Write “Empty Tree” 2. [Process the left sub tree] If LPTR (T) ≠ NULL then Call POSTORDER (LPTR (T)) 3. [Process the right sub tree] If RPTR (T) ≠ NULL then Call POSTORDER (RPTR (T)) 4. [Process the root node] Write (DATA (T)) 5. [Finished] Return By: Oza Hemant P Data Structure and Management 85 S-304 5.6.2 Insertion Operation on Binary Search tree 5.6.2.1 Insertion Operation  Binary search tree has following characteristics: Data Structure and Management (1) All the nodes to the left of root node have value less than the value of root node. (2) All the nodes to the right of root node have value greater than the value of root node.  Suppose we want to construct binary search tree for following set of data: 45 68 35 42 15 64 78  Step 1: First element is 45 so it is inserted as a root node of the tree. 45 Root Node  Step 2: Now we have to insert 68. First we compare 68 with the root node which is 45. Since the value of 68 is greater then 45 so it is inserted to the right of the root node. 45 68  Step 3: Now we have to insert 35. First we compare 35 with the root node which is 45. Since the value of 35 is less then 45 so it is inserted to the left of the root node. 45 35 68  Step 4: Now we have to insert 42. First we compare 42 with the root node which is 45. Since the value of 42 is less then 45 so it is inserted to the left of the root node. But the root node has already one left node 35. So now we compare 42 with 35. Since the value of 42 is greater then 35 we insert 42 to the right of node 35. 45 35 68 42  Step 5: Now we have to insert 15. First we compare 15 with the root node which is 45. Since the value of 15 is less then 45 so it is inserted to the left of the root node. But the root node has already one left node 35. So now we compare 15 with 35. Since the value of 15 is less then 35 we insert 15 to the left of node 35. By: Oza Hemant P 86 S-304 45 Data Structure and Management 35 68 15 42  Step 6: Now we have to insert 64. First we compare 64 with the root node which is 45. Since the value of 64 is greater then 45 so it is inserted to the right of the root node. But the root node has already one right node 68. So now we compare 64 with 68. Since the value of 64 is less then 68 we insert 64 to the left of node 68. 45 35 68 15 42 64  Step 7: Now we have to insert 78. First we compare 78 with the root node which is 45. Since the value of 78 is greater then 45 so it is inserted to the right of the root node. But the root node has already one right node 68. So now we compare 78 with 68. Since the value of 78 is greater then 68 we insert 78 to the right of node 68. 45 35 68 15 42 64 78 5.6.2.2 Example Ex-1: Construct a binary search tree for following set of data: 52 70 55 45 48 72 83 38 15 22 82 28 Reconstruct after adding node 63 and 43. Ex-2 Construct a lexically ordered binary tree for following set of data: Gandhinagar Mumbai Bhopal Jaipur Delhi Chandigarh Ex-3 Construct a lexically ordered binary tree for following set of data: Norma Roger John Bill Leo Paul Ken Maurice By: Oza Hemant P 87 S-304 Data Structure and Management 5.6.2.3 Algorithm for Insertion Operation on Binary Search tree  INSERT(T, info)  T is a pointer which points to the root node of tree.  Info is an element which we want to insert 1. [Check for empty tree] If T = NULL then T->DATAinfo T->LPTRNULL T->RPTRNULL 2. [set current to the Root node of the tree] CURRENTT 3. [left sub tree] If info < CURRENT->DATA then If CURRENT->LPTR ≠ NULL then CURRENTCURRENT->LPTR Else T->DATAinfo T->LPTR = NULL T->RPTR = NULL 4. [right sub tree] If info > CURRENT->DATA then If CURRENT->RPTR ≠ NULL then CURRENTCURRENT->RPTR Else T->DATA info T->LPTR NULL T->RPTR NULL 5. [Check for duplicate node] If CURRENT->DATA = T->DATA then Write “Item already in tree” 6. [finished] Return By: Oza Hemant P 88 S-304 5.6.3 Deletion Operation on Binary Search tree 5.6.3.1 Delete Operation  Consider the following binary tree: 42 Data Structure and Management 12 55 7 18 48 90 1 9 15 28 52 75 110  There are three possibilities when we want to delete an element from binary search tree. (1) If a node to be deleted has empty left and right sub tree then a node is deleted directly. Suppose we want to delete node 9. Here node 9 has no left or right sub tree so we can delete it directly. Thus tree after deleting node 9 is as follow: 42 12 55 7 18 48 90 1 15 28 52 75 110 (2) If a node to be deleted has only one left sub tree or right sub tree then the sub tree of the deleted node is linked directly with the parent of the deleted node. Suppose we want to delete node 48. Here node 48 has one right sub tree so we link this right sub tree with parent of node 48 which is 55. Now the node which we want to link with node 55 is 52 and value of 52 is less than 55 so we link it as a left sub tree of node 55. Thus tree after deleting node 48 is as follow: By: Oza Hemant P 89 S-304 Data Structure and Management 42 12 55 7 18 52 90 1 15 28 75 110 (3) If a node to be deleted has left and right sub tree then we have to do following steps: (a) Find in order successor of the deleted node. (b) Append the right sub tree of the in order successor to its grand parent. (c) Replace the node to be deleted with its in order successor. Suppose we want to delete node 12. Here node 12 has both left and right sub tree. So first we have to find in order successor of node 12 which is 15.now replace the node to be deleted with its in order successor. So we replace node 12 with node 15. Thus tree after deleting node 48 is as follow: 42 15 55 7 18 52 90 1 28 75 110 5.6.3.2 Example Ex-1: construct a binary search tree for following set of data: 50 65 68 45 48 72 81 35 38 85 15 Reconstruct the tree after deleting node 35. Ex-2 Construct a lexically ordered binary tree for following set of data: Norma Roger John Bill Leo Paul Ken Maurice Reconstruct the tree after deleting node John. By: Oza Hemant P 90 S-304 Data Structure and Management Ex-3: Redraw the below tree after node 17 is inserted and node 23 is deleted. 23 15 36 11 18 27 39 24 18 5.6.3.3 Algorithm for deletion operation on Binary Search tree  DELETE(HEAD,X) 1. [Initialize] If LPTR (HEAD) ≠ HEAD then CURLPTR (HEAD) PARENTHEAD D‟L‟ Else Write “Node not found” 2. [Search for the node marked for deletion] FOUNDfalse Repeat while not FOUND and CUR ≠ NULL If DATA (CUR) = X then FOUNDtrue Else If X < DATA (CUR) then PARENTCUR CURLPTR (CUR) D‟L‟ Else PARENTCUR CURRPTR (CUR) D‟R‟ If FOUND = false then Write “Node not found” By: Oza Hemant P 91 S-304 Data Structure and Management 3. [Perform the indicated deletion and reconstruct tree] If LPTR (CUR) = NULL then QRPTR (CUR) Else if RPTR (CUR) = NULL then QLPTR (CUR) Else SUCRPTR (CUR) If LPTR (SUC) LPTR (CUR) then QSUC Else PREDRPTR (CUR) SUCLPTR (PRED) Repeat while LPTR (SUC) ≠ NULL PREDSUC SUCLPTR (PRED) LPTR (PRED) RPTR (SUC) LPTR (SUC) LPTR (CUR) RPTR (SUC) RPTR (CUR) QSUC If D=‟L‟ then LPTR (PARENT) Q Else RPTR (PARENT) Q 4. [Finished] 5.6.4 Copy Operation on Binary Search tree 5.6.4.1 Algorithm for Copy Operation on Binary Search tree  COPY(T)  T is a pointer which points to the root node of tree. 1. [Check for empty tree] If T = NULL then Write “Empty Tree” 2. [Create a New Node] NEW NODE 3. [Copy information field] DATA (NEW) DATA (T) By: Oza Hemant P 92 S-304 4. [Set the structural links] LPTR (NEW) COPY (LPTR (T)) RPTR (NEW) COPY (RPTR (T)) 5. [Return Address of new node] Return (NEW) 5.6.5 Search Operation on Binary Search tree 5.6.5.1 Search Operation Data Structure and Management  First the key to be searched is compared with root node. If it is not in the root node then we have to possibilities :  (1) If the key to be searched having value less than root node then we search the key in left sub tree.  (2) If the key to be searched having value greater than root node then we search the key in right sub tree. 5.6.5.2 Algorithm for Search Operation on Binary Search tree  SEARCH(T,KEY)  This function searches the key in given binary tree.  T is a pointer which points to the root node of tree. 1. [Check for empty tree] If T = NULL then Write “Empty Tree” 2. [Compare the key with root node] If (KEY = T->DATA) then Return (T) 3. [Left or right sub tree?] If (KEY < T->DATA) then Call SEARCH (T->LPTR, KEY) Else Call SEARCH (T->RPTR, KEY) 4. [Finished] Return 5.7 Application of Tree  Following are the application of Tree:  (1) Manipulation of arithmetic expressions  (2) Construction and maintenance of symbol table  (3) Syntax Analysis By: Oza Hemant P 93 S-304 Data Structure and Management Exercise (1) Define following term (a) Tree (b) Graph (c) Forest (d) Binary tree (e) Directed Graph (f) terminal node (g) Ordered tree (h) in degree of node (i) directed tree (j) isolated node (k) Complete binary tree (l) M-ary tree (m) Total degree of node (2) What is difference between root node and leaf node? (3) What is tree traversal? What are different methods of traversal? Write an algorithm for any one operation. (4) Application of tree (5) Write an algorithm to insert node in binary tree (6) Construct a binary search tree for following set of data: 52 70 55 45 48 72 83 38 15 22 82 28 Reconstruct after adding node 63 and 43. (7) Construct a lexically ordered binary tree for following set of data: Gandhinagar Mumbai Bhopal Jaipur Delhi Chandigarh (8) Construct a binary search tree for following set of data: 50 65 68 45 48 72 81 35 38 85 15 Reconstruct the tree after deleting node 35. (9) Construct a lexically ordered binary tree for following set of data: Norma Roger John Bill Leo Paul Ken Maurice By: Oza Hemant P 94 S-304 Data Structure and Management By: Oza Hemant P 95 S-304 6.1 Define string and various string manipulation functions.  String is defined as a sequence of characters.  In terms of c language string is an array of characters. Data Structure and Management  While storing the string in character array the size of the array must be one more then the size of the string. Because the last character in the string is „\0‟ or NULL. The string terminated with NULL or „\0‟ is known as null terminated string.  For example: to store “ABC” in array the array must be declared as char a [4].  String Character set: Lower case: a to z Upper case: A to Z Number: 0 to 9 Special Characters: +, -, *, %, /, () [] {} $ # & , . ? “ “ „ „ @ etc  String Manipulation Functions: 1. String length: this function find the length of the string 2. String copy: this function copy one string in to another string 3. String Concate: this function concate two string and store it in to the another string 4. String compare: this function compare two string 5. substring: this function finds one string into the another string 6.2 Operations on String  Following are the operations that can be performed on string. (1) Reading string: This operation is used to read the string. (2) Writing string: This operation is used to write the string. (3) Count length of string: This operation is used to count number of character presents in to the string. (4) String Concate: This operation is used to combine two strings and store them into the third string. (5) Copy string: This operation is used to copy one string into another string. (6) Compare two strings: This operation is used to compare two strings for equality. (7) Find substring into given string: This operation is used to find substring into the given string. (8) Reverse string: this operation is used to reverse the given string. (9) Text editing (10) Pattern matching 6.2.1 Algorithm to find length of string.  SLEN( str )  This function finds the length of the string. By: Oza Hemant P 96 S-304  Str is the string whose length is to be find 1. [initialize] Count0 2. [Process until end of the string] Repeat step 3 until str[count] # NULL 3. [increment counter] Countcount + 1 4. [Finish] Return count 6.2.2 Algorithm to copy one string in to another string  SCOPY(source, destination) Data Structure and Management  This function copies the string source in to the string destination. 1. [initialize] Count0 2. [Process until end of the loop] Repeat step 3 until source [count] # NULL 3. [copy character by character and increment counter] Destination [count] source [count] Countcount+1 4. [finish] Destination [count]NULL 6.2.3 Algorithm to concate two strings.  SCAT(s1,s2,s3)  This function concate two strings s1 and s2 in to the string s3 1. [initialize] Count10 Count20 2. [process until end of first string] Repeat step 3 until s1 [count1] # NULL 3. [copy character by character and increment counter] s3 [count1] s1 [count1] Count1count1+1 4. [process until end of second string] Repeat step 5 until s2 [count2] # NULL 5. [copy character by character and increment counter] By: Oza Hemant P 97 S-304 s3 [count1]s2[count2] Count1count1+1 Count2count2+1 6. [finish] s3 [count1] NULL 6.2.4 Algorithm for compare two string  SCOMP(s1,s2) Data Structure and Management  This function compare two strings s1 and s2 and find weather they are equal or not. 1. [initialize] I0 FLAG0 2. [Find Length of two strings] L1strlen (s1) L2strlen (s2) 3. [Compare Length of two string] If (L1 ≠ L2) then Write “Strings are not equal” 4. [Process string] Repeat step 5 while I < L1 5. [compare character by character] If s1 [I] ≠ s2 [I] then FLAG1 Else II + 1 6. [Are string equal?] If FLAG = 0 then Write “Strings are equal” Else Write “Strings are not equal” 7. [Finished] 6.2.5 Algorithm to find whether a second string is a substring of first string  SSTR(S1, SUBSTR, ST,NUM)  This function finds the string SUBSTR starting at position ST in given string followed by NUM characters.  Here ST indicates the starting position in given string. By: Oza Hemant P 98 S-304 Data Structure and Management  NUM indicates number of character after starting position. 1. [initialize] I0 STST-1 2. [Process the string] Repeat step 3 until NUM < 0 3. [Compare Length of two string] SUBSTR [I] S1 [ST] II+1 STST+1 NUMNUM-1 4. [Finished] SUBSTR [I] NULL 6.2.6 Algorithm to reverse the string.  REVERSE(org, rev)  This function reverses the given string and stores it in rev string. 1. [Find Length of given string] Lstrlen (org) 2. [Initialize] COUNT1L-1 COUNT20 Rev [L] NULL 3. [Process until end of the string] Repeat step 4 until org [COUNT2] ≠ NULL 4. [Copy Character by character and increment counter] Rev [count1] org [COUNT2] COUNT1COUNT1-1 COUNT2COUNT2+1 5. [Finished] Write (rev) 6.2.7 Algorithm for duplicating a given character string N times. For example given the string “EXAM” and N=2 algorithm should generate the string “EXAMEXAM”.  DUPLICATE(S1, S2, N)  This function duplicates the string S1 N times and stores the result string in S2. 1 [Initialize] By: Oza Hemant P 99 S-304 COUNT10 COUNT20 2 [Loop N times] Repeat thru step 5 for I = 1 to N 3 [Process string] Repeat step 4 until S1 [COUNT1] ≠ NULL 4 [Copy Character by character] S2 [COUNT2]S1[COUNT1] COUNT1COUNT1+1 COUNT2COUNT2+1 5 [Reinitialize counter for S1] COUNT10 6 [Finished] S2 [COUNT2] NULL Data Structure and Management 6.2.8 Algorithm for finding a palindrome string using recursion.(i.e RADAR)  PALINDROME(FIRST,LAST,STRING)  FIRST indicate first element in the string.  LAST indicate the last element in the string. 1. [Find length of the string] L1strlen (STRING) 2. [initialize] FIRST0 LASTL1-1 3. [compare elements] If STRING [FIRST] ≠ STRING [LAST] then Return 0; Else if STRING [FIRST] = STRING [LAST] then Call PALINDROME (FIRST, LAST, STRING) Return 1 Else if STRING [FIRST] > STRING [LAST] then Return 1 4. [Check for return value] If PALINDROME (FIRST, LAST, STRING) = 1 then Write “String is palindrome” Else By: Oza Hemant P 100 S-304 Write “String is not palindrome” 5. [Finished] 6.3 Application of String Data Structure and Management  There are many applications of the string. But the two main areas where the string processing is widely used are: (1) Text Editing (2) Text Formatting  Text Editing: The general functions that are required in a text editor are as follow: (1) Insertion: Inserts a string, word or paragraph in given text (2) Deletion: Delete a string, word or paragraph from given text (3) Update: Update a string, word or paragraph (4) Replacement: Replace a string, sub-string, line paragraph etc in the given text  Text Formatting: The common text formatting functions are described below: (1) Formatting a paragraph (2) Setting a left margin (3) Setting a right margin (4) Left justified text (5) Right justified text Exercise (1) Write a program to find out palindrome string using recursion(i.e RADAR) (2) Write an algorithm to compare two string (3) Write an algorithm to concate two string (4) Write an algorithm to find length of string (5) Write an algorithm to copy one string into another string (6) Write an algorithm to find whether a second string is a substring of first string. (7) What is string? Explain various string manipulation functions. (8) Short note: Operation on string (9) Application of String (10) Algorithm for duplicating a given character string N times. For example given the string “EXAM” and N=2 algorithm should generate the string “EXAMEXAM”. (11) Write an Algorithm to reverse the string. By: Oza Hemant P 101

Related docs
Data base
Views: 5  |  Downloads: 0
Data base Management -2
Views: 4  |  Downloads: 1
Data Management Procedures and Data Base
Views: 1  |  Downloads: 0
Relational Data Base Fundamental
Views: 0  |  Downloads: 0
CONTENT MANAGEMENT SYSTEM
Views: 3  |  Downloads: 0
ccview management system
Views: 0  |  Downloads: 0
NEDSS Base System Training Guide
Views: 0  |  Downloads: 0
NC 02220 OSP 1086 DATA BASE ADMINISTRATOR
Views: 1  |  Downloads: 0
premium docs
Other docs by Jaimin Kaushik...
Encryption Algorithms Explained by Ankit Fadia
Views: 71  |  Downloads: 17
Dos Attacked by Ankit Fadia
Views: 92  |  Downloads: 7
Defacing Websites By Ankit Fadia
Views: 149  |  Downloads: 12
Batch File Programming By Ankit fadia
Views: 39  |  Downloads: 10
Fix your Own PC
Views: 406  |  Downloads: 27
Search Engine Optimization By Google
Views: 39  |  Downloads: 11
DOS Commands
Views: 73  |  Downloads: 18
Search Engins For File Hosting services
Views: 26  |  Downloads: 0