CH Gowri Kumar gkumar007@gmail.com 9/21/2018 Linked Lists CH Gowri Kumar gkumar007@gmail.com
typedef struct node Node; typedef struct node* List; 9/21/2018 struct node { int data; struct node* next; }; typedef struct node Node; typedef struct node* List; List Initialize(); void InsertBegin(List l,int d); void InsertEnd(List l, int d); void Insert(List l, Node* pos,int d); Node* Find(List l,int d); void Delete(List l, int d); This is header based implementation of linked lists. The typedefs help us to reduce the amount of typing. Instead of typing “struct node*” all the time, we can just use Node*; Initiliaze – Creates a dumy header node and returns it. InsertBegin – The element is inserted at the beginning of the list InsertEnd – The element is inserted at the end of the list. Insert – The general routine, inserts the elment after the node “pos”. Find – Return the node, whose next node points to the actual node. The intended use is: Node* p = Find(l,10); p->next->data == 10 If the element is no there in the list, it returns NULL. Delete – deletes the element d from the list. September 21, 2018 http://www.gowrikumar.com
Menu Initialize InsertBegin InsertEnd Insert Find Delete September 21, 2018 http://www.gowrikumar.com
Initialize September 21, 2018 http://www.gowrikumar.com
temp = (Node*)calloc(1,sizeof(Node)); return temp; } 9/21/2018 List Initialize() { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); return temp; } We would be using calloc in our implementation, instead of malloc. He difference between calloc and malloc is that: Malloc – just allocates the memory Calloc – Allocates the memory and also clears it. The initial values would be Data would be 0 and the next would be NULL. If we had used malloc, the code would have been: Node* temp; Temp = (Node8)malloc(sizeof(Node)); Temp->next = NULL; Return temp; The intended use of the Initialize is as follows: List head; Head=Initialize(); September 21, 2018 http://www.gowrikumar.com
temp = (Node*)calloc(1,sizeof(Node)); return temp; } main() { 9/21/2018 head X List Initialize() { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); return temp; } main() { List head; head = Initialize(); } We would be using calloc in our implementation, instead of malloc. He difference between calloc and malloc is that: Malloc – just allocates the memory Calloc – Allocates the memory and also clears it. The initial values would be Data would be 0 and the next would be NULL. If we had used malloc, the code would have been: Node* temp; Temp = (Node8)malloc(sizeof(Node)); Temp->next = NULL; Return temp; The intended use of the Initialize is as follows: List head; Head=Initialize(); September 21, 2018 http://www.gowrikumar.com
InsertBegin September 21, 2018 http://www.gowrikumar.com
1 10 8 4 6 3 2 5 head X September 21, 2018 http://www.gowrikumar.com 9/21/2018 1 10 8 4 6 3 2 5 head X Let’s try inserting a few elements shown above into the list. September 21, 2018 http://www.gowrikumar.com
void InsertBegin(List head,int d) { Node* temp; 9/21/2018 1 10 8 4 6 3 2 5 head X void InsertBegin(List head,int d) { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; temp->next = head->next; head->next = temp; } Let’s try inserting a few elements shown above into the list. September 21, 2018 http://www.gowrikumar.com
void InsertBegin(List head,int d) { Node* temp; 9/21/2018 1 10 8 4 6 3 2 5 head X void InsertBegin(List head,int d) { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; temp->next = head->next; head->next = temp; } 1 First we create a node temp, using calloc and copy the value into it. Now we need to insert the node into the list. For that first we make temp->next point to whatever head->next is pointing to. Then we make head->next point to temp; Here in this case, head->next is NULL, so you may think that it is not necessary to have the Statement temp->next = head->next; we will see the explanation for it when we insert another node At the beginning of the list. September 21, 2018 http://www.gowrikumar.com
1 10 8 4 6 3 2 5 head X 1 September 21, 2018 http://www.gowrikumar.com 9/21/2018 1 10 8 4 6 3 2 5 head X 1 Now the list looks this September 21, 2018 http://www.gowrikumar.com
void InsertBegin(List head,int d) { Node* temp; 1 10 8 4 6 3 2 5 head X void InsertBegin(List head,int d) { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; temp->next = head->next; head->next = temp; } 1 September 21, 2018 http://www.gowrikumar.com
void InsertBegin(List head,int d) { Node* temp; 9/21/2018 1 10 8 4 6 3 2 5 head X void InsertBegin(List head,int d) { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; temp->next = head->next; head->next = temp; } 1 The order of operations of the nodes is important. To understand it better, let’s try To interchange the instructions and see what happens. September 21, 2018 http://www.gowrikumar.com
void InsertBegin(List head,int d) { Node* temp; 9/21/2018 1 10 8 4 6 3 2 5 head X void InsertBegin(List head,int d) { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; head->next = temp; temp->next = head->next; } 1 If we execute head->next = temp first, we lose the remaining of the list. And if we execute the next statement, temp->next would be pointing to itself. September 21, 2018 http://www.gowrikumar.com
void InsertBegin(List head,int d) { Node* temp; 9/21/2018 1 10 8 4 6 3 2 5 head X 1 void InsertBegin(List head,int d) { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; temp->next = head->next; head->next = temp; } Now let us insert 10 into the list at the beginning of the list. September 21, 2018 http://www.gowrikumar.com
void InsertBegin(List head,int d) { Node* temp; 9/21/2018 1 10 8 4 6 3 2 5 head X 1 void InsertBegin(List head,int d) { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; temp->next = head->next; head->next = temp; } 10 It is similar to the earlier insertion. Here it should make clear why temp->next has to be assigned Head->next; Here we have remaining list to be taken care of (earlier it was an empty list). September 21, 2018 http://www.gowrikumar.com
void InsertBegin(List head,int d) { Node* temp; 9/21/2018 1 10 8 4 6 3 2 5 head X 1 void InsertBegin(List head,int d) { Node* temp; temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; head->next = temp; temp->next = head->next; } 10 As said earlier, the order of pointer manipulations is important. September 21, 2018 http://www.gowrikumar.com
InsertEnd September 21, 2018 http://www.gowrikumar.com
void InsertEnd(List head,int d) { Node *tail,*temp; tail = head; 9/21/2018 1 10 8 4 6 3 2 5 head X 10 1 tail void InsertEnd(List head,int d) { Node *tail,*temp; tail = head; . . . . . . . . } Now let’s insert 8 into the linked-list, but to the end of the list. September 21, 2018 http://www.gowrikumar.com
void InsertEnd(List head,int d) { Node *tail,*temp; tail = head; 9/21/2018 1 10 8 4 6 3 2 5 head X 10 1 tail void InsertEnd(List head,int d) { Node *tail,*temp; tail = head; while(tail->next != NULL) tail = tail->next; . . . . . . . . } First we need to go to the end of the list. For that we initialize tail to point head and Traverse the list using a while loop, with tail = tail->next, as the loop statement. September 21, 2018 http://www.gowrikumar.com
void InsertEnd(List head,int d) { Node *tail,*temp; tail = head; 9/21/2018 1 10 8 4 6 3 2 5 head X 10 1 tail void InsertEnd(List head,int d) { Node *tail,*temp; tail = head; while(tail->next != NULL) tail = tail->next; . . . . . . . . } When the loop terminates, tail is pointing to the last element. Note that we want to point to the last element and not to NULL. September 21, 2018 http://www.gowrikumar.com
void InsertEnd(List head,int d) { . . . . . . . . 9/21/2018 1 10 8 4 6 3 2 5 head tail X 10 1 8 void InsertEnd(List head,int d) { . . . . . . . . temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; tail->next = temp; } A new node is created and the last node’s next pointer is made to point to this new node. Because, calloc takes care of initializing the next to NULL, we don’t have to worry about pointing it to NULL. September 21, 2018 http://www.gowrikumar.com
Insert September 21, 2018 http://www.gowrikumar.com
void Insert(List head,Node* p,int d) { 9/21/2018 1 10 8 4 6 3 2 5 head X 10 1 8 void Insert(List head,Node* p,int d) { temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; temp->next = p->next; p->next = temp; } Now let’s look at the general version of Insert, where the element is to be inserted after the node. September 21, 2018 http://www.gowrikumar.com
void Insert(List head,Node* p,int d) { 9/21/2018 1 10 8 4 6 3 2 5 head X 10 1 8 4 void Insert(List head,Node* p,int d) { temp = (Node*)calloc(1,sizeof(Node)); temp->data = d; temp->next = p->next; p->next = temp; } Now let’s look at the general version of Insert, where the element is to be inserted after the node. September 21, 2018 http://www.gowrikumar.com
1 10 8 4 6 3 2 5 head X 10 1 4 8 September 21, 2018 http://www.gowrikumar.com
Find September 21, 2018 http://www.gowrikumar.com
void Find(List l,Node* p,int d) { Node *temp; temp = l; 1 10 8 4 6 3 2 5 head X 10 1 4 8 void Find(List l,Node* p,int d) { Node *temp; temp = l; while(temp->next != NULL) if(temp->next->data == d) return temp; temp = temp->next; } return NULL; September 21, 2018 http://www.gowrikumar.com
void Find(List l,Node* p,int d) { Node *temp; temp = l; 1 10 8 4 6 3 2 5 head temp X 10 1 4 8 void Find(List l,Node* p,int d) { Node *temp; temp = l; while(temp->next != NULL) if(temp->next->data == d) return temp; temp = temp->next; } return NULL; September 21, 2018 http://www.gowrikumar.com
Delete September 21, 2018 http://www.gowrikumar.com
void Delete(List l,Node* p,int d) { Node *temp,*del; temp = Find(l,d); 1 10 8 4 6 3 2 5 head X 10 1 4 8 void Delete(List l,Node* p,int d) { Node *temp,*del; temp = Find(l,d); if(temp != NULL) del = temp->next; temp->next = del->next; free(del); } September 21, 2018 http://www.gowrikumar.com
void Delete(List l,Node* p,int d) { Node *temp,*del; temp = Find(l,d); 1 10 8 4 6 3 2 5 head temp del X 10 1 4 8 void Delete(List l,Node* p,int d) { Node *temp,*del; temp = Find(l,d); if(temp != NULL) del = temp->next; temp->next = del->next; free(del); } September 21, 2018 http://www.gowrikumar.com
10 8 4 6 3 2 5 head X 10 4 8 September 21, 2018 http://www.gowrikumar.com
int main { List l; Node* temp; l = Initialize(); InsertBegin(l,1); InsertEnd(l,8); temp = Find(l,8); Insert(l,temp,4); Delete(l,1); } September 21, 2018 http://www.gowrikumar.com
The End September 21, 2018 http://www.gowrikumar.com