Mukesh Patel School of Technology Management & Engineering Mumbai

Data Structures

Vaishali Sodani Assistant Professor M.Tech.(CS)

Introduction to Data Structures
Review of data structures like:
1. 2. 3. 4. 5. 6. Linked Lists Stacks Queues Trees Binary Search Trees Tree Traversal : : : : : : Unit I Unit I Unit I Unit I Unit IV Unit III

Data Structures

Linear Data Structures

Example: Linear Data Structures

Hierarchical Data Structures .

Example: Hierarchical Data Structures .

Graph Data Structures .

Example: Graph Data Structures .

Set Data Structures .

Example: Set Data Structures .

Introduction  Linked lists  Allow insertions and removals anywhere  Stacks  Allow insertions and removals only at top of stack  Queues  Allow insertions at the back and removals from the front  Binary trees  High-speed searching and sorting of data and efficient elimination of duplicate data items .

called nodes  Connected by pointer links  Accessed via a pointer to the first node of the list  Subsequent nodes are accessed via the link-pointer member of the current node  Link pointer in the last node is set to null to mark the list’s end  Use a linked list instead of an array when  You have an unpredictable number of data elements  Your list needs to be sorted quickly . Linked Lists  Linked list  Linear collection of self-referential class objects.A linked list whose nodes contain two fields: an integer value & a link to the first node.

singly linked  Pointer in the last node points back to the first node A circular singly linked list .Linked Lists  Types of linked lists:  Singly linked list  Begins with a pointer to the first node  Terminates with a null pointer  Only traversed in one direction A singly linked list whose nodes contain two fields: an integer value & a link to the first node  Circular.

 Allows traversals both forwards and backwards.Linked Lists  Doubly linked list  Two “start pointers” – first element and last element. doubly linked list  Forward pointer of the last node points to the first node and backward pointer of the first node points to the last node .  Each node has a forward pointer and a backward pointer.  Circular.

Linked Lists  Insertion: .

Linked Lists  Deletion: .

struct listNode *nextPtr. printf( "? " ). . 12 typedef ListNode *ListNodePtr.c Operating and maintaining a list */ #include <stdio.h> struct listNode { char data.1 2 3 4 5 6 7 8 9 10 /* Fig. Define struct 1.h> #include <stdlib. 12. scanf( "%d". 16 int isEmpty( ListNodePtr ). 13 14 void insert( ListNodePtr *. }.2 Initialize variables 2. int choice. 19 20 int main() 21 { 22 23 24 25 26 27 28 instructions(). /* self-referential structure */ 1. /* display the menu */ ListNodePtr startPtr = NULL. 18 void instructions( void ). char ).3: fig12_03.1 Function prototypes 1. char item. &choice ). Input choice 11 typedef struct listNode ListNode. 15 char delete( ListNodePtr *. char ). 17 void printList( ListNodePtr ).

item ). } else printf( "%c not found. break.1 switch statement if ( delete( &startPtr. default: printf( "Invalid choice. break. scanf( "\n%c". case 2: if ( !isEmpty( startPtr ) ) { printf( "Enter character to be deleted: " ).29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 while ( choice != 3 ) { switch ( choice ) { case 1: printf( "Enter a character: " ). 2.\n\n" ). &item ). } . item ). } else printf( "List is empty. printList( startPtr ). scanf( "\n%c".\n". instructions(). printList( startPtr ).\n\n". item ). &item ).\n\n" ). item ) ) { printf( "%c deleted. break. insert( &startPtr.

if ( newPtr != NULL ) { newPtr->data = value. return 0. newPtr->nextPtr = NULL. } printf( "End of run. previousPtr = NULL.\n" ). Function definitions . char value ) { ListNodePtr newPtr. } /* Print the instructions */ void instructions( void ) { printf( "Enter your choice:\n" " 1 to insert an element into the list.\n" " 2 to delete an element from the list. &choice ). currentPtr = *sPtr. scanf( "%d". previousPtr.\n" " 3 to end. /* is space available */ 3.60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 printf( "? " ). currentPtr.\n" ). newPtr = malloc( sizeof( ListNode ) ). } /* Insert a new value into the list in sorted order */ void insert( ListNodePtr *sPtr.

*/ currentPtr = currentPtr->nextPtr. /* de-thread the node */ free( tempPtr ).. if ( value == ( *sPtr )->data ) { tempPtr = *sPtr. } } else printf( "%c not inserted. } else { previousPtr->nextPtr = newPtr. currentPtr. 3. *sPtr = newPtr.. tempPtr. char value ) { ListNodePtr previousPtr. /* free the de-threaded node */ return value. *sPtr = ( *sPtr )->nextPtr. next node */ } if ( previousPtr == NULL ) { newPtr->nextPtr = *sPtr.. } . newPtr->nextPtr = currentPtr. /* . /* walk to .\n".91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 } while ( currentPtr != NULL && value > currentPtr->data ) { previousPtr = currentPtr. No memory available.. value ). Function definitions /* Delete a list element */ char delete( ListNodePtr *sPtr.

. currentPtr = ( *sPtr )->nextPtr. return value. while ( currentPtr != NULL && currentPtr->data != value ) { previousPtr = currentPtr.. } if ( currentPtr != NULL ) { tempPtr = currentPtr.121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 else { previousPtr = *sPtr. next node */ 3. previousPtr->nextPtr = currentPtr->nextPtr. } /* Print the list */ void printList( ListNodePtr currentPtr ) { if ( currentPtr == NULL ) .. 0 otherwise */ int isEmpty( ListNodePtr sPtr ) { return sPtr == NULL. } /* walk to .. free( tempPtr ). */ /* . } } return '\0'. Function definitions /* Return 1 if the list is empty. currentPtr = currentPtr->nextPtr.

currentPtr = currentPtr->nextPtr.\n\n" ). } printf( "NULL\n\n" ). else { printf( "The list is:\n" ). 3. Function definitions . currentPtr->data ).151 152 153 154 printf( "List is empty. 155 156 157 158 159 160 161 162 } } while ( currentPtr != NULL ) { printf( "%c --> ".

? 1 Enter a character: B The list is: B --> NULL Program Output ? 1 Enter a character: A The list is: A --> B --> NULL ? 1 Enter a character: C The list is: A --> B --> C --> NULL ? 2 Enter character to be deleted: D D not found. ? 2 Enter character to be deleted: B B deleted. 2 to delete an element from the list. 3 to end.Enter your choice: 1 to insert an element into the list. The list is: A --> C --> NULL .

Stacks  Stack  New nodes can be added and removed only at the top  Last-in. first-out (LIFO)  Bottom of stack indicated by a link member to NULL  Constrained version of a linked list  push  Adds a new node to the top of the stack  pop  Removes a node from the top  Stores the popped value  Returns true if pop was successful .

&choice ). 12.2 Initialize variables 2. int ). 15 int pop( StackNodePtr * ). 13 14 void push( StackNodePtr *.1 Function definitions 1. /* points to stack top */ . 19 20 int main() 21 { 22 23 24 25 26 27 28 instructions(). int choice.1 2 3 4 5 6 7 8 9 10 /* Fig. }. StackNodePtr stackPtr = NULL. /* self-referential structure */ 1.h> #include <stdlib. 12 typedef StackNode *StackNodePtr.c dynamic stack program */ #include <stdio. 16 int isEmpty( StackNodePtr ).8: fig12_08. struct stackNode *nextPtr. 17 void printStack( StackNodePtr ). scanf( "%d". printf( "? " ).h> struct stackNode { int data. 18 void instructions( void ). Input choice 11 typedef struct stackNode StackNode. value. Define struct 1.

\n". break. .\n" ). default: printf( "Invalid choice. scanf( "%d". 2.\n\n" ).1 switch statement 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 } 58 case 2: /* pop value off stack */ if ( !isEmpty( stackPtr ) ) printf( "The popped value is %d. } printf( "End of run. break. printStack( stackPtr ).29 30 31 32 33 34 35 36 37 while ( choice != 3 ) { switch ( choice ) { case 1: /* push value onto stack */ printf( "Enter an integer: " ). return 0. &value ). &choice ). instructions(). pop( &stackPtr ) ). break. printStack( stackPtr ). } printf( "? " ). scanf( "%d". value ). push( &stackPtr.

No memory available.59 /* Print the instructions */ 60 void instructions( void ) 61 { 62 63 64 65 66 } 67 68 /* Insert a node at the stack top */ 69 void push( StackNodePtr *topPtr. info ).\n". StackNodePtr newPtr. . if ( newPtr != NULL ) { newPtr->data = info. 3. newPtr->nextPtr = *topPtr. int info ) 70 { 71 72 73 74 75 76 newPtr = malloc( sizeof( StackNode ) ). printf( "Enter choice:\n" "1 to push a value on the stack\n" "2 to pop a value off the stack\n" "3 to end program\n" ). Function definitions 77 78 79 80 81 82 } 83 } *topPtr = newPtr. else printf( "%d not inserted.

84 /* Remove a node from the stack top */ 85 int pop( StackNodePtr *topPtr ) 86 { 87 StackNodePtr tempPtr.\n\n" ). 88 int popValue. 93 free( tempPtr ). 95 } 96 97 /* Print the stack */ 98 void printStack( StackNodePtr currentPtr ) 99 { 100 if ( currentPtr == NULL ) 101 printf( "The stack is empty. 102 else { 103 printf( "The stack is:\n" ). 108 } 109 110 printf( "NULL\n\n" ). 104 105 while ( currentPtr != NULL ) { 106 printf( "%d --> ". 94 return popValue. 89 90 tempPtr = *topPtr. 91 popValue = ( *topPtr )->data. 107 currentPtr = currentPtr->nextPtr. 92 *topPtr = ( *topPtr )->nextPtr. 111 } 112 } 113 3. currentPtr->data ). Function definitions .

3.114 /* Is the stack empty? */ 115 int isEmpty( StackNodePtr topPtr ) 116 { 117 118 } Enter choice: 1 to push a value on the stack 2 to pop a value off the stack 3 to end program ? 1 Enter an integer: 5 The stack is: 5 --> NULL ? 1 Enter an integer: 6 The stack is: 6 --> 5 --> NULL ? 1 Enter an integer: 4 The stack is: 4 --> 6 --> 5 --> NULL ? 2 The popped value is 4. Function definitions Program Output . The stack is: 6 --> 5 --> NULL return topPtr == NULL.

? 2 The stack is empty. Enter choice: 1 to push a value on the stack 2 to pop a value off the stack 3 to end program ? 3 End of run. The stack is empty.? 2 The popped value is 6. ? 4 Invalid choice. Program Output . The stack is: 5 --> NULL ? 2 The popped value is 5.

1.3 Queues  Queue  Similar to a supermarket checkout line  First-in.1. first-out (FIFO)  Nodes are removed only from the head  Nodes are inserted only at the tail  Insert and remove operations  Enqueue (insert) and dequeue (remove) .

13: fig12_13. void instructions( void ). int isEmpty( QueueNodePtr ). instructions(). char dequeue( QueueNodePtr *. &choice ). /* function prototypes */ void printQueue( QueueNodePtr ). typedef QueueNode *QueueNodePtr.h> struct queueNode { /* self-referential structure */ char data.1 Function prototypes 1. scanf( "%d". printf( "? " ). Input choice 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 }. QueueNodePtr *.1 2 3 4 5 6 7 8 9 /* Fig. int main() { QueueNodePtr headPtr = NULL. Define struct 1. char ). QueueNodePtr * ).h> #include <stdlib. . char item. int choice. struct queueNode *nextPtr. 12. 1.1 Initialize variables 2. typedef struct queueNode QueueNode. void enqueue( QueueNodePtr *. tailPtr = NULL.c Operating and maintaining a queue */ #include <stdio.

return 0. instructions(). &tailPtr ). &item ).\n\n" ). default: printf( "Invalid choice. printf( "%c has been dequeued.\n". scanf( "%d". printQueue( headPtr ). } printf( "End of run. enqueue( &headPtr. case 2: if ( !isEmpty( headPtr ) ) { item = dequeue( &headPtr. item ). &tailPtr. break. break. } printQueue( headPtr ). . break.31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 } 64 while ( choice != 3 ) { switch( choice ) { 2. item ). scanf( "\n%c". } printf( "? " ). &choice ).\n" ).1 switch statement case 1: printf( "Enter a character: " ).

QueueNodePtr *tailPtr. } void enqueue( QueueNodePtr *headPtr. else ( *tailPtr )->nextPtr = newPtr. Function definitions if ( newPtr != NULL ) { newPtr->data = value. } else printf( "%c not inserted. char value ) { QueueNodePtr newPtr. value ). } .\n". newPtr->nextPtr = NULL. *tailPtr = newPtr. if ( isEmpty( *headPtr ) ) *headPtr = newPtr. No memory available. 3.65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 void instructions( void ) { printf ( "Enter your choice:\n" " 1 to add an item to the queue\n" " 2 to remove an item from the queue\n" " 3 to end\n" ). newPtr = malloc( sizeof( QueueNode ) ).

QueueNodePtr *tailPtr ) 97 { 98 99 100 101 102 103 104 105 106 107 108 109 110 } 111 112 int isEmpty( QueueNodePtr headPtr ) 113 { 114 115 } 116 117 void printQueue( QueueNodePtr currentPtr ) 118 { 119 120 121 122 if ( currentPtr == NULL ) printf( "Queue is empty. return headPtr == NULL. else { printf( "The queue is:\n" ). free( tempPtr ).96 char dequeue( QueueNodePtr *headPtr. value = ( *headPtr )->data. Function definitions . char value. *headPtr = ( *headPtr )->nextPtr. 3. tempPtr = *headPtr.\n\n" ). return value. QueueNodePtr tempPtr. if ( *headPtr == NULL ) *tailPtr = NULL.

} while ( currentPtr != NULL ) { printf( "%c --> ".123 124 125 126 127 128 129 130 131 } } printf( "NULL\n\n" ). 3. currentPtr->data ). Function definitions Enter your choice: 1 to add an item to the queue 2 to remove an item from the queue 3 to end ? 1 Enter a character: A The queue is: A --> NULL ? 1 Enter a character: B The queue is: A --> B --> NULL ? 1 Enter a character: C The queue is: A --> B --> C --> NULL Program Output . currentPtr = currentPtr->nextPtr.

? 2 A has been dequeued. Queue is empty. ? 2 Queue is empty. ? 4 Invalid choice. The queue is: C --> NULL Program Output ? 2 C has been dequeued. The queue is: B --> C --> NULL ? 2 B has been dequeued. Enter your choice: 1 to add an item to the queue 2 to remove an item from the queue 3 to end ? 3 End of run. .

 The root node is the first node in a tree. one.  A node with no children is called a leaf node. or both of which may be NULL.Trees  Binary trees  All nodes contain two links  None.  Each link in the root node refers to a child. .

Binary Search Trees  Values in left subtree less than parent  Values in right subtree greater than parent  Facilitates duplicate elimination  Fast searches . maximum of log n comparisons 47 25 11 7 17 43 31 44 65 68 77 93 .for a balanced tree.

Binary Search Trees Insertion Example: Insert 5 .

Let S(N) denote the inorder successor i. Then N is deleted from T by replacing the location of N in the parent node P(N) by the null pointer.Binary Search Trees Deletion  Deletion: Case1: N has no children. . Case3: N has two children. Then N is deleted from T by replacing the location of N in P(N) by the location of the only child of N. next higher number of N( The reader can verify that S(N) does not have left child). Case2: N has one child.e. Then N is deleted from T by first deleting S(N) from T using case 1 or case 2 & then replacing node N by the node S(N).

Deletion CASE 1 CASE 2 .Binary Search Trees .

Binary Search Trees .Deletion CASE 3 CASE 2 .

R) prints the node values in ascending order 1. R) 1.. Traverse the right subtree with a preorder traversal  Postorder traversal – (L. Traverse the right subtree with a postorder traversal 3. Traverse the left subtree with an inorder traversal 2. print the node value) 3. Process the value in the node (i. Traverse the left subtree with a preorder traversal 3. Process the value in the node . L. Traverse the left subtree with a postorder traversal 2. Process the value in the node 2. Root) 1. Root.e.Tree Traversal  Tree traversals:  Inorder traversal – (L. Traverse the right subtree with an inorder traversal  Preorder traversal – ( Root. R.

Tree Traversal  Example: A B D F E G C H J L K  Preorder: A B D E F C G H J LK  Inorder: D B F E A G C L J H K  Postorder: D F E B G L J K H C A .

1 2 3 4 5 6 7 8 9 10 /* Fig. inorder. 12. int data. 15 typedef TreeNode *TreeNodePtr. 18 void inOrder( TreeNodePtr ). 19 void preOrder( TreeNodePtr ).1 Function prototypes 1. TreeNodePtr rootPtr = NULL.c Create a binary tree and traverse it preorder.2 Initialize variables 11 12 }. and postorder */ #include <stdio. 21 22 int main() 23 { 24 25 26 27 28 srand( time( NULL ) ).h> struct treeNode { struct treeNode *leftPtr. 13 struct treeNode *rightPtr. 20 void postOrder( TreeNodePtr ). int ). 14 typedef struct treeNode TreeNode.h> #include <time. item. Define structure 1. int i. 1. 16 17 void insertNode( TreeNodePtr *.19: fig12_19.h> #include <stdlib. .

62 } 1.3 Insert random elements 2. 44 inOrder( rootPtr ). 36 } 37 38 /* traverse the tree preOrder */ 39 printf( "\n\nThe preOrder traversal is:\n" ). 34 printf( "%3d". Function definitions . 45 46 /* traverse the tree postOrder */ 47 printf( "\n\nThe postOrder traversal is:\n" ). item ). 31 32 for ( i = 1. 49 50 return 0. 41 42 /* traverse the tree inOrder */ 43 printf( "\n\nThe inOrder traversal is:\n" ). 57 58 if ( *treePtr != NULL ) { 59 ( *treePtr )->data = value. int value ) 54 { 55 if ( *treePtr == NULL ) { /* *treePtr is NULL */ 56 *treePtr = malloc( sizeof( TreeNode ) ). i <= 10. item ). Function calls 3. 60 ( *treePtr )->leftPtr = NULL. 48 postOrder( rootPtr ).29 /* insert random values between 1 and 15 in the tree */ 30 printf( "The numbers being placed in the tree are:\n" ). 61 ( *treePtr )->rightPtr = NULL. 40 preOrder( rootPtr ). 51 } 52 53 void insertNode( TreeNodePtr *treePtr. 35 insertNode( &rootPtr. i++ ) { 33 item = rand() % 15.

} } void preOrder( TreeNodePtr treePtr ) { if ( treePtr != NULL ) { printf( "%3d". inOrder( treePtr->rightPtr ). treePtr->data ). value ). preOrder( treePtr->leftPtr ).\n". value ). No memory available. } void inOrder( TreeNodePtr treePtr ) { if ( treePtr != NULL ) { inOrder( treePtr->leftPtr ). Function definitions 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 else printf( "dup" ). else if ( value > ( *treePtr )->data ) insertNode( &( ( *treePtr )->rightPtr ). } else if ( value < ( *treePtr )->data ) insertNode( &( ( *treePtr )->leftPtr ). 3. printf( "%3d".63 64 65 66 67 68 69 70 71 else printf( "%d not inserted. value ). } } . preOrder( treePtr->rightPtr ). treePtr->data ).

3. Function definitions The numbers being placed in the tree are: 7 8 0 6 14 1 0dup 13 0dup 7dup The preOrder traversal is: 7 0 6 1 8 14 13 The inOrder traversal is: 0 1 6 7 8 13 14 The postOrder traversal is: 1 6 0 13 14 8 7 Program Output . printf( "%3d".93 94 void postOrder( TreeNodePtr treePtr ) 95 { 96 97 98 99 100 101 } } if ( treePtr != NULL ) { postOrder( treePtr->leftPtr ). postOrder( treePtr->rightPtr ). treePtr->data ).

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.