📖 Scenario: You are building a simple queue system for a ticket counter. People join the queue one by one, and you want to keep track of them in the order they arrive.
🎯 Goal: Create a linked list to represent the queue and add new people to the end of the queue using the enqueue operation.
📋 What You'll Learn
Create a Node class with data and next fields
Create a Queue class with front and rear nodes
Implement an Enqueue method to add a new node at the end
Print the queue after adding new people
💡 Why This Matters
🌍 Real World
Queues are used in real life to manage lines of people waiting for service, like at banks, ticket counters, or call centers.
💼 Career
Understanding how to implement queues with linked lists is important for software developers working on systems that require ordered processing, such as task scheduling or messaging.
Progress0 / 4 steps
1
Create Node and Queue classes
Create a Node class with a public string field data and a public Node field next. Then create a Queue class with public Node fields front and rear, both initialized to null.
DSA C#
Hint
Start by defining the Node class with data and next. Then define the Queue class with front and rear nodes set to null.
2
Add Enqueue method to Queue
Inside the Queue class, add a public method Enqueue that takes a string parameter value. Create a new Node with value. If rear is null, set both front and rear to the new node. Otherwise, set rear.next to the new node and update rear to the new node.
DSA C#
Hint
Check if the queue is empty by seeing if rear is null. If empty, set both front and rear to the new node. Otherwise, link the new node after rear and update rear.
3
Add method to print the queue
Inside the Queue class, add a public method PrintQueue that prints all node data values from front to rear separated by -> . Use a Node variable current to traverse from front until null.
DSA C#
Hint
Use a while loop to move through the nodes starting at front. Print each data and add -> between nodes. End with -> null.
4
Enqueue people and print the queue
In the Main method, create a Queue object called ticketQueue. Enqueue these people in order: "Alice", "Bob", "Charlie". Then call ticketQueue.PrintQueue() to display the queue.
DSA C#
Hint
Create a Queue object named ticketQueue. Use Enqueue to add "Alice", "Bob", and "Charlie" in that order. Then call PrintQueue to show the queue.
Practice
(1/5)
1. What does the Enqueue operation do in a linked list implementation of a queue?
easy
A. Adds an element at the front of the linked list
B. Removes an element from the front of the linked list
C. Deletes the last element of the linked list
D. Adds an element at the end of the linked list
Solution
Step 1: Understand enqueue operation
Enqueue means adding an element to the queue.
Step 2: Identify linked list behavior for enqueue
In a linked list queue, enqueue adds the new node at the end (tail).
Final Answer:
Adds an element at the end of the linked list -> Option D
Quick Check:
Enqueue = Add at tail [OK]
Hint: Enqueue always adds at the tail in linked list queue [OK]
Common Mistakes:
Confusing enqueue with dequeue operation
Adding element at the front instead of the end
Thinking enqueue removes elements
2. Which of the following is the correct syntax to create a new node and enqueue it in a linked list queue in C#?
easy
A. Node newNode = new Node(data); tail.next = newNode; tail = newNode;