Powered By Blogger

Sunday, April 15, 2012

Program : 20 --> linear queue using Pointers...

Linear Queue using Pointers


#include<stdio.h>
#include<conio.h>
struct queue
{
int data;
struct queue *next;
}*front=NULL,*rear=NULL,*temp,*n;
void insert();
void delete();
void display();
void main()
{
int ch;
clrscr();
do
{
printf("MENU OPTIONS \n");
printf("1.Insert \n");
printf("2.Delete \n");
printf("3.Display \n");
printf("4.Exit\n");
printf("Enter ur choice \n");
scanf("%d",&ch);
switch(ch)
{
    case 1:insert();
           break;
    case 2:delete();
           break;
    case 3:display();
           break;
    case 4:exit(0);
           break;
    default:
    printf("Invalid choice \n");
    }
    }
    while(ch<=4);
    getch();
    }
void insert()
{
n=(struct queue *)malloc(sizeof(struct queue));
printf("Enter element to be inserted \n");
scanf("%d",&n->data);
n->next=NULL;
if(rear==NULL)
front=n;
else
{
rear->next=n;
}
rear=n;
}
void delete()
{
if(front==NULL)
printf("Queue underflow");
else
{
temp=front;
printf("%d is deleted \n",temp->data);
front=front->next;
}
}
void display()
{
temp=front;
printf("The elements in the queue are:\n ");
while(temp!=NULL)
{
printf("%d -->",temp->data);
temp=temp->next;
}
}

No comments:

Post a Comment