Project 1 - Implement a fully functional Queue in C language using Linked lists
Ans)1. #include <bits/stdc++.h>2. using namespace std; 3. struct QNode {4. int data;5. QNode* next;6. QNode(int d)7. {8. data = d;9. next = NULL;10. }11. }; 12. Struct Queue 13. {14. QNode *front, *rear;15. Queue()16. {17. front = rear = NULL;18. } 19. void enQueue(int x)20. { 21. // Create a new LL node22. QNode* temp = new QNode(x); 23. // If queue is empty, then24. // new node is front and rear both25. if (rear == NULL) {26. front = rear = temp;27. return;28. } 29. // Add the new node at30. // the end of queue and change rear31. rear->next = temp;32. rear = temp;33. } 34. // Function to remove35. // a key from given queue q36. void deQueue()37. {38. // If queue is empty, return NULL.39. if (front == NULL)40. return; 41. // Store previous front and42. // move front one node ahead43. QNode* temp = front;44. front = front->next; 45. // If front becomes NULL, then46. // change rear also as NULL47. if (front == NULL)48. rear = NULL; 49.delete (temp);50. }51. }; // Driven Program1. int main()2. { 3. Queue q;4. q.enQueue(10);5. q.enQueue(20);6. q.deQueue();7. q.deQueue();8. q.enQueue(30);9. q.enQueue(40);10. q.enQueue(50);11. q.deQueue();12. cout << "Queue Front : " << (q.front)->data << endl;13. cout << "Queue Rear : " << (q.rear)->data;14. } // This code is contributed by rathbhupendra Output :
Queue Front : 40 Queue Rear : 50