服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C语言数据结构之顺序表和单链表

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

2021-11-18 13:27Ja_king_ C/C++

在数据结构中,线性表是入门级数据结构,线性表又分为顺序表和链表,这篇文章主要给大家介绍了关于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
#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

延伸 · 阅读

精彩推荐