TA的每日心情 | 开心 2014-6-18 08:29 |
---|
签到天数: 14 天 [LV.3]偶尔看看II
滴水大师
- 积分
- 2345
|
题目
解决代码及点评
- <pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"></pre><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code">/************************************************************************/
- /*
- 70. 有两个链表a和b。设结点中包含学号、姓名。
- 从a链表中删除与b链表中有相同学号的那些结点
-
- */
- /************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
-
- typedef struct student STU;
- struct student
- {
- int num;
- char name[10];
- struct student * next;
- };
- STU * Init()
- {
- STU * p=(STU *)malloc(sizeof(STU));
- if (p==NULL)
- {
- return NULL;
- }
- else
- p->next=NULL;
- return p;
- }
- void Insert(STU * head,int num,char * name)
- { STU * last=head;
- if (last==NULL)
- {
- return;
- }
- while(last->next!=NULL)
- last=last->next;
- STU *p=(STU *)malloc(sizeof(STU));
- if (p==NULL)
- {
- return;
- }
- else
- {
- p->num=num;
- strcpy_s(p->name,name);
- last->next=p;
- p->next=NULL;
-
- }
- }
- void DeleteNode(STU* pre,STU *cur)
- {
- pre->next=cur->next;
- free(cur);
- }
- void printfNodes(STU *head)
- {
- STU *p=head->next;
- while(p!=NULL)
- {
- printf("%5d",p->num);
- p=p->next;
- }
- printf("\n");
- }
- void main()
- {
- STU * A=Init();
- Insert(A,1,"abc");
- Insert(A,2,"abc");
- Insert(A,3,"abc");
- Insert(A,4,"abc");
- STU * B=Init();
- Insert(B,5,"abc");
- Insert(B,3,"abc");
- Insert(B,6,"abc");
- Insert(B,7,"abc");
- printfNodes(A);
- printfNodes(B);
- STU *p1=A;
- STU *p2=B;
- p2=p2->next;
- p1=p1->next;
- STU *p=A;
- while(p1!=NULL)
- { p2=B->next;
-
- while(p2!=NULL)
- {
- if (p1->num==p2->num)
- {
- DeleteNode(p,p1);
- p1=p->next;
- break;;
- }
- p2=p2->next;
- }
- p1=p1->next;
- p=p->next;
- }
- printfNodes(A);
- system("pause");
- }</pre><br><br><br></pre></pre></pre>
|
|