c_cpp 链接列表的线性队列实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 链接列表的线性队列实现相关的知识,希望对你有一定的参考价值。

#include <bits/stdc++.h>
using namespace std;

// #Queue #LinkedList #BasicProblem
//Enqueue at tail of ll
//address of tail is store in a (node* tail) variable to avoid traversing everytime
//Dequeuing is easy to perform at the head

class node{
    public:
    int data;
    node* next;
    node(){ //constructor
        data=0;
        next=NULL;
    }
    node(int x){    //constructor with data
        data=x;
        next=NULL;
    }
};

class Queue{
    public:
    node* head;
    node* tail;
    Queue(){    //constructor
        head=NULL;
        tail=NULL; //initializing tail as NULL
    }
    bool isEmpty(); //checks if the ll is empty
    node* getLastNode(); //returns address of the last node of ll
    void Enq(int x); //adds an element at the end of ll
    bool Deq(int &removed); //deletes head node and tells if deletion is successfully performed
};
bool Queue :: isEmpty(){
    if(head==NULL){
        return true;
    }
    return false;
}
node* Queue :: getLastNode(){
    if(head==NULL){
        return head;
    }else{
        node* temp=head;
        while(temp->next!=NULL){
            temp=temp->next;
        }
        return temp;
    }
}
void Queue :: Enq(int x){
    node* n1=new node(x);
    if(head==NULL){
        head=n1;
    }else{
        if(tail==NULL){     //Initial condition
            node* last=getLastNode();
            tail=last;
        }
        tail->next=n1;  //Key part to avoid traversing everytime
        tail=n1;
    }
    //delete(n1); // to delete n1 from memory [DON'T DELETE when ADDing a node]
}
bool Queue :: Deq(int &removed){
    if(head==NULL){
        return false;
    }else{
        removed=head->data;
        node* temp=head;
        head=temp->next;
        delete(temp); // to delete the previous head node from memory
        return true;
    }
}
int main() {
    cout<<"Instructions: \n";
    cout<<"Type add to Enqueue"<<endl;
    cout<<"Type del to Dequeue"<<endl;
    cout<<"Type front to check front element"<<endl;
    cout<<"Type exit to stop using the Queue"<<endl;
    Queue Q;
    int removed; // variable to access the element dequeued
    while(1){
        string instruction;
        cout<<"Instruction: ";
        cin>>instruction;
        if(instruction=="exit"){
            break;
        }else if(instruction=="add"){
            cout<<"Enter the element top be enqueued"<<endl;
            int push; //element to be enqueued
            cin>>push;
            Q.Enq(push);
            cout<<"Element "<<push<<" successfully enqueued"<<endl;
        }else if(instruction=="del"){
            if(Q.Deq(removed)==true){
                cout<<"Element "<<removed<<" successfully Dequeued"<<endl;
                if(Q.isEmpty()==false){
                        cout<<"Front Element is:"<<Q.head->data<<endl;
                }else{
                    cout<<"Queue is now Empty!"<<endl;
                }
            }else{
                cout<<"ERROR : Queue is empty!"<<endl;
            }
        }else if(instruction=="front"){
            if(Q.isEmpty()==false){
                        cout<<"Front Element is:"<<Q.head->data<<endl;
            }else{
                cout<<"Queue is now Empty!"<<endl;
            }
        }else{
            cout<<"ERROR : Unknown operation! Please try again"<<endl;
        }
    }
    return 0;
}

以上是关于c_cpp 链接列表的线性队列实现的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 使用数组实现线性队列

c_cpp 链接列表实现与类

c_cpp 使用类链接列表实现

c_cpp 使用Struct实现链接列表

c_cpp 使用类和链接列表实现堆栈

数据结构_线性机构(队列)