编写c++链表

[复制链接]
查看11 | 回复0 | 2009-11-29 14:59:45 | 显示全部楼层 |阅读模式
#include#include#define NULL 0using namespace std;struct date{ int year; int month; int day;};struct student{ string name; date birthday; string sex; float score[5]; long num; string political; student *next;};int main(){ student *head_creat(void); student *insert(student *head,student *stud); student *del(student *head,long num); student *print(student*); void menu(); student *head; student *stud; long num; menu(); int n;while(n!=0) {cin>>n; switch(n) { case 1:
head=head_creat();menu();break;case 2:
print(head);menu();break; case 3:
cout>stud->num; cout>stud->name; cout>stud->political; cout>stud->sex; cout>stud->score[0]; cin>>stud->score[1]; cin>>stud->score[2]; cin>>stud->score[3]; cin>>stud->score[4]; cout>stud->birthday.year>>stud->birthday.month>>stud->birthday.day;
insert(head,stud);
menu();break; case 4:
cout>num;
del(head,num);break; default: break; } }return 0;} student *print(student*head){ student *p; p=head; while(p!=0) {
coutbirthday.yearbirthday.monthbirthday.daynamenumpoliticalsexscore[j]next; } cout>p1->num; cout>p1->name; cout>p1->political; cout>p1->sex; cout>p1->score[0]; cin>>p1->score[1]; cin>>p1->score[2]; cin>>p1->score[3]; cin>>p1->score[4]; cout>p1->birthday.year>>p1->birthday.month>>p1->birthday.day; int n=0; while(p1->num!=0) {n=n+1;head=p1;p1->next=p2;if(n==1){ student *p; p=p2; p->next=NULL;}elsep2=p1;p1=new student;cout>p1->num; cout>p1->name; cout>p1->political; cout>p1->sex; cout>p1->score; } cout>p1->birthday.year>>p1->birthday.month>>p1->birthday.day; }return(head);}student *insert(student *head,student *stud){ student *p0,*p1,*p2; p1=head; p0=stud; if(head==NULL)//如果原链表为空 {head=p0;head->next=NULL; } else {while((p1->next!=NULL)&&(p0->num>p1->num)){ p2=p1; p1=p1->next;}if(p0->numnum){ if(head==p1)
head=p0; else
p2->next=p0; p0->next=p1;}else{ p1->next=p0; p0->next=NULL;}
} return(head);}student *del(student *head,long num){ student *p1,*p2; p1=head; if(head==NULL) {coutnum&&p1->next!=NULL) {p2=p1;p1=p1->next; } if(num==p1->num) {if(p1==head){ head=NULL; coutnext=p1->next; coutusing namespace std;templateclass LinList;templateclass CirList;template class ListNode{ //friend void Concatenate(LinList &A, LinList &B); friend class LinList; friend class CirList; //friend void Concatenate(LinList &A, LinList &B);private: ListNode *next;public: // friend void Concatenate(LinList &A, LinList &B); ListNode* &Next() {return next; } T data; ListNode(ListNode *ptrNext=NULL); ListNode(const T& item,ListNode *ptrNext=NULL); ~ListNode(void){};//这个能删除吗};template ListNode::ListNode(ListNode *ptrNext):next(ptrNext){}template ListNode::ListNode(const T&item,ListNode *ptrNext){ data=item; next=ptrNext;}template class LinList{private: ListNode *head; int size; ListNode *currPtr;public: LinList(void); ~LinList(void); void changesize(int n) {size=n; } int ListSize(void) const; ListNode *Index(int pos); void Insert(const T& item,int pos); T Delete(int pos); T GetData(int pos); int ListEmpty(void)const; void ClearList(void); ListNode *Reset(int pos=0); ListNode* &Next(void); int EndOfList(void)const;};template LinList::LinList(){ head=new ListNode(); size =0;}template LinList::~LinList(){ ClearList(); // delete head;还没有把头结点删除;}template int LinList::ListSize(void) const{ return size;}template int LinList::ListEmpty(void)const{ if(sizeListNode *LinList::Index(int pos){ if(possize) {cout *p=head->next; int i=0; while(p!=NULL&&inext;i++; } return p;}template void LinList::Insert(const T& item,int pos){ if(possize) {cout *p=Index(pos-1); ListNode *newNode=new ListNode(item,p->next); p->next=newNode; size++;}template T LinList::Delete(int pos){ if(possize-1) {cout *q,*p=Index(pos-1); q=p->next; p->next=p->next->next; T data=q->data; delete q; size--; return data;}template T LinList::GetData(int pos){ if(possize-1) {cout *p=Index(pos); return p->data;}template void LinList::ClearList(void){ ListNode *p,*p1; p=head->next; while(p!=NULL) {p1=p;p=p->next;delete p1; } size=0;}template ListNode *LinList::Reset(int pos){ if(head==NULL)return NULL; if(pos=size) {coutnext; else {currPtr=head->next;ListNode prevPtr=head;for(int i=0;inext;} } return currPtr;}template ListNode* &LinList::Next(void){ if(currPtr!=NULL)currPtr=currPtr->next; return currPtr;}template int LinList::EndOfList(void)const{ if(currPtr==NULL)return 1; else return 0;}template void Concatenate(LinList &A, LinList &B){ ListNode *p1=A.Index(A.ListSize()-1); ListNode *p2=B.Index(-1); A.changesize(B.ListSize()+A.ListSize()); p1->Next()=p2->Next(); delete p2; B.changesize(0); p2->Next()=NULL;//怎样处理B;先将它脱链;再删除头结点;再将头结点NEXT置空; //delete p2; //ListNode *p2=B.Index(B.ListSize()-1);}struct Score{ int chinese; int english; int math; int history; int physics;};struct Date{ int day; int month; int year;};struct Student{ string name; char sex; Date date; string faceStr; Score score;};istream& operator>>(istream& input,Student &stu){ cout>stu.name; cout>stu.date.year; cout>stu.date.month; cout>stu.date.day; cout>stu.sex; cout>stu.faceStr; cout>stu.score.chinese; cout>stu.score.english; cout>stu.score.math; cout>stu.score.history; cout>stu.score.physics; return input;}ostream& operator A; Student stu; cout>n; for(int i=0;i>stu;A.Insert(stu,i); } cout<<endl; for(int i=0;i<n;i++) {Student temp;temp=A.Delete(0);cout<<temp; cout<<endl; } cout<<endl; return 0;}唉,整了这么久不给分就没有天理了。。。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行