• C/C++ 数据结构 - 栈


    1.栈 

    https://blog.csdn.net/CSDN___CSDN/article/details/82918436

    1. 1 #include <stdio.h>
    2. 2 #include <stdlib.h>
    3. 3
    4. 4 typedef struct link_node
    5. 5 {
    6. 6 int info;
    7. 7 struct link_node *next;
    8. 8 }N;
    9. 9
    10. 10 /*创建一个空的链式栈*/
    11. 11 N *init()
    12. 12 {
    13. 13 return NULL;
    14. 14 }
    15. 15
    16. 16 /*对链式栈进行初始化*/
    17. 17 N *creat(N *top)
    18. 18 {
    19. 19 int x;
    20. 20 N *p;
    21. 21 printf("以输入-1为结束\n");
    22. 22 scanf("%d",&x);
    23. 23 while(x!=-1)
    24. 24 {
    25. 25 p=(N*)malloc(sizeof(N));
    26. 26 p->info=x;
    27. 27 p->next=top;
    28. 28 top=p;
    29. 29 scanf("%d",&x);
    30. 30 }
    31. 31 return top;
    32. 32 }
    33. 33
    34. 34 /*取得链式栈的栈顶结点值*/
    35. 35 int read(N *top)
    36. 36 {
    37. 37 if(!top)
    38. 38 {
    39. 39 printf("\n该链式栈是空的\n");
    40. 40 }
    41. 41 return top->info;
    42. 42 }
    43. 43
    44. 44 /*输出链式栈中各结点的值*/
    45. 45 void display(N *top)
    46. 46 {
    47. 47 N *p=top;
    48. 48 if(!top)
    49. 49 {
    50. 50 printf("该链式栈是空的\n");
    51. 51 }
    52. 52 else
    53. 53 {
    54. 54 while(p)
    55. 55 {
    56. 56 printf("%d ",p->info);
    57. 57 p=p->next;
    58. 58 }
    59. 59 printf("\n");
    60. 60 }
    61. 61 }
    62. 62
    63. 63 /*链式栈的插入操作*/
    64. 64 N *insert(N *top,int x)
    65. 65 {
    66. 66 N *q=top,*p;
    67. 67 p=(N*)malloc(sizeof(N));
    68. 68 p->info=x;
    69. 69 p->next=top;
    70. 70 top=p;
    71. 71 return top;
    72. 72 }
    73. 73
    74. 74 /*链式栈的删除操作*/
    75. 75 N *dele(N *top)
    76. 76 {
    77. 77 N *p=top;
    78. 78 if(!top)
    79. 79 {
    80. 80 printf("\n该链式栈是空的,无法进行删除\n");return NULL;
    81. 81 }
    82. 82 top=top->next;
    83. 83 free(p);
    84. 84 return top;
    85. 85 }
    86. 86
    87. 87 int main()
    88. 88 {
    89. 89 N* top = init();
    90. 90 top = creat(top);
    91. 91 display(top);
    92. 92 int i = read(top);
    93. 93 printf("%d\n",i);
    94. 94 top = insert(top,5);
    95. 95 display(top);
    96. 96 top = dele(top);
    97. 97 display(top);
    98. 98
    99. 99 return 0;
    100. 100 }

    2.栈c++实现

    https://blog.csdn.net/zichen_ziqi/article/details/80807989

    1. 1 #include <iostream>
    2. 2 using namespace std;
    3. 3
    4. 4 template <class T>
    5. 5 class stack{
    6. 6 private:
    7. 7 struct Node{
    8. 8 T data;
    9. 9 struct Node *next;
    10. 10 };
    11. 11 Node *head;
    12. 12 int len;
    13. 13 public:
    14. 14 stack()
    15. 15 {
    16. 16 head = NULL;
    17. 17 len = 0;
    18. 18 }
    19. 19 //创建一个栈
    20. 20 void create()
    21. 21 {
    22. 22 int x;
    23. 23 Node *p;
    24. 24 cout << "以输入-1为结束" << endl;
    25. 25 cin >> x ;
    26. 26 while(x!=-1)
    27. 27 {
    28. 28 p=new Node;
    29. 29 p->data=x;
    30. 30 p->next=head;
    31. 31 head=p;
    32. 32 cin >> x ;
    33. 33 len++;
    34. 34 }
    35. 35 }
    36. 36 //入栈
    37. 37 void push(T x)
    38. 38 {
    39. 39 Node *q = new Node;
    40. 40 q->data = x;
    41. 41 q->next = head;
    42. 42 len++;
    43. 43 }
    44. 44 //出栈
    45. 45 T top()
    46. 46 {
    47. 47 Node *p;
    48. 48 T data;
    49. 49 data = head->data;
    50. 50 p = head;
    51. 51 head = head->next;
    52. 52 delete(p);
    53. 53 len--;
    54. 54 return data;
    55. 55 }
    56. 56 //返回栈内元素的个数
    57. 57 int size()
    58. 58 {
    59. 59 return len;
    60. 60 }
    61. 61 };
    62. 62
    63. 63 int main()
    64. 64 {
    65. 65 stack<int> s;
    66. 66 s.create();
    67. 67 s.push(7);
    68. 68 cout << s.size() << endl;
    69. 69 cout << s.top() << endl;
    70. 70 cout << s.size() << endl;
    71. 71
    72. 72 return 0;
    73. 73 }

    3.用两个栈实现队列的功能

    https://blog.csdn.net/weixin_40671425/article/details/83006485

    1. 1 #include<stdio.h>
    2. 2 #include<stdlib.h>
    3. 3
    4. 4 #define STACK_INIT_SIZE 10
    5. 5
    6. 6 typedef struct stack{
    7. 7 int *base;
    8. 8 int *top;
    9. 9 int stacksize;
    10. 10 }stack;
    11. 11
    12. 12 int initStack(stack *s)//构造一个空栈
    13. 13 {
    14. 14 s->base = (int *)malloc(sizeof(stack)*STACK_INIT_SIZE);
    15. 15 if(s->base == NULL)
    16. 16 exit(-1);
    17. 17 s->top = s->base;
    18. 18 s->stacksize = STACK_INIT_SIZE;
    19. 19 return 1;
    20. 20 }
    21. 21
    22. 22 void push(stack *s,int num)//压栈
    23. 23 {
    24. 24 *s->top++ = num;//注意 优先级
    25. 25 }
    26. 26
    27. 27 int pop(stack *s)//弹栈
    28. 28 {
    29. 29 if(s->base == s->top)//空栈,无栈可弹
    30. 30 return -1;
    31. 31 return *(--s->top);
    32. 32 }
    33. 33
    34. 34 int main()
    35. 35 {
    36. 36 stack s1,s2;
    37. 37 initStack(&s1);
    38. 38 initStack(&s2);
    39. 39 int nums[] = {1,2,3,4,5,6};
    40. 40 printf("压入栈 1\n");
    41. 41 for(int i=0;i<6;i++)
    42. 42 {
    43. 43 push(&s1,*(nums + i));
    44. 44 }
    45. 45 printf("出队的顺序为:\n");
    46. 46 for(int i=0;i<6;i++)
    47. 47 {
    48. 48 push(&s2,pop(&s1));
    49. 49 }
    50. 50 for(int i=0;i<6;i++)
    51. 51 {
    52. 52 printf("%d \t",pop(&s2));
    53. 53 }
    54. 54 return 0;
    55. 55 }

    4.栈的最小的值

    https://blog.csdn.net/ltc0106/article/details/105779396

    1. 1 #include <stdio.h>
    2. 2 #include <stdlib.h>
    3. 3
    4. 4 #define STACK_TYPE int
    5. 5
    6. 6 typedef struct STACK_NODE
    7. 7 {
    8. 8 STACK_TYPE value;
    9. 9 struct STACK_NODE *next;
    10. 10 struct STACK_NODE *next_min;
    11. 11 }Stack_Node;
    12. 12
    13. 13 //data
    14. 14 static Stack_Node *stack;
    15. 15 //只指向最小数的栈
    16. 16 static Stack_Node *stack_min;
    17. 17
    18. 18 void create_stack()
    19. 19 {
    20. 20 stack = NULL;
    21. 21 stack_min = NULL;
    22. 22 }
    23. 23
    24. 24 void push(int value)
    25. 25 {
    26. 26 Stack_Node *new_node;
    27. 27 new_node = (Stack_Node *)malloc(sizeof(Stack_Node));
    28. 28 //更新data
    29. 29 new_node->value = value;
    30. 30 new_node->next = stack; //单向循环链表
    31. 31 stack = new_node;
    32. 32 //更新min栈
    33. 33 if(stack_min == NULL ||value < stack_min->value )
    34. 34 {
    35. 35 new_node->next_min = stack_min;
    36. 36 stack_min = new_node;
    37. 37 }
    38. 38 else
    39. 39 {
    40. 40 new_node->next_min = NULL;
    41. 41 }
    42. 42 }
    43. 43
    44. 44 int pop()
    45. 45 {
    46. 46 STACK_TYPE value;
    47. 47 Stack_Node *first_node;
    48. 48
    49. 49 if(stack_min == stack)
    50. 50 stack_min = stack->next_min;
    51. 51
    52. 52 first_node = stack;
    53. 53 stack = first_node->next;
    54. 54 value = first_node->value;
    55. 55
    56. 56 free(first_node);
    57. 57 return value;
    58. 58 }
    59. 59
    60. 60 int top()
    61. 61 {
    62. 62 return stack->value;
    63. 63 }
    64. 64
    65. 65
    66. 66 int min()
    67. 67 {
    68. 68 return stack_min->value;
    69. 69 }
    70. 70
    71. 71 int main()
    72. 72 {
    73. 73 create_stack();
    74. 74 push(5);
    75. 75 printf("min:%d\n",min());
    76. 76 push(4);
    77. 77 printf("min:%d\n",min());
    78. 78 push(3);
    79. 79 printf("min:%d\n",min());
    80. 80 push(5);
    81. 81 printf("min:%d\n",min());
    82. 82 push(1);
    83. 83 printf("min:%d\n",min());
    84. 84 push(90);
    85. 85
    86. 86 printf("min:%d\n",min());
    87. 87 printf("pop:%d\n",pop());
    88. 88 printf("min:%d\n",min());
    89. 89 printf("pop:%d\n",pop());
    90. 90 printf("min:%d\n",min());
    91. 91 printf("pop:%d\n",pop());
    92. 92 printf("min:%d\n",min());
    93. 93 printf("pop:%d\n",pop());
    94. 94 printf("min:%d\n",min());
    95. 95 printf("pop:%d\n",pop());
    96. 96 printf("min:%d\n",min());
    97. 97 printf("pop:%d\n",pop());
    98. 98
    99. 99 return 0;
    100. 100 }

  • 相关阅读:
    【WinRAR】去除请购买WinRAR许可
    代码坏味道与重构之过长参数列表
    depthimage-to-laserscan
    Himall商城LinqHelper帮助类(2)
    计网第五章(运输层)(一)
    【第67题】JAVA高级技术-多线程1(查看线程的运行状态)
    从闪亮开始,到耀眼不止——浅谈飞利浦骨传导耳机的进化史
    栈、共享栈、链式栈(C++实现)
    日本IT行业现状 日本IT的优缺点
    IPV6(IPV6,RIPng的配置以及手工配置IPV4隧道)
  • 原文地址:https://blog.csdn.net/weixin_49303682/article/details/133534172