数据结构:使用指针

时间:2018-03-19 21:10:27

标签: c++ pointers data-structures queue

我尝试使用指针实现c ++队列列表,但是我遇到了一些错误:

#include <iostream>
#include <stdlib.h>
#include <ctype.h>
#include <string>
using namespace std;

struct node{
int age;
string firstname;
string lastname;
node *next;
};
typedef node*pqueue;
struct queue{
pqueue head;
pqueue tail;
};

初始化队列

void init(queue &q){
    (q->head) = (q->tail)=NULL;
}

从尾部插入数据

void insert(queue &q,int x,string fn,string ln){
    if((q->head=0)&&(q->tail=0)){
        q->head=new node;
        (q->head)->age=x;
        (q->head)->firstname=fn;
        (q->head)->lastname=ln;
        q->tail=q->head;
    }else{
        pqueue temp = new node;
        temp->age=x;
        temp->firstname=fn;
        temp->lastname=ln;
        temp->next=q->tail;
        q->tail=temp;
    }

}

删除或弹出功能

void remove(queue &q){
if((q->head=0)&&(q->tail=0))
    cout<<"the queue is empty nothing to remove .....!"<<endl;
else{
    pqueue temp = q->head;
    q->head=(q->head)->next;
    delete q->head;
}}
void forward(queue &q){
 pqueue temp =q->head;
while(temp != q->tail)
    {
    cout<<"age          :"<<temp->age<<endl;
    cout<<"first name   :"<<temp->firstname<<endl;
    cout<<"last  name   :"<<temp->lastname<<endl;
    cout<<endl;
    temp=temp->next;
}
}
void backward(queue &q){
pqueue temp =q->head;
if(temp->next != q->tail)
    backward(temp->next);
    cout<<"age          :"<<temp->age<<endl;
    cout<<"first name   :"<<temp->firstname<<endl;
    cout<<"last  name   :"<<temp->lastname<<endl;
}

主要代码......................

int main()
{
    queue q;
    string firstname;
    string lastname;
    int choice = 0,age;
    cout<<"********************LIST-QUEUE IMPLEMENTATION********************"<<endl;
    while(choice!=6)
    {
        cout<<endl<<"Choose an Option : \n 1.Initialization \n 2.Push \n 3.Pop \n 4.Traverse Forward \n 5.Traverse Backward \n 6.Quit \n ==> ";
        cin>>choice;
        switch(choice){
        case 1:
        init(q);
        break;
        case 2:
        cout<<"enter the first name : ";
        cin>>firstname;
        cout<<"enter the last name  : ";
        cin>>lastname;
        cout<<"enter the age        : ";
        cin>>age;
        insert(q,age,firstname,lastname);
        break;
        case 3:
            remove(q);
        break;
        case 4:
            forward(q);
        break;
        case 5:
            backward(q);
        break;
        case 6:
        break;
        default:
            cout<<" Not available.......! check again ..... [1...6] : ";
        }
    }
    return 0;
}
我得到的一个问题是 错误基本操作数' - &gt;'有非指针类型'队列' 似乎我对每个功能都有这个问题 拜托,我需要一些帮助.........

0 个答案:

没有答案