1、编程实现:

(1)在顺序表ajcniydu的第三个位置插入p。

首先构建顺序表arr1[10]

0 1 2 3 4 5 6 7
a j c n i y d u

找到第三个位置

0 1 2 3 4 5 6 7
a j c n i y d u

从arr[2]开始每个元素往后移一位

0 1 2 3 4 5 6 7 8
a j c c n i y d u

在arr[2]处插入p

0 1 2 3 4 5 6 7 8
a j p c n i y d u

附代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//代码过于简单,不做解释。
#include <iostream>
using namespace std;
int main() {
char arr1[10] = { 'a','j','c','n','i','y','d','u' };
cout << "插入前:";
for (int i = 0; i < 8; i++)
{
cout << arr1[i] << " ";
}
for (int i = 8; i > 2; i--)
{
arr1[i] = arr1[i - 1];
}
arr1[2] = 'p';
cout <<endl<< "插入后:";
for (int i = 0; i <= 8; i++)
{
cout << arr1[i] << " ";
}
return 0;
}

(2)删除顺序表ajcniydu第三个位置的元素。

首先构建顺序表arr1[10]

0 1 2 3 4 5 6 7
a j c n i y d u

找到第三个位置

0 1 2 3 4 5 6 7
a j c n i y d u

从arr[2]开始,arr[i]=arr[i+1]直到arr[6]=arr[7]

0 1 2 3 4 5 6 7
a j n i y d u u

最后让arr[7]=’\0’;

0 1 2 3 4 5 6
a j n i y d u
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//与上一题一样,不做解释
#include <iostream>
using namespace std;
int main() {
char arr1[10] = { 'a','j','c','n','i','y','d','u' };
cout << "删除前:";
for (int i = 0; i < 8; i++) {
cout << arr1[i];
}
for (int i = 2; i < 7; i++) {
arr1[i] = arr1[i + 1];
}
arr1[7] = '\0';
cout << endl << "删除后:";
for (int i = 0; i < 7; i++) {
cout << arr1[i];
}
return 0;
}

2、编程实现将顺序表acdijtuy和cfklns合并。

创建顺序表arr[20]和arr2[10]

0 1 2 3 4 5 6 7
a j c n i y d u
0 1 2 3 4 5
c f k l n s

用strlen函数获取arr和arr2的字符串长度得8和6

从arr[8]开始到arr[13],令arr[i]=arr2[i-8]。

0 1 2 3 4 5 6 7 8 9 10 11 12 13
a j c n i y d u c f k l n s
1
2
3
4
5
6
7
8
9
10
11
12
//不做解释
#include <iostream>
using namespace std;
int main() {
char arr[20] = { 'a','c','d','i','j','t','u','y' };
char arr2[10] = { 'c','f','k','l','n','s ' };
for (int i = 8; i < 14; i++)
{
arr[i] = arr2[i - 8];
}
return 0;
}

3、编程实现:在链表asdfghjkl的第四个位置插入z。

创建链表并初始化,然后创建添加元素的函数

(图中没有头节点)

img

申请一个新的内存用于存放z

(图中没有头节点)

将z所在结构体的next等于第三个结构体的next,第三个next重新赋值指向z所在结构体。

(图中没有头节点)

img

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
//全部代码

#include<stdio.h>
#include<stdlib.h>

typedef char ElemType;
typedef struct ListNode
{
ElemType data;
struct ListNode* next;
}ListNode, LinkNode;

ListNode* HeadNode();
ListNode* LinkInsert(ListNode* head, int index, char data);

int main() {
char arr1[10] = { 'a','s','d','f','g','h','j','k','l','z'};
ListNode* head_1 = HeadNode();

for (int i = 1; i <= 9; i++)
{
LinkInsert(head_1, i, arr1[i - 1]);
}
printf("插入前链表为:head");
while (head_1 != NULL)
{
printf("->%c", head_1->data);
head_1 = head_1->next;
}
printf("\n");
head_1=LinkInsert(head_1, 4, arr1[9]);
printf("插入后链表为:head");
while (head_1 != NULL)
{
printf("->%c", head_1->data);
head_1 = head_1->next;
}
printf("\n");
return 0;
}


ListNode* HeadNode() {
ListNode* head = (ListNode*)malloc(sizeof(ListNode));
if (head == NULL) exit(1);
head->next = NULL;
return head;
}

ListNode* LinkInsert(ListNode* head, int index, char data)
{
ListNode* p; p = head;
int m = 0;
while (m < index - 1)
{
p = p->next;
m++;
}
ListNode* q = (ListNode*)malloc(sizeof(ListNode));
q->next = p->next;
p->next = q;
q->data = data;
return head;
}

1、创建结构体:

1
2
3
4
5
6
typedef char ElemType;
typedef struct ListNode
{
ElemType data;
struct ListNode* next;
}ListNode, LinkNode;

2、创建并初始化头节点:

1
2
3
4
5
6
ListNode* HeadNode() {
ListNode* head = (ListNode*)malloc(sizeof(ListNode));
if (head == NULL) exit(1);
head->next = NULL;
return head;
}

3、建立插入函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ListNode* LinkInsert(ListNode* head, int index, char data)
{
//head为要被执行插入的链表的头节点
//index为要插入的位置
//data是要插入位置的数据
ListNode* p; p = head;//创建一个数据类型为ListNode*的p,指向链表起始位置
int m = 0;
while (m < index - 1)
{
//每一次循环p会到下一个节点,通过m和index控制传递几次,定位到要插入的位置
p = p->next;
m++;
}
ListNode* q = (ListNode*)malloc(sizeof(ListNode));//申请一个新节点q用于存入要插入节点
q->next = p->next;
p->next = q;
q->data = data;
return head;
}

4、main函数:

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
int main() {
char arr1[10] = { 'a','s','d','f','g','h','j','k','l','z'};
ListNode* head_1 = HeadNode();

for (int i = 1; i <= 9; i++)
{
LinkInsert(head_1, i, arr1[i - 1]);
}
printf("插入前链表为:head");
while (head_1 != NULL)
{
printf("->%c", head_1->data);
head_1 = head_1->next;
}
printf("\n");
head_1=LinkInsert(head_1, 4, arr1[9]);
printf("插入后链表为:head");
while (head_1 != NULL)
{
printf("->%c", head_1->data);
head_1 = head_1->next;
}
printf("\n");
return 0;
}

4、编程实现两个有序链表adfi和cefi的合并。

创建并初始化链表

传入数据

img

令i所在结构体的next=第二个头节点的next;

释放第二个头节点

img

全部代码:

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

#include<stdio.h>
#include<stdlib.h>

typedef char ElemType;
typedef struct ListNode
{
ElemType data;
struct ListNode* next;
}ListNode, *LinkNode;

ListNode* HeadNode();
ListNode* LinkInsert(ListNode* head, int index, char data);
ListNode* HeBing(ListNode* head_1, ListNode* head_2, ListNode* head_3);

int main() {
char arr1[4] = { 'a','d','f','i' };
char arr2[4] = { 'c','e','f','i' };
ListNode* head_1 = HeadNode();
ListNode* head_2 = HeadNode();
ListNode* head_3 = HeadNode();

for (int i = 1; i <= 4; i++)
{
LinkInsert(head_1, i, arr1[i - 1]);
LinkInsert(head_2, i, arr2[i - 1]);
}
HeBing(head_1, head_2, head_3);
//打印合并后列表
printf("合并后列表为:head");
head_3 = head_3->next;
while (head_3 != NULL)
{
printf("->%c", head_3->data);
head_3 = head_3->next;
}
printf("\n");
return 0;
}




ListNode* HeadNode() {
ListNode* head = (ListNode*)malloc(sizeof(ListNode));
if (head == NULL) exit(1);
head->next = NULL;
return head;
}

ListNode* LinkInsert(ListNode* head, int index, char data)
{
ListNode* p; p = head;
int m = 0;
while (m < index - 1)
{
p = p->next;
m++;
}
ListNode* q = (ListNode*)malloc(sizeof(ListNode));
q->next = p->next;
p->next = q;
q->data = data;
return head;
}


ListNode* HeBing(ListNode* head_1, ListNode* head_2, ListNode* head_3)
{
ListNode* p1, * p2, * p3;
p1 = head_1->next, p2 = head_2->next; p3 = head_3;
while (p1 != NULL && p2 != NULL)
{
if (p1->data <= p2->data)
{
p3->next = p1;
p3 = p3->next;
p1 = p1->next;
}
else
{
p3->next = p2;
p3 = p3->next;
p2 = p2->next;
}
}
return head_3;
}

1、创建结构体:

1
2
3
4
5
6
7
typedef char ElemType;
typedef struct ListNode
{
ElemType data;
struct ListNode* next;
}ListNode, *LinkNode;

2、创建并初始化头节点函数:

1
2
3
4
5
6
ListNode* HeadNode() {
ListNode* head = (ListNode*)malloc(sizeof(ListNode));
if (head == NULL) exit(1);
head->next = NULL;
return head;
}

3、创建插入函数(用于传入链表的数据):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ListNode* LinkInsert(ListNode* head, int index, char data) 
{
//上面那题写完就来顺便拿来用了
ListNode* p; p = head;
int m = 0;
while (m < index - 1)
{
p = p->next;
m++;
}
ListNode* q = (ListNode*)malloc(sizeof(ListNode));
q->next = p->next;
p->next = q;
q->data = data;
return head;
}

4、合并链表函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ListNode* HeBing(ListNode* head_1, ListNode* head_2, ListNode* head_3)
{
ListNode* p1, * p2, * p3;
p1 = head_1->next, p2 = head_2->next; p3 = head_3;
while (p1 != NULL && p2 != NULL)
{
if (p1->data <= p2->data)
{
p3->next = p1;
p3 = p3->next;
p1 = p1->next;
}
else
{
p3->next = p2;
p3 = p3->next;
p2 = p2->next;
}
}
return head_3;
}

5、main函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main() {
char arr1[4] = { 'a','d','f','i' };
char arr2[4] = { 'c','e','f','i' };
ListNode* head_1 = HeadNode();
ListNode* head_2 = HeadNode();
ListNode* head_3 = HeadNode();

for (int i = 1; i <= 4; i++)
{
LinkInsert(head_1, i, arr1[i - 1]);
LinkInsert(head_2, i, arr2[i - 1]);
}
HeBing(head_1, head_2, head_3);
//打印合并后列表
printf("合并后列表为:head");
head_3 = head_3->next;
while (head_3 != NULL)
{
printf("->%c", head_3->data);
head_3 = head_3->next;
}
printf("\n");
return 0;
}