自组织线性表-互换位置法

自组织线性表-互换位置法,第1张

自组织线性表-互换位置法

#include
#include
#define IS_FULL(ptr)  (!(ptr))
typedef int T;
typedef struct node {
	T Key;
	T Data;
	struct node* link;
}Node;

Node* BuildList() {
	Node* p;
	Node* r = NULL;
	Node* first = NULL;
	int i;
	for (i = 0; i < 10; i++) {
		p = (Node*)malloc(sizeof(Node));
		p->Key = 2 * i + 1;
		p->Data = 2 * i + 2;
		p->link = NULL;
		if (first != NULL) {
			r->link = p;
		}
		else {
			first = p;
		}
		r = p;
	}
	return first;
}
Node* SearchList(Node* first, int k) {
	Node* p;
	Node* r;
	int x, y;
	int i = 1;  //一个bool标志位,指示first结点是否就是待找的结点。
	r = (Node*)malloc(sizeof(Node));
	p = (Node*)malloc(sizeof(Node));
	r->link = first;
	p = first;
	if (first->Key == k) {
		i = 0;  //考虑到first结点的值就是k的情况
	}
	while (p != NULL) {
		if (p->Key != k) {
			r = r->link;
			p = p->link;
		}
		else {
			printf("%dt%dn", p->Key, p->Data);
			if (i) {// p为找到key值后的结点,与它的前一结点r进行数值交换
				x=p->Key;
				y = p->Data;
				p->Key = r->Key;
				p->Data = r->Data;
				r->Key = x;
				r->Data = y;
			}
			break;
		}
	}
	if (p == NULL) {
		printf("Can't find it!n");
		exit(1);
	}
	else {
		return first;
	}

}
void PrintList(Node* first) {
	printf("n the list contains: n");
	for (; first; first = first->link) {
		printf("%dt%dn", first->Key, first->Data);
	}
	printf("nn");
}
void Clear(Node** first) {
	Node* p;
	p = *first;
	while (*first) {
		p = (*first)->link;
		free(*first);
		*first = p;
	}
}
void main() {
	Node* lst;
	lst = BuildList();
	PrintList(lst);
	lst = SearchList(lst, 9);
	PrintList(lst);
	lst = SearchList(lst, 13);
	PrintList(lst);
	lst = SearchList(lst, 11);
	PrintList(lst);
	lst = SearchList(lst, 11);
	PrintList(lst);
	lst = SearchList(lst, 11);
	PrintList(lst);
	lst = SearchList(lst, 11);
	PrintList(lst);
	lst = SearchList(lst, 11);
	PrintList(lst);
	lst = SearchList(lst, 11);
	PrintList(lst);
	lst = SearchList(lst, 11);
	PrintList(lst);

	lst = SearchList(lst, 11);
	PrintList(lst);
	//Clear(&lst);
	//PrintList(lst);
}

采用单链表结构
程序运行结果:

the list contains:
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20

9 10

the list contains:
1 2
3 4
5 6
9 10
7 8
11 12
13 14
15 16
17 18
19 20

13 14

the list contains:
1 2
3 4
5 6
9 10
7 8
13 14
11 12
15 16
17 18
19 20

11 12

the list contains:
1 2
3 4
5 6
9 10
7 8
11 12
13 14
15 16
17 18
19 20

11 12

the list contains:
1 2
3 4
5 6
9 10
11 12
7 8
13 14
15 16
17 18
19 20

11 12

the list contains:
1 2
3 4
5 6
11 12
9 10
7 8
13 14
15 16
17 18
19 20

11 12

the list contains:
1 2
3 4
11 12
5 6
9 10
7 8
13 14
15 16
17 18
19 20

11 12

the list contains:
1 2
11 12
3 4
5 6
9 10
7 8
13 14
15 16
17 18
19 20

11 12

the list contains:
11 12
1 2
3 4
5 6
9 10
7 8
13 14
15 16
17 18
19 20

11 12

the list contains:
11 12
1 2
3 4
5 6
9 10
7 8
13 14
15 16
17 18
19 20

11 12

the list contains:
11 12
1 2
3 4
5 6
9 10
7 8
13 14
15 16
17 18
19 20

C:C程序&C语言数据结构C语言数据结构_从入门到入坑7-15互换位置-自组织线性表Debug互换位置-自组织线性表.exe (进程 6192)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5670847.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存