A linked list is a linear data structure consisting of nodes that are connected to each other through pointers. Each node contains a data field and a pointer to the next node. Linked lists allow for efficient insertion and removal of nodes and are more flexible than arrays in terms of memory allocation. Common types of linked lists include singly linked lists, doubly linked lists, circular linked lists, and header linked lists.
Organization of dataand theOrganization of data and the
relationship among its members isrelationship among its members is
called as data structurecalled as data structure
Data Structure
4.
Why we needLinked List?
What are the problems with Arrays?
Size is fixed
Array Items are stored contiguously
Insertions and deletion at particular position is
complex
Why Linked list ?
Size is not fixed
Data can be stored at any place
Insertions and deletions are simple and faster
5.
What are LinkedListsWhat 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.
6.
A linkedlist 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
7.
Linked List Types
Singly Linked list
Doubly linked lists
Circular singly linked list
Circular doubly linked lists
8.
Each nodehas 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 15 20
CircularSingly Linked List
Last node contains the address of the first node
First
11.
Doubly Linked list
Containsthe address of previous node and
next node
NULL
2000 30001000
10 15 202000 1000 2000 NULL3000
First
12.
Circular Doubly Linkedlist
Contains the address of first node and last
node
3000
2000 30001000
10 15 202000 1000 2000 10003000
13.
Operations on LinkedList
Creation of the linked list
Insertion of nodes
Traversal of the linked list
Search a specific node in the linked list
Deletion of a node in the linked list
14.
Creation of thelinked list
Creation 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 process
a)Creating the new node
b)Storing the address of the new node in the start
15.
10 20 3045 X
Next
Creation of the Linked List
Start
Info Next
16.
There arethree ways to insert a node into the list.
Insertion at the beginning
Insertion at the end
Insertion after a particular node
17.
There are twosteps 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;
19.
Algorithm to Insertan Element from the front
INSERT(Start,data)
1.Allocate memory to New
2. Set NewInfo=data
3.NewNext=start
4.Start=New
5.Return Start
20.
Here we simplyneed to make the next pointer
of the last node point to the new node
21.
Here we againneed 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
23.
Traversal of aLL 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
Searching involvesfinding 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
27.
Algorithm
1. Set Ptrto start
2. Input the item to be searched
3. While Ptr!=null do steps 4 and 5
4. 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 node
6. Report that the data found in the list .Otherwise it
doesn’t exist .
28.
In linear searcheach 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;
}
}
29.
Deletion of anode is carried out in two steps viz,
a) Logical deletion pointer shuffling in order to remove the
links
b) Physical deletion release the memory occupied using
free() function
Here also we have three cases:-
Deleting the first node
Deleting the last node
Deleting the intermediate node
30.
Here we apply3 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
31.
Here we apply2 steps:-
Traverse the list to find the last node.
Deleting the last node via delete keyword
node3node2node1
start
32.
Traverse thelist 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
33.
The node inDLL may be deleted
At the beginning
At the end
At any desired position
Steps to be followed
1. If the list is empty display underflow message
2. 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
34.
Generalized listA 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 zero
2. A=(a(b,c)) a list of length 2
3. 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.
35.
Linked stack
Inlinked 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.
Linked listrepresentations 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.
9 12 15X
Null
Header Linked List
Header Node
The header for a LL is a pointer variable that locates the beginning of the
list
40.
HLL isa 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.