Queue as a List

 #include<stdio.h>

#include<conio.h>

#define N 5

void display();

int queue[N],front=-1,rear=-1,i;

void enqueue(int x){

if(rear==N-1)

printf("Queue is full.");

else if(front==-1 && rear==-1){

front=rear=0;

queue[rear]=x;

}

else{

rear++;

queue[rear]=x;

}

}

void dequeue(){

if(front==-1 && rear==-1)

printf("Queue is empty.");

else if(front==rear)

front=rear=-1;

else

front++;

display();

}

void display(){

if(front==-1 && rear==-1)

printf("Queue is empty.");

else{

for(i=front;i<=rear;i++)

printf("%d\t",queue[i]);

}

}

void peek(){

if(front==-1 && rear==-1)

printf("Queue is empty.");

else

printf("%d\t",queue[front]);

}


void main(){

enqueue(1);

enqueue(3);

enqueue(5);

enqueue(7);

enqueue(9);

printf("After enqueuing:\n");

display();

printf("\nFirst element is:\n");

peek();

printf("\nAfter dequeuing:\n");

dequeue();

printf("\nFirst element is:\n");

peek();

printf("\nAgain:\n");

display();

getch();

}



No comments:

Post a Comment