搜索
首页 教育/科学 学习帮助

谁能给一段动态链表的c++代码

要完整的插入,删除,查找。。。

全部回答

2009-07-30

0 0
    #include #include #define LEN sizeof (struct student) struct student { int num; float score; struct student *next; }; int n;/* n是节点个数 */ struct student *creat(); /* 创建链表 */ struct student *del(struct student *head, int del_num);/* 删除 */ void print(struct student *head); /* 打印 */ struct student *insert(struct student *head,struct student *stu); main() { struct student *head, *stu; int del_num; head = creat(); print(head); printf("del_num:"); scanf("%d",&del_num); while(del_num != 0) { head=del(head,del_num); print(head); printf("del_num:"); scanf("%d",&del_num); } printf("input:"); stu = (struct student* ) malloc (LEN); scanf("%d,%f",&stu->num,&stu->score); while(stu->num != 0) { head=insert(head,stu); print(head); printf("input:"); stu = (struct student* ) malloc (LEN); scanf("%d,%f",&stu->num,&stu->score); } } struct student *creat()/*函数返回一个指针值,*/ { struct student *head,*p1,*p2; n = 0; p1 =p2 =(struct student* ) malloc (LEN);/*开辟1个长度为LEN的内存*/ printf("input records:\n"); scanf("%d,%f",&p1->num,&p1->score); head=NULL; while(p1->num != 0) { n++; if(n == 1) head= p2= p1;/*把p1所指的结点作为第一个结点*/ else { p2->next = p1;/*把p1所指的结点连接表尾*/ p2 = p2->next; } p1 = (struct student* ) malloc (LEN);/*开辟新结点,使p1指向它*/ scanf("%d,%f",&p1->num,&p1->score); p1->next = NULL; } return head; } void print(struct student *head) { struct student *p; p=head; printf("output:\n"); if(head != NULL) do { printf("%d,%5。
    1f\n",p->num,p->score); p = p->next; /* 将p原来所指的结点中next的值赋予p */ }while(p != NULL); } struct student *del(struct student *head, int del_num) { struct student *p1,*p2; if(head == NULL) printf("\nlist null!!!\n"); /* 空表的情况 */ else { p1 = head; while(del_num != p1->num && p1->next != NULL) { p2 = p1; p1 = p1->next; /* p1后移1个结点 */ } if(del_num == p1->num) /* 找到 */ { if(p1 == head) /* 若p1指向的是首结点 */ head = p1->next; /* 把第二个结点的地址赋予head */ else p2->next = p1->next; /*将下一结点地址富裕前一结点地址 */ } else printf("\n not found!!!\n"); } return(head); } struct student *insert(struct student *head,struct student *stu) { struct student *p0,*p1,*p2; p1 = head; p0 = stu; if(head == NULL) { head = p0; p0->next = NULL; } else { while( (p0->num > p1->num) && (p1->next != NULL) ) { p2 = p1; p1 = p1->next; if(p0->num num) { if(head == p1) head = p0; else p2->next = p0; p0->next = p1; } else { p1->next = p0; p0->next = NULL; } } } return(head); } 。
    。

类似问题换一批

热点推荐

热度TOP

相关推荐
加载中...

热点搜索 换一换

教育/科学
学习帮助
院校信息
升学入学
理工学科
出国/留学
职业教育
人文学科
外语学习
K12
学习帮助
学习帮助
举报
举报原因(必选):
取消确定举报