Your Ad Here

Thursday, July 5, 2007

Queue

A queue is a buffer abstract data structure providing services in computer science, transport and operations research where various entities such as data, objects, persons, or events are stored and held to be processed later. The most well known operation of the queue is the First-In-First-Out (FIFO) queue process. In a FIFO queue, the first element in the queue will be the first one out; this is equivalent to the requirement that whenever an element is added, all elements that were added before have to be removed before the new element can be invoked. Unless otherwise specified, the remainder of the article will refer to FIFO queues. There are also non-FIFO queue data structures, like priority queues.

/******** C Program For Impelmetation Of Queue ***********/

#define MAXSIZE 10

struct st
{
int front,rear;
int queue[MAXSIZE];
};

struct st s;

int empty(void);
int full(void);
void add(void);
void delete(void);
void display(void);

void main()
{
char ans;
int ch;
s.front = 0;
s.rear = 0;

do
{
clrscr();
printf("********Queue Program**********\n");
printf("1. ADD\n");
printf("2. DELETE\n");
printf("3. DISPLAY\n");
printf("4. QUIT\n");
printf("Enter Your Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
add();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
break;

default:
printf("INVALID CHOICE!!!!!!!!!!!!!!!!\n");
break;
}
printf("\nWant To Go To The Main Menu[y/n]");
flushall();
ans = getch();
}
while(ans == 'y' ans == 'Y');
printf("\nPress Any Key To Continue\n");
getch();
}

int full(void)
{
if (s.rear == MAXSIZE)
return(1);
else
return(0);
}
int empty(void)
{
if (s.front == s.rear + 1)
return(1);
else
return(0);
}

void add(void)
{
char ch;
int x;
do
{
if(full() == 1)
{
printf("\n\nQueue Full\n");
break;
}
else
{
s.rear = s.rear + 1;
printf("\nEnter An Element to Be Added ");
scanf("%d",&x);
s.queue[s.rear] = x;
if(s.rear == 1) s.front ++;
}
printf("\nDo You Want to Add More Elements[y/n]:");
flushall();
ch = getch();
}
while(ch=='y' ch == 'Y');
}

void delete(void)
{
char ch;
do
{
if(empty() == 1)
{
printf("\n\nQueue Empty\n");
break;
}
else
{
printf("% d Has Been Deleted!",s.queue[s.front]);
s.front = s.front +1;
}
printf("\nWant to Delete More [y\n]");
flushall();
ch = getch();
}
while(ch=='y' ch == 'Y');
}

void display(void)
{
int i;
clrscr();
if(empty () == 1)
printf("\nQueue Empty!!");
else
{
printf("\nDisplaying Queue\n");
for(i = s.front;i printf("%d\n",s.queue[i]);
}
}
/************ OUTPUT ***************

**********Queue Program**********
1. ADD
2. DELETE
3. DISPLAY
4. QUIT
Enter Your Choice : 1

Enter An Element to Be Added 1
Do You Want to Add More Elements[y/n]:y
Enter An Element to Be Added 2
Do You Want to Add More Elements[y/n]:y
Enter An Element to Be Added 3
Do You Want to Add More Elements[y/n]:y
Enter An Element to Be Added 4
Do You Want to Add More Elements[y/n]:y
Enter An Element to Be Added 5
Do You Want to Add More Elements[y/n]:n
Want To Go To The Main Menu[y\n] y

**********Queue Program**********
1. ADD
2. DELETE
3. DISPLAY
4. QUIT
Enter Your Choice : 3

Displaying Queue
1
2
3
4
5
Want To Go To The Main Menu[y\n] y

**********Queue Program**********
1. ADD
2. DELETE
3. DISPLAY
4. QUIT
Enter Your Choice : 2
1 Has Been Deleted!!
Do You Want To Delete More?[y/n] n
Want to Go To Main Menue[y/n] y

**********Queue Program**********
1. ADD
2. DELETE
3. DISPLAY
4. QUIT
Enter Your Choice : 3

Displaying Queue
2
3
4
5
Want To Go To The Main Menu[y\n] y

**********Queue Program**********
1. ADD
2. DELETE
3. DISPLAY
4. QUIT
Enter Your Choice : 4 */

8 comments:

Anonymous said...

I am a newbie to C and data structures. I found the program really useful. Thank you very much.

vinays said...

ya this program is very useful

Fen2x said...

hey. thanks for the sample program..

Anonymous said...

god...very lengthy.......

Anonymous said...

god...very lengthy.......

Prashanth said...

If any one got this not working, I found another program here:Link

James said...

wow nice program your logic is simple good job

Anonymous said...

wow nice program and your logic is simple good job @ahmed

Your Ad Here