1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > (四川大学出版社C语言程序设计第二版课后习题)两个链表 包括学号成绩 求交集(包

(四川大学出版社C语言程序设计第二版课后习题)两个链表 包括学号成绩 求交集(包

时间:2024-01-07 04:53:56

相关推荐

(四川大学出版社C语言程序设计第二版课后习题)两个链表 包括学号成绩 求交集(包

/*我的想法是创建一个新的链表存储交集或者是用其中一个链表删除不属于交集的部分新链表:很简单,就是比较两个链表,若是相同就创建一个链表来存储,依次链接,跟创建链表方法差不多删除结点:也就是当这个节点处不同同,我们就要将其上一个结点指向这个结点指向的地址,比如p->p1->p2,若是p1需要删除,就变成这样p->p2,这样链表中就将p1删除了,也就是在链接之外,在删除结点时注意,因为可能会删除头节点,但是要不同我们要去把这一个链表的元素与另一个链表的每个元素都比较一下,如果没有相同的就删除所以我们需要一个指向头指针的指针,也就是二重指针来进行删除结点的操作,注意这个删除要考虑到删除多个头节点的操作。(小白一个,也不知道理解的对不对,欢迎大家纠正)*///首先是创建一个新的链表去存储交集#include<stdio.h>#include<stdlib.h>#include<string.h>#define P 3//学生人数#define LEN sizeof(struct Student) //结构体长度,方便后面为其分配空间struct Student *Create(); //创建链表void Print(struct Student *head); //输出链表void Fang(struct Student **head); //释放链表struct Student *New(struct Student *head1,struct Student *head2);//新链表存储交集void Delet(struct Student **head1,struct Student *head2); //删除链表结点struct Student *Shan(struct Student *head); //删除链表中重复的元素struct Student{char xh[12];float cj;struct Student *next;};int main(){struct Student *head1,*head2,*head;head1=Create();head2=Create();head=New(head1,head2);printf("两个链表新链表交集如下:\n");Print(head);printf("两个链表删除结点交集如下:\n");Delet(&head1,head2);head1=Shan(head1);Print(head1);Fang(&head);Fang(&head1);Fang(&head2);return 0;}struct Student *Create(){struct Student *head=NULL,*p,*p1;;int i=0;while(i<P){p=(struct Student *)malloc(LEN);printf("请输入第%d位同学的学号,成绩,以空格间隔:\n",i+1);scanf("%s%f",p->xh ,&p->cj );p->next =NULL;i++;if(head==NULL){head=p;p1=p;}else{p1->next=p;p1=p;}}return head;}void Print(struct Student *head){struct Student *p;p=head;while(p!=NULL){printf("%s,%.1f\n",p->xh ,p->cj );p=p->next ;}}void Fang(struct Student **head){struct Student *p;while(*head!=NULL){p=*head;*head=(*head)->next ;free(p);}*head=NULL;}struct Student *New(struct Student *head1,struct Student *head2){struct Student *head=NULL,*p1,*p2,*p3,*p4;p1=head1;while(p1!=NULL){p2=head2;//注意每次循环都要让p2从头开始while(p2!=NULL) //因为需要p1与p2的每一个比较,所以需要两个循环{if(strcmp(p1->xh,p2->xh)==0&&p1->cj==p2->cj ){p3=head;p3=(struct Student *)malloc(LEN);strcpy(p3->xh ,p1->xh );p3->cj =p1->cj ;p3->next =NULL;if(head==NULL){head=p3;p4=p3;}else{p4->next=p3;p4=p3;}p3=p3->next ;break;} p2=p2->next ; }p1=p1->next ;}return head;}void Delet(struct Student **head1,struct Student *head2){struct Student *p,*p1,*p2;p2=head2;int state=0;//这里最开始我写错了,然后在写另一个程序的时候发现了,当时没有考虑删除头节点后万一下一个变成头节点也需要删除的情况//这里我用了一个循环,若是一直是头节点需要删除就一直删除,直到头节点不需要删除while(*head1!=NULL){p2=head2; //p2每次从头开始和他比较while(p2!=NULL){if(strcmp((*head1)->xh,p2->xh)==0&&(*head1)->cj==p2->cj ){state=1; //假如有相同的让他跳出标记为1break;} p2=p2->next ; //若是不相同继续比较}if(state==0){*head1=(*head1)->next ; //假如p2里没有和他相同的,删除}else if(state==1) //若是p2里有相同的表示不需要删除,也就是头节点保留下来了,我们就可以进行后续常规删除流程了break;}p1=*head1;// 让他俩都指向头头p=*head1;while(p!=NULL){p2=head2;//注意每次循环都要让p2从头开始state=0; //判断该节点是否删除while(p2!=NULL)//因为需要p1与p2的每一个比较,所以需要两个循环{if(strcmp(p->xh,p2->xh)==0||p->cj==p2->cj){state=1;break; //当发现已经有相同的就跳出,说明这个不需要删除}p2=p2->next ; //没有符合条件的就继续循环直到链表结束}if(state==0) //假如state==0,也就是说循环到底都没有相同的,那就需要删除它p1->next =p->next ;else//否则就把他交给p1,然后继续去比较p1=p;p=p->next ; //这里可以这么理解,我要去找下一个是不是我要找的人,,但是我不能找了因为不是就把他抛弃了乱放了//,所以我就把这个托付给你去安置好,,如果下一个是我要找的,我就拿出他,将他的下一个挨着你的放,因为我太着急了,没有你我就不知道拿出来怎么接上}}struct Student *Shan(struct Student *head) //这个函数是后来加上去的,删除开始两个链表中相同的但是重复的元素{struct Student *NEW,*n,*n1,*temp;NEW=(struct Student *)malloc(LEN);NEW->next =head;n1=NEW;n=head;int state=0;while(n!=NULL){temp=n->next ;while(temp!=NULL){if(strcmp(n->xh ,temp->xh )==0){state=1;break;}temp=temp->next ;}if(state==1){state=0;n=n->next ;n1->next =n;}else{n1=n1->next ;n=n->next ;}}return NEW->next;}

(四川大学出版社C语言程序设计第二版课后习题)两个链表 包括学号成绩 求交集(包含删除不同结点的方法)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。