Linear Data Structure Using Linked Organization (Linked List)
Total Page:16
File Type:pdf, Size:1020Kb
Linear data structure using Linked organization (Linked List) Unit VI: Linear data structures using linked organization • Concept of linked organization, singly linked list, doubly linked list, circular linked list. • Linked list as an ADT. • Representation and manipulations of polynomials using linked lists. • Comparison of a sequential and linked memory organization. • Concept of Generalized Linked List. • Representation polynomial using GLL. List Overview • What is a list and its ADT • Array as an list • Linked List • Why linked list ? – Limitations of array • Linked lists – Abstract data type (ADT) – Types of linked list • Basic operations of linked list and their implementations :: – Insert, fnd, delete, print, etc. • Variations of linked lists – Circular linked lists – Doubly linked lists The List ADT • A sequence of zero or more elements A1, A2, A3, … AN • N: length of the list • A1: frst element • AN: last element • Ai: position i • If N=0, then empty list • Linearly ordered – Ai precedes Ai+1 – Ai follows Ai-1 List ADT :: Operations • printList: print the list • makeEmpty: create an empty list • fnd: locate the position of an object in a list – list: 34,12, 52, 16, 12 – fnd(52) 3 • insert: insert an object to a list – insert(x,3) 34, 12, 52, x, 16, 12 • remove: delete an element from the list – remove(52) 34, 12, x, 16, 12 • Find Kth: retrieve the element at a certain position Implementation of an ADT • Two standard implementations for the list ADT – Array-based – Linked list Overall operations on the list: Insertion • Create – Insert at frst • Insertion – Insert at last • Deletion – Insert at particular position • Searching • Display • Sorting Deletion • Reverse – Deletion at Front • Split – Deletion at last – • Merge Deletion a particular element Array Implementation • Elements are stored in contiguous array positions Array Implementation... • Requires an estimate of the maximum size of the list • printList and fnd: linear • Find Kth: constant • insert and delete: slow – e.g. insert at position 0 (making a new element) • requires frst pushing the entire array down one spot to make room – e.g. delete at position 0 • requires shifting all the elements in the list up one – On average, half of the lists needs to be moved for either operation Why Linked list ? • Limitations of array – Dynamic vs. static data structure • unpredictable storage requirement, wastage of memory – Operations on array vs. linked list • Extensive manipulation of data make it slow • Hard to implement nonlinear data structure using array. Linked Lists A B C Head • Linked list is ordered list type. node • A linked list is a series of connected nodes A • Each node contains at least – A piece of data (any type) data pointer – Pointer /link to the next node in the list • Head: pointer to the frst node • The last node links points to NULL Pointer Implementation (Linked List) • Ensure that the list is not stored contiguously – use a linked list – a series of structures that are not necessarily adjacent in memory . Each node contains the element and a pointer to a structure containing its successor ✔the last cell’s next link points to NULL . Compared to the array implementation, the pointer implementation uses only as much space as is needed for the elements currently on the list but requires space for the pointers in each cell A Simple Linked List C implementation • Declare Node structure for the nodes – data: int/ char/ foating -type – next: a pointer to the next node in the list typedef strcuture Node { int data; // data Node* next; // pointer to next }; Node 10 data next Pointer-Based Linked Lists Figure :: A head pointer to a list Terminology • linked list a list consisting of items in which each item knows the location of the next item. • Node an item in a linked list. • Each node contains a piece of list data and the location of the next node (item). • link (of a node) the location of the next node. • Head node frst node in a linked list • Head pointer points to the head node Basic Structure Pseudo code • • Problems: Find last node in list • Create an empty list. P=head; head = NULL; While (p->next !=NULL) p= p->next; • Determine if a list is empty Find an item (x) in a list p = head while( p->next != NULL && if (head == NULL) p->info != x) { // list is empty access p->info p = p->link } Create void main() { Node *p,* head =NULL; int i; /* // multiple nodes printf(“\n\t How many elements” ); clrscr(); scanf( “%d”,&n); // One node creation p=head; for(i=0;i<n;i++) head =(Node *) { malloc( sizeof(Node)); p->next=(Node *) malloc (sizeof(Node)); scanf(“%d”,head->data); p=p->next; scanf(“%d”,p->data); head->next=NULL; } printf(“%d”,head->data); getch(); } Preliminaries Head Head Head Figure a) A linked list of integers; b) insertion; c) deletion Displaying the Contents of a Linked List Head Head The effect of the assignment cur = cur->next Displaying the Contents of a Linked List Void display(Node *HeadD) Recursive { void printR(Node *Head while(HeadD!=NULL) { { if(Head!=NULL) print HeadD; { HeadD = HeadD→next; printf(“%d”, head→ data) } printR(head->next) } } } Insertion • Insertin at start Insertion at MIDDLE 1. t next = start 1. Enter info of the node after which 2. start = t new node to be inserted Return start 2. Read x • Insertion at LAST 3. p = start 1. p = start 4. Repeat step 5 until p info < > x 2. Repeat step 3 until p next 5. p = p next NULL 6. t next = p next 3. p = p next 4. 5. p next = 7. p next = t getnode() 8. Return 6. Return start Void insert_E(Node *Headt) { Node *temp = allocate address(); while(Headt->next!=NULL) { Headt = Headt->next; } Headt ->next = temp; } Node *insert_S(Node *Headt) { Node *temp = allocate address(); temp->next =Headt; Headt = temp; Return Headt; } Void insert_Betw(Node *Head) { int t; Node *Headt=Head; Node *temp = allocate address(); printf(“Enter data after which we want to insert a Node”); scanf(“%d”, &t); If (Headt == NULL) Headt = temp; return Headt; while(Headt ->data !=t) { Headt = Headt->next; } Temp->next = headt->next; Headt->next = temp; return Head; } Deletion • Delete frst node • Delete at pos 1. x = start 1. Enter th e info of node to be 2. start = start next deleted 3. free(x) 2. Read n 3. p = start • Delete last node 4. c = start 1. p = start 5. while (c info < > NULL) c = start 2. while (c next < > NULL) { p = c p = c c = c next } c = c next 6. p next = c next 3. p next = c next 7. free ( c ) 4. free ( c) 8. Return start 5. return start Deleting a Specifed Node from a Linked List Figure :: Deleting a node from a linked list Figure :: Deleting the first node Deleting a Specifed Node from a Linked List • Deleting an interior node prev->next=cur->next; • Deleting the frst node head=head->next; • Return deleted node to system cur->next = NULL; delete cur; cur=NULL; Node *reverse(Node * start) { Node *current ,*temp, *result; temp=NULL; result=NULL; current =start; Reverse The if(start->next==NULL) return start; Singly Link List while(current!=NULL) { temp = current->next; current->next = result; result= current; current = temp; } start = result; return start; } Display List in reverse order void printR(Node *head) { if(head != NULL) { printR(head->next); printf("%d ",head->data); } } Assignment 9 Create a singly linked list with options: Insert (at front, at end, in the middle), Delete (at front, at end, in the middle), Display, Display Reverse, Revert the SLL without using additional data structure. Bubble sort to sort a linked list n=length(head) for(i = 1; i < n; i++) { p1 = head; p2 = head->next; p3 = p2->next; for(j = 1; j <= (n - i); j++) { if(p2->value < p3->value) { p2->next = p3->next; p3->next = p2; p1->next = p3; p1 = p3; p3 = p2->next; } else { p1 = p2; p2 = p3; p3 = p3->next; } } } Sorting Node * sortlist(N ode *start) { Node *index 1, Index 2; int temp; index1= start; for(; new1->next != NULL; index1= index1->next) { for(index 2 = index1->next; index2 != NULL; index2= index2->next) { if(index1->data > index2->data) { temp = index1->data; index1->data = index2->data; index1->data = temp; } } }} Merging two linked list Node *Merge (Node * head1, Node *head2 , Node * head3) Node * h1, *h2, h3 { // create third list frst node head3= (Node *) malloc(sizeof (Node)) head3- >info =h1->info head1=h1->next head3->next =NULL h3=head3 } Merging continued … • Case 1 – copy all the remaining node of list one to list 3 • Case 2 – Copy the all the remaining element of list two to list three • Last – Return header of list 3 Variations of Linked Lists • Circular linked lists • – The last node points to the frst node of the list A B C Head How do we know when we have fnished traversing the list? (Tip: check if the pointer of the current node is equal to the head.) Variations of Linked Lists • Doubly linked lists – Each node points to not only successor but the predecessor – There are two NULL: at the frst and last nodes in the list – Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists – backwards A B C Head Array versus Linked Lists • Linked lists are more complex to code and manage than arrays, but they have some distinct advantages. – Dynamic: a linked list can easily grow and shrink in size. • We don’t need to know how many nodes will be in the list. They are created in memory as needed. • In contrast, the size of a C++ array is fxed at compilation time. – Easy and fast insertions and deletions • To insert or delete an element in an array, we need to copy to temporary variables to make room for new elements or close the gap caused by deleted elements.