0% found this document useful (0 votes)
23 views20 pages

CLL

circular linked lists
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views20 pages

CLL

circular linked lists
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Why linked stacks?

• Stack implementation array drawback is we need to know size in advance.if the size is
fixed we cannot increase or decrease size.
• Stack full condition doesn’t arise in linked stacks.
Node representation
Struct node
{
int data;
struct node *next;
}*new,*tos,*temp;
New-allocation of memory
Tos-top of stack
Temp-traversing
PUSH
new=(struct node*)malloc(sizeof(struct node);
Printf(“enter a value”);
scanf(“%d”,&value);
new->data=value;
new->next=NULL;
if(toss==NULL)
tos=new;
else
{
new->next=tos;
tos=new;
}
POP
if(tos==NULL)
printf(“stack is empty”);
else
temp=tos;
Printf”deleted item is %d”,tos->data);
tos=tos->next;
temp->next=NULL;
free(temp);
PEEK(DISPALYING TOP MOST)
if(tos==NULL)
{ printf(“stack is empty”);
}
else
{
printf(“top most element is %d”,tos->data);
}
DISPLAYING
TEMP=TOS;
if(tos==NULL)
{ printf(“stack is empty”);
}
else
while(temp!=NULL)
{
printf(“%d->”,temp->data);
temp=temp->next;
}
}
Linked queues
• Drawback with respect to array implementation of queue is we must know the size
in advance.
• we are using single linked list to implement to queue in order to overcome this
drawback.
• Node representation
Node representation
Struct node
{
int data;
struct node *next;
}*new,*front,*rear,*front;
Linked queues operations

Enqueue-inserting an element into queue

Dequeue-deleting an element from a queue.

Display-displaying the elements of queue.


ENQUEUE
new=(struct node*)malloc(sizeof(struct node);
Printf(“enter a value”);
scanf(“%d”,&value);
new->data=value;
new->next=NULL;
if(front==NULL && rear==NULL)
{
front=new;
rear=new;
}
else
{
rear->next=new;
rear=new;
}
DEQUEUE
if(front==NULL && rear==NULL)
{
printf(“queue is empty”);
}
else
{
temp=front;
printf(“deleted item is %d”,front->data);
front=front->next;
temp->next=NULL;
free(temp);
}
DISPLAY
if(front==NULL && rear==NULL)
{
printf(“queue is empty”);
}
else
{
temp=front;
while(temp!=NULL)
{
printf(“%d->”,temp->data);
temp=temp->next;
}
}
Circular linked lists
• Circular Linked List is a type of linked list where list are linked in
such a manner to form a circle i.e. Last Node points the First Node.
In Circular linked list there exists no nodes which have null at address
space. It is of two types:
• 1) Singly Linked List: It provides the link to the first node by
replacing the NULL with the address of first node. Singly Linked List
helps in accessing the previous node with the help of link creation in
circular order.
• 2) Doubly Linked List: Doubly Linked List (DLL) consist of an extra
pointer known as previous pointer.
Insertion at beginning
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
new=(struct node*)malloc(sizeof(struct node);
temp->next=new;
printf(“enter a value”);
scanf(“%d”,&value);
new->data=value;
new->next=head;
Insertion at end
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
new=(struct node*)malloc(sizeof(struct node);
temp->next=new;
printf(“enter a value”);
scanf(“%d”,&value);
new->data=value;
new->next=head;
head=new;
Insert at specified position
temp=head;
for(i=0;i<pos-1;i++)
{
temp=temp->next;
}
new=(struct node*)malloc(sizeof(struct node);
new->data=value;
new->next=temp->next;
temp->next=new;

You might also like