How to Create a Circular Linklist in C++ With Source Code
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
}*head;
int front,rear,size,v;
void insertion();
void deletion();
void display();
void main()
{
int choice;
front=0;
rear=0;
printf("How many nodes you want to enter?");
scanf("%d",&size);
head=NULL;
do
{
clrscr();
printf("\nGive 1 for Insertion");
printf("\nGive 2 for Deletion");
printf("\nGive 3 for Display");
printf("\nGive 4 for Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: insertion();
break;
case 2: deletion();
break;
case 3: display();
break;
}
}while(choice!=4);
}
void insertion()
{
struct node *q,*temp;
if((front==1 && rear==size) || (rear==front-1))
{
printf("\nOverflow");
getch();
}
else
{
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter value:");
scanf("%d",&v);
temp->data=v;
temp->next=NULL;
if(front==0)
head=temp;
else
{
q=head;
while(q->next!=NULL)
q=q->next;
q->next=temp;
}
if(front==0)
{
front=1;
rear=1;
}
else if(rear==size)
rear=1;
else
rear=rear+1;
}
}
void deletion()
{
struct node *temp;
if(front==0)
{
printf("Underflow");
getch();
}
else
{
temp=head;
printf("the deleted data is:%d",temp->data);
getch();
if(head->next==NULL)
head=NULL;
else
head=head->next;
free(temp);
if(front==rear)
{
front=0;
rear=0;
}
else if(front==size)
front=1;
else
front++;
}
}
void display()
{
struct node *q;
q=head;
if(head==NULL)
printf("UNDERFLOW");
else
{
/* if(front==1)
printf("\n%d",q->data);*/
while(q!=NULL)
{
printf("\n%d",q->data);
q=q->next;
}
}
getch();
}
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
}*head;
int front,rear,size,v;
void insertion();
void deletion();
void display();
void main()
{
int choice;
front=0;
rear=0;
printf("How many nodes you want to enter?");
scanf("%d",&size);
head=NULL;
do
{
clrscr();
printf("\nGive 1 for Insertion");
printf("\nGive 2 for Deletion");
printf("\nGive 3 for Display");
printf("\nGive 4 for Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: insertion();
break;
case 2: deletion();
break;
case 3: display();
break;
}
}while(choice!=4);
}
void insertion()
{
struct node *q,*temp;
if((front==1 && rear==size) || (rear==front-1))
{
printf("\nOverflow");
getch();
}
else
{
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter value:");
scanf("%d",&v);
temp->data=v;
temp->next=NULL;
if(front==0)
head=temp;
else
{
q=head;
while(q->next!=NULL)
q=q->next;
q->next=temp;
}
if(front==0)
{
front=1;
rear=1;
}
else if(rear==size)
rear=1;
else
rear=rear+1;
}
}
void deletion()
{
struct node *temp;
if(front==0)
{
printf("Underflow");
getch();
}
else
{
temp=head;
printf("the deleted data is:%d",temp->data);
getch();
if(head->next==NULL)
head=NULL;
else
head=head->next;
free(temp);
if(front==rear)
{
front=0;
rear=0;
}
else if(front==size)
front=1;
else
front++;
}
}
void display()
{
struct node *q;
q=head;
if(head==NULL)
printf("UNDERFLOW");
else
{
/* if(front==1)
printf("\n%d",q->data);*/
while(q!=NULL)
{
printf("\n%d",q->data);
q=q->next;
}
}
getch();
}
How to Create a Circular Linklist in C++ With Source Code
Reviewed by Unknown
on
5:41:00 PM
Rating:
No comments: