Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct node
- {
- int data;
- struct node *next;
- }*front,*front1,*rear,*temp;
- void enq(int data)
- {
- if(rear == NULL)
- {
- rear = (struct node*)malloc(sizeof(struct node));
- rear->data = data;
- rear->next = NULL;
- front = rear;
- }
- else
- {
- temp = (struct node*)malloc(sizeof(struct node));
- rear->next = temp;
- temp->data = data;
- temp->next = NULL;
- rear = temp;
- }
- }
- void deq()
- {
- front1 = front;
- if(front1 == NULL)
- printf("Your queue is empty\n");
- else
- {
- if(front1->next != NULL)
- {
- front1 = front1->next;
- printf("Dequeued value %d\n",front->data);
- free(front);
- front = front1;
- }
- else
- {
- printf("Dequeued value %d\n",front->data);
- free(front);
- front = NULL;
- rear = NULL;
- }
- }
- }
- void display()
- {
- front1 = front;
- if(front1 == NULL && rear == NULL)
- printf("Queue is empty\n");
- while(front1 != rear)
- {
- printf("%d\n",front1->data);
- front1 = front1->next;
- }
- printf("%d\n",front1->data);
- }
- int main()
- {
- front = rear = NULL;
- enq(10);
- enq(20);
- enq(30);
- printf("Queue before dequeue:\n");
- display();
- deq();
- printf("Queue after dequeue:\n");
- display();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement