Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
PGDCA II Semester Data Structures and Algorithms Linear Data Structure – Linked List 1. Definition 2. Representing List in C 3. Implementing the operations a. Inserting a node b. Deleting a node c. List Traversal 4. Linked implementation of Stack 5. Linked implementation of Queue 6. Circular List a. Circular Stack b. Circular Queue 7. Doubly Linked List 8. Circular Doubly Linked List Introduction of Linked List What are the drawbacks of using sequential storage to represent stacks and queues? One major drawback is that a fixed amount of storage remains allocated to the stack or queue even when the structure is actually using a smaller amount or possibly no storage may be allocated, thus introducing the possibility of overflow. Definition [ Linked List ] Linear linked list is a data structure of explicit ordering of items and each item contains two portions; one is information portion and the other one is next address portion. In explicit ordering of items, each item contained within itself the address of another item. Such an explicit ordering gives rise to a data structure which is known as a linear linked list. list null Figure 1: Linear Linked List Each item in a list is called a node and contains two fields, an information field and a next address field. The information field holds the actual element on the list. The next address field contains the address of the next node in the list. Such an address, which is used to access a particular node, is known as a pointer. The entire linked list is accessed from an external pointer list that points to first node in the list. The next address field of the last node in the list contains a special value called null value. Representing List in C The following C code represents the List. typedef struct { int info; // information portion struct Node *next; // next address portion }Node; Page 1 of 8 Prepared by: Manoj Shakya PGDCA II Semester Data Structures and Algorithms typedef Node *Nodeptr; Implementing the operations Implementing various operations like insert, remove, and list_traversal. Nodeptr getNode(){ return ( (Nodeptr) malloc (sizeof(Node)) ); } void insert(Nodeptr List, int value){ Nodeptr p = getNode(); p->info = value; p->next = List; List = p; } int remove(Nodeptr List){ int x; Nodeptr p = List; List = p->next; x = p->info; free(p); return x; } void list_traversal(Nodeptr List){ Nodeptr p = List; for(;p!=NULL;){ int x = p->info; printf(“%d”, x); p = p->next; } } Note: Do not use the exact codes; you have to modify these codes to use in any program that implements Linked List. Linked Implementation of Stack struct node { int info; node *next; }; typedef struct node *Nodeptr; int isEmpty(Nodeptr &list) { if(list == NULL) return 1; else return 0; } void push(Nodeptr &list, int value) { Node p; p = (Nodeptr)malloc(sizeof(node)); if(p==NULL) { printf("Can't allocate memory."); Page 2 of 8 Prepared by: Manoj Shakya PGDCA II Semester Data Structures and Algorithms getch(); exit(0); } p->next = list; list = p; list->info = value; } int pop(Nodeptr &list) { int info; if(isEmpty(list)) { printf("Underflow"); getch(); exit(0); } Nodeptr p; p = list; info = p->info; list = p->next; free(p); return info; } Linked Implementation of Queue struct Node { int info; struct Node *next; }; typedef struct Node* nodeptr; int isEmpty(nodeptr& front) { if(front==NULL) return 1; else return 0; } void insert(nodeptr& front, nodeptr& rear, int v) { nodeptr p= (nodeptr)malloc(sizeof(Node)); p->next = NULL; p->info = v; if(front==NULL) front = p; if(rear == NULL) rear = p; else { rear->next = p; rear = rear->next; } } int remove(nodeptr &front) { if(isEmpty(front)) Page 3 of 8 Prepared by: Manoj Shakya PGDCA II Semester Data Structures and Algorithms { printf("Underflow"); getch(); exit(1); } nodeptr p; p = front; int x = p->info; front = p->next; free(p); return x; } Circular Linked List Although a linear linked list is a useful data structure, it has several shortcomings. Given a pointer p to a node in a linear list, we cannot reach any of the nodes that precede node p. If a list is traversed the external dummy pointer to the list must be preserved to be able to reference the list again. Suppose that a small change is made to the structure of a linear list, so that the next field in the last node contains a pointer back to the first node rather than the null pointer. Such a list is called a Circular Linked List. list Figure 2: Circular Linked List A Circular List is a linked list in which the node at the last of the list, instead of having a null pointer, points back to the node at the head of the list. We then need only one pointer (dummy) to access both ends of the list, since we know that last->next points back to the first node in the list. Stack as a Circular List int isEmpty(Nodeptr &stack) { if(stack == NULL) return 1; else return 0; } void push(Nodeptr &stack, int x) { Nodeptr p = (Node)malloc(sizeof(Node)); p->info = x; if(stack==NULL){ stack = p; }else{ p->next = stack->next; } stack->next = p; } Page 4 of 8 Prepared by: Manoj Shakya PGDCA II Semester Data Structures and Algorithms The following diagram illustrates the sequences of push operation. stack 3 4 p 1 2 x Figure 3: Sequences of push operation. int pop(Nodeptr &stack) { int info; Nodeptr p; if(stack == null) { printf("Underflow"); getch(); exit(0); } p = stack->next; info = p->info; if(p==stack){ // in case of one node only. stack = null; }else{ stack->next = p->next; } free(p); return info; } The following diagram illustrates the sequences of pop operation. stack 2 x 1 4 3 free p Figure 4: Sequences of pop operation. Page 5 of 8 Prepared by: Manoj Shakya PGDCA II Semester Data Structures and Algorithms Queue as a Circular List It is easier to represent a queue as a circular list than as a linear list. As a linear list, a queue is specified by two pointers, one to the front of the list and the other to its rear. However, by using a circular list, a queue may be specified by a single pointer to that list. The operations isEmpty(queue) and remove(queue) are identical with that of stack operations. void insert(Nodeptr &queue, int x) { Nodeptr p = (Node)malloc(sizeof(Node)); p->info = x; if(queue==NULL){ queue = p; }else{ p->next = queue->next; } queue->next = p; queue = p; } The following diagram illustrates the sequences of insert operation. queue 4 3 5 2 queue 1 x p Figure 5: Sequences of insert operation. Doubly Linked List Although a circularly linked list has advantages over a linear list, it still has several drawbacks. One cannot traverse such a list backward, nor can a node be deleted from a circularly linked list, given only a pointer to that node. In cases, where these facilities are required, the appropriate data structure is a doubly linked list. Each node in such a list contains two pointers, one to its predecessor and another to its successor. It is thus possible to move either direction through the list while keeping only one pointer. With a doubly linked list, traversals in direction, insertions and deletions from arbitrary positions in the list can be programmed without difficulty. The cost of a doubly linked list, of course, is the extra space required in each node for a second link. Figure 6: Doubly Linked List. Page 6 of 8 Prepared by: Manoj Shakya PGDCA II Semester Data Structures and Algorithms Implementing some operations int deleteNode(nodeptr &p){ nodeptr q, r; int res; if(p==null){ printf("void deletion."); exit(0); } res = p->info; q = p->previous; r = p->next; q->next = r; r->previous = q; free(p); return res; } p 4 2 3 5 q r 1 res Figure 7: Deleting a node from doubly linked list. void insertNodeInRight(nodeptr &p, int x){ nodeptr n, r = p->next; n = (nodeptr)malloc(sizeof(node)); n->previous = r->previous; n->next = p->next; p->next = n; r->previous = n; n->info = x; } p r 1 5 6 4 3 n 2 Figure 8: Inserting a node in right position. Page 7 of 8 Prepared by: Manoj Shakya PGDCA II Semester Data Structures and Algorithms void insertNodeInLeft(nodeptr &p, int x){ nodeptr n, l; n = (nodeptr)malloc(sizeof(node)); n->previous = p->previous; n->next = l->next; l->next = n; p->previous = n; n->info = x; } l p 1 5 6 4 3 n 2 Figure 8: Inserting a node in left position. Circular Doubly Linked List In a doubly-circularly-linked list, each node has two links, similar to a doubly-linked list, except that the previous link of the first node points to the last node and the next link of the last node points to the first node. As in a doubly-linked list, insertions and removals can be done at any point with access to any nearby node. Page 8 of 8 Prepared by: Manoj Shakya