liuyawen44 2015-06-26 15:48 采纳率: 37.5%
浏览 3058
已采纳

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;

}

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

  • 写回答

2条回答 默认 最新

  • zhjandcs 2015-06-27 01:43
    关注
    void headinsert(const StudentNode&stu)
    {
        StudentNode*t=new StudentNode(stu);
        t->next=head;
        head=t;
        if(tail == NULL)
            tail = t;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献