0% found this document useful (0 votes)
30 views11 pages

DS Unit 2 Part 2

Uploaded by

samharisson1986
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)
30 views11 pages

DS Unit 2 Part 2

Uploaded by

samharisson1986
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/ 11

Circular linked list

 The linked list where the last node points the header node is called
circular linked list.

Circular singly linked list

Forward pointer of the last node points to the first node


and backward pointer of the first node points to the
last node

Circular doubly linked list


WACP to implement circular linked list
#include<stdio.h> #include<stdlib.h>
void insertion(); void deletion(); void traverse(); void search();
struct node
{
int data;
struct node *link;
}*new,*ptr,*ptr1,*header,*last;
int ch,item,pos,i;
void main(){
header=NULL;
while(1) {
printf("\n****Menu****\n");
printf("\n 1.Insertion\n 2.Deletion\n 3.Traverse\n 4.Search\n 5.Exit\n");
printf("\nenter ur choice");
scanf("%d",&ch);
switch(ch){
case 1: insertion(); break;
case 2: deletion(); break;
case 3: traverse(); break;
case 4: search(); break;
case 5: exit(0);
default: printf("\n wrong choice\n");
}/*end of switch*/
}/*end of while*/
}/*end of main*/
void insertion()
{
last=ptr=header;
new=malloc(sizeof(struct node));
printf("\n enter the item to be inserted\n");
scanf("%d",&item);
new->data=item;
if(header==NULL)
{
new->link=new;
header=new;
}/*end of if*/
else
{
printf("\nEnter the place to insert the item\n");
printf("1.Start\n 2.Middle\n 3.End\n");
scanf("%d",&ch);
if(ch==1) {
while(last->link!=header)
last=last->link; //last node address
new->link=header;
header=new;
last->link=header;//last node points to first
node
}
if(ch==2)
{
printf("Enter the position to place an item: ");if(ch==3)
scanf("%d",&pos); {
while(ptr->link!=header)
for(i=1;i<pos-1;i++)
ptr=ptr->link;
ptr=ptr->link; new->link=header; //last node points to firs
new->link=ptr->link; ptr->link=new;
ptr->link=new; }//end of if
}/*end of else*/
}
}/*end of insertion*/
void deletion()
{
ptr=last=header;
if(header==NULL)
{
printf("\nThe list is empty");
}
else if(ptr->link==header)
{
printf(“\n the deleted item from the list is-> %d” ,ptr->data);
free(ptr);
header=NULL;
}
else
{

printf("\n1.Start \n2.Middle \n3.End");


printf("\nEnter the place to delete the element from list");
scanf("%d",&ch);
if(ch==1)
{
while(last->link!=header)
last=last->link; //last node address
printf("\nThe deleted item from the list is -> %d",ptr-
>data);
header=header->link;
last->link=header; //last node points to first node
free(ptr);
} if(ch==3)
if(ch==2) {
{ while(ptr->link!=header)
{
printf("\nEnter the position to delete the element “);
ptr1=ptr;
scanf("%d",&pos);
ptr=ptr->link;
for(i=1; i<pos; i++) }//while
{
ptr1=ptr; printf("\nThe deleted element from the list is ->%d", pt
ptr=ptr->link; ptr1->link=header; //last node points to first node
} free(ptr);
printf("\nThe deleted element is ->%d",ptr->data); }
ptr1->link=ptr->link; }/*end of else*/
free(ptr); }/*end of deletion*/
}
void search()
{
int loc=0,f=0;
ptr=header;
printf("\nEnter the element to be searched in the list");
scanf("%d",&item);
do
{
loc++;
if(ptr->data==item)
{ f=1; break; }
ptr=ptr->link;
} while(ptr!=header);
if(f==1)
printf("\nThe element found at location %d",loc);
else
printf("\nElement not found");
}//end of search
void traverse()
{
int i=0;
if(header==NULL)
printf("List is empty\n");
else
{
printf("\nThe elements in the list are:");
ptr=header;
do{
printf(“ %d“,ptr->data);
ptr=ptr->link;
}while(ptr!=header);
}/*end of traverse*/
/* Write a c program to implement circular queue with linked list*/
#include<stdio.h> #include<stdlib.h>
int choice,i,item;
struct node {
int data;
struct node *link;
}*front,*rear,*new,*ptr1,*ptr;
main() {
front=rear=NULL;
printf("\n select menu\n");
while(1) {
printf("\n1.Enqueue \n2.Dequeue \n3.Display \n4.Exit");
printf("\nEnter ur choice: ");
scanf("%d",&choice);
switch(choice) {
case 1: enqueue(); break;
case 2: dequeue(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong choice.");
}/*end of switch*/
}/*end of while*/
}/*end of main*/
void enqueue()
{
new=malloc(sizeof(struct node));
printf("\nEnter the item: "); void dequeue()
scanf("%d",&item); {
new->data=item; if(front==NULL)
if(front==NULL) printf("\nThe list is empty");
front=new; else
else {
rear->link=new; ptr=front;
rear=new; printf("\nThe deleted element is: %d“,ptr-
rear->link=front; >data);
if(front==rear) //List has sin-
}/*end of enqueue()*/ gle element
front=rear=NULL;
else
front=front->link;
free(ptr);
}
rear->link=front;

}/*end of dequeue*/
void display()
{
if(front==NULL)
printf("\nThe circular list is empty.");
else
{
printf("\nElements in the list are: ");
ptr=front;
do{
printf(" %d",ptr->data);
ptr=ptr->link;
}while(ptr!=front);

}/*end of else*/
}/*end of display*/

You might also like