#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;
}
這是我寫的代碼,請看主函數,如何使用了前插法後再用尾插法,我寫的代碼尾插法總是把前插法覆蓋了 !!!!求大神解答,感激不盡!!
void headinsert(const StudentNode&stu)
{
StudentNode*t=new StudentNode(stu);
t->next=head;
head=t;
if(tail == NULL)
tail = t;
}