数据结构:链表

单链表#

插入结点#

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
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <stdlib.h>
 
//单链表定义
struct slist
{
    int data;
    struct slist *next;
};
//简单插入一个数【p、q间插入】
void main()
{
    struct slist *head,*p,*q;  //head为头指针,始终指向链表的起始节点
    //构建链表
    q = (struct slist *)malloc(sizeof(struct slist));
    p = (struct slist *)malloc(sizeof(struct slist));
 
    head = p;
    p ->data = 10;
    q ->data = 20;
    p ->next = q;
    q ->next =NULL;
 
    //插入一个节点node = 15
    struct slist *node = (struct slist*)malloc(sizeof(struct slist));
    node ->data = 15;
    node ->next = p ->next;
    p ->next = node;
 
    //输出
    struct slist *cp = head;
    while(cp != NULL)
    {
        printf("%d\n",cp -> data);
        cp = cp -> next;
    }
    free(q);
    free(p);
    free(node);
}

表头插入#

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
//简单插入一个数【表头插入】
void main()
{
    struct slist *head,*p,*q;  //head为头指针,始终指向链表的起始节点
    //构建链表
    q = (struct slist *)malloc(sizeof(struct slist));
    p = (struct slist *)malloc(sizeof(struct slist));
 
    head = p;
    p ->data = 10;
    q ->data = 20;
    p ->next = q;
    q ->next =NULL;
 
    //表头插入一个节点node = 5
    struct slist *node = (struct slist*)malloc(sizeof(struct slist));
    node ->data = 5;
    node ->next = head;
    head = node;
 
    //输出
    struct slist *cp = head;
    while(cp != NULL)
    {
        printf("%d\n",cp -> data);
        cp = cp -> next;
    }
    free(q);
    free(p);
    free(node);
}

表尾插入#

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
//简单插入一个数【表尾插入】
void main()
{
    struct slist *head,*p,*q;  //head为头指针,始终指向链表的起始节点
    //构建链表
    q = (struct slist *)malloc(sizeof(struct slist));
    p = (struct slist *)malloc(sizeof(struct slist));
 
    head = p;
    p ->data = 10;
    q ->data = 20;
    p ->next = q;
    q ->next =NULL;
 
    //表尾插入一个节点 node = 30
    struct slist *n = head;  //求尾节点位置
    while(n ->next != NULL)
    {
        n = n ->next;
    }
    struct slist *node = (struct slist*)malloc(sizeof(struct slist));
    node ->data = 30;
    n ->next = node;
    node ->next = NULL;
 
 
    //输出
    struct slist *cp = head;
    while(cp != NULL)
    {
        printf("%d\n",cp -> data);
        cp = cp -> next;
    }
    free(q);
    free(p);
    free(node);
}

将数组中的元素逐一插入到链表中 #

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
#include <stdio.h>
#include <stdlib.h>
 
//Definition for singly-linked list.
struct ListNode {
    int val;
    struct ListNode *next;
};
 
//打印链表
void print_list(struct ListNode *list)
{
    struct ListNode *p=list;
    while(p!=NULL)
    {
        printf("%d ",p->val);
        p=p->next;
    }
    printf("\n");
}
//由数组构建链表
struct ListNode* insert(int *li)
{
    int length=sizeof(li)/sizeof(li[0])+1;
    struct ListNode *head=NULL,*temp,*last;//head为链表的头指针,temp为指向当前访问节点的指针
 
    for(int i=0;i<length;i++)
    {
        //为新插入的节点申请空间
        temp=(struct ListNode *)malloc(sizeof(struct ListNode));
        temp->val=li[i];
        //首次插入时
        if(head==NULL)
        {
            head=temp;
            temp->next=NULL;
            last=temp;
        }else{ //尾插法
            last->next=temp;
            temp->next=NULL;
            last=temp;
        }
    }
    return head;
}
 
int main()
{
    int l1[] = {1,2,4}, l2[] = {1,3,4};
    struct ListNode *list1=NULL,*list2=NULL;
 
    //数组转链表
    list1=insert(l1);
    list2=insert(l2);
 
    print_list(list1);
    print_list(list2);
    return 0;
}

删除结点#

删除链表中元素17后面的元素80#

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
#include <stdio.h>
#include <stdlib.h>
 
//单链表定义
struct slist
{
    int data;
    struct slist *next;
};
// 删除链表中元素17后面的元素80
void main()
{
    // 建立链表
    int i,a[8] = {35,46,17,80,25,78,66,54};
    struct slist *head = NULL,*p,*q;  // head为链表的头指针,p为指向当前访问节点的指针
    for(i = 0;i < 8;i++)
    {
        //为新插入节点申请存储空间
        q = (struct slist *)malloc(sizeof(struct slist));
        q -> data = a[i];
        // 第一个插入时
        if(head == NULL)
        {
            head = q;
            q ->next = NULL;
            p = q;
        }
        //后续插入
        else
        {
            p ->next = q;
            q ->next = NULL;
            p = q;
        }
    }
    //删除链表中元素17后面的元素80
    p = head;
    //找到17的位置,并用p指向17
    while(p ->data != 17)
    {
        p = p ->next;
    }
    struct slist *temp;
    temp = p ->next;
    p ->next = p ->next ->next;
    free(temp);  //将temp指向的存储空间释放
 
    // 打印输出
    struct slist *cp = head;
    while(cp != NULL)
    {
        printf("%d\n",cp -> data);
        cp = cp -> next;
    }
    free(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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <stdlib.h>
 
//单链表定义
struct slist
{
    int data;
    struct slist *next;
};
// 删除表头元素
void main()
{
    // 建立链表
    int i,a[8] = {35,46,17,80,25,78,66,54};
    struct slist *head = NULL,*p,*q;  // head为链表的头指针,p为指向当前访问节点的指针
    for(i = 0;i < 8;i++)
    {
        //为新插入节点申请存储空间
        q = (struct slist *)malloc(sizeof(struct slist));
        q -> data = a[i];
        // 第一个插入时
        if(head == NULL)
        {
            head = q;
            q ->next = NULL;
            p = q;
        }
        //后续插入
        else
        {
            p ->next = q;
            q ->next = NULL;
            p = q;
        }
    }
    //删除表头
    p = head;
    head = head ->next;
    free(p);
 
    // 打印输出
    struct slist *cp = head;
    while(cp != NULL)
    {
        printf("%d\n",cp -> data);
        cp = cp -> next;
    }
    free(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
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
#include <stdio.h>
#include <stdlib.h>
 
//单链表定义
struct slist
{
    int data;
    struct slist *next;
};
// 删除表尾元素
void main()
{
    // 建立链表
    int i,a[8] = {35,46,17,80,25,78,66,54};
    struct slist *head = NULL,*p,*q;  // head为链表的头指针,p为指向当前访问节点的指针
    for(i = 0;i < 8;i++)
    {
        //为新插入节点申请存储空间
        q = (struct slist *)malloc(sizeof(struct slist));
        q -> data = a[i];
        // 第一个插入时
        if(head == NULL)
        {
            head = q;
            q ->next = NULL;
            p = q;
        }
        //后续插入
        else
        {
            p ->next = q;
            q ->next = NULL;
            p = q;
        }
    }
    //删除表尾
    //【假设不知道p本就指向表尾】
    p = head;
    struct slist *temp;
    //找到表尾前一个位置
    while(p ->next ->next != NULL)
    {
        p = p ->next;
    }
    temp = p ->next;
    p ->next = NULL;
    free(temp);  //释放temp指向的空间
 
    // 打印输出
    struct slist *cp = head;
    while(cp != NULL)
    {
        printf("%d\n",cp -> data);
        cp = cp -> next;
    }
    free(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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stdlib.h>
 
//单链表定义
typedef struct slist
{
    int data;
    struct slist *next;
};
struct slist *L;  //L为头结点
 
//建立单链表【头插法】
void creatlist1()
{
    //从表尾到表头逆向建立单链表L,每次均在头结点之后插入元素
    struct slist *s;
    int x;
    L = (struct slist *)malloc(sizeof(struct slist));  // L为头结点
    L ->next = NULL;  //初始为空链表
    printf("请输入元素:");
    scanf("%d",&x);
    while(x != 9999)  //输入9999表示结束
    {
        s = (struct slist *)malloc(sizeof(struct slist));  //创建新链表
        s ->data = x;
        s ->next = L ->next;   //将新结点插入表中
        L ->next = s;
        printf("请输入元素:");
        scanf("%d",&x);
    }
}
//输出
void outlist()
{
    struct slist * cp;
    cp = L ->next;  //头结点不存数据
    while(cp != NULL)
    {
        printf("%d",cp ->data);
        printf("->");
        cp = cp ->next;
    }
    printf("NULL");
}
//建立单链表
void main()
{
    creatlist1();
    printf("单链表如下:\n");
    outlist();
}

尾插法
#

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
#include <stdio.h>
#include <stdlib.h>
 
//单链表定义
typedef struct slist
{
    int data;
    struct slist *next;
};
struct slist *L;  //L为头结点
 
//建立单链表【尾插法】
void creatlist2()
{
    //从表尾到表头正向建立单链表L,每次均在表尾之后插入元素
    struct slist *s,*r;
    int x;
    L = (struct slist *)malloc(sizeof(struct slist));  // L为头结点
    r = L; //r是尾指针
    printf("请输入元素:");
    scanf("%d",&x);
    while(x != 9999)  //输入9999表示结束
    {
        s = (struct slist *)malloc(sizeof(struct slist));  //创建新链表
        s ->data = x;
        r ->next = s;   //将新结点插入表中
        r = s;
        printf("请输入元素:");
        scanf("%d",&x);
    }
    r ->next = NULL;  // 尾结点指针置空
}
//输出
void outlist()
{
    struct slist * cp;
    cp = L ->next;  //头结点不存数据
    while(cp != NULL)
    {
        printf("%d",cp ->data);
        printf("->");
        cp = cp ->next;
    }
    printf("NULL");
}
//建立单链表
void main()
{
    creatlist2();
    printf("单链表如下:\n");
    outlist();
}

查找 #

按序号查找值#

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
#include <stdio.h>
#include <stdlib.h>
 
//单链表定义
typedef struct slist
{
    int data;
    struct slist *next;
};
struct slist *L;  //L为头结点
//建立单链表【尾插法】
void creatlist2()
{
    //从表尾到表头正向建立单链表L,每次均在表尾之后插入元素
    struct slist *s,*r;
    int x;
    L = (struct slist *)malloc(sizeof(struct slist));  // L为头结点
    r = L; //r是尾指针
    printf("请输入元素:");
    scanf("%d",&x);
    while(x != 9999)  //输入9999表示结束
    {
        s = (struct slist *)malloc(sizeof(struct slist));  //创建新链表
        s ->data = x;
        r ->next = s;   //将新结点插入表中
        r = s;
        printf("请输入元素:");
        scanf("%d",&x);
    }
    r ->next = NULL;  // 尾结点指针置空
}
//按序号查找结点值
void getelem(int i)
{
    //取单链表L(带头结点)中第i个位置的结点指针
    int j = 1;
    struct slist *p = L ->next;  //头结点无值
    if(i < 1)
        printf("NULL");  // 若i无效,则返回NULL
    while(p ->data && j < i)
    {
        p = p ->next;
        j++;
    }
    printf("%d\n",p ->data);
}
//输出
void outlist()
{
    struct slist * cp;
    cp = L ->next;  //头结点不存数据
    while(cp != NULL)
    {
        printf("%d",cp ->data);
        printf("->");
        cp = cp ->next;
    }
    printf("NULL\n");
}
//建立单链表
void main()
{
    int i;
    creatlist2();
    printf("单链表如下:\n");
    outlist();
    printf("查找第几个元素:");
    scanf("%d",&i);
    getelem(i);
}

按值查找#

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
#include <stdio.h>
#include <stdlib.h>
 
//单链表定义
typedef struct slist
{
    int data;
    struct slist *next;
};
struct slist *L;  //L为头结点
//建立单链表【尾插法】
void creatlist2()
{
    //从表尾到表头正向建立单链表L,每次均在表尾之后插入元素
    struct slist *s,*r;
    int x;
    L = (struct slist *)malloc(sizeof(struct slist));  // L为头结点
    r = L; //r是尾指针
    printf("请输入元素:");
    scanf("%d",&x);
    while(x != 9999)  //输入9999表示结束
    {
        s = (struct slist *)malloc(sizeof(struct slist));  //创建新链表
        s ->data = x;
        r ->next = s;   //将新结点插入表中
        r = s;
        printf("请输入元素:");
        scanf("%d",&x);
    }
    r ->next = NULL;  // 尾结点指针置空
}
//按值查找表结点
void getelem(int elem)
{
    //查找单链表L(带头结点)中数据等于elem的结点指针,返回位置,否则返回NULL
    struct slist *p = L ->next;  //头结点无值,从第一个结点开始
    int j = 1;
    while(p != NULL && p ->data != elem)
    {
        p = p ->next;
        j++;
    }
    printf("%d\n",j);
}
//输出
void outlist()
{
    struct slist * cp;
    cp = L ->next;  //头结点不存数据
    while(cp != NULL)
    {
        printf("%d",cp ->data);
        printf("->");
        cp = cp ->next;
    }
    printf("NULL\n");
}
//建立单链表
void main()
{
    int elem;
    creatlist2();
    printf("单链表如下:\n");
    outlist();
    printf("查找元素是:");
    scanf("%d",&elem);
    getelem(elem);
}

合并#

两个有序链表合并(带重复值)#

方法1:逐个比较

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
#include <stdio.h>
#include <stdlib.h>
 
//Definition for singly-linked list.
struct ListNode {
    int val;
    struct ListNode *next;
};
 
//打印链表
void print_list(struct ListNode *list)
{
    struct ListNode *p=list;
    while(p!=NULL)
    {
        printf("%d ",p->val);
        p=p->next;
    }
    printf("\n");
}
//由数组构建链表
struct ListNode* insert(int *li)
{
    int length=sizeof(li)/sizeof(li[0])+1;
    struct ListNode *head=NULL,*temp,*last;//head为链表的头指针,temp为指向当前访问节点的指针
 
    for(int i=0;i<length;i++)
    {
        //为新插入的节点申请空间
        temp=(struct ListNode *)malloc(sizeof(struct ListNode));
        temp->val=li[i];
        //首次插入时
        if(head==NULL)
        {
            head=temp;
            temp->next=NULL;
            last=temp;
        }else{ //尾插法
            last->next=temp;
            temp->next=NULL;
            last=temp;
        }
    }
    return head;
}
 
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
    struct ListNode *list,*node;//list为链表的头指针
    list=(struct ListNode*) malloc(sizeof(struct ListNode));//头节点
    node=list;
    while(list1!=NULL && list2!=NULL)
    {
        if(list1->val < list2->val)
        {
            node->next=list1;
            node=node->next;
            list1=list1->next;
        }else
        {
            node->next=list2;
            node=node->next;
            list2=list2->next;
        }
    }
    //如果有一个为空,则直接链接另外一条
    if(list1==NULL)
    {
        node->next=list2;
    }else
    {
        node->next=list1;
    }
    return list->next;
}
 
int main()
{
    int l1[] = {1,2,4}, l2[] = {1,3,4};
    struct ListNode *list1=NULL,*list2=NULL,*list3=NULL;
 
    //数组转链表
    list1=insert(l1);
    list2=insert(l2);
 
    print_list(list1);
    print_list(list2);
 
    //链表合并(升序)
    list3=mergeTwoLists(list1,list2);
 
    print_list((list3));
    return 0;
}

方法2递归:  

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
#include <stdio.h>
#include <stdlib.h>
 
//Definition for singly-linked list.
struct ListNode {
    int val;
    struct ListNode *next;
};
 
//打印链表
void print_list(struct ListNode *list)
{
    struct ListNode *p=list;
    while(p!=NULL)
    {
        printf("%d ",p->val);
        p=p->next;
    }
    printf("\n");
}
//由数组构建链表
struct ListNode* insert(int *li)
{
    int length=sizeof(li)/sizeof(li[0])+1;
    struct ListNode *head=NULL,*temp,*last;//head为链表的头指针,temp为指向当前访问节点的指针
 
    for(int i=0;i<length;i++)
    {
        //为新插入的节点申请空间
        temp=(struct ListNode *)malloc(sizeof(struct ListNode));
        temp->val=li[i];
        //首次插入时
        if(head==NULL)
        {
            head=temp;
            temp->next=NULL;
            last=temp;
        }else{ //尾插法
            last->next=temp;
            temp->next=NULL;
            last=temp;
        }
    }
    return head;
}
 
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
    if(list1==NULL)
        return list2;
    if(list2==NULL)
        return  list1;
    if(list1->val <list2->val)
    {
        list1->next= mergeTwoLists(list1->next,list2);
        return list1;
    } else
    {
        list2->next= mergeTwoLists(list1,list2->next);
        return  list2;
    }
}
 
int main()
{
    int l1[] = {1,2,4}, l2[] = {1,3,4};
    struct ListNode *list1=NULL,*list2=NULL,*list3=NULL;
 
    //数组转链表
    list1=insert(l1);
    list2=insert(l2);
 
    print_list(list1);
    print_list(list2);
 
    //链表合并(生序)
    list3=mergeTwoLists(list1,list2);
 
    print_list((list3));
    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
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stdlib.h>
 
//双向链表定义
typedef struct dlist
{
    int data;
    struct dlist *next;
    struct dlist *prior;
};
struct dlist *head;  //L 为头结点
//构建双向链表
void creatdlist()
{
    struct dlist *p,*q;
    head =(struct dlist *)malloc(sizeof(struct dlist));
    head ->prior = NULL;
    head ->next = NULL;
    q = head;
    int x;
    printf("请输入元素:");
    scanf("%d",&x);
    while(x != 9999)
    {
        p =(struct dlist *)malloc(sizeof(struct dlist));
        p ->data =x;
 
        p ->next = q ->next;
        p ->prior = q;
        q ->next = p;
        q = p;
        printf("请输入元素:");
        scanf("%d",&x);
    }
}
//输出
void outdlist()
{
    struct dlist * cp;  //指向当前结点
    cp = head ->next;  //头结点不存数据
    while(cp != NULL)
    {
        printf("%d\t",cp ->data);
        cp = cp ->next;
    }
}
void main()
{
    creatdlist();
    outdlist();
}

插入 #

 

在指定的链表元素后插入给定的元素#

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
#include <stdio.h>
#include <stdlib.h>
 
//双向链表定义
typedef struct dlist
{
    int data;
    struct dlist *next;
    struct dlist *prior;
};
struct dlist *head;  //L 为头结点
//构建双向链表
void creatdlist()
{
    struct dlist *p,*q;
    head =(struct dlist *)malloc(sizeof(struct dlist));
    head ->prior = NULL;
    head ->next = NULL;
    q = head;
    int x;
    printf("请输入元素:");
    scanf("%d",&x);
    while(x != 9999)
    {
        p =(struct dlist *)malloc(sizeof(struct dlist));
        p ->data =x;
 
        p ->next = q ->next;
        p ->prior = q;
        q ->next = p;
        q = p;
        printf("请输入元素:");
        scanf("%d",&x);
    }
}
//输出
void outdlist()
{
    struct dlist * cp;  //指向当前结点
    cp = head ->next;  //头结点不存数据
    while(cp != NULL)
    {
        printf("%d\t",cp ->data);
        cp = cp ->next;
    }
}
//在指定的链表元素后插入给定的元素
void indlist(int n,int temp)
{
    struct dlist *dp,*node;  //指向当前结点
    dp = head ->next;
    node = (struct dlist *)malloc(sizeof(struct dlist));
    node ->data = temp;
    //找到元素n的位置,cp指向
    while(dp ->data != n)
    {
        dp = dp ->next;
    }
    //插入
    node ->next = dp ->next;  //1
    dp ->next ->prior = node; //2
    node ->prior = dp;  //3
    dp ->next = node;  //4
}
void main()
{
    int n,temp;
    creatdlist();
    outdlist();
    printf("\n指定在谁后插入:");
    scanf("%d",&n);
    printf("\n插入元素为:");
    scanf("%d",&temp);
    indlist(n,temp);
    printf("插入后链表:\n");
    outdlist();
}

在指定的链表元素前插入给定的元素#

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
#include <stdio.h>
#include <stdlib.h>
 
//双向链表定义
typedef struct dlist
{
    int data;
    struct dlist *next;
    struct dlist *prior;
};
struct dlist *head;  //L 为头结点
//构建双向链表
void creatdlist()
{
    struct dlist *p,*q;
    head =(struct dlist *)malloc(sizeof(struct dlist));
    head ->prior = NULL;
    head ->next = NULL;
    q = head;
    int x;
    printf("请输入元素:");
    scanf("%d",&x);
    while(x != 9999)
    {
        p =(struct dlist *)malloc(sizeof(struct dlist));
        p ->data =x;
 
        p ->next = q ->next;
        p ->prior = q;
        q ->next = p;
        q = p;
        printf("请输入元素:");
        scanf("%d",&x);
    }
}
//输出
void outdlist()
{
    struct dlist * cp;  //指向当前结点
    cp = head ->next;  //头结点不存数据
    while(cp != NULL)
    {
        printf("%d\t",cp ->data);
        cp = cp ->next;
    }
}
//在指定的链表元素前插入给定的元素
void indlist(int n,int temp)
{
    struct dlist *dp,*node;  //指向当前结点
    dp = head ->next;
    node = (struct dlist *)malloc(sizeof(struct dlist));
    node ->data = temp;
    //找到元素n的位置,cp指向
    while(dp ->data != n)
    {
        dp = dp ->next;
    }
    //插入
    node ->next = dp;  //1
    node ->prior = dp ->prior;  //2
    dp ->prior ->next = node;  //3
    dp ->prior = node;  //4
}
void main()
{
    int n,temp;
    creatdlist();
    outdlist();
    printf("\n指定在谁前插入:");
    scanf("%d",&n);
    printf("\n插入元素为:");
    scanf("%d",&temp);
    indlist(n,temp);
    printf("插入后链表为:\n");
    outdlist();
}

删除  #

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
#include <stdio.h>
#include <stdlib.h>
 
//双向链表定义
typedef struct dlist
{
    int data;
    struct dlist *next;
    struct dlist *prior;
};
struct dlist *head;  //L 为头结点
//构建双向链表
void creatdlist()
{
    struct dlist *p,*q;
    head =(struct dlist *)malloc(sizeof(struct dlist));
    head ->prior = NULL;
    head ->next = NULL;
    q = head;
    int x;
    printf("请输入元素:");
    scanf("%d",&x);
    while(x != 9999)
    {
        p =(struct dlist *)malloc(sizeof(struct dlist));
        p ->data =x;
 
        p ->next = q ->next;
        p ->prior = q;
        q ->next = p;
        q = p;
        printf("请输入元素:");
        scanf("%d",&x);
    }
}
//输出
void outdlist()
{
    struct dlist * cp;  //指向当前结点
    cp = head ->next;  //头结点不存数据
    while(cp != NULL)
    {
        printf("%d\t",cp ->data);
        cp = cp ->next;
    }
}
//删除指定的链表元素
void dedlist(int temp)
{
    struct dlist *dp;
    dp = head ->next;
    //找到temp的位置,并dp指向
    while(dp ->data != temp)
    {
        dp = dp ->next;
    }
    //删除
    dp ->prior ->next = dp ->next;
    dp ->next ->prior = dp ->prior;
    free(dp);
}
void main()
{
    int temp;
    creatdlist();
    outdlist();
    printf("\n删除元素为:");
    scanf("%d",&temp);
    dedlist(temp);
    printf("删除后链表为:\n");
    outdlist();
}

作者:Hang Shao

出处:https://www.cnblogs.com/pam-sh/p/12446087.html

版权:本作品采用「知识共享」许可协议进行许可。

声明:欢迎交流! 原文链接 ,如有问题,可邮件(mir_soh@163.com)咨询.

posted @   PamShao  阅读(340)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
menu