Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // PROGRAM TO IMPLEMENT QUEUE USING LINKED LIST
- #include <iostream>
- using namespace std;
- class node{
- protected:
- int element;
- node* link;
- public:
- //constructor that accepts only element
- node(int element) {
- this->element = element;
- this->link = NULL;
- }
- //constructor that accepts both link and element
- node(int element, node* link){
- this->element = element;
- this->link = link;
- }
- //method to update the element
- void updateData(int element){
- this->element = element;
- }
- //method to update or setup link
- void updateLink(node* link){
- this->link = link;
- }
- //method to get the element from the node
- int getElement(){
- return this->element;
- }
- //method to get the next node
- node* getNextNode(){
- return this->link;
- }
- };
- class Linkedlist{
- protected:
- node* head;
- public:
- //constructor for the Linked List class
- Linkedlist(){
- head = NULL;
- }
- //method returns true if the list is empty
- bool isEmpty(){
- return head == NULL;
- }
- //returns head node
- node* getHead(){
- return this->head;
- }
- //method to add a node at starting
- void addToStart(int element){
- node* tempNode = new node(element);
- if(head == NULL){
- head = tempNode;
- }
- else{
- tempNode->updateLink(head);
- head = tempNode;
- }
- }
- // method to add a node at the end
- void addToEnd(int element){
- node* tempNode = new node(element);
- node* p = head;
- if(head == NULL){
- head = tempNode;
- return;
- }
- else{
- while(p->getNextNode()!= NULL){
- p = p->getNextNode();
- }
- p->updateLink(tempNode);
- return;
- }
- }
- //method to display all the elements of the Linked List
- void display(){
- cout<<"\n";
- node* tempNode = head;
- while(tempNode != NULL){
- if(tempNode->getNextNode() != NULL)
- cout<<tempNode->getElement()<<" --> ";
- else
- cout<<tempNode->getElement();
- tempNode = tempNode->getNextNode();
- }
- return;
- }
- //method to delete a element at a given position
- void deletePosition(int position){
- node* tempNode = head;
- node* prevNode = head;
- if(position == 1){
- head = tempNode->getNextNode();
- return;
- }
- else{
- int pos = 1;
- while(pos != position){
- prevNode = tempNode;
- tempNode = tempNode->getNextNode();
- pos += 1;
- }
- prevNode->updateLink(tempNode->getNextNode());
- }
- }
- bool enqueue(int element){
- if(this->isEmpty()){
- this->addToStart(element);
- }else{
- this->addToEnd(element);
- }
- }
- int dequeue(){
- if(this->isEmpty()){
- cout<<"Queue Empty.";
- return -99999;
- }else{
- int temp = this->head->getElement();
- this->deletePosition(1);
- return temp;
- }
- }
- };
- int main() {
- Linkedlist l1;
- l1.enqueue(1);
- l1.enqueue(2);
- l1.enqueue(3);
- l1.enqueue(4);
- l1.enqueue(5);
- l1.enqueue(6);
- cout<<"The Contents of the Queue are : ";
- l1.display();
- cout<<"\n\nDequeue 3 elements from the Queue";
- l1.dequeue();
- l1.dequeue();
- l1.dequeue();
- cout<<"\n\nThe Contents of the Queue are : ";
- l1.display();
- return 0;
- }
- /*
- The Contents of the Queue are :
- 1 --> 2 --> 3 --> 4 --> 5 --> 6
- Dequeue 3 elements from the Queue
- The Contents of the Queue are :
- 4 --> 5 --> 6
- */
Add Comment
Please, Sign In to add comment