Problems: Write a C program to implement a circular queue that contains a list of names.
Data Structure
Definition:
Node:
A node is a basic building block of a linked list. A node may contain different
segments, but basically it contains two parts which are as followed,
Data or
information part : It stores the data in the node
Link part : It points to the next node or holds the address to the next node.
Source Code:
- #include<stdlib.h>
- #include<stdio.h>
- #include<string.h>
- #define MAX 10
- //defining the basic element of the queue
- typedef struct Q
- {
- int R,F;
- char *data[MAX];
- }Q;
- //declaration of functions
- void initialise(Q *P);
- int empty(Q *P);
- int full(Q *P);
- void enqueue(Q *P,char *x);
- char * dequeue(Q *P);
- void print(Q *P);
- //main function
- int main()
- {
- //intializing a node named queue
- Q q;
- //declaration and initialization fof variables
- int op;
- char x[40],*y;
- //initializ the queue
- initialise(&q);
- do{
- //displaying the menu
- printf("\n\n 1) Enqueue\n 2) Dequeue\n 3) Print\n 4) Quit");
- //inputting the input choice
- printf("\n Enter Your Choice : ");
- scanf("%d",&op);
- //calling functions and passing arguments through the functions as per th choice of the user
- switch(op)
- {
- case 1:
- printf("\n Enter string : ");
- fflush(stdin);
- scanf("%s",x);
- if(!full(&q))
- {
- y=(char*)malloc(strlen(x)+1);
- strcpy(y,x);
- enqueue(&q,y);
- }
- else
- printf("\n Queue is full !!!!");
- break;
- case 2:
- if(!empty(&q))
- {
- y=dequeue(&q);
- printf("\n Deleted Data = %s",y);
- }
- else
- printf("\n Queue is empty !!!!");
- break;
- case 3:
- print(&q);a
- break;
- }
- }while(op!=4);
- }
- //function to initialize a circular queue
- void initialise(Q *P)
- {
- //declaration of variable
- int i;
- //initializing the queue
- for(i=0;i<MAX;i++)
- P->data[i]=NULL;
- P->R=-1;
- P->F=-1;
- }
- //function to check if the queue is empty
- int empty(Q *P)
- {
- //if the queue is empty, return 1 else, return 0
- if(P->R==-1)
- return(1);
- return(0);
- }
- //function to check if the queue is full
- int full(Q *P)
- {
- //return 1 if the queue is full, else return 0
- if((P->R+1)%MAX==P->F)
- return(1);
- return(0);
- }
- //function to enqueue and element in the queue
- void enqueue(Q *P,char *x)
- {
- //enqueueing the element
- if(P->R==-1)
- {
- P->R=P->F=0;
- if(P->data[P->R]!=NULL)
- free(P->data[P->R]);
- P->data[P->R]=x;
- }
- else
- {
- P->R=(P->R+1)%MAX;
- if(P->data[P->R]!=NULL)
- free(P->data[P->R]);
- P->data[P->R]=x;
- }
- }
- //function to dequeue
- char * dequeue(Q *P)
- {
- //declaring a character type pointer
- char *x;
- //dequeing the element from the queue
- x=P->data[P->F];
- if(P->R==P->F)
- {
- P->R=-1;
- P->F=-1;
- }
- else
- P->F=(P->F+1)%MAX;
- return(x);
- }
- //function to display the elements of the queue
- void print(Q *P)
- {
- int i;
- if(!empty(P))
- {
- printf("\n");
- for(i=P->F;i!=P->R;i=(i+1)%MAX)
- printf("%s\t",P->data[i]);
- }
- printf("%s\t",P->data[i]);
- }
Output:
1) Enqueue
2) Dequeue
3) Print
4) Quit
Enter Your Choice : 1
Enter string : computer
1) Enqueue
2) Dequeue
3) Print
4) Quit
Enter Your Choice : 1
Enter string : science
1)
Enqueue
2) Dequeue
3) Print
4) Quit
Enter Your Choice : 1
Enter
string : honours
1) Enqueue
2) Dequeue
3) Print
4) Quit
Enter Your Choice : 3
computer science honours
1)
Enqueue
2) Dequeue
3) Print
4) Quit
Enter Your Choice : 2
Deleted Data = computer
1) Enqueue
2) Dequeue
3) Print
4) Quit
Enter Your Choice : 2
Deleted Data = science
1) Enqueue
2) Dequeue
3) Print
4) Quit
Enter Your Choice : 2
Deleted Data = honours
1) Enqueue
2) Dequeue
3) Print
4) Quit
Enter
Your Choice : 2
Queue is empty !!!!
0 comments:
Post a Comment