链接列表:存储的元素未完全显示

时间:2018-09-02 07:23:16

标签: stored-procedures linked-list element display

我试图以链接方式存储5个元素,输入代码工作正常,但显示代码未显示第5个元素“ RollNumber”。 我找不到错误的地方。

class Node
{
    friend class LinkedList;
    private:
        string BookName;
        string BookNo;
        string Price;
        string StudentName;
        string RollNumber;
        Node *next;
    public:
        Node(string bname = "", string bno = "", string pr = "", string sname = "", string rn = "", Node *nxt = NULL)
        {
            BookName=bname;
            BookNo=bno;
            Price=pr;
            StudentName=sname;
            RollNumber:rn;
            next=nxt;
        }
};

class LinkedList
{
    private:
        Node *head;
        Node *tail;
        int count;
    public:
        LinkedList()
        {
            head = NULL;
            tail = NULL;
            count = 0;
        }
    bool isEmpty()
        {
            if (head == NULL)
            {
                return true;
            }
            return false;
        }

    void add_input_head(string bname = "", string bno="", string pr="", string sname="", string rn="")
    {
        if (isEmpty())
        {
            head = new Node(bname, bno, pr, sname, rn, NULL);
            tail = head;
            count++;
        }
        else
        {
            Node *p = new Node(bname, bno, pr, sname, rn, head);
            head = p;
            count++;
        }
    }

    void traverse() 
    {
        Node *t = head;
        for (int i = 1; i <= count; i++) {
            cout <<endl<<endl<<endl<<endl<<"Book Name: "<< t->BookName << endl;
            cout <<endl<<endl<<"Book No: "<< t->BookNo << endl;
            cout <<endl<<endl<<"Price: "<< t->Price << endl;
            cout <<endl<<endl<<"Stuent Name: "<< t->StudentName << endl;
            cout <<endl<<endl<<"Roll No: "<< t->RollNumber << endl;
            t = t->next;
        }
    }

    void Exit()
    {
    cout<<"\t\t\tThank You for using this Program";
    }
    //Destructor for Linked List
    ~LinkedList()
    {
        Node *t;
        while (head != NULL)
        {
            t = head;
            head = head->next;
            delete t;
        }
        head = NULL;
        count = 0;
    }

};

int main()
{
    LinkedList l;
    int ch;
    string a,b,c,d,e;
do
{

    cout<<"\n\t\t\t  Press 1 for Addition";
    cout<<"\n\t\t\t  Press 2 for Display";
    cout<<"\n\t\t\t  Press 3 for Exit";
    cout<<"\n\t\t\t  Enter Option=";
    cin>>ch;
    switch(ch)
    {
        case 1:
        cout<<endl<<endl<<endl<<endl<<"Enter Book Name: ";
        cin>>a; 
        cout<<endl<<"Enter Book No: ";
        cin>>b;
        cout<<endl<<"Enter Price: ";
        cin>>c;
        cout<<endl<<"Enter Student Name: ";
        cin>>d;
        cout<<endl<<"Enter Roll No: ";
        cin>>e;
        l.add_input_head(a,b,c,d,e);
        break;
        case 2:
        l.traverse();
        case 3:
        l.Exit();
        break;
    }   
}while(ch!=6);  
}

编译时没有错误,但是仍然错过最后一个元素。我不知道错误是在输入还是输出函数中。 我正在做一个课堂项目,但无法解决此问题。 有人可以帮我发现错误吗?

0 个答案:

没有答案