Editing
Queues
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== C++ Example == Here's an example of a simple queue implementation in C++ using a linked list: <pre> #include <iostream> class Node { public: int data; Node* next; }; class Queue { public: Queue() : front(nullptr), rear(nullptr) {} void enqueue(int data) { Node* newNode = new Node(); newNode->data = data; newNode->next = nullptr; if (isEmpty()) { front = rear = newNode; } else { rear->next = newNode; rear = newNode; } } void dequeue() { if (!isEmpty()) { Node* temp = front; front = front->next; if (front == nullptr) { rear = nullptr; } delete temp; } } int getFront() { if (!isEmpty()) { return front->data; } return -1; } int getRear() { if (!isEmpty()) { return rear->data; } return -1; } bool isEmpty() { return front == nullptr; } private: Node* front; Node* rear; }; int main() { Queue queue; queue.enqueue(1); queue.enqueue(2); queue.enqueue(3); std::cout << "Front element is: " << queue.getFront() << std::endl; std::cout << "Rear element is: " << queue.getRear() << std::endl; queue.dequeue(); std::cout << "Front element after dequeue is: " << queue.getFront() << std::endl; return 0; } </pre> In this example, the Node class represents a single node in the queue, and the Queue class implements the queue operations using a linked list. The enqueue() function adds a new node to the rear of the queue, the dequeue() function removes the front node from the queue, the getFront() function retrieves the front element, the getRear() function retrieves the rear element, and the isEmpty() function checks if the queue is empty.
Summary:
Please note that all contributions to Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
My wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information