Thursday, 17 October 2013

Circular Queue Using Double linked list

Circular Queue Using Double linked list

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

struct node
{
int data;
struct node *next;
struct node *prev;
};

struct node *head=NULL,*temp;

void insert(int ele)
{
struct node *tempe;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=ele;
temp->next=NULL;
if(head==NULL)
{
head=temp;
temp->next=head;
}
else
{
tempe=head;
while(tempe->next!=head)
{
tempe=tempe->next;
}
tempe->next=temp;
temp->next=head;
      temp->prev=tempe;
}
}

void display()
{
struct node *temp1;
temp1=head;
while(temp1->next!=head)
{
printf("%d\t",temp1->data);
temp1=temp1->next;
}
printf("%d",temp1->data);
}


void del(int pos)
{
struct node *temp1,*temp2;

if(head==NULL)
{
printf("Circular Queue is Emplty");
}
else if(pos==1)
{
temp1=head;
temp2=head;
while(temp1->next!=head)
{
temp1=temp1->next;
}
head=head->next;
temp1->next=head;
free(temp2);
}
else
{
temp2=head;
while(--pos!=0)
{
temp1=temp2;
temp2=temp2->next;

}
temp1->next=temp2->next;
free(temp2);
}
}
  void main()
{
int n,e,ch;
head=NULL;
while(1)
{
printf("\n1.Insert\n2.Delete\n3.Display\n4.Exit");
printf("\nEnter your Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the Number : ");
scanf("%d",&n);
insert(n);break;
case 2: printf("\nEnter the Position for Deleting : ");
scanf("%d",&e);
del(e);  break;
case 3:
display();  break;
case 4: exit(0);   break;
default: printf("\nYour Choice is Wrong");
}
}

}

No comments:

Post a Comment