Linked List - Data Structures Lecture Notes
Document Details

Uploaded by EminentSard6676
Er. Mohanapriya
Tags
Summary
These lecture notes cover the concept of linked lists, including their introduction, node organization, and different types like singly-linked and doubly-linked lists. It also discusses basic operations on linked lists such as creating, traversing, inserting, and deleting nodes, along with code examples.
Full Transcript
UNIT -5 LINKED LIST LECTURER ER.MOHANAPRIYA Introduction to the Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures null list head Introduction to the...
UNIT -5 LINKED LIST LECTURER ER.MOHANAPRIYA Introduction to the Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures null list head Introduction to the Linked List ADT References may be addresses or array indices Data structures can be added to or removed from the linked list during execution newNode list head null Linked Lists vs. Arrays and Vectors Linked lists can grow and shrink as needed, unlike arrays, which have a fixed size Linked lists can insert a node between other nodes easily null list head Node Organization A node contains: data: one or more data fields – may be organized as structure, object, etc. a pointer that can point to another node pointer data Linked List Organization Linked list contains 0 or more nodes: null list head Has a list head to point to first node Last node points to null(address 0) Empty List If a list currently contains 0 nodes, it is the empty list In this case the list head points to null list head NULL Declaring a Node Declare a node: struct ListNode { int data; ListNode *next; }; No memory is allocated at this time Defining a Linked List Define a pointer for the head of the list: ListNode *head = nullptr; Head pointer initialized to nullptr to indicate an empty list head null The Null Pointer Is used to indicate end-of-list Should always be tested for before using a pointer: ListNode *p; while (!p) Illustration: Insertion A B C Item to be tmp X inserted A B C curr X Pseudo-code for insertion typedef struct nd { struct item data; struct nd * next; } node; void insert(node *curr) { node * tmp; tmp=(node *) malloc(sizeof(node)); tmp->next=curr->next; curr->next=tmp; } Illustration: Deletion Item to be deleted A B C tmp curr A B C Pseudo-code for deletion typedef struct nd { struct item data; struct nd * next; } node; void delete(node *curr) { node * tmp; tmp=curr->next; curr->next=tmp->next; free(tmp); } In essence... For insertion: – A record is created holding the new item. – The next pointer of the new record is set to link it to the item which is to follow it in the list. – The next pointer of the item which is to precede it must be modified to point to the new item. For deletion: – The next pointer of the item immediately preceding the one to be deleted is altered, and made to point to the item following the deleted item. Array versus Linked Lists Arrays are suitable for: – Inserting/deleting an element at the end. – Randomly accessing any element. – Searching the list for a particular value. Linked lists are suitable for: – Inserting an element. – Deleting an element. – Applications where sequential access is required. – In situations where the number of elements cannot be predicted beforehand. Types of Lists Depending on the way in which the links are used to maintain adjacency, several different types of linked lists are possible. – Linear singly-linked list (or simply linear list) One we have discussed so far. head A B C – Circular linked list The pointer from the last element in the list points back to the first element. head A B C – Doubly linked list Pointers exist between adjacent nodes in both directions. The list can be traversed either forward or backward. Usually two pointers are maintained to keep track of the list, head and tail. head tail A B C Basic Operations on a List Creating a list Traversing the list Inserting an item in the list Deleting an item from the list Concatenating two lists into one List is an Abstract Data Type What is an abstract data type? – It is a data type defined by the user. – Typically more complex than simple data types like int, float, etc. Why abstract? – Because details of the implementation are hidden. – When you do some operation on the list, say insert an element, you just call a function. – Details of how the list is implemented or how the insert function is written is no longer required. Conceptual Idea Insert List implementation Delete and the related functions Traverse Example: Working with linked list Consider the structure of a node as follows: struct stud { int roll; char name; int age; struct stud *next; }; typedef struct stud node; node *head; Creating a List How to begin? To start with, we have to create a node (the first node), and make head point to it. head = (node *) malloc(sizeof(node)); head roll name next age Contd. If there are n number of nodes in the initial linked list: – Allocate n records, one by one. – Read in the fields of the records. – Modify the links of the records so that the chain is formed. head A B C node *create_list() { int k, n; node *p, *head; printf ("\n How many elements to enter?"); scanf ("%d", &n); for (k=0; knext = (node *) malloc(sizeof(node)); p = p->next; } scanf ("%d %s %d", &p->roll, p->name, &p->age); } p->next = NULL; return (head); } To be called from main() function as: node *head; ……… head = create_list(); Traversing the List What is to be done? Once the linked list has been constructed and head points to the first node of the list, – Follow the pointers. – Display the contents of the nodes as they are traversed. – Stop when the next pointer points to NULL. void display (node *head) { int count = 1; node *p; p = head; while (p != NULL) { printf ("\nNode %d: %d %s %d", count, p->roll, p->name, p->age); count++; p = p->next; } printf ("\n"); } To be called from main() function as: node *head; ……… display (head); Inserting a Node in a List How to do? The problem is to insert a node before a specified node. – Specified means some value is given for the node (called key). – In this example, we consider it to be roll. Convention followed: – If the value of roll is given as negative, the node will be inserted at the end of the list. Contd. When a node is added at the beginning, – Only one next pointer needs to be modified. head is made to point to the new node. New node points to the previously first element. When a node is added at the end, – Two next pointers need to be modified. Last node now points to the new node. New node points to NULL. When a node is added in the middle, – Two next pointers need to be modified. Previous node now points to the new node. New node points to the next node. void insert (node **head) { int k = 0, rno; node *p, *q, *new; new = (node *) malloc(sizeof(node)); printf ("\nData to be inserted: "); scanf ("%d %s %d", &new->roll, new->name, &new->age); printf ("\nInsert before roll (-ve for end):"); scanf ("%d", &rno); p = *head; if (p->roll == rno) { new->next = p; *head = new; } else { while ((p != NULL) && (p->roll != rno)) { q = p; p = p->next; } The pointers if (p == NULL) q and p { always point q->next = new; to consecutive new->next = NULL; nodes. } else if (p->roll == rno) { q->next = new; new->next = p; } } } To be called from main() function as: node *head; ……… insert (&head); Deleting a node from the list What is to be done? Here also we are required to delete a specified node. – Say, the node whose roll field is given. Here also three conditions arise: – Deleting the first node. – Deleting the last node. – Deleting an intermediate node. void delete (node **head) { int rno; node *p, *q; printf ("\n Delete for roll :"); scanf ("%d", &rno); p = *head; if (p->roll == rno) { *head = p->next; free (p); } else { while ((p != NULL) && (p->roll != rno)) { q = p; p = p->next; } if (p == NULL) printf ("\n No match :: deletion failed"); else if (p->roll == rno) { q->next = p->next; free (p); } } }