Sunday, 17 June 2012

Queue using linked list


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
};
void main()
{
struct node *front,*rear,*tp,*temp;
int ch,key,value;
clrscr();
front = rear = NULL;
do
{
printf("1.Insert @ last\t ");
printf("2.Delete @ first\t 3.Display");
printf("\n\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:  {
             printf("\nenter a no to insert @ last:");
             temp=(struct node*)malloc(sizeof(struct node));
             scanf("%d",&value);
             temp->data=value;
             temp->next=NULL;
             if(rear==NULL&& front==NULL)
             {
             rear=temp;
             front=temp;
             }
             else
             {
              tp=rear;
              while(tp->next!=NULL)
              {
              tp=tp->next;
              }
              tp->next=temp;
              }}
             break;
 case 2: {
              if(front==NULL)
              printf("\n list is empty");
              else
              {
              tp=front;
              front=front->next;
              free(tp);
              }
             }break;
 case 3: {
             if(rear==NULL)
             printf("\nlist is empty");
             else
             {
             tp=front;
             printf("\nvalues are:\n");
             while(tp!=NULL)
             {
             printf("%d\t",tp->data);
             tp=tp->next;
             }
             }
             break;
default: printf("\nenter correct choice");
             break;
}}
}while(ch<4);
getch();
}

No comments:

Post a Comment