This document discusses different operations on linked lists such as insertion, deletion, and traversal. It begins with an introduction to linked lists explaining that each node contains a data field and pointer to the next node. It then covers implementing a basic node structure and various functions like creating a new node, adding a node to the beginning or end of the list, and deleting a node from the beginning, end, or a given position. Traversal and keeping track of previous nodes is important for operations like deletion from within the list. The document provides pseudocode to demonstrate common linked list operations.
Index • Introduction • LinkedList implementation • Node Creation • Insertion at Beginning • Insertion at End of the List • Insertion at given Position • Deletion at Beginning • Deletion at End of the List • Deletion at given Position 2
3.
Introduction • A linkedlist is a data structure that consists of sequence of nodes. Each node is composed of two fields: data field and reference field which is a pointer that points to the next node in the sequence. 3
4.
Continue… • Each nodein the list is also called an element. The reference field that contains a pointer which points to the next node is called next pointer or next link. • A head pointer is used to track the first element in the linked list, therefore, it always points to the first element. • The following picture illustrates a linked list. 4
5.
Continue… • The linkedlist data structure is designed to be efficient for insertion or removal of elements from any position in the list. • However other operations such as getting the last element or finding an element that stores specific data requires scanning most or all the elements in the list. • A linked list is also used to implement other data structures like stack and queue. 5
6.
Linked List implementation •We can model a node of the linked list using a structure as follows: • The node structure has two members: • data stores the information • next pointer holds the address of the next node. 1 2 3 4 typedef struct node{ int data; struct node* next; }; 6
7.
Add a nodeat the beginning of the linked list • First, we declare a head pointer that always points to the first node of the list. • To add a node at the beginning of the list: • First, we need to create a new node. We will need to create a new node each time we want to insert a new node into the list so we can develop a function that creates a new node and return it. 1node* head; 7
Adding a nodeat beginning •Steps to insert node at the beginning of singly linked list • Create a new node, say newNode points to the newly created node. • Link the newly created node with the head node, i.e. the newNode will now point to head node. • Make the new node as the head node, i.e. now head node will point to newNode 11
Insert node atLast / End Position in Singly Linked List •Inserting node at start in the Single Linked List (Steps): • Create New Node • Fill Data into “Data Field“ • Make it’s “Pointer” or “Next Field” as NULL • Node is to be inserted at Last Position so we need to traverse SLL upto Last Node. • Make link between last node and newnode 16
Continue… • Check ifthe given prev_node is NULL • Allocate new node • Put in the data • Make next of new node as next of prev_node. • Move the next of prev_node as new_node 20
Continue… •Steps to deletefirst node from Singly Linked List • Copy the address of first node i.e. head node to some temp variable say toDelete. • Move the head to the second node of the linked list i.e. head = head->next. • Disconnect the connection of first node to second node. • Free the memory occupied by the first node. 23
Deletion of aNode at End of the List • Steps to delete last node of a Singly Linked List • Traverse to the last node of the linked list keeping track of the second last node in some temp variable say secondLastNode. • If the last node is the head node then make the head node as NULL else disconnect the second last node with the last node i.e. secondLastNode->next = NULL. • Free the memory occupied by the last node. 29
30.
Step 1: Traverseto the last node of the linked list keeping track of the second last node 30
Deletion at givenPosition •Steps to delete middle node of Singly Linked List •Traverse to the nth node of the singly linked list and also keep reference of n-1th node in some temp variable say prevnode. 34
35.
Continue… • Reconnect then-1th node with the n+1th node i.e. prevNode->next = toDelete->next (Where prevNode is n-1th node and toDelete node is the nth node and toDelete->next is the n+1th node). 35
Continue… // Find previousnode of the node to be deleted for (int i=0; prev!=NULL && i<pos-1; i++) prev = prev->next; for (int i=0; next!=NULL && i<pos+1; i++) next = next->next; prev->next=next; 37