在c ++中使用链表进行队列排队

时间:2016-11-04 13:56:15

标签: list linked-list queue

我使用链表程序在队列中出现一个小错误。

程序成功执行但是当我按1进行插入时,语句“输入要插入的元素”重复并且不会返回主菜单..这里是代码:

#include <iostream>
#include<conio.h>
using namespace std;

template<class T>
class node
{
    public:
        node<T> *next;
        T data;

};
template<class T>
class queue
{
    private:
        node<T> *head,*tail;
    public:
        queue();
        void enqueue(T &x);
        void dequeue();
        void display();        
};
template<class T>
queue<T>::queue()
{
    head=tail=NULL;

}
template<class T>
void queue<T>::enqueue(T &x)
{

node<T> *n=new node<T>;
n->data=x;
n->next=NULL;

    if(tail==NULL)
    {
        head=tail=n;
        return;
    }
        tail->next=n;
        tail=n;


}
template<class T>
void queue<T>::dequeue()
{
    if(head==NULL)
    {
        cout<<"EMPTY";
    }
    if(head==tail)
    {
        head=tail=NULL;
    }

    head=head->next;

}
template<class T>
void queue<T>::display()
{
    if(head==NULL)
    {
        cout<<"Empty";
        return;
    }
    cout<<"Queue elements are";
node<T> *temp=head;
while(temp!=NULL)
{
    cout<<"  "<<temp->data;
    temp=temp->next;
}
}
main()
{
queue<int> q;
int choice,ele;
cout<<"Main Menu \n 1. Insert \n 2.Delete \n 3.Display \n 4.Exit \n Enter your choice \n";
cin>>choice;
do
{
    switch(choice)
    {
        case 1: 
        {
            cout<<"Enter Element to insert";
            cin>>ele;
            q.enqueue(ele);
            break;

        }
        case 2:
            {
                q.dequeue();
                break;

            }
        case 3:
            {
                q.display();
                break;
            }
            case 4: 
            {
                cout<<"End of program";
                break;
            }
    }

}while(choice!=4);


}

1 个答案:

答案 0 :(得分:0)

问题在于主要方法:

//angularjs2 service methods
     getHeroes(): Observable<Hero[]> {
            return this.http.get(this.heroesUrl)
                .map(res => res.json())
                .catch(this.handleError);
           
           
        }
        addHero(hero: Hero): Observable<Hero> {
         
            let headers = new Headers({ 'Content-Type': 'application/json' });
            var options = new RequestOptions({
                method: RequestMethod.Post,
                headers: headers,
                url: this.heroesUrl,
                body:hero
            });