operations on linked list
TRANSCRIPT
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
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
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.
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
Linked List Types Singly Linked list Doubly linked lists Circular singly linked list Circular doubly linked lists
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
10
1000
1000
2000
200015 NULL20
4000
Singly Linked ListEach node points the next node . It is
a one way list.First
10 15 20
Circular Singly Linked ListLast node contains the address of the first node
First
Doubly Linked listContains the address of previous node
and next node
NULL
2000 30001000
10 15 202000 1000 2000 NULL3000
First
Circular Doubly Linked listContains the address of first node and
last node
3000
2000 30001000
10 15 202000 1000 2000 10003000
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
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
10 20 30 45 X
Next
Creation of the Linked ListStart
Info Next
There are three ways to insert a node into the list.
Insertion at the beginning Insertion at the end Insertion after a particular node
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;
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
Here we simply need to make the next pointer of the last node point to the new node
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
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
a b c d
start Info
NextPtr
Logic using CTraverse(){Struct node *ptr;Ptr=start;While(ptr){Print(“The current node %s”, ptritem-no);Ptr=ptrnext;}}
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
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 .
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; }}
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
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
Here we apply 2 steps:-
Traverse the list to find the last node. Deleting the last node via delete keyword
node3 node2 node1
start
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
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
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.
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.
6 9 12 15
Top
X
Null
Linked Stack
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.
6 9 12 15
Front
X
Rear
Null
Linked Queue
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
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.