精华内容
下载资源
问答
  • /*有两个链表a和b,设结点中包含学号、姓名。从a链表中删去与b链表中有相同学号的那些结点*/ #include <stdio.h> #include <stdlib.h> typedef struct student{ long num; char name[10]; struct ...
    /*有两个链表a和b,设结点中包含学号、姓名。从a链表中删去与b链表中有相同学号的那些结点*/
    
    #include <stdio.h>
    #include <stdlib.h>
    typedef struct student{
    	long num;
    	char name[10];
    	struct student *next;
    }student,*Student;
    
    //创建链表 
    Student creat()
    {
        Student s;
    	int n = 0;
    	Student p1,p2;
    	s = p1 = p2 = (Student) malloc(sizeof(student));
    	
    	printf("请输入学生学号和姓名:\n");
    	scanf("%ld %s",&p1->num,&p1->name);
    	
    	while(p1->num != 0)
    	{
    		n = n + 1;
    		if(n == 1) s = p1;
    		else p2->next = p1;
    		p2 = p1;
    		p1 = (Student) malloc(sizeof(student));
    		scanf("%ld %s",&p1->num,&p1->name); 
    	}
    	p2->next = NULL;
    	
    	return s;
    } 
    
    //输出链表 
    void print(Student s)
    {
    	Student p;
    	printf("\nNow,The records are:\n");
    	p = s->next;
    	if(s != NULL)
    	{
    		do
    		{
    			printf("%ld %s\n",p->num,p->name);
    			p = p->next;
    		}while(p != NULL);
    	}
    } 
    
    void delect(Student a,Student b)
    {
    	Student p1,p2,q1,q2;
    	int flag = 0;
        p1 = a;
        q1 = b->next;
        
        while(q1)
        {
        	p1 = a;
        	while(p1->next != NULL)
        	{	
        	    flag = 0;
        	    p2 = p1->next;
        		if(p2->num == q1->num)
        		{
        			p1->next = p2->next;
        			flag = 1;
    			}
    			if(flag == 0)
    			p1 = p1->next;
    		}
    		q1 = q1->next;
    	}
     } 
     
    int main()
    {
    	Student a;
    	Student b;
    	a = (Student)malloc(sizeof(student));
    	b = (Student)malloc(sizeof(student));
    	
    	a->next = creat();
    	b->next = creat();
    	print(a);
    	print(b);
    	
    	delect(a,b);
    	print(a); 
    }
    

    谭浩强C程序设计(第四版)p330第11题
    为了方便删除,建立带头结点的链表,每次循环都是从a链表的头部寻找与b链表学号相同的结点,然后删除,直到b链表结束。

    展开全文
  • 有两个链表a和b,设结点中包含学号、姓名。从a链表中删去与b链表中有相同学号的那些结点。 方法:(链表统一用动态分配) #include <stdio.h> #include <stdlib.h> //声明结构体struct Student struct ...

    有两个链表a和b,设结点中包含学号、姓名。从a链表中删去与b链表中有相同学号的那些结点。

    方法一:(链表删除操作得动态初始化链表,其余操作动态初始化或静态初始化链表都行)

    #include <stdio.h>
    #include <stdlib.h>
    
    //声明结构体struct Student
    struct Student
    {                                       
        int num;
        char name[20];
    	struct Student *prev;
        struct Student *next;
    };
    
    struct Student *input(int n);//声明输入函数
    void append(struct Student **head2, struct Student *stu);//声明建立链表操作 
    void del_list(struct Student**, struct Student*);//声明删除两个链表中重复的多个节点函数
    struct Student* del_item(struct Student**, struct Student*);//声明删除单个节点函数
    void print(struct Student *);//声明输出函数操作 
    
    int main()
    {
    	struct Student *a,*b;
    	a=input(3);                                            //调用输入函数,生成a链表
    	print(a);
    	printf("\n");
    	b=input(2);                                            //调用输入函数,生成b链表
    	del_list(&a, b);                                     //从a中删除b
    	print(a);
        return 0;
    }
    
    //输入函数
    struct Student *input(int n)
    {
    	struct Student *head = NULL, *stu;
    	int i=0;
        for (i=1; i<=n; i++)//动态初始化链表 
    	{
    		stu	= (struct Student*)malloc(sizeof(struct Student));
            printf("Please enter No.%d student info: ", i);
            scanf("%d %s", &stu->num,stu->name);
    		append(&head, stu);
        }
    	return head;
    }
    
    //建立链表操作 
    void append(struct Student **head2, struct Student *stu) 
    {	
    	stu->next = NULL;	
    	if (*head2 == NULL) 
    	{
    		stu->prev = NULL;
    		*head2 = stu;
    	}
    	else 
    	{
    		struct Student *p = *head2;
    		while (p->next!=NULL)
    			p=p->next;
    		p->next = stu;
    		stu->prev = p;
    	}
    }
    
    //删除多个节点函数
    void del_list(struct Student** list1, struct Student* list2)
    {
    	struct Student* p, * q;
    
    	for (q = list2; q != NULL; q = q->next)
    	{
    		for (p = *list1; p != NULL; )
    		{
    			if (p->num == q->num)
    				p = del_item(list1, p);
    			else
    				p = p->next;
    		}
    	}
    }
    
    //删除单个节点函数
    struct Student* del_item(struct Student** head, struct Student* p)
    {
    	if (p == *head)
    		*head = p->next;
    	if (p->prev != NULL)
    		p->prev->next = p->next;
    	if (p->next != NULL)
    		p->next->prev = p->prev;
    	struct Student* next = p->next;
    	return next;
    }
    
    //输出函数
    void print(struct Student *stu)
    {
    	if(stu == NULL)
    		printf("Empty Linked list.\n");
    	else 
    	{
    		struct Student *p;
    		for (p=stu; p!=NULL; p=p->next) 
    		{
    			printf("%d %s\n", p->num, p->name);
    		}		
    	}
    }
    

    在VS2019下,需将源文件的scanf做些修改:

    #include <stdio.h>
    #include <stdlib.h>
    
    //声明结构体struct Student
    struct Student
    {
    	int num;
    	char name[20];
    	struct Student* prev;
    	struct Student* next;
    };
    
    struct Student* input(int n);//声明输入函数
    void append(struct Student** head2, struct Student* stu);//声明建立链表操作 
    void del_list(struct Student**, struct Student*);//声明删除两个链表中重复的多个节点函数
    struct Student* del_item(struct Student**, struct Student*);//声明删除单个节点函数
    void print(struct Student*);//声明输出函数操作 
    
    int main()
    {
    	struct Student* a, * b;
    	a = input(3);                                            //调用输入函数,生成a链表
    	print(a);
    	printf("\n");
    	b = input(2);                                            //调用输入函数,生成b链表
    	del_list(&a, b);                                     //从a中删除b
    	print(a);
    	return 0;
    }
    
    //输入函数
    struct Student* input(int n)
    {
    	struct Student* head = NULL, * stu;
    	int i = 0;
    	for (i = 1; i <= n; i++)//动态初始化链表 
    	{
    		stu = (struct Student*)malloc(sizeof(struct Student));
    		printf("Please enter No.%d student info: ", i);
    		scanf_s("%d %s", &stu->num, stu->name, (unsigned int)sizeof(stu->name));
    		append(&head, stu);
    	}
    	return head;
    }
    
    //建立链表操作 
    void append(struct Student** head2, struct Student* stu)
    {
    	stu->next = NULL;
    	if (*head2 == NULL)
    	{
    		stu->prev = NULL;
    		*head2 = stu;
    	}
    	else
    	{
    		struct Student* p = *head2;
    		while (p->next != NULL)
    			p = p->next;
    		p->next = stu;
    		stu->prev = p;
    	}
    }
    
    //删除多个节点函数
    void del_list(struct Student** list1, struct Student* list2)
    {
    	struct Student* p, * q;
    
    	for (q = list2; q != NULL; q = q->next)
    	{
    		for (p = *list1; p != NULL; )
    		{
    			if (p->num == q->num)
    				p = del_item(list1, p);
    			else
    				p = p->next;
    		}
    	}
    }
    
    //删除单个节点函数
    struct Student* del_item(struct Student** head, struct Student* p)
    {
    	if (p == *head)
    		*head = p->next;
    	if (p->prev != NULL)
    		p->prev->next = p->next;
    	if (p->next != NULL)
    		p->next->prev = p->prev;
    	struct Student* next = p->next;
    	return next;
    }
    
    //输出函数
    void print(struct Student* stu)
    {
    	if (stu == NULL)
    		printf("Empty Linked list.\n");
    	else
    	{
    		struct Student* p;
    		for (p = stu; p != NULL; p = p->next)
    		{
    			printf("%d %s\n", p->num, p->name);
    		}
    	}
    }
    

    方法一简化版:(简化input函数)

    #include <stdio.h>
    #include <stdlib.h>
    
    //声明结构体struct Student
    struct Student
    {                                       
        int num;
        char name[20];
    	struct Student *prev;
        struct Student *next;
    };
    
    struct Student *input(struct Student *stu, int n);//声明输入函数
    void del_list(struct Student**, struct Student*);//声明删除两个链表中重复的多个节点函数
    struct Student* del_item(struct Student**, struct Student*);//声明删除单个节点函数
    void print(struct Student *);//声明输出函数操作 
    
    int main()
    {
    	struct Student *a=(struct Student*)malloc(3*sizeof(struct Student));//相当于struct Student a[3]
    	struct Student *b=(struct Student*)malloc(2*sizeof(struct Student));//相当于struct Student b[3]
    	a=input(a, 3);                                            //调用输入函数,生成a链表
    	print(a);
    	printf("\n");
    	b=input(b, 2);                                            //调用输入函数,生成b链表
    	del_list(&a, b);                                     //从a中删除b
    	print(a);
    	free(a);
    	free(b);
        return 0;
    }
    
    //输入函数
    struct Student *input(struct Student *stu, int n)//先建立两个链表 
    {
    	int i=0;
    	struct Student *p;
    	for (p=stu; p<stu+n; p++, i++)
    	{
    		printf("Please enter No.%d student info; ", i+1);
    		scanf("%d %s", &p->num, p->name);
    		i==0?(p->prev=NULL):(p->prev=&stu[i-1]);
    		i==n-1 ? (p->next=NULL) : (p->next=&stu[i+1]);
    	}
    	return stu; 
    }
    
    //删除多个节点函数
    void del_list(struct Student** list1, struct Student* list2)
    {
    	struct Student* p, * q;
    
    	for (q = list2; q != NULL; q = q->next)
    	{
    		for (p = *list1; p != NULL; )
    		{
    			if (p->num == q->num)
    				p = del_item(list1, p);
    			else
    				p = p->next;
    		}
    	}
    }
    
    //删除单个节点函数
    struct Student* del_item(struct Student** head, struct Student* p)
    {
    	if (p == *head)
    		*head = p->next;
    	if (p->prev != NULL)
    		p->prev->next = p->next;
    	if (p->next != NULL)
    		p->next->prev = p->prev;
    	struct Student* next = p->next;
    	return next;
    }
    
    //输出函数
    void print(struct Student *stu)
    {
    	if(stu == NULL)
    		printf("Empty Linked list.\n");
    	else 
    	{
    		struct Student *p;
    		for (p=stu; p!=NULL; p=p->next) 
    		{
    			printf("%d %s\n", p->num, p->name);
    		}		
    	}
    }
    

    在VS2019下,需将源文件的scanf做些修改:

    #include <stdio.h>
    #include <stdlib.h>
    
    //声明结构体struct Student
    struct Student
    {
    	int num;
    	char name[20];
    	struct Student* prev;
    	struct Student* next;
    };
    
    struct Student* input(struct Student* stu, int n);//声明输入函数
    void del_list(struct Student**, struct Student*);//声明删除两个链表中重复的多个节点函数
    struct Student* del_item(struct Student**, struct Student*);//声明删除单个节点函数
    void print(struct Student*);//声明输出函数操作 
    
    int main()
    {
    	struct Student* a = (struct Student*)malloc(3 * sizeof(struct Student));//相当于struct Student a[3]
    	struct Student* b = (struct Student*)malloc(2 * sizeof(struct Student));//相当于struct Student b[3]
    	a = input(a, 3);                                            //调用输入函数,生成a链表
    	print(a);
    	printf("\n");
    	b = input(b, 2);                                            //调用输入函数,生成b链表
    	del_list(&a, b);                                     //从a中删除b
    	print(a);
    	free(a);
    	free(b);
    	return 0;
    }
    
    //输入函数
    struct Student* input(struct Student* stu, int n)//先建立两个链表 
    {
    	int i = 0;
    	struct Student* p;
    	for (p = stu; p < stu + n; p++, i++)
    	{
    		printf("Please enter No.%d student info; ", i + 1);
    		scanf_s("%d %s", &p->num, p->name, (unsigned int)sizeof(p->name));
    		i == 0 ? (p->prev = NULL) : (p->prev = &stu[i - 1]);
    		i == n - 1 ? (p->next = NULL) : (p->next = &stu[i + 1]);
    	}
    	return stu;
    }
    
    //删除多个节点函数
    void del_list(struct Student** list1, struct Student* list2)
    {
    	struct Student* p, * q;
    
    	for (q = list2; q != NULL; q = q->next)
    	{
    		for (p = *list1; p != NULL; )
    		{
    			if (p->num == q->num)
    				p = del_item(list1, p);
    			else
    				p = p->next;
    		}
    	}
    }
    
    //删除单个节点函数
    struct Student* del_item(struct Student** head, struct Student* p)
    {
    	if (p == *head)
    		*head = p->next;
    	if (p->prev != NULL)
    		p->prev->next = p->next;
    	if (p->next != NULL)
    		p->next->prev = p->prev;
    	struct Student* next = p->next;
    	return next;
    }
    
    //输出函数
    void print(struct Student* stu)
    {
    	if (stu == NULL)
    		printf("Empty Linked list.\n");
    	else
    	{
    		struct Student* p;
    		for (p = stu; p != NULL; p = p->next)
    		{
    			printf("%d %s\n", p->num, p->name);
    		}
    	}
    }
    
    展开全文
  • 有两个链表a和b,设结点中包含学号,姓名.从a链表中删去与b链表中有相同学号的那些节点.

    有两个链表a和b,设结点中包含学号,姓名.从a链表中删去与b链表中有相同学号的那些节点.

    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    #define N sizeof(stu)
    struct student {
    	int id;
    	char name[1024];
    	struct student* next;
    };
    typedef struct student stu;
    stu* creat(int n) {//创建
    	stu* head;
    	stu* s = (stu*)malloc(N);
    	head = s;
    	stu* p = head;
    	for (int i = 0; i < n; ++i) {
    		s = (stu*)malloc(N);
    		printf("请输入第%d个学生的信息\n", i + 1);
    		printf("请输入学号\n");
    		scanf("%d", &s->id);
    		printf("请输入姓名\n");
    		scanf("%s", &s->name);
    		s->next = p->next;
    		p->next = s;
    		p = p->next;
    		if (i == n - 1) {
    			p->next = NULL;
    		}
    	}
    	return head;
    }
    void print(stu* head) {//输出
    	int n = 0;
    	for (stu* p = head->next; p != NULL; p = p->next, n = 1) {
    		printf("%d\t%s\n", p->id, p->name);
    	}
    	if (n == 0) {
    		printf("链表为空\n");
    	}
    }
    void fun(stu* a, stu* b) {
    	stu* temp;
    	for (stu* p = a; p->next != NULL;) {
    		for (stu* q = b; q->next != NULL; q = q->next) {
    			if (p->next->id == q->next->id) {
    				temp = p->next;
    				p->next = temp->next;
    				free(temp);
    				break;
    			}
    			if (q->next->next == NULL) {
    				p = p->next;
    			}
    		}
    	}
    }
    void main() {
    	int n, m;
    	printf("请输入链表a的长度\n");
    	scanf("%d", &n);
    	printf("请输入链表b的长度\n");
    	scanf("%d", &m);
    	printf("请输入链表a中的学生信息\n");
    	stu* a = creat(n);
    	printf("请输入链表b中的学生信息\n");
    	stu* b = creat(m);
    	printf("链表a学生信息\n学号\t姓名\n");
    	print(a);
    	printf("链表b学生信息\n学号\t姓名\n");
    	print(b);
    	printf("删除相同学号后a链表为\n学号\t姓名\n");
    	fun(a, b);
    	print(a);
    	system("pause");
    }
    

    在这里插入图片描述在这里插入图片描述

    展开全文
  • //题目:有两个链表a和b,设结点中包含学号、姓名。从a链表中删去与b链表中有相同学号的那些结点。 #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #define LEN sizeof(struct student) struct...
    //C程序设计第四版(谭浩强)
    //章节:第九章 用户自己建立数据类型 
    //题号:9.11
    //题目:有两个链表a和b,设结点中包含学号、姓名。从a链表中删去与b链表中有相同学号的那些结点。
    #include <stdio.h>
    #include <stdlib.h>
    #define LEN sizeof(struct student)
    struct student
    {
    	long num;
    	char name[10];
    	struct student *next;
    };
    struct student *create()
    {
    	struct student *head=NULL,*p1,*p2;
    	int n=0;
    	p1=p2=(struct student *)malloc(LEN);
    	printf("input num,name(if num=0 stop):\n");
    	scanf("%ld %s",&p1->num,p1->name);
    	while(p1->num!=0)
    	{
    		n=n+1;
    		if(n==1)
    			head=p1;
    		else
    			p2->next=p1;
    		p2=p1;
    		p1=(struct student *)malloc(LEN);
    		scanf("%ld %s",&p1->num,p1->name);
    	}
    	p2->next=NULL;
    	return head;
    }
    struct student *search(struct student *head,int x)
    {
    	struct student *p=head;
    	while(p!=NULL)
    	{
    		if(p->num==x)
    			return p;
    		p=p->next;
    	}
    	return NULL;
    }
    struct student *del(struct student *head,int x)
    {
    	struct student *p1,*p2;
    	if(head==NULL)
    	{
    		printf("list is null!\n");
    		return NULL;
    	}
    	p1=head;
    	while(p1->num!=x&&p1->next!=NULL)
    	{
    		p2=p1;
    		p1=p1->next;
    	}
    	if(p1->num==x)
    	{
    		if(head==p1)
    			head=p1->next;
    		else
    			p2->next=p1->next;
    		printf("the student whose num is %ld is deleted.\n",p1->num);
    		free(p1);
    	}
    	else
    		printf("num %ld is not found!\n",x);
    	return head;
    }
    void print(struct student *head)
    {
    	struct student *p;
    	p=head;
    	printf("output list:\n");
    	while(p!=NULL)
    	{
    		printf("%ld\t%s\n",p->num,p->name);
    		p=p->next;
    	}
    }
    int main()
    {
    	struct student *head_a,*head_b,*p1,*p2;
    	int x;
    	printf("input list a:\n");
    	head_a=create();
    	printf("input list b:\n");
    	head_b=create();
    	p1=head_b;
    	while(p1!=NULL)
    	{
    		x=p1->num;
    		while((p2=search(head_a,x))!=NULL)
    		{
    			head_a=del(head_a,x);
    		}
    		p1=p1->next;
    	}
    	print(head_a);
    	return 0;
    }
    
    展开全文
  • 习题 9.11 有两个链表a和b,设结点中包含学号、姓名。从a链表中删去与b链表中有相同学号的那些结点。 代码块: #include &amp;lt;stdio.h&amp;gt; #include &amp;lt;stdlib.h&amp;gt; struct ...
  • 1,先创建两个链表 2,删去a链表中与b链表中相同学号的节点 3,打印处理后的a链表 处理方法: 1 :创建链表: 链表 = 头指针 + 节点 所以先创建节点 typedef struct node { int sid; char name[20]; struct ...
  • #include #include #define LEN sizeof(struct student) #define NULL 0 int n=0; struct student { int num; char name[20];...struct student *next;...struct student *creat(void) //创建学生结构体链表 { struct
  • \n请输入链表a的信息,学号小于零时结束输入:格式(学号 成绩)\n " );  a = create();  printf( " \n请输入链表b的信息,学号小于零时结束输入:格式(学号 成绩)\n " );  b = create();  system( " cls " ...
  • #include<stdio.h> #define LEN sizeof(struct student) #define NULL 0 int n; struct student {char num[10]; char name[10]; struct student *next; }*ahead,*bhead; main() ...}

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 711
精华内容 284
关键字:

有两个链表a和b