0% found this document useful (0 votes)
59 views54 pages

Circular Linked List - Creation

The document discusses code for creating and traversing a circular linked list in C. It defines a node struct with an info field and link pointer. Functions are included to create a list by dynamically allocating nodes and linking them, and to traverse the list by iterating from the first node and following links until reaching the first node again. The code demonstrates the basic operations for a circular linked list data structure.

Uploaded by

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

Circular Linked List - Creation

The document discusses code for creating and traversing a circular linked list in C. It defines a node struct with an info field and link pointer. Functions are included to create a list by dynamically allocating nodes and linking them, and to traverse the list by iterating from the first node and following links until reaching the first node again. The code demonstrates the basic operations for a circular linked list data structure.

Uploaded by

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

Circular linked list - Creation

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node *link;
};
struct node *first;
void main()
{
void create();
void traverse();
clrscr();
create();
traverse();
getch();
}
void create()
{ struct node *ptr,*cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nInput first node information : ");
scanf("%d",&ptr->info);
first=ptr;
do
{ cpt=(struct node*)malloc(sizeof(struct node));
printf("\nInput next node information : ");
scanf("%d",&cpt->info);
ptr->link=cpt;
ptr=cpt;
printf("\nPress<Y/N> for more node - ");
ch=getch();
}while((ch=='y')||(ch=='Y'));
ptr->link=first;
}
void traverse()
{
struct node *ptr;
printf("\nTraversing of link is\n");
ptr=first;
while(ptr->link!=first)
{
printf("\n %d || %u ",ptr->info,ptr->link);
ptr=ptr->link;
}
printf("\n %d || %u ",ptr->info,ptr->link);
}

OUTPUT

Circular linked list - Insertion of element at the beginning


#include<stdio.h>

#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node *link;
};
struct node *first;
void main()
{
void create();
void insert_beg();
void traverse();
clrscr();
create();
traverse();
insert_beg();
traverse();
getch();
}
void create()
{
struct node *ptr,*cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nInput first node information : ");
scanf("%d",&ptr->info);
first=ptr;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("\nInput next node information : ");
scanf("%d",&cpt->info);
ptr->link=cpt;
ptr=cpt;
printf("\nPress<Y/N> for more node - ");
ch=getch();
}while((ch=='y')||(ch=='Y'));
ptr->link=first;
}
void insert_beg()
{
struct node *ptr,*cpt;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nInput the information of the node to be inserted at beginning : ");
scanf("%d",&ptr->info);
cpt=first;
while(cpt->link!=first)
{
cpt=cpt->link;
}
ptr->link=first;
first=ptr;
cpt->link=first;

}
void traverse()
{ struct node *ptr;
printf("\nTraversing of link is \n");
ptr=first;
while(ptr->link!=first)
{
printf("\n %d || %u ",ptr->info,ptr->link);
ptr=ptr->link;
}
printf("\n %d || %u \n",ptr->info,ptr->link);
}

OUTPUT

Circular linked list - Insertion of element at the end


#include<stdio.h>
#include<conio.h>

#include<alloc.h>
struct node
{
int info;
struct node *link;
};
struct node *first;
void main()
{
void create();
void insert_end();
void traverse();
clrscr();
create();
traverse();
insert_end();
traverse();
getch();
}
void create()
{
struct node *ptr,*cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nInput first node information : ");
scanf("%d",&ptr->info);
first=ptr;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("\nInput next node information : ");
scanf("%d",&cpt->info);
ptr->link=cpt;
ptr=cpt;
printf("\nPress<Y/N> for more node - ");
ch=getch();
}while((ch=='y')||(ch=='Y'));
ptr->link=first;
}
void insert_end()
{
struct node *ptr,*cpt;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nInput the information of the node to be inserted at the end : ");
scanf("%d",&ptr->info);
cpt=first;
while(cpt->link!=first)
cpt=cpt->link;
cpt->link=ptr;
ptr->link=first;
}
void traverse()
{ struct node *ptr;
printf("\nTraversing of link is \n");

ptr=first;
while(ptr->link!=first)
{
printf("\n %d || %u ",ptr->info,ptr->link);
ptr=ptr->link;
}
printf("\n %d || %u \n",ptr->info,ptr->link);
}

OUTPUT

Circular linked list - Deletion of an Element at the


beginning

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node *link;
};
struct node *first;
void main()
{
void create();
void delete_beg();
void traverse();
clrscr();
create();
traverse();
delete_beg();
traverse();
getch();
}
void create()
{
struct node *ptr,*cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nInput first node information : ");
scanf("%d",&ptr->info);
first=ptr;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("\nInput next node information : ");
scanf("%d",&cpt->info);
ptr->link=cpt;
ptr=cpt;
printf("\nPress<Y/N> for more node - ");
ch=getch();
}while((ch=='y')||(ch=='Y'));
ptr->link=first;
}
void delete_beg()
{
struct node *ptr,*cpt;
cpt=first;
while(cpt->link!=first)
cpt=cpt->link;
ptr=first;
first=ptr->link;
cpt->link=first;
free(ptr);
}
void traverse()
{ struct node *ptr;

printf("\nTraversing if link is \n");


ptr=first;
while(ptr->link!=first)
{
printf("\n %d || %u ",ptr->info,ptr->link);
ptr=ptr->link;
}
printf("\n %d || %u ",ptr->info,ptr->link);
}

OUTPUT

Circular linked list - Deletion of an Element at the end


#include<stdio.h>
#include<conio.h>

#include<alloc.h>
struct node
{
int info;
struct node *link;
};
struct node *first;
void main()
{
void create();
void delete_end();
void traverse();
clrscr();
create();
traverse();
delete_end();
printf("\n After deletion");
traverse();
getch();
}
void create()
{
struct node *ptr,*cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nInput first node information : ");
scanf("%d",&ptr->info);
first=ptr;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("\nInput next node information : ");
scanf("%d",&cpt->info);
ptr->link=cpt;
ptr=cpt;
printf("\nPress<Y/N> for more node - ");
ch=getch();
}while((ch=='y')||(ch=='Y'));
ptr->link=first;
}
void delete_end()
{
struct node *ptr,*cpt;
cpt=first;
while(cpt->link!=first)
{
ptr=cpt;
cpt=cpt->link;
}
ptr->link=first;
free(cpt);
}
void traverse()
{ struct node *ptr;

printf("\nTraversing of link is \n");


ptr=first;
while(ptr->link!=first)
{
printf("\n %d || %u ",ptr->info,ptr->link);
ptr=ptr->link;
}
printf("\n %d || %u\n",ptr->info,ptr->link);
}

OUTPUT

Doubly linked list Creation


#include<stdio.h>
#include<conio.h>

#include<alloc.h>
struct node
{
int info;
struct node *lpt;
struct node *rpt;
};
struct node *first;
void main()
{
void create();
void ftraverse();
void btraverse();
clrscr();
create();
ftraverse();
btraverse();
getch();
}
void create()
{
struct node *ptr,*cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nInput first node information : ");
scanf("%d",&ptr->info);
ptr->lpt=NULL;
first=ptr;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("\nInput next node information : ");
scanf("%d",&cpt->info);
ptr->rpt=cpt;
cpt->lpt=ptr;
ptr=cpt;
printf("\nPress<Y/N> for more node - ");
ch=getch();
}while((ch=='y')||(ch=='Y'));
ptr->rpt=NULL;
}
void ftraverse()
{
struct node *ptr;
printf("\n\nForward traversing \n");
ptr=first;
while(ptr!=NULL)
{
printf("\n %u | %d | %u ", ptr->lpt,ptr->info,ptr->rpt);
ptr=ptr->rpt;
}
}
void btraverse()
{

struct node *ptr;


printf("\n\nBackward traversing \n");
ptr=first;
while(ptr->rpt!=NULL)
ptr=ptr->rpt;
while(ptr!=NULL)
{ printf("\n %u | %d | %u ", ptr->lpt,ptr->info,ptr->rpt);
ptr=ptr->lpt;
}
}

OUTPUT

Doubly linked list - Insertion of element at the beginning


#include<stdio.h>
#include<conio.h>
#include<alloc.h>

struct node
{
int info;
struct node *lpt;
struct node *rpt;
};
struct node *first;
void main()
{
void create();
void insert_beg();
void ftraverse();
void btraverse();
clrscr();
create();
insert_beg();
ftraverse();
btraverse();
getch();
}
void create()
{
struct node *ptr,*cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nInput first node information : ");
scanf("%d",&ptr->info);
ptr->lpt=NULL;
first=ptr;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("\nInput next node information : ");
scanf("%d",&cpt->info);
ptr->rpt=cpt;
cpt->lpt=ptr;
ptr=cpt;
printf("\nPress<Y/N> for more node - ");
ch=getch();
}while((ch=='y')||(ch=='Y'));
ptr->rpt=NULL;
}
void insert_beg()
{
struct node *ptr;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nInput new node : ");
scanf("%d",&ptr->info);
ptr->rpt=first;
first->lpt=ptr;
ptr->lpt=NULL;
first=ptr;
printf("\nNew node is inserted");
}

void ftraverse()
{
struct node *ptr;
printf("\nForward traversing \n");
ptr=first;
while(ptr!=NULL)
{
printf("\n %u | %d | %u ", ptr->lpt,ptr->info,ptr->rpt);
ptr=ptr->rpt;
}
}
void btraverse()
{
struct node *ptr;
printf("\nBackward traversing \n");
ptr=first;
while(ptr->rpt!=NULL)
ptr=ptr->rpt;
while(ptr!=NULL)
{ printf("\n %u | %d | %u", ptr->lpt,ptr->info,ptr->rpt);
ptr=ptr->lpt;
}
}

OUTPUT

Doubly linked list - Insertion of an element at the end


#include<stdio.h>
#include<conio.h>

#include<alloc.h>
struct node
{
int info;
struct node *lpt;
struct node *rpt;
};
struct node *first;
void main()
{
void create();
void insert_end();
void ftraverse();
void btraverse();
clrscr();
create();
insert_end();
ftraverse();
btraverse();
getch();
}
void create()
{
struct node *ptr,*cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nInput first node information : ");
scanf("%d",&ptr->info);
ptr->lpt=NULL;
first=ptr;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("\nInput next node information : ");
scanf("%d",&cpt->info);
ptr->rpt=cpt;
cpt->lpt=ptr;
ptr=cpt;
printf("\nPress<Y/N> for more node - ");
ch=getch();
}while((ch=='y')||(ch=='Y'));
ptr->rpt=NULL;
}
void insert_end()
{
struct node *ptr,*cpt;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nInput new node : ");
scanf("%d",&ptr->info);
cpt=first;
while(cpt->rpt!=NULL)
cpt=cpt->rpt;
cpt->rpt=ptr;
ptr->lpt=cpt;

ptr->rpt=NULL;
printf("\nNew node is inserted\n");
}
void ftraverse()
{
struct node *ptr;
printf("\nForward traversing \n");
ptr=first;
while(ptr!=NULL)
{
printf("\n %u | %d | %u ", ptr->lpt,ptr->info,ptr->rpt);
ptr=ptr->rpt;
}
}
void btraverse()
{
struct node *ptr;
printf("\nBackward traversing\n");
ptr=first;
while(ptr->rpt!=NULL)
ptr=ptr->rpt;
while(ptr!=NULL)
{ printf("\n %u | %d | %u ", ptr->lpt,ptr->info,ptr->rpt);
ptr=ptr->lpt;
}
}

OUTPUT

Double linked list - Deletion of an element from the


beginning

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node *lpt;
struct node *rpt;
};
struct node *first;
void main()
{
void create();
void delete_beg();
void ftraverse();
void btraverse();
clrscr();
create();
delete_beg();
ftraverse();
btraverse();
getch();
}
void create()
{
struct node *ptr,*cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nInput first node information : ");
scanf("%d",&ptr->info);
ptr->lpt=NULL;
first=ptr;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("\nInput next node information : ");
scanf("%d",&cpt->info);
ptr->rpt=cpt;
cpt->lpt=ptr;
ptr=cpt;
printf("\nPress<Y/N> for more node - ");
ch=getch();
}while((ch=='y')||(ch=='Y'));
ptr->rpt=NULL;
}
void delete_beg()
{
struct node *ptr;
ptr=first;
first=ptr->rpt;
first->lpt=NULL;
free(ptr);
printf("\nDeletion is done\n");
}

void ftraverse()
{
struct node *ptr;
printf("\nForward traversing \n");
ptr=first;
while(ptr!=NULL)
{
printf("\n%u | %d | %u ", ptr->lpt,ptr->info,ptr->rpt);
ptr=ptr->rpt;
}
}
void btraverse()
{
struct node *ptr;
printf("\nBackward traversing \n");
ptr=first;
while(ptr->rpt!=NULL)
ptr=ptr->rpt;
while(ptr!=NULL)
{ printf("\n%u | %d | %u ", ptr->lpt,ptr->info,ptr->rpt);
ptr=ptr->lpt;
}
}

OUTPUT

Doubly linked list - Deletion of an element at the end


#include<stdio.h>

#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node *lpt;
struct node *rpt;
};
struct node *first;
void main()
{
void create();
void delete_end();
void ftraverse();
void btraverse();
clrscr();
create();
delete_end();
ftraverse();
btraverse();
getch();
}
void create()
{
struct node *ptr,*cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nInput first node information : ");
scanf("%d",&ptr->info);
ptr->lpt=NULL;
first=ptr;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("\nInput next node information : ");
scanf("%d",&cpt->info);
ptr->rpt=cpt;
cpt->lpt=ptr;
ptr=cpt;
printf("\nPress <Y/N> for more node - ");
ch=getch();
}while((ch=='y')||(ch=='Y'));
ptr->rpt=NULL;
}
void delete_end()
{
struct node *ptr,*cpt;
ptr=first;
while(ptr->rpt!=NULL)
{
cpt=ptr;
ptr=ptr->rpt;
}
cpt->rpt=NULL;

free(ptr);
printf("\nDeletion is done\n");
}
void ftraverse()
{
struct node *ptr;
printf("\nForward traversing \n");
ptr=first;
while(ptr!=NULL)
{
printf("\n%u | %d | %u ", ptr->lpt,ptr->info,ptr->rpt);
ptr=ptr->rpt;
}
}
void btraverse()
{
struct node *ptr;
printf("\nBackward traversing \n");
ptr=first;
while(ptr->rpt!=NULL)
ptr=ptr->rpt;
while(ptr!=NULL)
{ printf("\n%u | %d | %u ", ptr->lpt,ptr->info,ptr->rpt);
ptr=ptr->lpt;
}
}

OUTPUT

Double circular linked list - Creation


#include<stdio.h>
#include<conio.h>

#include<alloc.h>
struct node
{
int info;
struct node *lpt;
struct node *rpt;
};
struct node *first;
void main()
{
void create();
void traverse();
clrscr();
create();
printf("\nTraversing\n");
traverse();
getch();
}
void create()
{
struct node *ptr,*cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nInput first node information : ");
scanf("%d",&ptr->info);
first=ptr;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("\nInput next node information : ");
scanf("%d",&cpt->info);
ptr->rpt=cpt;
cpt->lpt=ptr;
ptr=cpt;
printf("\nPress<Y/N> for more node - ");
ch=getch();
}while((ch=='y')||(ch=='Y'));
ptr->rpt=first;
first->lpt=ptr;
}
void traverse()
{
struct node *ptr;
ptr=first;
while(ptr->rpt!=first)
{
printf("\n%u | %d | %u ", ptr->lpt,ptr->info,ptr->rpt);
ptr=ptr->rpt;
}
printf("\n%u | %d | %u ",ptr->lpt,ptr->info,ptr->rpt);
}

OUTPUT

Double circular linked list - Insertion of element at the


beginning

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node *lpt;
struct node *rpt;
};
struct node *first;
void main()
{
void create();
void insert_beg();
void traverse();
clrscr();
create();
traverse();
insert_beg();
printf("\nAfter insertion\n");
traverse();
getch();
}
void create()
{
struct node *ptr,*cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nInput first node information : ");
scanf("%d",&ptr->info);
first=ptr;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("\nInput next node information : ");
scanf("%d",&cpt->info);
ptr->rpt=cpt;
cpt->lpt=ptr;
ptr=cpt;
printf("\nPress<Y/N> for more node - ");
ch=getch();
}while((ch=='y')||(ch=='Y'));
ptr->rpt=first;
first->lpt=ptr;
}
void insert_beg()
{
struct node *ptr,*cpt;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the node information : ");
scanf("%d",&ptr->info);
cpt=first;
ptr->rpt=first;
first->lpt=ptr;

while(cpt->rpt!=first)
cpt=cpt->rpt;
cpt->rpt=ptr;
ptr->lpt=cpt;
first=ptr;
}
void traverse()
{
struct node *ptr;
ptr=first;
while(ptr->rpt!=first)
{
printf("\n%u | %d | %u ", ptr->lpt,ptr->info,ptr->rpt);
ptr=ptr->rpt;
}
printf("\n%u | %d | %u ",ptr->lpt,ptr->info,ptr->rpt);
}

OUTPUT

Double circular linked list - Insertion of element at the end


#include<stdio.h>
#include<conio.h>

#include<alloc.h>
struct node
{
int info;
struct node *lpt;
struct node *rpt;
};
struct node *first;
void main()
{
void create();
void insert_end();
void traverse();
clrscr();
create();
traverse();
insert_end();
printf("\nAfter insertion\n");
traverse();
getch();
}
void create()
{
struct node *ptr,*cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nInput first node information : ");
scanf("%d",&ptr->info);
first=ptr;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("\nInput next node information : ");
scanf("%d",&cpt->info);
ptr->rpt=cpt;
cpt->lpt=ptr;
ptr=cpt;
printf("\nPress<Y/N> for more node - ");
ch=getch();
}while(ch=='y');
ptr->rpt=first;
first->lpt=ptr;
}
void insert_end()
{
struct node *ptr,*cpt;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the node information : ");
scanf("%d",&ptr->info);
cpt=first;
while(cpt->rpt!=first)
cpt=cpt->rpt;
cpt->rpt=ptr;
ptr->lpt=cpt;

first->lpt=ptr;
ptr->rpt=first;
}
void traverse()
{
struct node *ptr;
ptr=first;
while(ptr->rpt!=first)
{
printf("\n%u | %d | %u ", ptr->lpt,ptr->info,ptr->rpt);
ptr=ptr->rpt;
}
printf("\n%u | %d | %u ",ptr->lpt,ptr->info,ptr->rpt);
}

OUTPUT

Double circular linked list - Deletion of element at the


beginning

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node *lpt;
struct node *rpt;
};
struct node *first;
void main()
{
void create();
void del();
void traverse();
clrscr();
create();
traverse();
del();
printf("\nAfter deletion\n");
traverse();
getch();
}
void create()
{
struct node *ptr,*cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nInput first node information : ");
scanf("%d",&ptr->info);
first=ptr;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("\nInput next node information : ");
scanf("%d",&cpt->info);
ptr->rpt=cpt;
cpt->lpt=ptr;
ptr=cpt;
printf("\nPress<Y/N> for more node - ");
ch=getch();
}while(ch=='y');
ptr->rpt=first;
first->lpt=ptr;
}
void del()
{
struct node *ptr,*cpt;
cpt=ptr=first;
while(cpt->rpt!=first)
cpt=cpt->rpt;
first=ptr->rpt;
first->lpt=cpt;
cpt->rpt=first;

free(ptr);
}
void traverse()
{
struct node *ptr;
ptr=first;
while(ptr->rpt!=first)
{
printf("\n%u | %d | %u ", ptr->lpt,ptr->info,ptr->rpt);
ptr=ptr->rpt;
}
printf("\n%u | %d | %u ",ptr->lpt,ptr->info,ptr->rpt);
}

OUTPUT

Double circular linked list - Deletion of an element at the


end

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node *lpt;
struct node *rpt;
};
struct node *first;
void main()
{
void create();
void del_end();
void traverse();
clrscr();
create();
traverse();
del_end();
printf("\nAfter deletion\n");
traverse();
getch();
}
void create()
{
struct node *ptr,*cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nInput first node information : ");
scanf("%d",&ptr->info);
first=ptr;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("\nInput next node information : ");
scanf("%d",&cpt->info);
ptr->rpt=cpt;
cpt->lpt=ptr;
ptr=cpt;
printf("\nPress<Y/N> for more node - ");
ch=getch();
}while((ch=='y')||(ch=='Y'));
ptr->rpt=first;
first->lpt=ptr;
}
void del_end()
{
struct node *ptr,*cpt;
cpt=first;
while(cpt->rpt!=first)
{
ptr=cpt;
cpt=cpt->rpt;
}

ptr->rpt=first;
first->lpt=ptr;
free(ptr);
}
void traverse()
{
struct node *ptr;
ptr=first;
while(ptr->rpt!=first)
{
printf("\n%u | %d | %u ", ptr->lpt,ptr->info,ptr->rpt);
ptr=ptr->rpt;
}
printf("\n%u | %d | %u ",ptr->lpt,ptr->info,ptr->rpt);
}

OUTPUT

Implement queue using array


#include<stdio.h>

#include<conio.h>
#define maxsize 50
void main()
{
int queue[maxsize],data,front=-1,rear=-1,size,i,y;
clrscr();
while(1)
{
printf("\n1 - Insertion ");
printf("\n2 - Deletion ");
printf("\n3 - Exit ");
printf("\nEnter 1 or 2 or 3 : ");
scanf("%d",&y);
switch(y)
{
case 1:if(rear==maxsize)
{
printf("\nQueue is overflow");
}
else
{
printf("\nEnter the element to be inserted : ");
scanf("%d",&data);
if(front==-1)
{
front=front+1;
rear=rear+1;
queue[rear]=data;
}
else
{
rear=rear+1;
queue[rear]=data;
}
}
printf("\nAfter insertion \n");
for(i=0;i<=rear;i++)
{
printf("\n%d",queue[i]);
}
printf("\n\nFront %d", front);
printf("\nRear %d", rear);
break;
case 2:if(front==-1&&rear==-1)
{ printf("\nQueue is underflow");
}
else
{
if(front==rear)
{
queue[front]=NULL;
front=-1;
rear=-1;
}

else
{
queue[front]=NULL;
front=front+1;
}
}
printf("\nAfter deletion");
for(i=0;i<=rear;i++)
{
printf("\n%d",queue[i]);
}
printf("\n\nFront %d",front);
printf("\nRear %d",rear);
break;
case 3: exit(0);
default: printf("\nWrong input");
}
getch();
}
}

OUTPUT

OUTPUT

Array operation Creation


#include<stdio.h>
#include<conio.h>

#define max 10
void main()
{
int a[max],i,n;
clrscr();
printf("\nEnter how many elements you want to type : ");
scanf("%d",&n);
printf("\nEnter a array : ");
for(i=0;i<n;i++)
{
scanf("\n%d",&a[i]);
}
getch();
}

OUTPUT

Array operation: Insertion at the beginning


#include<stdio.h>
#include<conio.h>

#define max 10
void main()
{
int a[max],i,n,lb=0,b,ub;
clrscr();
printf("\nEnter how many elements you want to type : ");
scanf("%d",&n);
ub=n-1;
printf("\nEnter a array : ");
for(i=0;i<n;i++)
{
scanf("\n%d",&a[i]);
}
if(ub==max)
printf("\nArray overflow ");
else
{
printf("\nInsertion at beginning ");
printf("\nEnter number : ");
scanf("%d",&b);
for(i=ub;i>=lb;i--)
{
a[i+1]=a[i];
}
a[lb]=b;
printf("\nArray after insertion");
for(i=0;i<=n;i++)
{
printf("\n%d",a[i]);
}
}
getch();
}

OUTPUT

Implement queue using linked list


#include<stdio.h>
#include<conio.h>

#include<alloc.h>
struct node
{
int info;
int top;
struct node *link;
};
struct node *front,*rear;
void main()
{
void create();
void display();
void ins();
void del();
int c;
clrscr();
create();
printf("\nEnter 1 for insert and 2 for delete : ");
scanf("%d",&c);
switch(c)
{
case 1: ins();
break;
case 2: del();
break;
default:printf("\nInvalid Choice");
}
getch();
}
void create()
{
struct node *ptr,*cpt;
char ch;
ptr=(struct node*)malloc(sizeof(struct node));
printf("\nInput first node information : ");
scanf("%d",&ptr->info);
front=ptr;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("\nInput next node information : ");
scanf("%d",&cpt->info);
ptr->link=cpt;
ptr=cpt;
printf("\nPress<Y/N> for more node - ");
ch=getch();
}while((ch=='y')||(ch=='Y'));
rear=ptr;
rear->link=NULL;
}
void display()
{
struct node *ptr;

ptr=front;
if(front==NULL)
printf("\nQueue is empty ");
else
{
printf("\nTraversing of queue is \n");
printf("\nQueue elements are ");
while(ptr!=NULL)
{
printf("\n %d || %u ",ptr->info,ptr->link);
ptr=ptr->link;
}
printf("\n");
}
}
void ins()
{
struct node *ptr;
ptr=(struct node *)malloc(sizeof(struct node));
printf("\nInput the element for inserting : ");
scanf("%d",&ptr->info);
ptr->link=NULL;
if(front==NULL)
front=ptr;
else
rear->link=ptr;
rear=ptr;
display();
}
void del()
{
struct node *ptr;
if(front==NULL)
{
printf("\nQueue is underflow");
return;
}
if(front==rear)
{
free(front);
rear=NULL;
}
else
{
ptr=front;
front=ptr->link;
free(ptr);
}
display();
}

You might also like