zoukankan      html  css  js  c++  java
  • 【数据结构】顺序栈与链表栈

    顺序栈

    头文件

     1 //@ author 成鹏致远
     2 //@ net http://infodown.tap.cn
     3 //@ qq 552158509
     4 //@ blog lcw.cnblogs.com
     5 
     6 //顺序栈
     7 
     8 #ifndef __SQSTACK_H
     9 #define __SQSTACK_H
    10 
    11 
    12 #include <stdio.h>
    13 #include <stdlib.h>
    14 #include <stdbool.h>
    15 
    16 #define MAXSIZE 1024
    17 
    18 typedef int datatype;
    19 
    20 typedef struct stack
    21 {
    22     datatype data[MAXSIZE];
    23     int top;//指向栈顶
    24 }sqstack,*p_sqstack;
    25 
    26 extern void sqstack_init(p_sqstack *pstack);//栈的初始化
    27 extern bool is_empty(p_sqstack pstack);
    28 extern bool is_full(p_sqstack pstack);
    29 extern bool sqstack_push(p_sqstack pstack, datatype data);//压栈操作
    30 extern bool sqstack_pop(p_sqstack pstack, datatype *result);//出栈操作
    31 extern void sqstack_show(p_sqstack pstack);//显示栈元素
    32 
    33 #endif
    View Code

    顺序栈文件

     1 //@ author 成鹏致远
     2 //@ net http://infodown.tap.cn
     3 //@ qq 552158509
     4 //@ blog lcw.cnblogs.com
     5 
     6 //输入数字则入栈
     7 //输入字符则出栈
     8 
     9 #include "sqstack.h"
    10 
    11 void sqstack_init(p_sqstack *pstack)//栈的初始化
    12 {
    13     *pstack = (p_sqstack)malloc(sizeof(sqstack));
    14     if(NULL == *pstack)
    15     {
    16         perror("malloc");
    17         exit(1);
    18     }
    19     (*pstack)->top = -1;//初始化栈顶
    20 }
    21 
    22 bool is_empty(p_sqstack pstack)
    23 {
    24     if(-1 == pstack->top)
    25     {
    26         printf("Empty !
    ");
    27         return true;
    28     }
    29     else
    30         return false;
    31 }
    32 
    33 bool is_full(p_sqstack pstack)
    34 {
    35     if(MAXSIZE-1 == pstack->top)
    36     {
    37         printf("Full !
    ");
    38         return true;
    39     }
    40     else
    41         return false;
    42 }
    43 
    44 bool sqstack_push(p_sqstack pstack, datatype data)//压栈操作
    45 {
    46     if(is_full(pstack))
    47         return false;
    48     else
    49     {
    50         pstack->top++;//先栈顶加1,再赋值
    51         pstack->data[pstack->top] = data;
    52         return true;
    53     }
    54 }
    55 
    56 bool sqstack_pop(p_sqstack pstack, datatype *result)//出栈操作
    57 {
    58     if(is_empty(pstack))
    59         return false;
    60     else
    61     {
    62         *result = pstack->data[pstack->top];
    63         pstack->top--;//先出栈,再减栈顶指针
    64         return true;
    65     }
    66 }
    67 
    68 
    69 void sqstack_show(p_sqstack pstack)//显示栈元素
    70 {
    71     int i;
    72 
    73     for(i=0; i<pstack->top+1; i++)
    74     {
    75         printf("%d	",pstack->data[i]);
    76     }
    77     printf("
    ");
    78 }
    View Code

    主文件

     1 //@ author 成鹏致远
     2 //@ net http://infodown.tap.cn
     3 //@ qq 552158509
     4 //@ blog lcw.cnblogs.com
     5 
     6 //输入数据则入栈
     7 //输入字符则出栈
     8 
     9 #include "sqstack.h"
    10 
    11 int main()
    12 {
    13     p_sqstack pstack;
    14     datatype data;
    15     datatype result;//用来存放出栈的元素
    16 
    17     sqstack_init(&pstack);//初始化栈
    18 
    19     while(1)
    20     {
    21         printf("Pls input data: ");
    22         if(1 == scanf("%d",&data))//输入数据,入栈
    23         {
    24             sqstack_push(pstack,data);//入栈操作
    25             sqstack_show(pstack);//显示栈内内容
    26         }
    27         else//输入字符,出栈
    28         {
    29             if(sqstack_pop(pstack,&result))//元素出栈
    30             {
    31                 printf("The pop is %d
    ",result);
    32                 sqstack_show(pstack);
    33             }
    34             while('
    ' != getchar());//缓冲区处理
    35         }
    36     }
    37 
    38     return 0;
    39 }
    View Code

      


    链表栈

    头文件

     1 //@ author 成鹏致远
     2 //@ net http://infodown.tap.cn
     3 //@ qq 552158509
     4 //@ blog lcw.cnblogs.com
     5 
     6 //链表栈
     7 
     8 #ifndef __LINKSTACK_H
     9 #define __LINKSTACK_H
    10 
    11 #include <stdio.h>
    12 #include <stdlib.h>
    13 #include <stdbool.h>
    14 
    15 typedef int datatype;
    16 
    17 typedef struct stack
    18 {
    19     datatype data;
    20     struct stack *next;
    21 }linkstack,*p_linkstack;
    22 
    23 extern void linkstack_init(p_linkstack *top);//链表栈的初始化
    24 extern bool is_empty(p_linkstack top);
    25 extern bool linkstack_push(p_linkstack *top, datatype data);//压栈,因为需要改变头结点,所以需要传*top
    26 extern bool linkstack_pop(p_linkstack *top,datatype *result);//出栈
    27 extern void linkstack_show(p_linkstack top);//显示链栈元素
    28 
    29 #endif
    View Code

     链表栈文件

     1 //@ author 成鹏致远
     2 //@ net http://infodown.tap.cn
     3 //@ qq 552158509
     4 //@ blog lcw.cnblogs.com
     5 
     6 //输入数字入栈
     7 //输入字符出栈
     8 
     9 #include "linkstack.h"
    10 
    11 void linkstack_init(p_linkstack *top)//链表栈的初始化
    12 {
    13     *top = NULL;//为空
    14 }
    15 
    16 bool is_empty(p_linkstack top)
    17 {
    18     if(NULL == top)
    19     {
    20         printf("Empty 
    ");
    21         return true;
    22     }
    23     else
    24         return false;
    25 }
    26 
    27 bool linkstack_push(p_linkstack *top, datatype data)//压栈,因为需要改变头结点,所以需要传*top
    28 {
    29     p_linkstack new;
    30     new = (p_linkstack)malloc(sizeof(linkstack));
    31     if(NULL == new)
    32     {
    33         perror("malloc");
    34         return false;
    35     }
    36     else//压栈
    37     {
    38         new->data = data;
    39         new->next = *top;
    40         *top = new;//top指向新申请的结点
    41         return true;
    42     }
    43 }
    44 
    45 bool linkstack_pop(p_linkstack *top,datatype *result)//出栈
    46 {
    47     p_linkstack tmp;//指向将被出栈的元素
    48     if(is_empty(*top))
    49         return false;
    50     else//出栈
    51     {
    52         tmp = *top;
    53         *top = (*top)->next;//出栈
    54         *result = tmp->data;
    55         free(tmp);//释放结点
    56 
    57         return true; 
    58     }
    59 }
    60 
    61 void linkstack_show(p_linkstack top)//显示链栈元素
    62 {
    63     while(NULL != top)
    64     {
    65         printf("%d	",top->data);
    66         top = top->next;
    67     }
    68     printf("
    ");
    69 }
    View Code

    主文件

     1 //@ author 成鹏致远
     2 //@ net http://infodown.tap.cn
     3 //@ qq 552158509
     4 //@ blog lcw.cnblogs.com
     5 
     6 
     7 //输入数字则入栈
     8 //输入字符则出栈
     9 
    10 #include "linkstack.h"
    11 
    12 int main()
    13 {
    14     p_linkstack top;
    15     datatype data;//用于保存输入的数
    16     datatype result;//用于保存出栈的结果
    17 
    18     linkstack_init(&top);//初始化链表栈
    19 
    20     while(1)
    21     {
    22         printf("Pls input data: ");
    23         if(1 == scanf("%d",&data))//输入数字,入栈
    24         {
    25             linkstack_push(&top,data);//入栈
    26             linkstack_show(top);
    27         }
    28         else//输入字符,出栈
    29         {
    30             if(linkstack_pop(&top,&result))//出栈成功
    31                 printf("The top is %d
    ",result);
    32             linkstack_show(top);
    33 
    34             while('
    ' != getchar());//缓冲区处理
    35         }
    36     }
    37 
    38     return 0;
    39 }
    View Code
  • 相关阅读:
    Cactus详细讲解
    jexus处理静态文件(处理后缀)
    简单的html兼容(参考js和css的常规写法)
    IOHelper(自制常用的输入输出的帮助类)
    @Html.AntiForgeryToken() 源码分析,表单防伪码的生成
    缓存处理类(MemoryCache结合文件缓存)
    MVC过滤大法(过滤静态文件)
    C#文件监控工具(对追加内容的监控并输出)
    C#在Linux上的开发指南
    对java面试文章的技术漫谈的C#技术理解
  • 原文地址:https://www.cnblogs.com/lcw/p/3234920.html
Copyright © 2011-2022 走看看