Monday, 20 December 2010

Program to implement Queue operations by using the pointers

Description :


In this program we have to implement the Queue operation by using the pointers. Here they Queue operation are push and pop. Push operation is used to insert the elements into a Queue and pop operation is used to remove the elements in to a Queue.

Program:


 #define true 1
 #define false 0
#include<stdio.h>
#include<conio.h>
#include<process.h>

struct q_point
{
int ele;
 struct q_point* n;
};

struct q_point *f_ptr = NULL;
 int e_que(void);
 void add_ele(int); 
int rem_ele(void); 
void show_ele();

/*main function*/ void main()

{
int ele,choice,j; while(1)

{
clrscr();
printf("\n\n****IMPLEMENTATION OF QUEUE USING POINTERS****\n");
printf("==============================================");
printf("\n\t\t MENU\n"); printf("==============================================");
printf("\n\t[1] To insert an element"); printf("\n\t[2] To remove an element"); printf("\n\t[3] To display all the elements"); printf("\n\t[4] Exit");
printf("\n\n\tEnter your choice:"); scanf("%d", &choice);

switch(choice)
{
case 1:
{
printf("\n\tElement to be inserted:"); scanf("%d",&ele);
add_ele(ele); getch(); 
break;

}

case 2:
{
if(!e_que())
{
j=rem_ele();
printf("\n\t%d is removed from the queue",j); getch();
}
else
{
printf("\n\tQueue is Empty."); getch();
}
break;
}

case 3:
show_ele(); getch(); 
break;

case 4:
exit(1);
break;

default:
printf("\n\tInvalid choice."); getch();
break;
}

}
}

/* Function to check if the queue is empty*/
 int e_que(void)
{
if(f_ptr==NULL) return true; return false;
}

/* Function to add an element to the queue*/
 void add_ele(int ele)
{
struct q_point *queue = (struct q_point*)malloc(sizeof(struct q_point)); queue->ele = ele;
queue->n = NULL;               
if(f_ptr==NULL) f_ptr = queue; 

else
{
struct q_point* ptr; ptr = f_ptr;
for(ptr=f_ptr ;ptr->n!=NULL; ptr=ptr->n);
ptr->n = queue;
}
}
/* Function to remove an element from the queue*/ 
int rem_ele()
{
struct q_point* queue=NULL;
if(e_que()==false)
{
 int j = f_ptr->ele; queue=f_ptr;
 f_ptr = f_ptr->n; 
free (queue);
 return j;
}
else
{
printf("\n\tQueue is empty."); return -9999;
}
}

/* Function to display the queue*/ void show_ele()
{
struct q_point *ptr=NULL; ptr=f_ptr;
if(e_que())
{
printf("\n\tQUEUE is Empty."); return;
}

else
{
printf("\n\tElements present in Queue are:\n\t"); while(ptr!=NULL)
{
printf("%d\t",ptr->ele); ptr=ptr->n;
}
}
}


Output:


****IMPLEMENTATION OF QUEUE USING POINTERS****
==============================================
MENU
==============================================
[1] To insert an element 
[2] To remove an element 
[3] To display all the elements 
[4] Exit

Enter your choice:1

Element to be inserted:23


****IMPLEMENTATION OF QUEUE USING POINTERS****
==============================================
MENU
==============================================
[1] To insert an element 
[2] To remove an element 
[3] To display all the elements 
[4] Exit 

Enter your choice:3

Elements present in Queue are: 23


****IMPLEMENTATION OF QUEUE USING POINTERS****
==============================================
MENU
==============================================
[1] To insert an element 
[2] To remove an element 
[3] To display all the elements 
[4] Exit 
Enter your choice:2
23 is removed from the queue





****IMPLEMENTATION OF QUEUE USING POINTERS****
==============================================
MENU
==============================================
[1] To insert an element 
[2] To remove an element 
[3] To display all the elements 
[4] Exit 

Enter your choice:4
Exit

Conclusion : the program is error free


VIVA QUESTIONS: 

1) Define queue ?
Ans: A queue is a linear, sequential list of that are accessed in the oeder first in first out(FIFO).

2) Define circular queues ?
Ans: A queue can also be circular in which case, it is called as a circular queue







No comments:

Post a Comment