环形链表实现队列java_数据结构:双向链表实现队列与循环链表

一、双向链表(double linked list)是在单链表的每个结点中,再设置一个指向其前驱结点的指针域。双向链表的基本操作与单链表基本一样,除了插入和删除的时候需要更改两个指针变量,需要注意的是修改的顺序很重要,插入如图3-14-5,删除如图3-14-6。

b9ad5e9a3029b7bc89cc94366d9076f3.png

0bf5532ec11cb19c3a1b4765bc3dc6a4.png

由于引入了prev指针,insert和delete函数中都有一些特殊情况需要用特殊的代码处理,不能和一般情况用同样的代码处理,这非常不爽,如果在表头和表尾各添加一个Sentinel节点(这两个节点只用于界定表头和表尾,不保存数据),就可以把这些特殊情况都转化为一般情况了。如图26.6

dadb9d71319ee7faba78a24fa31a73b1.png

在《队列的链式存储结构》中我们使用单链表实现队列的尾进头出,下面我们演示使用双向链表实现队列的头进尾出。

参考:《Linux C编程 一站式学习》

C++ Code

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

/*************************************************************************

> File Name: doublylinkedlist.h

> Author: Simba

> Mail: dameng34@163.com

> Created Time: Fri 28 Dec 2012 08:02:35 PM CST

************************************************************************/

#ifndef DOUBLYLINKEDLIST_H

#define DOUBLYLINKEDLIST_H

typedef

struct node

{

unsigned

char item;

struct node *prev;

struct node *next;

} node;

typedef node *link;

link make_node(

unsigned

char item);

void free_node(link p);

link search(

unsigned

char key);

void insert(link p);

void deletep(link p);

void traverse(

void (*visit)(link));

void destroy(

void);

void enqueue(link p);

link dequeue(

void);

#endif

C++ Code

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

/*************************************************************************

> File Name: doublylinkedlist.c

> Author: Simba

> Mail: dameng34@163.com

> Created Time: Fri 28 Dec 2012 08:07:21 PM CST

************************************************************************/

#include

#include

#include

"doublylinkedlist.h"

node tailsentinel;

node headsentinel = {

0,

NULL, &tailsentinel};

node tailsentinel = {

0, &headsentinel,

NULL};

static link head = &headsentinel;

static link tail = &tailsentinel;

link make_node(

unsigned

char item)

{

link p =  malloc(

sizeof(node));

p->item = item;

p->prev = p->next =

NULL;

printf(

"make node from Item %d\n", item);

return p;

}

void free_node(link p)

{

printf(

"free node ...\n");

free(p);

}

link search(

unsigned

char key)

{

link p;

printf(

"search by key %d\n", key);

for (p = head->next; p != tail; p = p->next)

if (p->item == key)

return p;

return

NULL;

}

void insert(link p)

{

printf(

"insert node from head ...\n");

p->next = head->next;

head->next->prev = p;

head->next = p;

p->prev = head;

}

void deletep(link p)

{

printf(

"delete node from ptr ...\n");

p->prev->next = p->next;

p->next->prev = p->prev;

}

void traverse(

void (*visit)(link))

{

link p;

printf(

"doublylinkedlist traverse ...\n");

for (p = head->next; p != tail; p = p->next)

visit(p);

printf(

"\n");

}

void destroy(

void)

{

link q, p = head->next;

printf(

"destory doublylinkedlist ...\n");

head->next = tail;

tail->prev = head;

while (p != tail)

{

q = p;

p = p->next;

free_node(q);

}

}

void enqueue(link p)

{

printf(

"enqueue from head ...\n");

insert(p);

}

link dequeue(

void)

{

if (tail->prev == head)

return

NULL;

else

{

link p = tail->prev;

printf(

"dequeue from tail ...\n");

deletep(p);

return p;

}

}

C++ Code

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

/*************************************************************************

> File Name: main2.c

> Author: Simba

> Mail: dameng34@163.com

> Created Time: Fri 28 Dec 2012 08:18:57 PM CST

************************************************************************/

#include

#include

"doublylinkedlist.h"

void print_item(link p)

{

printf(

"print item %d \n", p->item);

}

int main(

void)

{

link p = make_node(

10);

insert(p);

p = make_node(

5);

insert(p);

p = make_node(

90);

insert(p);

p = search(

5);

deletep(p);

free_node(p);

traverse(print_item);

destroy();

printf(

"..................\n");

p = make_node(

100);

enqueue(p);

p = make_node(

200);

enqueue(p);

p = make_node(

250);

enqueue(p);

while ((p = dequeue()))

{

print_item(p);

free_node(p);

}

return

0;

}

输出为:

48fa802e97c39bfe605deee7b856acd3.png

解决的error:

关于错误 error C2275: “XXX”: 将此类型用作表达式非法

在移植c++代码到c的时候,经常会出现一个奇怪的错误, error C2275: “XXX”: 将此类型用作表达式非法,这个错误是由于c的编译器要求将变量的定义放在所有函数调用语句之前,而c++没有这样的要求造成的。解决的办法就是把变量的定义全部放在变量的生存块的开始。

------------------------------------------------------------------------------------------------------------------------------------

二、将单链表中终端结点的指针端由空指针改为指向头结点,就使整个单链表形成一个环,这种头尾相接的单链表就称为单循环链表,简称循环链表(circular linked list)。如下图所示。

d0c0007c4392fd388533384ee0567959.png

其实循环链表和单链表的主要差异就在于循环的判断条件上,原来是判断p->next是否为空,现在则是p->next不等于头结点,则循环未结束。

我们在《队列的顺序存储结构(循环队列)》中使用数组实现了环形队列,我们还要“假想”它是首尾相接的,而如果基于链表实现环形队列,我们本来就可以用指针串成首尾相接的。把上面的程序改成双向环形链表也非常简单,只需要将

把doublylinkedlist.c中的

node tailsentinel;

node headsentinel = {0, NULL, &tailsentinel};

node tailsentinel = {0, &headsentinel, NULL};

static link head = &headsentinel;

static link tail = &tailsentinel;

改成:

node sentinel = {0, &sentinel, &sentinel};

static link head = &sentinel;

再把doublylinkedlist.c中所有的tail替换成head即可,相当于把head和tail合二为一了。如图26.7:

a5e08c7c1578c37fb94f15557bb1caea.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值