Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views2 pages

Include

The document contains C++ code for a queue implementation using a linked list. It defines a 'node' structure and a 'queue' class with methods to append, serve, retrieve, and print elements. The main function demonstrates creating a queue, appending elements, retrieving the front element, and serving the queue.

Uploaded by

newsuper444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Include

The document contains C++ code for a queue implementation using a linked list. It defines a 'node' structure and a 'queue' class with methods to append, serve, retrieve, and print elements. The main function demonstrates creating a queue, appending elements, retrieving the front element, and serving the queue.

Uploaded by

newsuper444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream> {

if (front == 0)
struct node return false;
{
int data; if (front == rear)
node *next; {
delete front;
// Constructor for node front = rear = 0;
node(int d, node *n = 0) }
{ else
data = d; {
next = n; node *t = front;
} front = front->next;
}; delete t;
}
class queue return true;
{ }
node *front, *rear;
// Retrieve the front element without removing
public: it
// Constructor for queue bool queue::retrieve(int &el)
queue(); {
if (front == 0)
// Check if the queue is empty return false;
bool empty(); el = front->data;
return true;
// Append an element to the queue }
void append(int el);
// Print the elements of the queue
// Serve (remove) the front element void queue::print()
from the queue {
bool serve(); node *current = front;
while (current != 0)
// Retrieve the front element without {
removing it std::cout << current->data <<
bool retrieve(int &el); " ";
current = current->next;
// Destructor (not implemented in the }
provided code) std::cout << std::endl;
// ... }

// Print the elements of the queue // Main function


void print(); int main()
}; {
// Create a queue
// Check if the queue is empty queue myQueue;
bool queue::empty()
{ // Append elements to the queue
return front == 0; myQueue.append(10);
} myQueue.append(20);
myQueue.append(30);
// Constructor for the queue
queue::queue() // Print the initial state of the
{ queue
front = rear = 0; std::cout << "Initial queue: ";
} myQueue.print();

// Append an element to the queue // Retrieve and print the front


void queue::append(int el) element without removing it
{ int frontElement;
if (front == 0) if (myQueue.retrieve(frontElement))
front = rear = new node(el); std::cout << "Front element:
else " << frontElement << std::endl;
rear = rear->next = new
node(el); // Serve (remove) the front element
} from the queue
myQueue.serve();
// Serve (remove) the front element from the
queue // Print the updated state of the
bool queue::serve() queue
std::cout << "Updated queue: ";
myQueue.print();

return 0;
}

You might also like