CEG's Linux User Group

  • Increase font size
  • Default font size
  • Decrease font size
Home Labs CIRCULAR QUEUE IMPLEMENTATION USING LINKED LIST - SOURCE CODE
CIRCULAR QUEUE IMPLEMENTATION USING LINKED LIST

CIRCULAR QUEUE IMPLEMENTATION USING LINKED LIST - SOURCE CODE

E-mail Print
User Rating: / 7
PoorBest 
Article Index
CIRCULAR QUEUE IMPLEMENTATION USING LINKED LIST
SOURCE CODE
DOCUMENTATION
TEST CASE AND COMPATIBILITY
All Pages

 

CODE:

#include"stdio.h"
#include"stdlib.h"
typedef struct qnode
{
int a;
struct qnode *next;
}*node;

node prev,new,temp,end;
typedef node queue;
queue createqueue()
{
queue q;
q=(queue)malloc(sizeof(struct qnode));
if(q==NULL)
{
printf("\nOut of space");
return NULL;
}
q->next=q;
return q;
}

void displayq(queue q)
{
printf("\nThe contents of the queue are:");
if(isempty(q))
printf("\nNo items to delete");
else
{
printf("\n");
temp=q->next;
while(temp!=q)
{
printf("%d\t",temp->a);
temp=temp->next;
}
}
}

int isempty(queue q)
{
return(q->next==q);
}

queue insert(queue q,int x)
{
node new;
new=(node)malloc(sizeof(struct qnode));
new->a=x;
if(isempty(q))
{
new->next=q;
q->next=new;
end=new;
}
else
{
end->next=new;
new->next=q;
end=new;
}
return q;
}

void delete(queue q)
{
if(isempty(q))
printf("\nUnderflow");
else
q->next=(q->next)->next;
}


int front(queue q)
{
if(isempty(q))
{
printf("\nUnderflow");
return -1;
}
else
return ((q->next)->a);
}

int frontdel(queue q)
{
int x;
if(isempty(q))
{
printf("\nUnderflow");
return -1;
}
else
{
x=(q->next)->a;
temp=q->next;
free(temp);
q->next=(q->next)->next;
return x;
}
}

int main()
{
int n,x;
queue q;
q=createqueue();
printf("\nThe choices are\n1.Insert\n2.Delete\n3.Front\n4.Frontdelete\n5.Exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1:printf("\nEnter the element:");
scanf("%d",&x);
insert(q,x);
displayq(q);
break;
case 2:delete(q);
printf("\nElement deleted");
displayq(q);
break;
case 3:printf("\nFront value is %d",front(q));
displayq(q);
break;
case 4:printf("\nDeleted element is %d",frontdel(q));
displayq(q);
break;
case 5:exit(1);

default:printf("\nInvalid choice");
}
}
}



Last Updated on Saturday, 13 June 2009 08:21  
Please register or login to add your comments to this article.