• 栈的实现(c语言)


    顺序栈(c语言实现)

    1. #include
    2. #include
    3. //这是定义顺序链表的第一种方法,采用指针方式的来标注底部指针和顶部指针
    4. typedef struct sqStack{
    5. char *top;
    6. char *base;
    7. int stackSize;
    8. }sqStack;
    9. //这是定义顺序链表的第一种方法,采用数组下标的方式来标注底部指针和顶部指针
    10. //typedef struct sqStack{
    11. // int top;
    12. // int base;
    13. // int stackSize;
    14. //}sqStack;
    15. //初始化顺序表
    16. void initStack(char **chars,sqStack *stack,int stackSize){
    17. // char chars[stackSize]={}; //对于数组初始化,也就是分配内存可以这样,也可以使用malloc函数
    18. (*chars)=(char*)malloc(sizeof(char)*stackSize);
    19. if(!chars){
    20. printf("内存分配失败!");
    21. exit(0);
    22. }
    23. stack->stackSize=stackSize;
    24. stack->top=(*chars);
    25. stack->base=(*chars);
    26. }
    27. //入栈
    28. void push(sqStack *stack,char value){
    29. if(stack->top-stack->base==stack->stackSize){
    30. //满栈了
    31. printf("栈上溢出操作!");
    32. }else{
    33. *(stack->top)=value;
    34. stack->top+=1;
    35. }
    36. }
    37. //出栈
    38. char pop(sqStack *stack){
    39. if(stack->top==stack->base){
    40. printf("栈下溢出操作!");
    41. return ' ';
    42. }else{
    43. --(stack->top);
    44. return *stack->top;
    45. }
    46. }
    47. //判断栈是否为空
    48. int isEmpty(sqStack *stack){
    49. return stack->base==stack->top?1:0;
    50. }
    51. //获取栈的大小
    52. int getSize(sqStack *stack){
    53. return stack->top-stack->base;
    54. }
    55. //清空栈
    56. void clearStack(sqStack *stack){
    57. stack->top=stack->base;
    58. }
    59. //销毁栈
    60. void destroyStack(char *chars,sqStack *stack){
    61. if(chars){
    62. free(chars);
    63. stack->base=NULL;
    64. stack->top=NULL;
    65. stack->stackSize=0;
    66. }
    67. }
    68. int main(){
    69. sqStack stack;
    70. char *chars;
    71. initStack(&chars,&stack,100);
    72. push(&stack,'d');
    73. push(&stack,'e');
    74. push(&stack,'t');
    75. push(&stack,'o');
    76. printf("%c\n",pop(&stack));
    77. system("pause");
    78. return 0;
    79. }
    顺序栈的重要操作图解:

    顺序栈(java语言实现)

    1. package linearList;
    2. /*
    3. * 实现一个顺序栈
    4. * */
    5. public class sqStack {
    6. private int top;
    7. private int base;
    8. private T[] stackArray;
    9. public sqStack(int stackSize) {
    10. this.top =0;
    11. this.base =0;
    12. stackArray=(T[]) new Object[stackSize];
    13. }
    14. //压栈操作
    15. sqStack push(T value){
    16. if (this.top-this.base!=stackArray.length){
    17. stackArray[this.top]=value;
    18. ++this.top;
    19. }else{
    20. System.out.println("满栈操作!");
    21. }
    22. return this;
    23. }
    24. //出栈操作
    25. T pop(){
    26. if (this.top==this.base){
    27. System.out.println("下溢出操作!");
    28. return null;
    29. }
    30. return stackArray[--this.top];
    31. }
    32. //判断栈是否为空
    33. boolean isEmpty(){
    34. return this.base==this.top?true:false;
    35. }
    36. //获取栈的大小
    37. int getSize(){
    38. return this.top-this.base;
    39. }
    40. //清空栈
    41. void clearStack(){
    42. this.top=this.base;
    43. }
    44. }
    45. class sqStackTest{
    46. public static void main(String[] args) {
    47. sqStack stringsqStack = new sqStack(3);
    48. stringsqStack.push("huhua").push("sdasd").push("sdasd");
    49. System.out.println(stringsqStack.pop());
    50. System.out.println(stringsqStack.pop());
    51. System.out.println(stringsqStack.pop());
    52. System.out.println(stringsqStack.pop());
    53. }
    54. }

    链栈(c语言实现):

    1. #include
    2. #include
    3. typedef struct stackNode{
    4. char value;
    5. struct stackNode* next;
    6. }*linkStack,stackNode;
    7. typedef struct lqStack{
    8. linkStack top;
    9. }lqStack;
    10. //初始化栈
    11. void initStack(lqStack *stack){
    12. stack->top=NULL;
    13. }
    14. //入栈
    15. void push(char value,lqStack *stack){
    16. stackNode *node=(stackNode*)malloc(sizeof(stackNode));
    17. node->value=value;
    18. node->next=stack->top;
    19. stack->top=node;
    20. }
    21. //出栈
    22. char pop(lqStack *stack){
    23. if(stack->top){
    24. stackNode *node=stack->top;
    25. char value=node->value;
    26. stack->top=node->next;
    27. free(node);
    28. return value;
    29. }else{
    30. printf("链栈下溢操作!");
    31. return ' ';
    32. }
    33. }
    34. //判断链栈是否为空
    35. int isEmpty(lqStack *stack){
    36. return stack->top?0:1;
    37. }
    38. int main(){
    39. lqStack stack;
    40. linkStack node;
    41. initStack(&stack);
    42. push('d',&stack);
    43. push('q',&stack);
    44. push('r',&stack);
    45. printf("%c\n",pop(&stack));
    46. printf("%c\n",pop(&stack));
    47. printf("%c\n",pop(&stack));
    48. printf("%c\n",pop(&stack));
    49. system("pause");
    50. return 0;
    51. }
    链栈的重要操作图解:

    链栈(java语言实现):

    1. package linearList;
    2. /*
    3. * 实现一个链栈
    4. * */
    5. class stackNode{
    6. T value;
    7. stackNode next;
    8. public stackNode(T value, stackNode next) {
    9. this.value = value;
    10. this.next = next;
    11. }
    12. }
    13. public class lqStack {
    14. stackNode top=null;
    15. //入栈
    16. lqStack push(T value){
    17. stackNode newNode = new stackNode<>(value,this.top);
    18. this.top=newNode;
    19. return this;
    20. }
    21. //出栈
    22. T pop(){
    23. if (this.top!=null){
    24. T value = this.top.value;
    25. this.top=this.top.next;
    26. return value;
    27. }else{
    28. System.out.println("栈下溢出!");
    29. return null;
    30. }
    31. }
    32. //判断链栈是否为空
    33. boolean isEmpty(){
    34. return this.top==null?true:false;
    35. }
    36. }
    37. class lqStackTest{
    38. public static void main(String[] args) {
    39. lqStack Stack = new lqStack<>();
    40. System.out.println(Stack.isEmpty());
    41. Stack.push("asas").push("asda").push("ASdsa");
    42. System.out.println(Stack.pop());
    43. System.out.println(Stack.pop());
    44. System.out.println(Stack.pop());
    45. System.out.println(Stack.pop());
    46. }
    47. }

  • 相关阅读:
    [渗透测试]—7.1 漏洞利用开发和Shellcode编写
    史上最详细的PyCharm安装教程,小白建议收藏!
    Android Studio开发项目——记账簿应用
    [蓝桥杯 2022 省 A] 求和
    python利用pandas.DataFram批量写入clickhouse
    基于Android+OpenCV+CNN+Keras的智能手语数字实时翻译——深度学习算法应用(含Python、ipynb工程源码)+数据集(三)
    设计模式__单例模式的八种写法
    【C语言】#define宏与函数的优劣对比
    Kafka安装与配置
    【查找算法】——二分查找
  • 原文地址:https://blog.csdn.net/m0_57254953/article/details/133843837