Linked List
Linked List
Address of
Next Data
Define Linked list
• A linked list is a linear collection of data
elements whose order is not given by their
physical placement in memory.
• Instead, each element points to the next. It is
a data structure consisting of a collection of
nodes which together represent a sequence
Structure of linked list
• A linked list is a way to store a collection of
elements.
• Like an array these can be character or
integers.
• Each element in a linked list is stored in the
form of a node.
What is Node
• Dynamism
–Dynamic List
• Running Time Complexity
–Reducing Running Time
Array Vs Linked List
Data Structure
struct node
{
int data;
struct node *next;
};
•Linked List
•Singly
•Doubly
•Circular
•Circular Singly
•Circular Doubly
1. Singly Linked list
• It is the commonly used linked list in programs.
• If we are talking about the linked list, it means it is
a singly linked list.
• The singly linked list is a data structure that
contains two parts, i.e., one is the data part, and
the other one is the address part, which contains
the address of the next or the successor node.
• The address part in a node is also known as
a pointer.
• Suppose we have three nodes, and the
addresses of these three nodes are 100, 200
and 300 respectively.
• The representation of three nodes as a linked
list is shown in the below figure:
• The first node contains the address of the next
node, i.e., 200, the second node contains the
address of the last node, i.e., 300, and the
third node contains the NULL value in its
address part as it does not point to any node.
• The pointer that holds the address of the
initial node is known as a head pointer.
• Above diagram, is known as a singly linked list
as it contains only a single link. In this list, only
forward traversal is possible; we cannot
traverse in the backward direction as it has
only one link in the list.
Representation of the node in a singly
linked list
struct node
{
int data;
struct node *next;
}
2. Doubly linked list
•Delete
•Begin
•End
•Middle
•Search
Operations
(Insertion at end)
Operations
(Insertion at Position)
Operations
(Deletion at Position)
Operations
(Insertion and Deletion)
Examples
Examples(Continued)
Create
struct node* createlist() if(r==NULL)
{ {
struct node *r,*n; r=n;
int i,k; current=n;
r=NULL; }
printf("Enter the size of list"); else
scanf("%d",&k); {
for(i=0;i<k;i++) current->next=n;
{ current=n;
n=(struct }
node*)malloc(sizeof(struct } // End of For Loop
node)); return r;
scanf("%d",&n->data); } // End of function
n->next=NULL;
Insert Begin
struct node *insert_begin(struct node *r,int x)
{
struct node *n;
n=(struct node*)malloc(sizeof(struct node));
n->data=x;
n->next=NULL;
if(r==NULL)
{
r=n;
} Running Time!?
else
{
n->next=r;
r=n;
}
return r;
}
Insert End
struct node *insert_end(struct node *r,int x)
{
struct node *n;
n=(struct node*)malloc(sizeof(struct node));
n->data=x;
n->next=NULL;
if(r==NULL)
{
r=n;
current=n;
Running Time!?
}
else
{
current->next=n;
current=n;
}
return r;
}
Insert Middle
struct node *insert(struct node *r,int else
element,int pos)
{
{
k=2;
Running Time!?
struct node *n,*p; for(p=r;p!=NULL;p=p->next)
int k=0; {
n=(struct node*)malloc(sizeof(struct if(k==pos)
node)); {
n->data=element; n->next=p->next;
n->next=NULL; p->next=n;
if(r==NULL) return r;
{ }
printf("Empty List"); k=k+1;
return NULL; }
} } printf("Position not available");
else if(pos==1) return r;
{ }
n->next=r;
r=n;
return r;
}
Display
void display(struct node *r)
{
while(r!=NULL)
{ Running Time!?
printf(" %d",r->data);
r=r->next;
}
printf(" null");
}
Delete
• Delete Begin
• Delete End
• Delete Middle
Delete Begin
struct node *delete_begin(struct node *r)
{
if(r==NULL)
printf(“No element to delete”);
else
return r->next;
}
Delete End
struct node *delete_end(struct node *r)
{
struct node *p;
if( r==NULL)
{
printf("No Element to Delete");
return NULL;
}
else
{
p=r;
while(p!=NULL)
{
if(p->next->next==NULL)
{
p->next=NULL;
current=p;
return r;
}
p=p->next;
}
}
}
Delete Middle
struct node *delete_middle(struct else
node *r, int element) {
{ p=r;
struct node *p; while(p!=NULL)
if(r==NULL) {
{ if(p->next->data==element)
printf("no element found"); {
return NULL; p->next=p->next->next;
} }
if(r->data==element) p=p->next;
{ }
return NULL; return r;
} }
}
Search
struct node *search(struct node *r,int element)
{
struct node *p;
p=r;
while(p!=NULL)
{
if(p->data==element)
return p;
p=p->next;
}
return NULL;
}
THANK YOU!!!