Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include<string.h>
- struct node
- {
- char info[20];
- struct node *ptr;
- }*front,*rear,*temp,*front1;
- void enq(char data[]);
- void deq();
- void empty();
- void display();
- void create();
- void queuesize();
- int count = 0;
- void main()
- {
- int ch;
- char no[20];
- create();
- while (1)
- {
- printf("\n 1 - Enque");
- printf("\n 2 - Deque");
- printf("\n 3 - Empty");
- printf("\n 4 - Exit");
- printf("\n 5 - Display");
- printf("\n 6 - Queue size");
- printf("\n Enter choice : ");
- scanf("%d", &ch);
- switch (ch)
- {
- case 1:
- printf("Please enter the Vehical ID : ");
- scanf("%s", &no);
- enq(no);
- break;
- case 2:
- deq();
- break;
- case 3:
- empty();
- break;
- case 4:
- exit(0);
- case 5:
- display();
- break;
- case 6:
- queuesize();
- break;
- default:
- printf("Wrong choice, Please enter correct choice ");
- break;
- }
- }
- }
- /* Create an empty queue */
- void create()
- {
- front = rear = NULL;
- }
- /* Returns queue size */
- void queuesize()
- {
- printf("\n Queue size : %d", count);
- }
- /* Enqueing the queue */
- void enq(char data[])
- {
- if (rear == NULL)
- {
- rear = (struct node *)malloc(1*sizeof(struct node));
- rear->ptr = NULL;
- strcpy(rear->info, data);
- front = rear;
- }
- else
- {
- temp=(struct node *)malloc(1*sizeof(struct node));
- rear->ptr = temp;
- strcpy(temp->info, data);
- temp->ptr = NULL;
- rear = temp;
- }
- count++;
- }
- /* Displaying the queue elements */
- void display()
- {
- front1 = front;
- if ((front1 == NULL) && (rear == NULL))
- {
- printf("Queue is empty");
- return;
- }
- while (front1 != rear)
- {
- printf("%s ", front1->info);
- front1 = front1->ptr;
- }
- if (front1 == rear)
- printf("%s", front1->info);
- }
- /* Dequeing the queue */
- void deq()
- {
- front1 = front;
- if (front1 == NULL)
- {
- printf("\n Error: Trying to display elements from empty queue");
- return;
- }
- else if (front1->ptr != NULL)
- {
- front1 = front1->ptr;
- printf("\n Dequed value : %s", front->info);
- free(front);
- front = front1;
- }
- else
- {
- printf("\n Dequed value : %s", front->info);
- free(front);
- front = NULL;
- rear = NULL;
- }
- count--;
- }
- /* Display if queue is empty or not */
- void empty()
- {
- if ((front == NULL) && (rear == NULL))
- printf("\n Queue empty");
- else
- printf("Queue not empty");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement