Challenge

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 node
22. QNode* temp = new QNode(x);
23. // If queue is empty, then
24. // new node is front and rear both
25. if (rear == NULL) {
26. front = rear = temp;
27. return;
28. }
29. // Add the new node at
30. // the end of queue and change rear
31. rear->next = temp;
32. rear = temp;
33. }
34. // Function to remove
35. // a key from given queue q
36. void deQueue()
37. {
38. // If queue is empty, return NULL.
39. if (front == NULL)
40. return;
41. // Store previous front and
42. // move front one node ahead
43. QNode* temp = front;
44. front = front->next;
45. // If front becomes NULL, then
46. // change rear also as NULL
47. if (front == NULL)
48. rear = NULL;
49.delete (temp);
50. }
51. };
// Driven Program
1. 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
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.