我是靠谱客的博主 包容烤鸡,这篇文章主要介绍数据结构算法设计题汇总(2),现在分享给大家,希望可以做个参考。

一.向一个已经排好序的链表中插入新元素并保持有序

A:我的思路是,定义p作为游标在链表上移动,直到某个节点的数据域大于待插入元素为止,代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
/*保持递增的顺序插入新元素*/ status insertinorder(linklist l,elemtype e){ linklist p=l->next,q; /*让p在链表上移动,直到找到一个节点的数据域大于e为止*/ while(p&&p->next->data<e) p=p->next; q=(linklist)malloc(sizeof(node)); if(!q) return ERROR; q->data=e; q->next=p->next; p->next=q; }

二:合并两个已排序的递增单链表,不改变其有序性;

A:我的思路是定义新链表L3,先对两链表首节点进行判断,找出较小的节点作为新链表的首节点,然后利用p,q两个游标在两个链表上移动,找出较小值插入新链表尾部,之后p,q向后移动即可,代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
status mergelinklist(linklist l1,linklist l2,linklist l3){ linklist p=l1->next,q=l2->next,r=l3,head; /*先确定较小的值作为首节点*/ if(p->data>q->data) head=q; else head=p; l3->next=head; while(p&&q){ /*找较小元素并插入新链表尾部*/ if(p->data<=q->data){ r->next=p; r=p; p=p->next; } else{ r->next=q; r=q; q=q->next; } } while(p){ r->next=p; r=p; p=p->next; } while(q){ r->next=q; r=q; q=q->next; } }

三.假设长度大于1的循环单链表中,既无头节点也无头指针,p为指向该链表中某一节点的指针,删除该节点前驱节点

四.已知两个单链表A,B分别表示两个集合,其元素递增排列,求A,B交集C,要求C以递增序列形式存储

A:我的思路是,从B中取元素,利用getelem()操作,之后使用locateelem()操作,如果B中元素也在A中,则插入到链表C中即可,代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*链表中插入元素使其仍然有序*/ #include<stdio.h> #include<stdlib.h> #include<time.h> #define ERROR 0 #define OK 1 #define TRUE 1 #define FALSE 0 typedef int status; typedef int elemtype; //创建链表 struct node { elemtype data; struct node *next; }node; typedef struct node* linklist; //访问链表中的元素 status visit(elemtype c) { printf("%d ",c); return OK; } status initlist(linklist *l){ *l=(linklist)malloc(sizeof(node)); if(!(*l)){ return ERROR; } (*l)->next=NULL; } /*尾插法创建链表*/ status createlisttail(linklist l,int start,int end){ linklist p=l,q; int i=0; for(i=start;i<end;i++){ q=(linklist)malloc(sizeof(node)); q->data=i; p->next=q; p=q; } p->next=NULL; } //依次输出链表中的各个元素 status traverselist(linklist l) { linklist p=l->next; while(p) { visit(p->data); p=p->next; } printf("n"); return OK; } status getelem(linklist l,int n,elemtype *data){ int i=1; linklist p=l->next; while(p&&i<n){ p=p->next; i++; } *data=p->data; } /*判断e是否存在于链表中*/ status locatelist(linklist l,elemtype e){ linklist p=l->next; while(p){ if(e==p->data) return TRUE; else p=p->next; } return FALSE; } /*求单链表长*/ status listlength(linklist l) { int count=0; linklist p=l->next; while(p) { count++; p=p->next; } return count; } /*求两链表交集*/ /*求两集合交集*/ status intersection(linklist l1,linklist l2,linklist l3){ linklist head=l1->next,p=l3,q; int i=1,n=listlength(l1); elemtype data; for(i=1;i<=n;i++){ getelem(l1,i,&data); if(locatelist(l2,data)){ q=(linklist)malloc(sizeof(node)); q->data=data; p->next=q; p=q; } } p->next=NULL; } int main(void){ linklist l,l2,l3; int i=0; elemtype data=0; initlist(&l); initlist(&l2); initlist(&l3); createlisttail(l,0,10); traverselist(l); createlisttail(l2,5,15); traverselist(l2); intersection(l,l2,l3); traverselist(l3); }

最后

以上就是包容烤鸡最近收集整理的关于数据结构算法设计题汇总(2)的全部内容,更多相关数据结构算法设计题汇总(2)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(93)

评论列表共有 0 条评论

立即
投稿
返回
顶部