双向链表二

1.   双向链表的插入操作

如图所示:

41.png

对于每一次的双向链表的插入操作,我们首先需要创建一个独立的结点并通过malloc操作开辟相应的空间,其次我们选中这个新创建的独立节点,将其的pre指针指向所需插入位置的前一个结点,同时,其所需插入的前一个结点的next指针修改指向为该新的结点,同理,该新的结点的next指针将会指向一个原本的下一个结点,而修改下一个结点的pre指针为指向新结点自身,这样的一个操作我们称之为双向链表的插入操作。证券模拟试题

其代码可以表示为:

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

//插入数据

line * insertLine(line * head,int data,int add){

    //三个参数分别为:进行此操作的双链表,插入的数据,插入的位置

    //新建数据域为data的结点

    line * temp=(line*)malloc(sizeof(line));

    temp->data=data;

    temp->pre=NULL;

    temp->next=NULL;

    //插入到链表头,要特殊考虑

    if (add==1) {

        temp->next=head;

        head->pre=temp;

        head=temp;

    }else{

        line * body=head;

        //找到要插入位置的前一个结点

        for (int i=1; i<add-1; i++) {

            body=body->next;

        }

        //判断条件为真,说明插入位置为链表尾

        if (body->next==NULL) {

            body->next=temp;

            temp->pre=body;

        }else{

            body->next->pre=temp;

            temp->next=body->next;

            body->next=temp;

            temp->pre=body;

        }

    }

    return head;

}

2.   双向链表的删除操作

如图:

42.png

删除操作的过程是:选择需要删除的结点,选中这个结点的前一个结点,将前一个结点的next指针指向自己的下一个结点,同时,选中该节点的下一个结点,将下一个结点的pre指针修改指向为自己的上一个结点,这样产生的效果就是在进行遍历的时候直接将这一个结点给跳过了。

在这样的指针修改操作之后,我们释放删除结点,归还空间给内存,这样的操作我们称之为双链表的删除操作。

其代码可以表示为:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

//删除元素

line * deleteLine(line * head,int data){

    //输入的参数分别为进行此操作的双链表,需要删除的数据

    line * list=head;

    //遍历链表

    while (list) {

        //判断是否与此元素相等

        //删除该点方法为将该结点前一结点的next指向该节点后一结点

        //同时将该结点的后一结点的pre指向该节点的前一结点

        if (list->data==data) {

            list->pre->next=list->next;

            list->next->pre=list->pre;

            free(list);

            printf("--删除成功--\n");

            return head;

        }

        list=list->next;

    }

    printf("Error:没有找到该元素,没有产生删除\n");

    return head;

}

3   双向链表的遍历

如同单链表的遍历一样,利用next指针逐步向后进行索引即可,注意判断这里,我们既可以用while(list)的操作直接判断是否链表为空,也可以使用while(list->next)的操作判断该链表是否为空,其下一节点为空和本结点是否为空的判断条件是一样的效果,当然了,善用双向链表的pre指针进行有效的遍历也是值得去尝试的。

其简单的代码可以表示为:

1

2

3

4

5

6

7

8

9

//遍历双链表,同时打印元素数据

void printLine(line *head){

    line *list = head;

    int pos=1;

    while(list){

        printf("第%d个数据是:%d\n",pos++,list->data);

        list=list->next;

    }

}

4.   题目银行从业模拟试题

双向链表的单独训练数据并不是很多,在网上的资料相比单链表也是偏少,不过我们在熟悉原理之后可以同单链表的题目,可以尝试使用双链表进行做题。

5.   本题目案例代码

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

#include<stdio.h>

#include<stdlib.h>

typedef struct line{

    int data;           //data

    struct line *pre;   //pre node

    struct line *next;  //next node

}line;

//分别表示该结点的前驱(pre),后继(next),以及当前数据(data)

//遍历双链表,同时打印元素数据

void printLine(line *head){

    line *list = head;

    int pos=1;

    while(list){

        printf("第%d个数据是:%d\n",pos++,list->data);

        list=list->next;

    }

}

//创建双链表

line* initLine(line * head){

    int number,pos=1,input_data;

    printf("请输入创建结点的大小\n");

    scanf("%d",&number);

    if(number<1){return NULL;} //输入非法直接结束

    //头结点创建///

    head=(line*)malloc(sizeof(line));

    head->pre=NULL;

    head->next=NULL;

    printf("输入第%d个数据\n",pos++);

    scanf("%d",&input_data);

    head->data=input_data;

    line * list=head;

    while (pos<=number) {

        line * body=(line*)malloc(sizeof(line));

        body->pre=NULL;

        body->next=NULL;

        printf("输入第%d个数据\n",pos++);

        scanf("%d",&input_data);

        body->data=input_data;

        

        list->next=body;

        body->pre=list;

        list=list->next;

    }

    return head;

}

//插入数据

line * insertLine(line * head,int data,int add){

    //三个参数分别为:进行此操作的双链表,插入的数据,插入的位置

    //新建数据域为data的结点

    line * temp=(line*)malloc(sizeof(line));

    temp->data=data;

    temp->pre=NULL;

    temp->next=NULL;

    //插入到链表头,要特殊考虑

    if (add==1) {

        temp->next=head;

        head->pre=temp;

        head=temp;

    }else{

        line * body=head;

        //找到要插入位置的前一个结点

        for (int i=1; i<add-1; i++) {

            body=body->next;

        }

        //判断条件为真,说明插入位置为链表尾

        if (body->next==NULL) {

            body->next=temp;

            temp->pre=body;

        }else{

            body->next->pre=temp;

            temp->next=body->next;

            body->next=temp;

            temp->pre=body;

        }

    }

    return head;

}

//删除元素

line * deleteLine(line * head,int data){

    //输入的参数分别为进行此操作的双链表,需要删除的数据

    line * list=head;

    //遍历链表

    while (list) {

        //判断是否与此元素相等

        //删除该点方法为将该结点前一结点的next指向该节点后一结点

        //同时将该结点的后一结点的pre指向该节点的前一结点

        if (list->data==data) {

            list->pre->next=list->next;

            list->next->pre=list->pre;

            free(list);

            printf("--删除成功--\n");

            return head;

        }

        list=list->next;

    }

    printf("Error:没有找到该元素,没有产生删除\n");

    return head;

}

int main(){

    line *head=NULL;

    printf("创建双链表操作\n"); 

    head=initLine(head);

    printLine(head);

//create line

    printf("插入操作\n"); 

    head=insertLine(head,40,2);     //为了简化直接写参数了

    printLine(head);

//insert Line

    printf("删除操作\n"); 

    head=deleteLine(head,2);       //为了简化直接写参数了

    printLine(head);

//delete Line  

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值