Module 3 LINKED LIST-1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

SINGLY LINKED

LIST
Operations on a singly linked list.

• Insert an element at the beginning.


• Insert an element at the end.
• Insert an element at a position.
• Delete element from beginning.
• Delete element from the end.
• Delete an element from a position.
Representation of Singly Linked List
struct node
{
int data;
struct node * link;
};

struct node *start=NULL,*temp,*loc;


Insert an element at the
beginning
void insertbeg()
{
struct node *p;
p=(struct node *)malloc(sizeof(struct node));
Read item
p->data=item;
p->link=head;
head=p;
}
Insert an element at the end
void insertend()
{ temp->link=p;
struct node *p; p->data=item;
if(head!=NULL) p->link=NULL;
{ }
temp=head; else
p=(struct node*) malloc(sizeof(struct insertbeg();
node)); }
Read item
while(temp->link!=NULL)
{
temp=temp->link;
}
Insert an element at a specified position
void insertpos()
{ else
struct node *temp,*p; {
int n, item,i; temp=start;
Read pos, item for(i=1;i<(pos-1);i++)
p=(structnode*)malloc(sizeof(struct {
node));
temp=temp->link;
p->data=item;
}
if(pos==1)
p->link=temp->link;
{
temp->link=p;
p->link=head;
}
head=p;
}
}
Delete element from beginning

void deletebeg()
{
struct node *p;
if(start==NULL)
printf("\nEmpty List!");
else
{
temp=head;
head=head->link;
Print temp->data
free(temp);
}
}
Delete element from the end
else
void delete_end()
{
{
temp=head;
if(head==NULL)
loc=temp->link;
print “Empty List"
while(loc->link!=NULL)
{
else if(head->link==NULL)
temp=temp->link;
{
loc=temp->link;
temp=head;
}
Print temp->data
temp->link=NULL;
head=NULL;
Print loc->data
free(temp);
free(loc);
}
}}
Delete an element from a position
void deletepos() else
{ {
if(start==NULL) temp=head;
Print “Empty List” loc=temp->link;
else for(i=1;i<(pos-1);i++)
{ {
Read pos temp=temp->link;
if(pos==1) loc=temp->link;
{ }
temp=start; Print loc->data
start=start->link; temp->link=loc->link;
Print temp->data free(loc);
free(temp); }
} }
}
Stack using linked list
• In linked list implementation of a stack, every new element is
inserted as 'top' element.
• That means every newly inserted element is pointed by 'top'.
• Whenever we want to remove an element from the stack,
simply remove the node which is pointed by 'top' by moving
'top' to its next node in the list.
Stack using linked list Contin..
void push ( int item)
struct node {
struct node * p;
{
p = ( struct node *) malloc (sizeof ( struct
int data; node));
struct node *link; p -> data = item;
}; p-> link = top;
top = x;
}

struct node *top; void pop ()


{
top = NULL; int item;
temp=top;
item = top -> data;
top = top -> link;
print item; free(temp);
}
• In linked list implementation of a queue, the last inserted
node is always pointed by 'rear' and the first node is always
pointed by 'front'.
• Example

• In above example, the last inserted node is 50 and it is pointed


by 'rear' and the first inserted node is 10 and it is pointed by
'front'. The order of elements inserted is 10, 15, 22 and 50.
Queue using linked list
struct node
{ else
{
int data;
rear -> link = p;
struct node *link; rear = p;
}; }
struct node *front, *rear; }

front = NULL; void deleteq ( )


{
rear = NULL;
int item;
void insertq ( int item) if ( front = = NULL)
{ { queue empty; stop; }
struct node * p; else
p = ( struct node *) malloc ( sizeof ( struct node {
)); p=front;
p-> data = item; item = front -> data;
if(front==rear)
p-> link = NULL;
front=rear=NULL;
if ( front == NULL) else
( front = front -> link;
front = p; print item;
rear = p; free(p);
} }
}
DOUBLY LINKED LIST AND
OPERATIONS
Operations
a. Insert an element at the beginning of the Doubly Linked list .
b. Insert an element at the end of the Doubly Linked list .
c. Insert an element at a specific position in the Doubly Linked list .
d. Delete the element at the beginning of the Doubly Linked list .
e. Delete the element at the end of the Doubly Linked list .
f. Delete the element at a specific position in the Doubly Linked list .
g. Forward Traversal.
h. Reverse Traversal.
Representation of Doubly Linked List

struct node
{
int data;
struct node *next,*prev;
};
struct node *head=NULL,*tail=NULL;
Insert an element at the beginning of the
linked list

void insertbeg()
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
Read p->data
p->next=head;
if(head!=NULL)
head->prev=p;
else
tail=p;
head=p;
p->prev=NULL;
}
Insert an element at the end of the linked
list

void insertend()
{
struct node *p;
p=(struct node *)malloc(sizeof(struct node));
Read p->data
p->prev=tail;
if(tail!=NULL)
tail->next=p;
else
head=p;
p->next=NULL;
tail=p;
}
Insert an element at a specific position in the
linked list
void insertpos() if(b==NULL) insertend();
{ else
int pos,i; {
struct node *p,*a,*b; p->prev=a;
p=(struct p->next=b;
node*)malloc(sizeof(struct node)); a->next=p;
Read pos b->prev=p;
if(pos!=1) }}
{ else
Read p->data insertbeg();
a=head; }
for(i=1;i<pos-1;i++)
{
a=a->next;
b=a->next;
}
Delete the element at the beginning of the linked
list
void deletebeg() if(head!=tail)
{ {
struct node *p; head=head->next;
if(head!=NULL) head->prev=NULL;
{ }
p=head; else
Print p->data head=tail=NULL;
free(p);
}
else
Print “Empty list."
}
Delete the element at the end of the linked list

void delete_end() else


{ head=tail=NULL;
struct node *p; free(p);
if(tail!=NULL) }
{ else
p=tail; Print “Empty list."
Print p->data }
if(head!=tail)
{
tail=tail->prev;
tail->next=NULL;
}
Delete the element at a specific position in the
linked list
void deletepos() If(b->next==Null)
{ {
struct node *a,*b; a->next=Null;
if(head!=NULL) tail=tail->prev;
{ }
Read pos else{
if(pos!=1) a->next=b->next;
{ (b->next)->prev=a;
a=head; }
b=a->next; Print b->data
for(i=1;i<pos-1;i++) free(b);
{ }
a=a->next; else
b=b->next; deletebeg();
} }
else
Print “Empty list."
}
Forward Traversal

void displayft()
{
struct node *a;
a=head;
if(a==NULL)
Print “Empty list."
else
{
Print “Forward Traversal”
while(a!=NULL)
{
Print a->data
a=a->next;
}
}
}
Reverse Traversal

void displayrt()
{
struct node *a;
a=tail;
if(a==NULL)
Print “Empty list."
else
{
Print “Reverse Traversal”
while(a!=NULL)
{
Print a->data
a=a->prev;
}
}
}
CIRCULAR LINKED LIST
Operations on a circular
linked list.

▪Insert an element at the beginning.


▪Insert an element at the end.
▪Insert an element at a position.
▪Delete element from beginning.
▪Delete element from the end.
▪Delete an element from a position.
Insert an element at the
beginning
void insertbeg()
{
struct node *p,*temp;
p=(struct node *)malloc(sizeof(struct node));
Read item
p->data=item;
If(start== NULL)
{
start=p;
p->next=start;
}
else
{
temp = start;
while(temp->next !=start)
temp = temp->next;
p->next = start;
start = p;
temp->next =start;
}
Insert an element at the end
void insertend()
{
struct node *p,*temp;
p=(struct node *)malloc(sizeof(struct node));
Read item
p->data=item;
If(start== NULL)
{
start=p;
p->next=start;
}
else
{
temp = start;
while(temp->next !=start)
temp = temp->next;
tem p->next = p;
p->next =start;
}
Input:
1st number = 5x^2 + 4x^1 + 2x^0
2nd number = 5x^1 + 5x^0
Output:
5x^2 + 9x^1 + 7x^0
Input:
1st number = 5x^3 + 4x^2 + 2x^0
2nd number = 5x^1 + 5x^0
Output:
5x^3 + 4x^2 + 5x^1 + 7x^0
First fit algorithm
Best Fit
Worst fit

You might also like