• 数据结构 2 第二章 线性结构 代码实现


    头文件

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include //malloc函数头文件
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. # include

    一、单链表

    1.单链表

    线性表:1.有限的序列 2.序列中的每一个元素都有唯一的前驱和后继,除了开头和结尾的两个节点。

    顺序表:分配一块连续的内存去存放这些元素,eg、数组

    链表:内存是不连续的,元素会各自被分配一块内存,内存和内存之间用指针进行相连。

    顺序表和链表的区别是内存的连续与否

     data域 | next指针域 ——> data域 | next指针域 ——> data域 | next指针域 ——> NULL

    2.单链表的操作

    1.增加 :1>头插法 2>尾插法
    1>插入——> data域 | next指针域 ——> data域 | next指针域 ——> data域 | next指针域 ——> NULL
    2>data域 | next指针域 ——> data域 | next指针域 ——> data域 | next指针域 ——> 插入——>NULL
    2.删除:用前一个节点的指针直接指向对应节点的后一个节点的前驱,只操作一个指针。
    为了使操作方便,在操作中添加一个头节点。头节点并不实际存储,只保存链表中的元素个数。

    代码实现

    1.定义一个结构体

    1. typedef struct Node {//定义一个结构体
    2. int data;
    3. struct Node* next;
    4. }Node;

    2.初始化一个链表 

    1. Node* initList() {//初始化一个链表
    2. Node* list = (Node*)malloc(sizeof(Node));
    3. list->data = 0;
    4. list->next = NULL;
    5. return list;
    6. }

     3.头插法

    1. void headInsert(Node* list,int data){//头插法
    2. Node* node = (Node*)malloc(sizeof(Node));
    3. node->data = data;
    4. node->next = list->next;
    5. list->next = node;
    6. list->data++;//代表当前链表之中插入元素
    7. }

    4.尾插法

    1. void tailInsert(Node* list, int data){//尾插法
    2. Node* head = list;
    3. Node* node = (Node*)malloc(sizeof(Node));
    4. node->data = data;
    5. node->next = NULL;
    6. list = list->next;
    7. while (list->next) {
    8. list = list->next;
    9. }
    10. list->next = node;
    11. head->data++;
    12. }

    5.删除操作

    1. void Delete(Node* list, int data){//删除
    2. Node* head = list;
    3. Node* pre = list;
    4. Node* current = list->next;
    5. list = list->next;
    6. while (current)
    7. {
    8. if (current->data == data)
    9. {
    10. pre->next = current->next;
    11. free(current);
    12. break;
    13. }
    14. pre = current;
    15. current = current->next;
    16. }
    17. list->data--;
    18. }

     6.遍历打印操作

    1. void printList(Node* list) {//遍历操作
    2. list = list->next;
    3. while (list)
    4. {
    5. printf("%d ", list->data);
    6. list = list->next;
    7. }
    8. printf("\n");
    9. }

     7.代码实现

    1. typedef struct Node {//定义一个结构体
    2. int data;
    3. struct Node* next;
    4. }Node;
    5. Node* initList() {//初始化一个链表
    6. Node* list = (Node*)malloc(sizeof(Node));
    7. list->data = 0;
    8. list->next = NULL;
    9. return list;
    10. }
    11. void headInsert(Node* list,int data){//头插法
    12. Node* node = (Node*)malloc(sizeof(Node));
    13. node->data = data;
    14. node->next = list->next;
    15. list->next = node;
    16. list->data++;//代表当前链表之中插入元素
    17. }
    18. void tailInsert(Node* list, int data){//尾插法
    19. Node* head = list;
    20. Node* node = (Node*)malloc(sizeof(Node));
    21. node->data = data;
    22. node->next = NULL;
    23. list = list->next;
    24. while (list->next) {
    25. list = list->next;
    26. }
    27. list->next = node;
    28. head->data++;
    29. }
    30. void Delete(Node* list, int data){//删除
    31. Node* head = list;
    32. Node* pre = list;
    33. Node* current = list->next;
    34. list = list->next;
    35. while (current)
    36. {
    37. if (current->data == data)
    38. {
    39. pre->next = current->next;
    40. free(current);
    41. break;
    42. }
    43. pre = current;
    44. current = current->next;
    45. }
    46. list->data--;
    47. }
    48. void printList(Node* list) {//遍历操作
    49. list = list->next;
    50. while (list)
    51. {
    52. printf("%d ", list->data);
    53. list = list->next;
    54. }
    55. printf("\n");
    56. }
    57. int main()
    58. {
    59. Node* list = initList();
    60. headInsert(list, 1);
    61. headInsert(list, 2);
    62. headInsert(list, 3);
    63. headInsert(list, 4);
    64. headInsert(list, 5);
    65. tailInsert(list, 6);
    66. tailInsert(list, 7);
    67. tailInsert(list, 8);
    68. tailInsert(list, 9);
    69. tailInsert(list, 10);
    70. printList(list);
    71. Delete(list, 5);
    72. printList(list);
    73. Delete(list, 10);
    74. printList(list);
    75. Delete(list, 6);
    76. printList(list);
    77. return 0;
    78. }

    8.结果

    二、单循环链表

    1..单循环链表

    data|next——>data|next——>data|next——>头节点
    1.初始化链表
    2.增加节点(头插法、尾插法)
    3.删除节点
    4.遍历链表

    代码实现

    1.定义一个结构体

    1. typedef struct Node {//定义一个结构体,存放data域和指针域
    2. int data;//数据域类型
    3. struct Node* next;
    4. }Node;

    2. 初始化链表

    1. Node* initList() {//初始化链表
    2. Node* L = (Node*)malloc(sizeof(Node));
    3. L->data = 0;
    4. L->next = L;
    5. return L;
    6. }

    3.头插法

    1. void headInsert(Node* L, int data) {//头插法
    2. Node* node = (Node*)malloc(sizeof(Node));
    3. node->data = data;
    4. node->next = L->next;
    5. L->next = node;
    6. }

    4尾插法

    1. void tailInsert(Node* L, int data) {//尾插法
    2. Node* n = L;
    3. Node* node = (Node*)malloc(sizeof(Node));
    4. node->data = data;
    5. while (n->next != L) {
    6. n = n->next;
    7. }
    8. node->next = L;
    9. n->next = node;
    10. }

    5.删除操作

    1. int Delete(Node* L, int data)//删除
    2. {
    3. Node* preNode = L;
    4. Node* node = L->next;
    5. while (node != L)
    6. {
    7. if (node->data == data) {
    8. preNode->next = node->next;
    9. free(node);
    10. return TRUE;
    11. }
    12. preNode = node;
    13. node = node->next;
    14. }
    15. return FALSE;
    16. }

    6.遍历链表打印

    1. void printList(Node* L) {//遍历链表
    2. Node* node = L->next;
    3. while (node != L) {
    4. printf("%d->", node->data);
    5. node = node->next;
    6. }
    7. printf("NULL\n");
    8. }

    7.代码实现

    1. #define TRUE 1
    2. #define FALSE 0
    3. typedef struct Node {//定义一个结构体,存放data域和指针域
    4. int data;//数据域类型
    5. struct Node* next;
    6. }Node;
    7. Node* initList() {//初始化链表
    8. Node* L = (Node*)malloc(sizeof(Node));
    9. L->data = 0;
    10. L->next = L;
    11. return L;
    12. }
    13. void headInsert(Node* L, int data) {//头插法
    14. Node* node = (Node*)malloc(sizeof(Node));
    15. node->data = data;
    16. node->next = L->next;
    17. L->next = node;
    18. }
    19. void tailInsert(Node* L, int data) {//尾插法
    20. Node* n = L;
    21. Node* node = (Node*)malloc(sizeof(Node));
    22. node->data = data;
    23. while (n->next != L) {
    24. n = n->next;
    25. }
    26. node->next = L;
    27. n->next = node;
    28. }
    29. int Delete(Node* L, int data)//删除
    30. {
    31. Node* preNode = L;
    32. Node* node = L->next;
    33. while (node != L)
    34. {
    35. if (node->data == data) {
    36. preNode->next = node->next;
    37. free(node);
    38. return TRUE;
    39. }
    40. preNode = node;
    41. node = node->next;
    42. }
    43. return FALSE;
    44. }
    45. void printList(Node* L) {//遍历链表
    46. Node* node = L->next;
    47. while (node != L) {
    48. printf("%d->", node->data);
    49. node = node->next;
    50. }
    51. printf("NULL\n");
    52. }
    53. int main()
    54. {
    55. Node* L = initList();
    56. headInsert(L, 1);
    57. headInsert(L, 2);
    58. headInsert(L, 3);
    59. headInsert(L, 4);
    60. headInsert(L, 5);
    61. tailInsert(L, 6);
    62. tailInsert(L, 7);
    63. tailInsert(L, 8);
    64. tailInsert(L, 9);
    65. tailInsert(L, 10);
    66. printList(L);
    67. Delete(L, 4);
    68. Delete(L, 5);
    69. printList(L);
    70. return 0;
    71. }

    8.结果

    三、双链表

    1.双链表

    pre指针|data域|next指针<——>pre|data|next<——>pre|data|next
     与单链表相比多一个指针域

    2.双链表的操作:

     1.初始化链表
     2.插入节点(头插法、尾插法)
     3.删除结点
     4.遍历链表

    代码实现

    1.数据结构的定义

    1. typedef struct Node {//数据结构的定义
    2. int data;//data域
    3. struct Node* pre;//pre指针
    4. struct Node* next;//next指针
    5. }Node;

    2. 初始化链表

    1. Node* initList() {//初始化链表
    2. Node* L = (Node*)malloc(sizeof(Node));//新建指针变量并开辟空间 返回一个Node*类型指针
    3. L->data = 0;//头节点data域初始化为0
    4. L->pre = NULL;//头指针为NULL
    5. L->next = NULL;//next指针为NULL
    6. return

    3. 头插法

    1. void headInsert(Node* L, int data) {//头插法
    2. Node* node = (Node*)malloc(sizeof(Node));//新建一个结点并为他开辟空间
    3. node->data = data;
    4. if (L->data == 0)
    5. {
    6. node->next = L->next;
    7. node->pre = L;
    8. L->next = node;
    9. }
    10. else {
    11. node->pre = L;//node指向的pre指向头节点
    12. node->next = L->next;
    13. L->next->pre = node;
    14. L->next = node;
    15. L->data++;
    16. }
    17. }

    4.尾插法

    1. void tailInsert(Node* L,int data) //尾插法
    2. {
    3. Node* node = L;
    4. Node* n = (Node*)malloc(sizeof(Node));
    5. n->data = data;
    6. while (node->next) {
    7. node = node->next;
    8. }
    9. n->next = node->next;
    10. node->next = n;
    11. n->pre = node;
    12. L->data++;
    13. }

     5.删除操作

    1. int Delete(Node* L, int data) //删除
    2. {
    3. Node* node = L->next;
    4. while (node)
    5. {
    6. if (node->data == data)
    7. {
    8. node->pre->next = node->next;
    9. node->next->pre = node->pre;
    10. free(node);
    11. return TRUE;
    12. }
    13. node = node->next;
    14. }
    15. return FALSE;
    16. }

    6.遍历链表打印

    1. void printList(Node* L)//遍历
    2. {
    3. Node* node = L->next;
    4. while (node) {
    5. printf("%d -> ", node->data);
    6. node = node->next;
    7. }
    8. printf("NULL\n");
    9. }

     7.代码实现

    1. #define TRUE 1
    2. #define FALSE 0
    3. typedef struct Node {//数据结构的定义
    4. int data;//data域
    5. struct Node* pre;//pre指针
    6. struct Node* next;//next指针
    7. }Node;
    8. Node* initList() {//初始化链表
    9. Node* L = (Node*)malloc(sizeof(Node));//新建指针变量并开辟空间 返回一个Node*类型指针
    10. L->data = 0;//头节点data域初始化为0
    11. L->pre = NULL;//头指针为NULL
    12. L->next = NULL;//next指针为NULL
    13. return L;//返回L
    14. }
    15. void headInsert(Node* L, int data) {//头插法
    16. Node* node = (Node*)malloc(sizeof(Node));//新建一个结点并为他开辟空间
    17. node->data = data;
    18. if (L->data == 0)
    19. {
    20. node->next = L->next;
    21. node->pre = L;
    22. L->next = node;
    23. }
    24. else {
    25. node->pre = L;//node指向的pre指向头节点
    26. node->next = L->next;
    27. L->next->pre = node;
    28. L->next = node;
    29. L->data++;
    30. }
    31. }
    32. void tailInsert(Node* L,int data) //尾插法
    33. {
    34. Node* node = L;
    35. Node* n = (Node*)malloc(sizeof(Node));
    36. n->data = data;
    37. while (node->next) {
    38. node = node->next;
    39. }
    40. n->next = node->next;
    41. node->next = n;
    42. n->pre = node;
    43. L->data++;
    44. }
    45. int Delete(Node* L, int data) //删除
    46. {
    47. Node* node = L->next;
    48. while (node)
    49. {
    50. if (node->data == data)
    51. {
    52. node->pre->next = node->next;
    53. node->next->pre = node->pre;
    54. free(node);
    55. return TRUE;
    56. }
    57. node = node->next;
    58. }
    59. return FALSE;
    60. }
    61. void printList(Node* L)//遍历
    62. {
    63. Node* node = L->next;
    64. while (node) {
    65. printf("%d -> ", node->data);
    66. node = node->next;
    67. }
    68. printf("NULL\n");
    69. }
    70. int main()
    71. {
    72. Node* L = initList();
    73. headInsert(L, 1);
    74. headInsert(L, 2);
    75. headInsert(L, 3);
    76. headInsert(L, 4);
    77. printList(L);
    78. //4 -> 3 -> 2 -> 1 -> NULL
    79. tailInsert(L, 5);
    80. tailInsert(L, 6);
    81. tailInsert(L, 7);
    82. tailInsert(L, 8);
    83. printList(L);
    84. //4 -> 3 -> 2 -> 1 -> 5 -> 6 -> 7 -> 8 -> NULL
    85. Delete(L, 2);
    86. Delete(L, 4);
    87. printList(L);
    88. //1 -> 5 -> 6 -> 7 -> 8->NULL
    89. }

    8.结果

     

    四、双循环链表

    1.双循环链表

    pre前指针|data域|next后指针<——>pre|data|next<——>pre|data|next
    最后一个节点的next指针和头节点的pre指针相互指向对方,其他节点与双链表相同。
    功能:
     1.初始化链表
     2.插入节点(头插法、尾插法)
     3.删除结点
     4.遍历链表

    代码实现

    1.定义一个结构体链表数据节点

    1. typedef struct Node//定义一个结构体类型
    2. {
    3. int data;
    4. struct Node* pre;
    5. struct Node* next;
    6. }Node;

    2.初始化链表 

    1. Node* initList()//初始化链表
    2. {
    3. Node* L = (Node*)malloc(sizeof(Node));
    4. L->data = 0;
    5. L->next = L;
    6. L->pre = L;
    7. return L;
    8. }

    3.头插法

    1. void headInsert(Node* L,int data)//头插法
    2. {
    3. Node* node = (Node*)malloc(sizeof(Node));
    4. node->data = data;
    5. if (L->data == 0)
    6. {//链表为空
    7. node->pre = L;
    8. node->next = L->next;
    9. L->next = node;
    10. L->pre = node;
    11. L->data++;
    12. }
    13. else {
    14. //链表不为空
    15. node->next = L->next;
    16. node->pre = L;
    17. L->next->pre = node;
    18. L->next = node;
    19. L->data++;
    20. }
    21. }

    4.尾插法 

    1. void tailInsert(Node* L,int data)//尾插法
    2. {
    3. Node* node = L;
    4. while (node->next != L)
    5. {
    6. node = node->next;
    7. }
    8. Node* n = (Node*)malloc(sizeof(Node));
    9. n->data = data;
    10. n->pre = node;
    11. n->next = L;
    12. L->pre = n;
    13. node->next = n;
    14. L->data++;
    15. }

    5.删除操作

    1. int Delete(Node* L, int data)//删除
    2. {
    3. Node* node = L->next;
    4. while (node != L)
    5. {
    6. if (node->data == data)
    7. {
    8. node->pre->next = node->next;
    9. node->next->pre = node->pre;
    10. free(node);
    11. L->data--;
    12. return 1;
    13. }
    14. node = node->next;
    15. }
    16. return 0;
    17. }

     6.遍历链表打印

    1. void printList(Node* L)//遍历
    2. {
    3. Node* node = L->next;
    4. while (node != L) {
    5. printf("%d -> ", node->data);
    6. node = node->next;
    7. }
    8. printf("NULL\n");
    9. }

    7.代码实现

    1. typedef struct Node//定义一个结构体类型
    2. {
    3. int data;
    4. struct Node* pre;
    5. struct Node* next;
    6. }Node;
    7. Node* initList()//初始化链表
    8. {
    9. Node* L = (Node*)malloc(sizeof(Node));
    10. L->data = 0;
    11. L->next = L;
    12. L->pre = L;
    13. return L;
    14. }
    15. void headInsert(Node* L,int data)//头插法
    16. {
    17. Node* node = (Node*)malloc(sizeof(Node));
    18. node->data = data;
    19. if (L->data == 0)
    20. {//链表为空
    21. node->pre = L;
    22. node->next = L->next;
    23. L->next = node;
    24. L->pre = node;
    25. L->data++;
    26. }
    27. else {
    28. //链表不为空
    29. node->next = L->next;
    30. node->pre = L;
    31. L->next->pre = node;
    32. L->next = node;
    33. L->data++;
    34. }
    35. }
    36. void tailInsert(Node* L,int data)//尾插法
    37. {
    38. Node* node = L;
    39. while (node->next != L)
    40. {
    41. node = node->next;
    42. }
    43. Node* n = (Node*)malloc(sizeof(Node));
    44. n->data = data;
    45. n->pre = node;
    46. n->next = L;
    47. L->pre = n;
    48. node->next = n;
    49. L->data++;
    50. }
    51. int Delete(Node* L, int data)//删除
    52. {
    53. Node* node = L->next;
    54. while (node != L)
    55. {
    56. if (node->data == data)
    57. {
    58. node->pre->next = node->next;
    59. node->next->pre = node->pre;
    60. free(node);
    61. L->data--;
    62. return 1;
    63. }
    64. node = node->next;
    65. }
    66. return 0;
    67. }
    68. void printList(Node* L)//遍历
    69. {
    70. Node* node = L->next;
    71. while (node != L) {
    72. printf("%d -> ", node->data);
    73. node = node->next;
    74. }
    75. printf("NULL\n");
    76. }
    77. int main()
    78. {
    79. Node* L = initList();
    80. headInsert(L, 1);
    81. headInsert(L, 2);
    82. headInsert(L, 3);
    83. headInsert(L, 4);
    84. printList(L);
    85. //4 -> 3 -> 2 -> 1 -> NULL
    86. tailInsert(L, 5);
    87. tailInsert(L, 6);
    88. tailInsert(L, 7);
    89. tailInsert(L, 8);
    90. printList(L);
    91. //4 -> 3 -> 2 -> 1 -> 5 -> 6 -> 7 -> 8 -> NULL
    92. Delete(L, 2);
    93. Delete(L, 4);
    94. printList(L);
    95. //3 -> 1 -> 5 -> 6 -> 7 -> 8 -> NULL
    96. }

     8.运行结果

    五、栈

    1.栈

    存放:1->2->3 取出:3->2->1   先进后出
    应用场景:1.表达式的值 2.解决一些递归问题 3.计算进制转换
    栈是一种特殊的线性表,它只能在一端进行存取操作,所以存取的元素有先进后出的特点。
    栈的总体特点:先进后出

    2.实现的功能:

     初始化栈
     出栈
     入栈
     判断栈空

    代码实现

    1.结构体定义数据结点

    1. typedef struct Node //结构体创建节点
    2. {
    3. int data;//data域
    4. struct Node* next;//next指针
    5. }Node;

    2.初始化栈

    1. Node* initStack()//初始化栈
    2. {
    3. Node* S = (Node*)malloc(sizeof(Node));//新建一个节点
    4. S->data = 0;
    5. S->next = NULL;
    6. return S;//返回S
    7. }

    3.出栈 

    1. int isEmpty(Node* S)//出栈功能前的判断栈空功能
    2. {
    3. if (S->data == 0 || S->next == NULL)//栈空
    4. {
    5. return 1;
    6. }
    7. else {
    8. return 0;
    9. }
    10. }
    11. int getTop(Node* S)//出栈 传入头指针
    12. {
    13. if (isEmpty(S))
    14. {
    15. return -1;
    16. }
    17. else
    18. {
    19. return S->next->data;//不为空的话 S指向第一个节点的data域
    20. }
    21. }

    4. 

     

    六、队

    1.队

    一种先进先出的特殊线性表,只允许在一端进行存取,在头出,在尾进

    2.实现的功能:

    1.初始化队
    2.出队
    3.入队 (尾插法)

    代码实现

    1.结构体定义数据节点

    1. typedef struct Node //定义数据节点结构体
    2. {
    3. int data;//数据域data
    4. struct Node* next;//next指针
    5. }Node;

    2.初始化队列

    1. Node* initQueue() //初始化队列
    2. {
    3. Node* Q = (Node*)malloc(sizeof(Node));//新建指针变量
    4. Q->data = 0;
    5. Q->next = NULL;
    6. return Q;
    7. }

    3. 入队操作 尾插法

    1. void enQueue(Node* Q, int data)//入队操作 尾插法
    2. {
    3. Node* q = Q;//传入头指针
    4. Node* node = (Node*)malloc(sizeof(Node));
    5. node->data = data;
    6. for (int i = 0; i < Q->data; i++)//循环遍历队列找到最后一个节点
    7. {
    8. q = q->next;
    9. }
    10. node->next = q->next;
    11. q->next = node;
    12. Q->data++;//!!!!
    13. }

     4.出队操作,删除队列中第一个结点

    1. int isEmpty(Node* Q)//判空
    2. {
    3. if (Q->data == 0 || Q->next == NULL)
    4. {
    5. return 1;
    6. }
    7. else
    8. {
    9. return 0;
    10. }
    11. }
    12. int deQueue(Node* Q) //出队操作 删除队列中的第一个节点
    13. {
    14. if (isEmpty(Q))//判空操作
    15. {
    16. return -1;
    17. }
    18. else
    19. {
    20. Node* node = Q->next;
    21. int data = node->data;
    22. Q->next = node->next;
    23. free(node);
    24. return data;
    25. }
    26. }

    5.遍历队列打印

    1. void printQueue(Node* Q)//遍历队列打印
    2. {
    3. Node* node = Q->next;//传入第一个节点
    4. while (node)
    5. {
    6. printf("%d -> ", node->data);
    7. node = node->next;
    8. }
    9. printf("NULL\n");
    10. }

    6.代码实现

    1. typedef struct Node //定义数据节点结构体
    2. {
    3. int data;//数据域data
    4. struct Node* next;//next指针
    5. }Node;
    6. Node* initQueue() //初始化队列
    7. {
    8. Node* Q = (Node*)malloc(sizeof(Node));//新建指针变量
    9. Q->data = 0;
    10. Q->next = NULL;
    11. return Q;
    12. }
    13. void enQueue(Node* Q, int data)//入队操作 尾插法
    14. {
    15. Node* q = Q;//传入头指针
    16. Node* node = (Node*)malloc(sizeof(Node));
    17. node->data = data;
    18. for (int i = 0; i < Q->data; i++)//循环遍历队列找到最后一个节点
    19. {
    20. q = q->next;
    21. }
    22. node->next = q->next;
    23. q->next = node;
    24. Q->data++;//!!!!
    25. }
    26. int isEmpty(Node* Q)//判空
    27. {
    28. if (Q->data == 0 || Q->next == NULL)
    29. {
    30. return 1;
    31. }
    32. else
    33. {
    34. return 0;
    35. }
    36. }
    37. int deQueue(Node* Q) //出队操作 删除队列中的第一个节点
    38. {
    39. if (isEmpty(Q))//判空操作
    40. {
    41. return -1;
    42. }
    43. else
    44. {
    45. Node* node = Q->next;
    46. int data = node->data;
    47. Q->next = node->next;
    48. free(node);
    49. return data;
    50. }
    51. }
    52. void printQueue(Node* Q)//遍历队列打印
    53. {
    54. Node* node = Q->next;//传入第一个节点
    55. while (node)
    56. {
    57. printf("%d -> ", node->data);
    58. node = node->next;
    59. }
    60. printf("NULL\n");
    61. }
    62. int main()
    63. {
    64. Node* Q = initQueue();
    65. enQueue(Q, 1);
    66. enQueue(Q, 2);
    67. enQueue(Q, 3);
    68. enQueue(Q, 4);
    69. printQueue(Q);//1 -> 2 -> 3 -> 4 -> NULL
    70. int data = deQueue(Q);
    71. printf("data=%d\n", data);//data=1
    72. printQueue(Q);//2 -> 3 -> 4 -> NULL
    73. data = deQueue(Q);
    74. printQueue(Q);//3 -> 4 -> NULL
    75. data = deQueue(Q);
    76. printQueue(Q);//4 -> NULL
    77. data = deQueue(Q);
    78. printf("data=%d\n", data);//data=4
    79. printQueue(Q);//NULL
    80. return 0;
    81. }

    7.运行结果

     

    七、循环队列

    队列组成一周 需要两个指针指向队列的队首与队尾 当插入一个元素后,队尾指针后移一项,

    如何判断队列是空/满:
     1.在实际操作中,牺牲掉队列一个空间来判断队列是满/空
     2.判断逻辑如下:
       1>队空的话,头指针等于尾指针:front==rear
         2>队满的话:rear+1%MAXSIZE==front;

    实现的功能:

      1.初始化队列
      2.入队
      3.出队
      4.遍历循环队列

     代码实现

    1.定义队列结构体

    1. #define MAXSIZE 5
    2. typedef struct Queue//定义队列结构体
    3. {
    4. int front;//front指针
    5. int rear;//rear指针
    6. int data[MAXSIZE];//data域
    7. }Queue;

    2.初始化队列 

    1. Queue* initQueue()//初始化队列
    2. {
    3. Queue* Q = (Queue*)malloc(sizeof(Queue));
    4. Q->front = Q->rear = 0;
    5. return Q;
    6. }

    3.判满/空操作

    1. int isFull(Queue* Q)//判满操作
    2. {
    3. if ((Q->rear + 1) % MAXSIZE == Q->front)
    4. {
    5. return 1;
    6. }
    7. else {
    8. return 0;
    9. }
    10. }
    11. int isEmpty(Queue* Q)//判空操作
    12. {
    13. if (Q->front == Q->rear)
    14. {
    15. return 1;
    16. }
    17. else {
    18. return 0;
    19. }
    20. }

     4.入队操作

    1. int enQueue(Queue* Q, int data)//插入函数 入队操作
    2. {
    3. if (isFull(Q))
    4. {
    5. return 0;
    6. }
    7. else {
    8. Q->data[Q->rear] = data;
    9. Q->rear = (Q->rear + 1) % MAXSIZE;
    10. return 1;
    11. }
    12. }

     5。出队操作

    1. int deQueue(Queue* Q)//出队操作
    2. {
    3. if (isEmpty(Q))
    4. {
    5. return -1;
    6. }
    7. else {
    8. int data = Q->data[Q->front];
    9. Q->front = (Q->front + 1) % MAXSIZE;
    10. return data;
    11. }
    12. }

    6,遍历队列打印

    1. void printQueue(Queue* Q)//遍历队列打印
    2. {
    3. //要知道队列当前有多少个元素
    4. int length = (Q->rear - Q->front + MAXSIZE) % MAXSIZE;
    5. int index = Q->front;
    6. for (int i = 0; i < length; i++)
    7. {
    8. printf("%d -> ", Q->data[index]);
    9. index = (index + 1) % MAXSIZE;
    10. }
    11. printf("NULL\n");
    12. }

    7.代码实现

    1. #define MAXSIZE 5
    2. typedef struct Queue//定义队列结构体
    3. {
    4. int front;//front指针
    5. int rear;//rear指针
    6. int data[MAXSIZE];//data域
    7. }Queue;
    8. Queue* initQueue()//初始化队列
    9. {
    10. Queue* Q = (Queue*)malloc(sizeof(Queue));
    11. Q->front = Q->rear = 0;
    12. return Q;
    13. }
    14. int isFull(Queue* Q)//判满操作
    15. {
    16. if ((Q->rear + 1) % MAXSIZE == Q->front)
    17. {
    18. return 1;
    19. }
    20. else {
    21. return 0;
    22. }
    23. }
    24. int isEmpty(Queue* Q)//判空操作
    25. {
    26. if (Q->front == Q->rear)
    27. {
    28. return 1;
    29. }
    30. else {
    31. return 0;
    32. }
    33. }
    34. int enQueue(Queue* Q, int data)//插入函数 入队操作
    35. {
    36. if (isFull(Q))
    37. {
    38. return 0;
    39. }
    40. else {
    41. Q->data[Q->rear] = data;
    42. Q->rear = (Q->rear + 1) % MAXSIZE;
    43. return 1;
    44. }
    45. }
    46. int deQueue(Queue* Q)//出队操作
    47. {
    48. if (isEmpty(Q))
    49. {
    50. return -1;
    51. }
    52. else {
    53. int data = Q->data[Q->front];
    54. Q->front = (Q->front + 1) % MAXSIZE;
    55. return data;
    56. }
    57. }
    58. void printQueue(Queue* Q)//遍历队列打印
    59. {
    60. //要知道队列当前有多少个元素
    61. int length = (Q->rear - Q->front + MAXSIZE) % MAXSIZE;
    62. int index = Q->front;
    63. for (int i = 0; i < length; i++)
    64. {
    65. printf("%d -> ", Q->data[index]);
    66. index = (index + 1) % MAXSIZE;
    67. }
    68. printf("NULL\n");
    69. }
    70. int main()
    71. {
    72. Queue* Q = initQueue();
    73. enQueue(Q, 1);
    74. enQueue(Q, 2);
    75. enQueue(Q, 3);
    76. enQueue(Q, 4);
    77. printQueue(Q);//1 -> 2 -> 3 -> 4 -> NULL
    78. deQueue(Q);
    79. printQueue(Q);//2 -> 3 -> 4 -> NULL
    80. return 0;
    81. }

    8.运行结果

     

     

     

  • 相关阅读:
    john 探测(爆破)弱口令(包含linux机器,aix小机)亲测可用
    代码随想录算法训练营第23期day10 |232.用栈实现队列、225. 用队列实现栈
    redis——缓存雪崩、缓存穿透、缓存击穿
    git基础操作随记
    C#练习题1和2
    【LeetCode】删除无效的括号 [H](递归)
    重新认识下JVM级别的本地缓存框架Guava Cache——优秀从何而来
    Oracle主机变量锚定、游标变量
    如何设置从小程序跳转到其它小程序
    SSL、TLS、HTTPS的关系
  • 原文地址:https://blog.csdn.net/m0_73983707/article/details/133874129