Implementation of LIST ADT Using Linked list Data


































- Slides: 34

Implementation of LIST ADT Using Linked list Data Structures (SLL, DLL & CLL)

LIST using Linkedlist Data Structures • Linked list is a linear collection of elements called Nodes where the linear order is maintained by means of Links. • The elements of a Linked list are not stored in adjacent location as in Array DS. • Linked list may increase or decrease as and when required. Not fixed in size. Nodes in a linked list are not organized by their physical placement in memory (i. e. ) they may or may not be placed in a continuous memory location. Thus, the Nodes are logically related with each Note: other by storing the address of another node in its link field. So a node can be identified by the address stored on its predecessor or its successor Node.

Merits & Demerits ADVANTAGES OF LINKED LIST • Insertion and Deletion becomes easy and consumes very less time compare to Array DS because it needs to change only the link values rather than shifting other elements like Array. • Utilization of memory is high because of its dynamic memory allocation. • It is suitable for the application where the list size is not fixed. DISADVANTAGES OF LINKED LIST • A node requires memory space not only for storing elements but also needs extra spaces for storing the link value of next adjacent node. So double amount of memory space is needed in SLL and triple amount is needed in DLL. • Search operation become complex, only sequential search is possible in Linked List DS. It needs O(N) time to search an element.

STRUCTURE OF A NODE • Data Field: Used to store the Element values. A node may contain one or more Data fields. • Link Field (or) Address Field: Store the Address of adjacent node. This field is mainly used for making the Linked List. A node must contain atleast one Link field. • Head pointer: It holds the address of the first node for accessing the linked list. • Tail Pointer: It holds the address of the Last node in Linked list. Usage of this pointer is optional. • NULL pointer: To indicate the end of list, NULL value is stored in the address field of a node. This is called Null Address. (One or More Data with Single Link) Node with address F 001 (in Hex Code Format) 15 Data F 012 Link to the node “F 012” Data Fields Field Link

TYPES OF LINKEDLIST Based on the structure of a node, A Linked list is classified as 1. Singly Linked List (SLL) 2. Doubly Linked List (DLL) 3. Circular Singly Linked List (CSLL) 4. Circular Doubly Linked List (CDLL)

Singly Linked List (SLL) • It is a Basic form of linked list. • Node contains one or more data fields and only one Link field for holding the address of next node i. e. successor node. • NULL address is stored in the Last node link field, to indicate the end of List. • Traversing is done only from Head to Tail node. The reverse (Tail to Head) is not possible. Head 000 F 5 0002 Data Link 0002 10 0004 15 NULL Pointer (End of List)

Doubly Linked List (DLL) • For some applications, especially those where it is necessary to traverse a List in both direction need DLL. • It contains one or more data fields and two Link fields used to hold the address of predecessor and successor node. • In doubly linked list, two NULL addresses are stored in List, one is in Predecessor link field of the first node and another one is in Successor link field of the Last node. Head 000 F NULL BLink 5 Data 0002 FLink 000 F 6 0004 0002 8 NULL

Circular Linked List (CLL) • It is an option to represent a List which is naturally circular. • In some application, such as for a pool of buffers that are used and released in FIFO order. For a set of processes that should be time-shared in round-robin order, needs a circular list. • With a circular list, a pointer to the last node gives easy access also to the first node, by following one link. • There is no NULL address in any Node (i. e. ) Nodes are connected like a circular fashion. • This will be further classified as, – Circular Single Linked list – Circular Doubly Linked list

CSLL & CDLL Circular Singly Linked List (CSLL) the link field of last node holds the address of first node instead of NULL value. So the last node is again linked with a first node. Head 000 F 5 0002 10 0004 15 000 F Circular Doubly Linked List (CDLL) the successor field of last node holds the address of first node and the predecessor link field of first node holds the address of last node instead of NULL value. Head 000 F 0004 5 0002 000 F 6 0004 0002 8 000 F

Implementation of List using Singly Linked List Basic Operations (Createnode, Insertnode, Deletenode, Traversal, List. Size/Count, Search, Update)

Define node structure - SLL • Node can be defined using Structure DS. typedef struct node { int data; //data filed of node struct node *next; //Self-referential structure }; node *head=NULL, *temp=NULL;

Creation of a Node Algorithm Step-1: create memory for a node with size of structure using dynamic memory library function malloc(). Step-2: Read the data & assign it to the data field of a node. Step-3: Assign NULL value to its next field node* createnode(int data) { node* new_node = (node*)malloc(sizeof(node)); if(new_node == NULL) { printf("Error creating a new node. n"); exit(0); } new_node->data = data; new_node->next = NULL; return new_node; }

Insertion of Node in SLL Algorithm Step-1: Call to createnode() function Step-2: Check the head pointer is NULL or not (List Empty). If it is NULL then assign a newnode address to the head & exit. otherwise follow the next steps. Step-3: Read the data & position in which the new element is to be inserted. Step-4: By traversing the list, find the address of node in (position-1)th place and name it as temp Step-5: Assign temp->next to newnode->next and temp->next=newnode. Head 000 F 5 0002 10 Insert a node at position 2 0004 000 F 0009 NULL Before Insertion NULL 0009 000 F 5 15 0009 7 Head 0004 7 0002 10 0004 15 After Insert at position 2 NULL

void insertnode (int pos, int data) { int i=1; node *newnode=createnode(data); temp=head; if(head= =NULL) head=newnode; else { while(temp!=NULL && i<pos-1) { temp=temp->next; i=i+1; } newnode->next=temp->next; temp->next=newnode; } }

Deletion of Node from SLL Algorithm • Step-1: Check whether the list is empty or not, If empty then terminate the process, otherwise do next. • Step-2: Read the position of node to be deleted • Step-3: Check head->next is NULL or position given is 1, if so then assign head to temp, head=head->next and deallocate memory of temp using free() function. • Step-4: Otherwise, find the corresponding node in (position -1)th by traversing the list & store it in temp. • Step-5: Assign temp->next = temp->next and deallocate the memory for that deleted node. 0009 000 F Head 5 000 F 0009 0002 10 000 F Head 000 F 7 0002 5 0002 10 0004 15 Before Delete position 2 0004 15 After Delete at position 2 NULL

void deletenode( int pos) { int i=1; node *del; if(head= =NULL) printf(“List is emptyn”); else { if((pos= =1)|| (head->next= =NULL)) { temp=head; head=head->next; free(temp); } else { temp=head; while(temp!=NULL && i<(pos-1)) { temp=temp->next; i=i+1; } del=temp->next; temp->next=temp->next; free(del); } } }

Algorithm for List Traversal & node count 1. Check head is equal to NULL. if so then return count is 0 & terminate process. 2. SET temp=head & i=0 3. Repeat step 4 & 5 until temp!=NULL 4. Print temp->data 5. Increment i by 1 and temp by temp->next 6. Return i. int traversal( ) { int i; if(head= =NULL) { printf(“List emptyn”); return 0; } else { i=0; temp=head; printf(“elements aren); while(temp!=NULL) { printf(“%dt”, temp->data); i=i+1; temp=temp->next; } return i; } }

Home Task 1. Write algorithm & function in C for search an element in SLL? 2. Write algorithm & function in C for modify/update an element in SLL?

Implementation of List using Doubly Linked List Basic Operations (Createnode, Insertnode, Deletenode, Traversal, List. Size/Count, Search, Update)

Doubly Linked List (DLL) • For some applications, especially those where it is necessary to traverse a List in both direction need DLL. • It contains one or more data fields and two Link fields used to hold the address of predecessor and successor node. • In doubly linked list, two NULL addresses are stored in List, one is in Predecessor link field of the first node and another one is in Successor link field of the Last node. Head 000 F NULL BLink 5 Data 0002 FLink 000 F 6 0004 0002 8 NULL

Define node structure - DLL • Node can be defined using Structure DS. typedef struct node { int data; //data filed of node struct node *next, *prev; //Self-referential }; node *head=NULL, *temp=NULL;

Creation of a Node Algorithm Step-1: create memory for a node with size of structure using dynamic memory library function malloc(). Step-2: Read the data & assign it to the data field of a node. Step-3: Assign NULL value to its both link field node* createnode(int data) { node* new_node = (node*)malloc(sizeof(node)); if(new_node == NULL) { printf("Error creating a new node. n"); exit(0); } new_node->data = data; new_node->next= NULL; new_node->prev= NULL; return new_node; }

Insertion of Node in DLL Algorithm Step-1: Call to createnode() function Step-2: Check the head pointer is NULL or not (List Empty). If it is NULL then assign a newnode address to the head & exit. otherwise follow the next steps. Step-3: Read the data & position in which the new element is to be inserted. Step-4: By traversing the list, find the address of node in (position-1)th place and name it as temp Step-5: Assign newnode->next=temp->next and newnode->prev=temp. Step-6: Assign temp->next-> prev=newnode and temp->next=newnode. Head 000 F NULL 5 0002 Insert a node at 2 0002 6 0004 000 F 0009 NULL 7 NULL 0004 0002 8 NULL Before Insertion Head NULL 0009 000 F 5 0009 000 F 7 0002 0009 6 0004 0002 8 After Insert at 2 position NULL

void insertnode (int pos, int data) { int i=1; node *newnode=createnode(data); temp=head; if(head= =NULL) head=newnode; else { while(temp!=NULL && i<pos-1) { temp=temp->next; i=i+1; } newnode->next=temp->next; newnode->prev=temp; temp->next-> prev=newnode; temp->next=newnode; } }

Deletion of Node from DLL Algorithm • Step-1: Check whether the list is empty or not, If empty then terminate the process, otherwise do next. • Step-2: Read the position of node to be deleted • Step-3: Check next field of head node is NULL or position given is 1, if so then assign head to temp, head of next to head and deallocate memory of temp. • Step-4: Otherwise, find the corresponding node in (position -1)th by traversing the list & update temp. • Step-5: Assign the temp->next to temp->next and also assign temp to temp->next->prev. • Step-6: Deallocate the memory for deleted node. Head 000 F NULL 0009 000 F 5 0009 000 F 7 0002 0009 6 0004 0002 8 NULL Before Delete 2 position Head 000 F NULL 5 0002 000 F 6 0004 0002 8 NULL After Delete 2 position

void deletenode( int pos) { int i=1; node *del; if(head= =NULL) printf(“List is emptyn”); else { if((pos= =1)|| (head->next= =NULL)) { temp=head; head=head->next; free(temp); } else { temp=head; while(temp!=NULL && i<(pos-1)) { temp=temp->next; i=i+1; } del=temp->next; //deleted node temp->next=temp->next; temp->next->prev=temp; free(del); } } }

Note: Algorithm & function in C for other operations like Traversal, List. Size/node count, Search, Modify are similar to SLL.

Solution 1. Write algorithm & function in C for search an element in SLL? 2. Write algorithm & function in C for modify/update an element in SLL?

void search( int ele) { int i=1; if(head= =NULL) printf(“list emptyn”); else { temp=head; while(temp!=NULL) { if(temp->data= =ele) { printf(“data found at %d”, i); break; } i++; temp=temp->next; } if(temp= =NULL) printf(“element not foundn”); } }

void modify(int old, int new ) { if(head= =NULL) printf(“List is emptyn”); else { temp=head; while(temp!=NULL) { if(temp->data= =old) { temp->data=new; break; } temp=temp->next; } if(temp= =NULL) printf(“Data Not found”); } }

Implementation of List using Circular Singly Linked List Basic Operations (Createnode, Insertnode, Deletenode, Traversal, List. Size/Count, Search, Update)

Common operations like SLL The following are same as SLL • Node Structure • Node creation • Traversal, Search, Modify and Count operations of CSLL is same as SLL except that wherever we specify “NULL” in SLL program, here we change Note: that as “Head” • Instead of using “while loop” here use “do-while loop” for traversing a list.

void insertnode (int pos, int ele) { int i=1; node*newnode=createnode(ele); if((head= =NULL) { head=tail=newnode; tail->next=head; } else if (pos==1) { newnode->next=head; head=newnode; tail->next=head; } else { temp=head; do { if(i<pos-1) { temp=temp->next; i=i+1; } } while(temp!=head); If(temp->next==head) tail=newnode; newnode->next=temp->next; temp->next=newnode; } }

void deletenode (int pos ) { int i=1; node *del; if(head= =NULL) printf(“List is emptyn”); else { temp=head; if(head->next==head) { head=tail=NULL; free(temp); } else if (pos==1) { head=head->next; free(temp); tail->next=head; } else { do { if(i<pos-1) { temp=temp->next; i=i+1; } } while(temp!=head); del=temp->next; if(del->next==head) tail=temp; temp->next=temp->next; free(del); } } }