开发者社区> 问答> 正文

C++链表类前插法与尾插法问题

#include
using namespace std;
class StudentRecord
{
public:
string stuName;
int stuNo;
StudentRecord(string s,int n)
{
stuName=s;
stuNo=n;
}
void print()
{
cout<<"Name:"< }
};
class StudentNode
{
public:
StudentRecord data;
StudentNode *next;
StudentNode(const StudentRecord&stu,StudentNode *pNext=NULL):data(stu),next(pNext) {};
};
class LinkedList
{
public:
StudentNode * head;
StudentNode * tail;
LinkedList():head(0),tail(0) {};
void headinsert(const StudentNode&stu)
{
StudentNode*t=new StudentNode(stu);
t->next=head;
head=t;
}
void headdelete()
{
StudentNode*t=head;
head=head->next;
delete t;
}
void backinsert(const StudentNode&stu)
{
StudentNode*t=new StudentNode(stu);
    if(tail!=0)
        tail->next=t;
    else
        head=t;
    tail=t;

}
void traverse()
{
    StudentNode*t=head;
    while(t!=0)
    {
        t->data.print();
        t=t->next;

    }
}
private:
};
int main()
{
StudentRecord b("td",4),c("sb",4),d("da",4);
LinkedList a;
a.headinsert(d);
a.backinsert(c);
a.traverse();
return 0;
}

这是我写的代码,请看主函数,如何使用了前插法后再用尾插法,我写的代码尾插法总是把前插法覆盖了 !!!!求大神解答,感激不尽!!

展开
收起
a123456678 2016-03-23 13:27:54 1989 0
1 条回答
写回答
取消 提交回答
  • void headinsert(const StudentNode&stu)
    {
        StudentNode*t=new StudentNode(stu);
        t->next=head;
        head=t;
        if(tail == NULL)
            tail = t;
    }
    2019-07-17 19:10:45
    赞同 展开评论 打赏
问答分类:
C++
问答地址:
问答排行榜
最热
最新

相关电子书

更多
使用C++11开发PHP7扩展 立即下载
GPON Class C++ SFP O;T Transce 立即下载
GPON Class C++ SFP OLT Transce 立即下载