有兩個鏈表a和b,設結點中包含學號、姓名。從a鏈表中刪去與b鏈表中有相同學號的那些結點


有兩個鏈表a和b,設結點中包含學號、姓名。從a鏈表中刪去與b鏈表中有相同學號的那些結點

解題思路:

對於b鏈表中的每一個節點,都從a鏈表的表頭開始查找,如果可以找到,直接刪除,如果找不到,繼續從a鏈表表頭找下一個b的節點。

#include <stdio.h>
typedef struct student
{
	int num;
	double grade;
	struct student *next;
} student;
student *del(student *a, student *b)
{
	student *pre, *current, *head;
	head = a;

	while (b != NULL)
	{
		//重置指針指向a鏈表的頭部
		pre = head;
		current = head->next;
		//a 鏈表的頭等於b
		if (pre->num == b->num)
		{
			pre->next = NULL;
			pre = current;
			current = current->next;
			//更新表頭
			head = pre;
		}
		else
		{
			while (pre->next != NULL)
			{
				if (current->num == b->num)
				{
					//找到就刪除
					pre->next = current->next;
					break;
				}
				else
				{
					//否則繼續遍歷
					pre = pre->next;
					current = current->next;
				}
			}
		}
		b = b->next;
	}
	return head;
}

void printList(student *root)
{
	printf("----\n");
	int i = 0;
	while (root != NULL)
	{
		printf("student %d -> %d -> %.2lf\n", i, root->num, root->grade);
		root = root->next;
		i++;
	}
}

int main()
{
	student a[3] = { { 1, 79 }, { 4, 36 }, { 5, 79 } };
	for (int i = 0; i < 2; i++)
	{
		a[i].next = &a[i + 1];
	}
	a[2].next = NULL;
	printf("鏈表a:\n");
	printList(&a[0]);

	student b[2] = { { 5, 38 }, { 4, 98 } };
	for (int i = 0; i < 1; i++)
	{
		b[i].next = &b[i + 1];
	}
	b[1].next = NULL;
	printf("鏈表b:\n");
	printList(&b[0]);
	student *combine = del(a, b);
	printf("刪除之后:\n");
	while (combine != NULL)
	{
		printf("%d -> %.2lf\n", combine->num, combine->grade);
		combine = combine->next;
	}

	return 0;
}

運行截圖:
有兩個鏈表a和b,設結點中包含學號、姓名。從a鏈表中刪去與b鏈表中有相同學號的那些結點


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM