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

LINKED LIST

(Basics & Singly Linked List)


What is Linked List
• A Linked List consists of set of nodes related to
each other through pointers.

• Each node in a Linked List consists of data and


address of next data.
data

Address of
Next Data
Linked List
Why Linked List?
• Dynamism
–Dynamic List
• Running Time Complexity
–Reducing Running Time
Array Vs Linked List
Data Structure

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

Self referential structure!?


self-referential
• A structure having references to itself is called
a self-referential structure.
Types of Linked List

Linked List

Singly Doubly Circular

Circula Circular
r Singly Doubly
Operations
Operations

Insert Delete Search

Beg Mid Beg Mid


End End
in dle in dle
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;
Running Time!?
current=n;
}
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 else
*delete_middle(struct node *r, int {
element) p=r;
{ while(p!=NULL)
struct node *p; {
if(r==NULL) if(p->next->data==element)
{ {
printf("no element found"); p->next=p->next->next;
return NULL; }
} p=p->next;
if(r->data==element) }
{ return r;
return NULL; }
} }
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!!!

You might also like