Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Implementing Queue in C. Using Linked List */
- #include <stdio.h>
- #include <stdlib.h>
- struct Node
- {
- int data;
- struct Node* next;
- }*front = NULL, *rear = NULL;
- void insert(int value)//enqueue
- {
- struct Node* newNode;
- newNode = (struct Node*)malloc(sizeof(struct Node));
- newNode->data = value;
- newNode->next = NULL;
- if (front == NULL)
- front = rear = newNode;
- else
- {
- rear->next = newNode;
- rear = newNode;
- }
- printf("\nInsertion is Successful!!!\n");
- }
- void delete()//dequeue
- {
- if (front == NULL)
- printf("\nQueue is Empty!!!\n");
- else
- {
- struct Node* temp = front;
- front = front->next;
- printf("\nDeleted element: %d\n", temp->data);
- free(temp);
- }
- }
- void print()
- {
- if (front == NULL)
- printf("\nQueue is Empty!!!\n");
- else
- {
- struct Node* temp = front;
- while (temp->next != NULL) {
- printf("%d--->", temp->data);
- temp = temp->next;
- }
- printf("%d--->NULL\n", temp->data);
- }
- }
- int main()
- {
- int choice, value;
- printf("\n:: Queue Implementation using Linked List ::\n");
- while (1)
- {
- printf("\n****** MENU ******\n");
- printf("1. Insert\n2. Delete\n3. Print\n4. Exit\n");
- printf("Enter your choice: ");
- scanf("%d", &choice);
- switch (choice)
- {
- case 1: printf("Enter the value to be insert: ");
- scanf("%d", &value);
- insert(value);
- break;
- case 2: delete(); break;
- case 3: print(); break;
- case 4: exit(0);
- default: printf("\nWrong selection!!! Please try again!!!\n");
- }
- }
- }
- /*
- Output:
- ::Queue Implementation using Linked List ::
- ****** MENU******
- 1. Insert
- 2. Delete
- 3. Print
- 4. Exit
- Enter your choice : 1
- Enter the value to be insert : 10
- Insertion is Successful!!!
- ******MENU * *****
- 1. Insert
- 2. Delete
- 3. Print
- 4. Exit
- Enter your choice : 1
- Enter the value to be insert : 20
- Insertion is Successful!!!
- ******MENU * *****
- 1. Insert
- 2. Delete
- 3. Print
- 4. Exit
- Enter your choice : 2
- Deleted element : 10
- * *****MENU * *****
- 1. Insert
- 2. Delete
- 3. Print
- 4. Exit
- Enter your choice : 3
- 20--->NULL
- * *****MENU * *****
- 1. Insert
- 2. Delete
- 3. Print
- 4. Exit
- Enter your choice : 4
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement