operations on linked list
TRANSCRIPT
![Page 1: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/1.jpg)
![Page 2: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/2.jpg)
Organization of data and the Organization of data and the relationship among its members relationship among its members is called as data structureis called as data structure
Data Structure
![Page 3: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/3.jpg)
![Page 4: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/4.jpg)
Why we need Linked List?What are the problems with Arrays?Size is fixedArray Items are stored contiguouslyInsertions and deletion at particular position is complexWhy Linked list ? Size is not fixed Data can be stored at any place Insertions and deletions are simple and faster
![Page 5: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/5.jpg)
What are Linked ListsWhat are Linked Lists
A linked list is a linear data structure.
Nodes make up linked lists.Nodes are structures made
up of data and a pointer to another node.
Usually the pointer is called next.
![Page 6: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/6.jpg)
A linked list consists of:◦ A sequence of nodes
6
a b c d
Each node contains a value
and a link (pointer or reference) to some other node
The last node contains a null link
The list may (or may not) have a header
myList
![Page 7: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/7.jpg)
Linked List Types Singly Linked list Doubly linked lists Circular singly linked list Circular doubly linked lists
![Page 8: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/8.jpg)
Each node has only one link part
Each link part contains the address of the next node in the list
Link part of the last node contains NULL value which signifies the end of the node
![Page 9: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/9.jpg)
10
1000
1000
2000
200015 NULL20
4000
Singly Linked ListEach node points the next node . It is
a one way list.First
![Page 10: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/10.jpg)
10 15 20
Circular Singly Linked ListLast node contains the address of the first node
First
![Page 11: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/11.jpg)
Doubly Linked listContains the address of previous node
and next node
NULL
2000 30001000
10 15 202000 1000 2000 NULL3000
First
![Page 12: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/12.jpg)
Circular Doubly Linked listContains the address of first node and
last node
3000
2000 30001000
10 15 202000 1000 2000 10003000
![Page 13: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/13.jpg)
Operations on Linked ListCreation of the linked listInsertion of nodesTraversal of the linked listSearch a specific node in the linked listDeletion of a node in the linked list
![Page 14: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/14.jpg)
Creation of the linked listCreation of a LL is a special case of insertion, i.e. insertion
into an empty list.If the value of start is null, it implies that there are no nodes in the list.Creation is a two step processa)Creating the new node
b)Storing the address of the new node in the start
![Page 15: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/15.jpg)
10 20 30 45 X
Next
Creation of the Linked ListStart
Info Next
![Page 16: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/16.jpg)
There are three ways to insert a node into the list.
Insertion at the beginning Insertion at the end Insertion after a particular node
![Page 17: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/17.jpg)
There are two steps to be followed:-
a)Make the next pointer of the node point towards the first node of the list
b)Make the start pointer point towards this new node
If the list is empty simply make the start pointer point towards the new node;
![Page 18: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/18.jpg)
![Page 19: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/19.jpg)
Algorithm to Insert an Element from the front INSERT(Start,data)1.Allocate memory to New2. Set NewInfo=data3.NewNext=start4.Start=New5.Return Start
![Page 20: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/20.jpg)
Here we simply need to make the next pointer of the last node point to the new node
![Page 21: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/21.jpg)
Here we again need to do 2 steps :-
Make the next pointer of the node to be inserted point to the next node of the node after which you want to insert the node
Make the next pointer of the node after which the node is to be inserted, point to the node to be inserted
![Page 22: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/22.jpg)
![Page 23: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/23.jpg)
Traversal of a LL means to traverse each node of the list, and visiting each node exactly once.Algorithm:1.Set Ptr to Start.2.Repeat steps 3 and 4 until Ptr is not equal to Null.3.Print the Info part of each node to which Ptr is pointing currently.4.Advance the pointer Ptr so that it points to the next node.5.End
![Page 24: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/24.jpg)
a b c d
start Info
NextPtr
![Page 25: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/25.jpg)
Logic using CTraverse(){Struct node *ptr;Ptr=start;While(ptr){Print(“The current node %s”, ptritem-no);Ptr=ptrnext;}}
![Page 26: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/26.jpg)
Searching involves finding the required element in the list
We can use various techniques of searching like linear search or binary search where binary search is more efficient in case of Arrays
But in case of linked list since random access is not available it would become complex to do binary search in it
We can perform simple linear search traversal
![Page 27: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/27.jpg)
Algorithm1. Set Ptr to start2. Input the item to be searched3. While Ptr!=null do steps 4 and 54. Compare the item with the given value. If
they are same ,exit from the loop and goto step 6, else
5. Set Ptr to next node6. Report that the data found in the
list .Otherwise it doesn’t exist .
![Page 28: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/28.jpg)
In linear search each node is traversed till the data in
the node matches with the required value
void search(int x){ node*temp=start; while(temp!=NULL) { if(temp->data==x) { Print “FOUND ”; break; } temp=temp->next; }}
![Page 29: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/29.jpg)
Deletion of a node is carried out in two steps viz,a)Logical deletion pointer shuffling in order to
remove the linksb)Physical deletion release the memory
occupied using free() functionHere also we have three cases:- Deleting the first node Deleting the last node Deleting the intermediate node
![Page 30: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/30.jpg)
Here we apply 3 steps:- Making the start pointer point towards the 2nd node Free the space associated with first node Operating system collects the free space of the
memory and use it with available free space.The process is given by Temp=start; Start=startnext; Free(temp)
threetwoone
start
![Page 31: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/31.jpg)
Here we apply 2 steps:-
Traverse the list to find the last node. Deleting the last node via delete keyword
node3 node2 node1
start
![Page 32: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/32.jpg)
Traverse the list to find the node to be deleted.
Search is used for this purpose. Refer that as curr_ptr. Then delete that node by setting the pointers as
Prevnext=curr_ptrnext; Then physical deletion is achieved by free().
node1 node2 node3
To be deleted
![Page 33: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/33.jpg)
The node in DLL may be deleted At the beginning At the end At any desired positionSteps to be followed1. If the list is empty display underflow message2. If a single node exists, set both the pointers as Null. Else if it is the
first node, then delete it and update the pointers.3. Else if, it is the last node, then delete it and update the right
pointer.4. Restore the deleted node to the free list
![Page 34: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/34.jpg)
Generalized list A is a finite sequence of n>=0 elements. A=(α0 ….. α n-1 ), here A name of the list and α0 ….. α n-1
represents the elements. It is either atom or a list. Name is given by capital letter and elements by small case.Ex:1. D=() the null (or) empty list and its length is zero2. A=(a(b,c)) a list of length 23. B=(A,A,()) a list of length 3, the third one is null.4. C=(a,c) a recursive list of length 2, c corresponds to the
infinite list.
![Page 35: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/35.jpg)
Linked stack In linked stack we can easily add a node at
the top or delete a node from the top. It is similar to push and pop the elements. A stack can be accessed only through its
top element,and a linked list can be accessed by using the pointer to its first element.
![Page 36: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/36.jpg)
6 9 12 15
Top
X
Null
Linked Stack
![Page 37: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/37.jpg)
Linked list representations of queue are easy to handle.
We need two pointers namely Front and Rear.
Front represents the beginning of the queue and rear represents the end of the queue.
Q.Rear=null indicates the queue is empty.
![Page 38: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/38.jpg)
6 9 12 15
Front
X
Rear
Null
Linked Queue
![Page 39: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/39.jpg)
9 12 15 X
Null
Header Linked List
Header Node
The header for a LL is a pointer variable that locates the beginning of the list
![Page 40: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/40.jpg)
HLL is a LL which always contains a special node called the header node.
Header node is at the beginning of the list. Dummy nodes placed as the first node of a list
which is used to simplify linked list processing. It may or may not contain meaningful
information. It does not represents an item in the list. The info portion of the such a header node
might be unused. It is used to keep the global information about
the entire list.
![Page 41: Operations on linked list](https://reader035.vdocuments.site/reader035/viewer/2022070601/587dc8001a28ab1b498b7825/html5/thumbnails/41.jpg)