Data Structures
Data Structures
Structures
1
Introduction
• Dynamic data structures
• Data structures that grow and shrink during execution
• Use dynamic data structures to perform
• 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
Self-Referential Structures
• Self-referential structures
• Structure that contains a pointer to a structure of the same type
• Can be linked together to form useful data structures such as lists, queues, stacks and trees
• Terminated with a NULL pointer
struct node {
int data;
struct node *nextPtr;
}
• nextPtr
• Points to an object of type node
• Referred to as a link
• Ties one node to another node
Self-referential structures linked together
Dynamic Memory Allocation: Recall
• Dynamic memory allocation
• Obtain and release memory during execution
• malloc
• Takes number of bytes to allocate
• Use sizeof to determine the size of an object
• Returns pointer of type void *
• A void * pointer may be assigned to any pointer
• If no memory available, returns NULL
• Example
newPtr = malloc( sizeof( struct node ) );
• free
• Deallocates memory allocated by malloc
• Takes a pointer as an argument
• free ( newPtr );
• Notes:
• Freeing memory not allocated dynamically with malloc is an error;
• Referring to memory that has been freed is an error.
Linked Lists
• Linked list
• Linear collection of self-referential class objects, 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 maintained in sorted order
Arrays vs. Linked List
Linked List Arrays
Dynamic data structure (Length increase or decrease): Fixed-size data structure
Appropriate when the number of data items is
unpredictable
Saves memory (Note: Pointers require additional Can be declared to contain more elements than the
memory and overhead of function calls.) number of items expected: waste of memory
Linked lists can become full when the system has Fixed-size arrays can become full
insufficient memory
Maintaining sorted order is easy in the presence of Requires shifting elements after insert/delete
inserts/deletes
7
Linked list graphical representation
1 /* Fig. 12.3: fig12_03.c
2 Operating and maintaining a list */
3 #include <stdio.h>
4 #include <stdlib.h> Each node in the list contains a data
5 element and a pointer to the next node
Operations 6
7
/* self-referential structure */
struct listNode {
on Linked
8 char data; /* each listNode contains a character */
9 struct listNode *nextPtr; /* pointer to next node */
10 }; /* end structure listNode */
Lists 11
12 typedef struct listNode ListNode; /* synonym for struct listNode */
13 typedef ListNode *ListNodePtr; /* synonym for ListNode* */
14
15 /* prototypes */
16 void insert( ListNodePtr *sPtr, char value );
17 char delete( ListNodePtr *sPtr, char value );
18 int isEmpty( ListNodePtr sPtr );
19 void printList( ListNodePtr currentPtr );
20 void instructions( void );
21
22 int main( void )
23 {
24 ListNodePtr startPtr = NULL; /* initially there are no nodes */
25 int choice; /* user's choice */
26 char item; /* char entered by user */
27
28 instructions(); /* display the menu */
29 printf( "? " );
30 scanf( "%d", &choice );
31
32 /* loop while user does not choose 3 */
33 while ( choice != 3 ) {
34
35 switch ( choice ) {
36
37 case 1:
Operations 38
39
printf( "Enter a character: " );
scanf( "\n%c", &item );
on Linked
40 insert( &startPtr, item ); /* insert item in list */
41 printList( startPtr );
42 break;
Function insert inserts data into the list
Lists
43
44 case 2: /* delete an element */
45
46 /* if list is not empty */
47 if ( !isEmpty( startPtr ) ) {
48 printf( "Enter character to be deleted: " );
49 scanf( "\n%c", &item );
50
51 /* if character is found, remove it*/
52 if ( delete( &startPtr, item ) ) { /* remove item */
53 printf( "%c deleted.\n", item );
54 printList( startPtr ); Function delete removes data from the list
55 } /* end if */
56 else {
57 printf( "%c not found.\n\n", item );
58 } /* end else */
59
60 } /* end if */
61 else {
62 printf( "List is empty.\n\n" );
63 } /* end else */
Operations
64
65 break;
66
on Linked
67 default:
68 printf( "Invalid choice.\n\n" );
69 instructions();
Lists 70
71
72
break;
} /* end switch */
73
74 printf( "? " );
75 scanf( "%d", &choice );
76 } /* end while */
77
78 printf( "End of run.\n" );
79
80 return 0; /* indicates successful termination */
81
82 } /* end main */
83
How to insert a new node into a linked list?
12
84 /* display program instructions to user */
85 void instructions( void )
86 {
87 printf( "Enter your choice:\n"
88 " 1 to insert an element into the list.\n"
Insert
89 " 2 to delete an element from the list.\n"
90 " 3 to end.\n" );
91 } /* end function instructions */
Operation
92
93 /* Insert a new value into the list in sorted order */
94 void insert( ListNodePtr *sPtr, char value )
on 95 {
96 ListNodePtr newPtr; /* pointer to new node */
Linked Lists
97 ListNodePtr previousPtr; /* pointer to previous node in list */
98 ListNodePtr currentPtr; /* pointer to current node in list */
99
100 newPtr = malloc( sizeof( ListNode ) ); /* create node */
101
102 if ( newPtr != NULL ) { /* is space available */
To insert a node into the list, memory
103 newPtr->data = value; /* place value in node */ must first be allocated for that node
104 newPtr->nextPtr = NULL; /* node does not link to another node */
105
106 previousPtr = NULL;
107 currentPtr = *sPtr;
while loop searches for new
108 node’s place in the list
109 /* loop to find the correct location in the list */
110 while ( currentPtr != NULL && value > currentPtr->data ) {
111 previousPtr = currentPtr; /* walk to ... */
112 currentPtr = currentPtr->nextPtr; /* ... next node */
113 } /* end while */
114
115 /* insert new node at beginning of list */
116 if ( previousPtr == NULL ) {
Insert
117 newPtr->nextPtr = *sPtr; If there are no nodes in the list, the
118 *sPtr = newPtr; new node becomes the “start” node
119 } /* end if */
Operation 120
121
else { /* insert new node between previousPtr and currentPtr */
previousPtr->nextPtr = newPtr;
on
122 newPtr->nextPtr = currentPtr;
123 } /* end else */ Otherwise, the new node is inserted
124 between two others (or at the end
Linked Lists
125 } /* end if */ of the list) by changing pointers
126 else {
127 printf( "%c not inserted. No memory available.\n", value );
128 } /* end else */
129
130 } /* end function insert */
131
132 /* Delete a list element */
133 char delete( ListNodePtr *sPtr, char value )
134 {
135 ListNodePtr previousPtr; /* pointer to previous node in list */
136 ListNodePtr currentPtr; /* pointer to current node in list */
137 ListNodePtr tempPtr; /* temporary node pointer */
138
Inserting a node in order in a list
How to delete a node from a linked list?
16
139 /* delete first node */
140 if ( value == ( *sPtr )->data ) {
141 tempPtr = *sPtr; /* hold onto node being removed */
142 *sPtr = ( *sPtr )->nextPtr; /* de-thread the node */
Delete
143 free( tempPtr ); /* free the de-threaded node */
144 return value;
145 } /* end if */
Operation 146
147
else {
previousPtr = *sPtr;
on
148 currentPtr = ( *sPtr )->nextPtr;
149
150 /* loop to find the correct location in the list */
Check on
166 return '\0';
167
168 } /* end function delete */
Print 182
183
if ( currentPtr == NULL ) {
printf( "List is empty.\n\n" );
Linked Lists
184 } /* end if */
185 else {
186 printf( "The list is:\n" );
187
188 /* while not the end of the list */
189 while ( currentPtr != NULL ) {
190 printf( "%c --> ", currentPtr->data );
191 currentPtr = currentPtr->nextPtr;
192 } /* end while */
193
194 printf( "NULL\n\n" );
195 } /* end else */
196
197 } /* end function printList */
Output of the program
Enter your choice:
1 to insert an element into the list.
2 to delete an element from the list.
3 to end.
? 1
Enter a character: B
The list is:
B --> NULL
? 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.
The list is:
A --> C --> NULL
? 2
Enter character to be deleted: C
C deleted.
The list is:
A --> NULL
(continued on next slide… )
Output of the program
(continued from previous slide…)
? 2
Enter character to be deleted: A
A deleted.
List is empty.
? 4
Invalid choice.
? 2
Enter character to be deleted: C
C deleted.
The list is:
A --> NULL
? 2
Enter character to be deleted: A
A deleted.
List is empty.
? 4
Invalid choice.