Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 39

Linked list

• Linked list is a set of nodes where each node has two


fields data and next.
1. Data − Data field contain actual element
2.Next − It contain address of next node.
• Structure of node

PROF.SHOBHA PATIL,JSPM NTC


Linked List Representation
• Linked list can be visualized as a chain of nodes, where every node points
to the next node.

PROF.SHOBHA PATIL,JSPM NTC


Basic terminologies of linked list

• Pointer :- A pointer always points to the first node of the linked list. The
first node to which the pointer points is called header node.

• Empty List :- when there is no node in any linked list , then it is called as
empty linked list.

• Null Pointer : - when linked list is empty , then null value is set to the
pointer.
• E.g Head = NULL;

PROF.SHOBHA PATIL,JSPM NTC


ARRAY vs linked list
ARRAY LINKED LIST
Array is a collection of elements of similar data type. Linked List is an ordered collection of elements of same type,
which are connected to each other using pointers.

Array supports Random Access, which means elements can be Linked List supports Sequential Access, which means to
accessed directly using their index, like arr[0] for 1st access any element/node in a linked list, we have to
element, arr[6] for 7th element etc. sequentially traverse the complete linked list, upto that element.
Hence, accessing elements in an array is fast with a constant To access nth element of a linked list, time complexity is O(n).
time complexity of O(1).

Memory is allocated as soon as the array is declared, Memory is allocated at runtime, as and when a new node is
at compile time. It's also known as Static Memory Allocation. added. It's also known as Dynamic Memory Allocatio

Size of the array must be specified at time of array decalaration. Size of a Linked list is variable. It grows at runtime, as more
nodes are added to it.
In an array, elements are stored in contiguous memory In a linked list, new elements can be stored anywhere in the
location or consecutive manner in the memory. memory.
Address of the memory location allocated to the new element is
stored in the previous node of linked list, hence formaing a link
between the two nodes/elements.

In array, Insertion and Deletion operation takes more time, as In case of linked list, a new element is stored at the first free
the memory locations are consecutive and fixed. and available memory location, with only a single overhead
step of storing the address of memory location in the previous
node of linked list.
Insertion and Deletion operations are fast in linked list.

PROF.SHOBHA PATIL,JSPM NTC


Advantages of linked list

• Size of linked lists is not fixed, they can expand and shrink
during run time.
• Insertion and Deletion Operations are fast and easier in Linked
Lists.
• Memory allocation is done during run-time (no need to
allocate any fixed memory).
• Data Structures like Stacks, Queues, and trees can be easily
implemented using Linked list.

PROF.SHOBHA PATIL,JSPM NTC


disadvantages of linked list
• Memory consumption is more in Linked Lists when compared
to arrays. Because each node contains a pointer in linked list
and it requires extra memory.

• Elements cannot be accessed at random in linked lists.

• In linked list the nodes are not stored in a contiguous memory


location, Hence the access time for individual node is O(n)
while in array it is O(1).

PROF.SHOBHA PATIL,JSPM NTC


Applications of Linked Lists

• Linked Lists can be used to implement Stacks, Queues and


Trees.

• Linked Lists can be also used to implement Graphs.


(Adjacency list representation of Graph).

• Linked Lists can be used to Implement Hash Tables - Each


Bucket of the hash table can be a linked list (Open chain
hashing).

PROF.SHOBHA PATIL,JSPM NTC


Types of Linked List

• Singly Linked List

• Doubly Linked List

• Circular Linked List 

PROF.SHOBHA PATIL,JSPM NTC


Singly Linked List

• It is one way traversing list which means we can


traverse only in forward direction.

PROF.SHOBHA PATIL,JSPM NTC


structure Singly Linked List

Struct  node   
{  
   int data;   
   struct node *next;  
};  

PROF.SHOBHA PATIL,JSPM NTC


doubly Linked List
• A doubly linked list contains a pointer to the next as well as
the previous node in sequence, Therefore, it contains three
parts are data, a pointer to the next node, and a pointer to
the previous node. This would enable to traverse the list in
the backward direction as well.

PROF.SHOBHA PATIL,JSPM NTC


structure of doubly Linked List

struct node   
{  
   int data;   
   struct node *next;  
struct node *prev; 
};  

PROF.SHOBHA PATIL,JSPM NTC


circular Linked List
• A circular linked list is that in which the last node contains the
pointer to the first node of the list. While traversing a circular
liked list, we can begin at any node and traverse the list until
we reach the same node we started. Thus, a circular linked list
has no beginning and no end.

PROF.SHOBHA PATIL,JSPM NTC


structure of circular Linked List
struct node   
{  
   int data;   
   struct node *next;  
};  

PROF.SHOBHA PATIL,JSPM NTC


Basic Operations

• Creation
• Traversing
• Searching
• Insertion
• Deletion

PROF.SHOBHA PATIL,JSPM NTC


To Create a Link List
struct Node
{  int data;    
struct Node *next;
};

void create()
{    struct Node *head,*newnode,*temp;
head = 0;
newnode = (struct Node*) malloc(sizeof(struct Node));    
cout<<“Enter the data”
cin>> newnode->data ; 
newnode->next = 0;  
if(head = = 0)
 {
head=temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
} PROF.SHOBHA PATIL,JSPM NTC
PROF.SHOBHA PATIL,JSPM NTC
Display linked list

struct Node
{  int data;    
struct Node *next;
};

Void disply()
{
 struct Node *head,* newnode,*temp;

temp=head;

while(temp!=0)
{
temp->data;
temp=temp->next;
}

} PROF.SHOBHA PATIL,JSPM NTC


Algorithm

• display() will display the nodes present in the


list:
– Define a node current which initially points to the
head of the list.
– Traverse through the list till current points to null.
– Display each node by making current to point to
node next to it in each iteration.

PROF.SHOBHA PATIL,JSPM NTC


Deletion is also a more than one step process. We shall learn with pictorial
representation. First, locate the target node to be removed, by using searching
algorithms.

The left (previous) node of the target node now should point to the next node of the target node −
LeftNode.next −> TargetNode.next;

PROF.SHOBHA PATIL,JSPM NTC


PROF.SHOBHA PATIL,JSPM NTC
Inserting node at beginning
struct Node
{  int data;    
struct Node *next;
};

Void addatbeg()
{
 struct Node *head,* newnode;

newnode = (struct Node*) malloc(sizeof(struct Node));    


cout<<“Enter the data”
cin>> newnode->data ; 
if(head= = 0)
{head=temp=newnode;}
else
{
newnode->next = head;  
head = newnode;
}
PROF.SHOBHA PATIL,JSPM NTC
}
Inserting node at end
struct Node
{  int data;    
struct Node *next;
};

Void addatend()
{
 struct Node *head,* newnode,*temp;

newnode = (struct Node*) malloc(sizeof(struct Node));    


cout<<“Enter the data”
cin>> newnode->data ; 
newnode->next = 0;  
temp=head;
while(temp->next!=0)
{
temp=temp->next;
}
temp->next= newnode;
}
PROF.SHOBHA PATIL,JSPM NTC
To Create a Circular Link List
struct Node
{  int data;    
struct Node *next;
};

void create()
{    struct Node *head,*newnode,*temp;

newnode = (struct Node*) malloc(sizeof(struct Node));    


cout<<“Enter the data”
cin>> newnode->data ; 
newnode->next = 0;  
if(head = = 0)
 {
head=temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
temp->next = head;
}
} PROF.SHOBHA PATIL,JSPM NTC
Display Circular linked list
struct Node
{  int data;    
struct Node *next;
};

Void disply()
{
 struct Node *head,* newnode,*temp;

temp=head;

while(temp->next!=head)
{ temp->data;
temp=temp->next;
}
temp->data;
} PROF.SHOBHA PATIL,JSPM NTC
Inserting node at beginning
struct Node
{  int data;    
struct Node *next;
};

Void addatbeg()
{
 struct Node *tail,* newnode;

newnode = (struct Node*) malloc(sizeof(struct Node));    


cout<<“Enter the data”
cin>> newnode->data ; 
newnode->next = 0;  
if(tail==0)
{
tail=newnode;
tail->next=newnode;
}
else
{newnode->next=tail->next;
tail->next = newnode;}
} PROF.SHOBHA PATIL,JSPM NTC
Inserting node at end
struct Node
{  int data;    
struct Node *next;
};

Void addatbeg()
{
 struct Node *tail,* newnode;

newnode = (struct Node*) malloc(sizeof(struct Node));    


cout<<“Enter the data”
cin>> newnode->data ; 
newnode->next = 0;  
if(tail==0)
{
tail=newnode;
tail->next=newnode;
}
else
{newnode->next=tail->next;
tail->next = newnode;
tail=newnode;}
} PROF.SHOBHA PATIL,JSPM NTC
To Create a Doubly Link List
struct Node
{  int data;    
struct Node *next;*prev
};

void create()
{    struct Node *head,*newnode,*temp;
head = 0;
newnode = (struct Node*) malloc(sizeof(struct Node));    
cout<<“Enter the data”
cin>> newnode->data ; 
newnode->next = 0;  
newnode->prev= 0
if(head = = 0)
 {
head=temp=newnode;
}
else
{
temp->next=newnode;
newnode->prev=temp;
temp= newnode;
}
}
PROF.SHOBHA PATIL,JSPM NTC
Display doubly linked list
struct Node
{  int data;    
struct Node *next;
};

Void disply()
{
 struct Node *head,* newnode,*temp;

temp=head;

while(temp!=0)
{
temp->data;
temp=temp->next;
}

} PROF.SHOBHA PATIL,JSPM NTC


Inserting node at beginning
struct Node
{  int data;    
struct Node *next;
};

Void addatbeg()
{
 struct Node *head,* newnode,*tail;

newnode = (struct Node*) malloc(sizeof(struct Node));    


cout<<“Enter the data”
cin>> newnode->data ; 
newnode->next=0  
if(head= = 0)
{head=tail=newnode;}
else
{ head->prev=newnode;
newnode->next=head;
head=newnode;
}

} PROF.SHOBHA PATIL,JSPM NTC


Inserting node at end
struct Node
{  int data;    
struct Node *next;
};

Void addatbeg()
{
 struct Node *head,* newnode,*temp;

newnode = (struct Node*) malloc(sizeof(struct Node));    


cout<<“Enter the data”
cin>> newnode->data ; 
newnode->next=0  
if(head= = 0)
{head=tail=newnode;}
else
{ tail->nextv=newnode;
newnode->prev=tail;
tail=newnode;
}

} PROF.SHOBHA PATIL,JSPM NTC


Array representation
• Represent Following polynomial using array

4x3+6x2+7x+9

PROF.SHOBHA PATIL,JSPM NTC


Linked list representation
• Represent Following polynomial using linked
list

5x2+4x+2

PROF.SHOBHA PATIL,JSPM NTC


Generalized linked list
• A Generalized Linked List L, is defined as a finite
sequence of n>=0 elements, l1, l2, l3, l4, …, ln, such
that li are either atom or the list of atoms. Thus
L = (l1, l2, l3, l4, …, ln)
where n is total number of nodes in the list.

PROF.SHOBHA PATIL,JSPM NTC


Representation of GLL

• Flag = 1 implies that down pointer exists


• Flag = 0 implies that next pointer exists
• Data means the atom
• Down pointer is the address of node which is down of the current
node
• Next pointer is the address of node which is attached as the next
node

PROF.SHOBHA PATIL,JSPM NTC


Example of GLL 

PROF.SHOBHA PATIL,JSPM NTC


structure of Generalized Linked List

struct node
{
int c;                    //Data
 int index;                 //Flag
 struct node *next, *down;   //Next & Down pointer
};

PROF.SHOBHA PATIL,JSPM NTC


Polynomial Representation using Generalized Linked
List

• Here Flag = 0 means variable is present


• Flag = 1 means down pointer is present
• Flag = 2 means coefficient and exponent is present

PROF.SHOBHA PATIL,JSPM NTC


Example of polynomial GLL 
9x5 + 7x4y + 10xz

PROF.SHOBHA PATIL,JSPM NTC

You might also like