zoukankan      html  css  js  c++  java
  • 2- 栈和队列----之栈

    --------------------------1)栈: stack

    栈:只能在表尾进行删除和插入的操作,它是一种线性表。 因此,先进后出。

    参考文档:

     大话数据结构;

    http://blog.chinaunix.net/uid-25908383-id-2965531.html

    2)基本操作

    • 创建栈
    • 销毁栈
    • 清空栈
    • 进栈
    • 出栈
    • 获取栈顶元素
    • 获取栈的大小

    3) 代码

      1 #define MAXSIZE 20
      2 
      3 //0. 栈的结构
      4 typedef struct  
      5 {
      6     int data[MAXSIZE];
      7     int top;
      8 } Seqstack;
      9 
     10 //1. 初始化栈
     11 Seqstack * initial_stack ()
     12 {
     13     Seqstack *s=NULL;
     14 
     15     s=(Seqstack*)malloc(sizeof(Seqstack));
     16     s->top=-1;
     17     return  s;
     18 }
     19 
     20 //2. 销毁栈
     21 int detory_stack(Seqstack *s) 
     22 {
     23     if(s!=NULL)
     24     {
     25         free(s);
     26         s=NULL;
     27     }
     28     return 0;
     29 }
     30 
     31 // 3. 清空栈
     32 int clear_stack(Seqstack *s)
     33 {
     34     if(s != NULL)
     35     {
     36         s->top = -1;
     37         memset(s,0,MAXSIZE);
     38     } 
     39     return 0;
     40 }
     41 
     42 //4. 判断栈是否为空
     43 int isEmpty_stack(Seqstack *s)
     44 {
     45     return s->top==-1;
     46 }
     47 
     48 //5. 返回栈顶元素,(若栈存在且非空)
     49 int getTop_stack(Seqstack *s,int *e)
     50 {
     51     if(s->top==-1)
     52     {
     53         return -1;
     54     }
     55     else
     56         *e=s->data[s->top];
     57     return 0;
     58 }
     59 
     60 // 6. 进栈
     61 int push_stack(Seqstack *s,int *e)
     62 {
     63     if(s->top==MAXSIZE-1)//栈满
     64     {
     65         return -1;
     66     }
     67     s->top++;
     68     s->data[s->top]=*e;
     69     return 0;
     70 }
     71 
     72 //7. 出栈
     73 int pop_stack(Seqstack *s,int &e)
     74 {
     75     if(s->top==-1)
     76     {
     77         return -1;
     78     }
     79     e=s->data[s->top];
     80     s->top--;
     81     return 0;
     82 }
     83 
     84 // 8. 栈的长度
     85 int length_stack(Seqstack *s)
     86 {
     87     int i=0;
     88     i=s->top+1;
     89     return i;
     90 }
     91 
     92 //9. 遍历
     93 int print_stack(Seqstack *s)
     94 {
     95     int k=s->top;
     96     while(k!=-1)
     97     {
     98         printf("%5d",s->data[k]);
     99         (k)--;
    100     }
    101     return 0;
    102         
    103 }

    4) 测试代码

     1 int main()
     2 {
     3     Seqstack *s1;
     4     int elem=0;
     5     int ret=0;
     6     int t1=1;
     7     int t2=2;
     8     int t3=3;
     9     int t4=4;
    10     int t5=5;
    11 
    12     //1. int initial_stack (Seqstack *s)
    13     s1=initial_stack();
    14 
    15     //2. 入栈int push_stack(Seqstack *s,int *e)
    16     push_stack(s1,&t1);
    17     push_stack(s1,&t2);
    18     push_stack(s1,&t3);
    19     push_stack(s1,&t4);
    20     push_stack(s1,&t5);
    21 
    22     //3. 此时栈元素的个数int length_stack(Seqstack *s)
    23     ret=length_stack(s1);
    24     printf("栈元素的个数: %5d",ret);
    25 
    26     //4. 遍历 int print_stack(Seqstack *s)
    27     printf("
    栈中的元素
    ");
    28     ret=print_stack(s1);
    29     ret=length_stack(s1);
    30 
    31     //5. 出栈int pop_stack(Seqstack *s,int *e)
    32     printf("
    出栈:
    ");
    33     ret=pop_stack(s1,elem);
    34     printf(" %5d",elem);
    35     ret=pop_stack(s1,elem);
    36     printf(" %5d",elem);
    37     ret=pop_stack(s1,elem);
    38     printf(" %5d",elem);
    39     ret=pop_stack(s1,elem);
    40     printf(" %5d",elem);
    41     ret=pop_stack(s1,elem);
    42     printf(" %5d",elem);
    43     cout<<'
    ';
    44 
    45     //6. 清空栈int clear_stack(Seqstack *s)
    46     ret =clear_stack(s1);
    47 
    48     //7. 判断栈是否为空int isEmpty_stack(Seqstack *s)
    49     ret=isEmpty_stack(s1);
    50     printf("0代表栈为空: %5d",ret);
    51     cout<<'
    ';
    52 
    53     // 8. 销毁栈
    54     ret= detory_stack(s1) ;
    55     printf("0代表栈为空: %5d",ret);
    56     cout<<'
    ';
    57 
    58     cout<<"hello";
    59     system("pause");
    60     return 0;
    61 }

    5) 运行结果

     

  • 相关阅读:
    IBM实习
    软件测试——一些想法
    ftp文件上传下载实用命令
    Json解析,Json-lib
    eclipse Swt编程—窗口小部件widget
    Java 泛型
    我的理想与现实
    Java泛型介绍——HashMap总结
    Java语言面向对象的一些基本特点
    Java的一些常见问题,JRE,JDK,JVM,包等概念理解
  • 原文地址:https://www.cnblogs.com/584709796-qq-com/p/5810667.html
Copyright © 2011-2022 走看看