服务器之家

服务器之家 > 正文

C语言数据结构之顺序表和单链表

时间:2021-11-18 13:27     来源/作者:Ja_king_

一、顺序表的创建、删除和插入

?
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
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
struct sqlist {
    int date[10];
    int length;
};
void InitList(sqlist& L) {
    for (int i = 0;i < 10;i++) {
        L.date[i] = 0;
    }
    L.length = 0;
}
void charu(sqlist& L) {
    for (int j = 0;j < 5;j++) {
        scanf("%d", &L.date[j]);
        L.length++;
    }
}
void ListInsert(sqlist& L, int i, int e) {
    for (int k = L.length;k >= i;k--) {
        L.date[k] = L.date[k - 1];
    }
    L.date[i - 1] = e;
    L.length++;
}
void print(sqlist& L) {
    for (int i = 0;i < L.length;i++) {
        printf("%d ", L.date[i]);
    }
    printf("\n");
}
void ListDelete(sqlist& L, int i, int e) {
    for (int j = i;j < L.length;j++) {
        L.date[j-1] = L.date[j];
    }
    L.length--;
}
int main() {
    sqlist L;//创建顺序表L
    InitList(L);//初始化顺序表
    shuru(L);//输入值
    ListInsert(L, 3, 3);//插入值
    print(L);//打印
    ListDelete(L, 3, 3);//删除值
    print(L);
    return 0;
}

以上操作分别实现了对顺序表的创建,插入,删除和打印

二、单链表的创建、删除、增加和输出

?
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
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
struct ListNode {
    int num;
    struct ListNode* next;
};
struct ListNode* create(struct ListNode* head) {
    struct ListNode * p1, * p2;
    p1 = p2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    scanf("%d", &p1->num);
    while (p1->num!=0){
        if (head == NULL) {
            head = p1;
        }
        else {
            p2->next = p1;
        }
        p2 = p1;
        p1= (struct ListNode*)malloc(sizeof(struct ListNode));
        scanf("%d", &p1->num);
    }
    p2->next = NULL;
    free(p1);
    return head;
}
void print(struct ListNode* head) {
    while (head != NULL) {
        printf("%d ", head->num);
        head = head->next;
    }
}
int main() {
    struct ListNode* head=NULL;
    head=create(head);//创建链表
    print(head);//输出链表
    return 0;
}

以上操作为创建链表并打印,效果如下:

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
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
struct ListNode {
    int num;
    struct ListNode* next;
};
struct ListNode* create(struct ListNode* head) {
    struct ListNode * p1, * p2;
    p1 = p2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    scanf("%d", &p1->num);
    while (p1->num!=0){
        if (head == NULL) {
            head = p1;
        }
        else {
            p2->next = p1;
        }
        p2 = p1;
        p1= (struct ListNode*)malloc(sizeof(struct ListNode));
        scanf("%d", &p1->num);
    }
    p2->next = NULL;
    free(p1);
    return head;
}
void print(struct ListNode* head) {
    while (head != NULL) {
        printf("%d ", head->num);
        head = head->next;
    }
    printf("\n");
}
struct ListNode* insert(struct ListNode* head,int i) {
    struct ListNode* p1,*p2,*p;
    p1 =p2= head;
    for (int j = 1;j < i;j++) {
        p2 = p1;
        p1 = p1->next;
    }
    p= (struct ListNode*)malloc(sizeof(struct ListNode));
    printf("请输入插入的数:");
    scanf("%d", &p->num);
    p2->next = p;
    p->next = p1;
    return head;
}
int main() {
    struct ListNode* head=NULL;
    int a, b;
    head=create(head);
    print(head);
    printf("请输入插入位置:");
    scanf("%d", &a);
    head = insert(head,a);//插入新数据
    print(head);
    return 0;
}

效果如下:

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
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
struct ListNode {
    int num;
    struct ListNode* next;
};
struct ListNode* create(struct ListNode* head) {
    struct ListNode * p1, * p2;
    p1 = p2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    scanf("%d", &p1->num);
    while (p1->num!=0){
        if (head == NULL) {
            head = p1;
        }
        else {
            p2->next = p1;
        }
        p2 = p1;
        p1= (struct ListNode*)malloc(sizeof(struct ListNode));
        scanf("%d", &p1->num);
    }
    p2->next = NULL;
    free(p1);
    return head;
}
void print(struct ListNode* head) {
    while (head != NULL) {
        printf("%d ", head->num);
        head = head->next;
    }
    printf("\n");
}
struct ListNode* insert(struct ListNode* head,int i) {
    struct ListNode* p1,*p2,*p;
    p1 =p2= head;
    for (int j = 1;j < i;j++) {
        p2 = p1;
        p1 = p1->next;
    }
    p= (struct ListNode*)malloc(sizeof(struct ListNode));
    printf("请输入插入的数:");
    scanf("%d", &p->num);
    p2->next = p;
    p->next = p1;
    return head;
}
struct ListNode* Delete(struct ListNode* head, int i) {
    struct ListNode* p1, * p2;
    p1 = p2 = head;
    while (p1!=NULL&&p1->num != i) {
        p2 = p1;
        p1 = p1->next;
    }
    if (p1 == head) {
        head = head->next;
    }
    else {
        p2->next = p1->next;
    }
    return head;
}
int main() {
    struct ListNode* head=NULL;
    int a, b;
    head=create(head);
    print(head);
    printf("请输入插入位置:");
    scanf("%d", &a);
    head = insert(head,a);
    print(head);
    printf("请输入删除值:");
    scanf("%d", &b);
    head = Delete(head, b);//删除数据
    print(head);
    return 0;
}

效果如下:

C语言数据结构之顺序表和单链表

因此,我们便实现了对单链表的创建、删除、增加和输出

总结

到此这篇关于C语言数据结构之顺序表和单链表的文章就介绍到这了,更多相关C语言顺序表和单链表内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/Ja_king_/article/details/118194821

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部