分享

数据结构(二)——栈及实现、括号匹配

 lchjczw 2012-11-27

一、栈的概念与特点

  一种特殊的线性表,它的插入和删除运算均在同一端进行。这一端被称为栈顶,另一端为栈底,插入称为进栈,删除称为出栈。有后进先出的性质。栈顶top相当于顺序表中的size,即元素个数。关于顺序表可以参考数据结构(一)——顺序表及实现 。

 

 

 [注]没有a[n]这个元素。n是元素的数量

二、栈的操作及实现

1、结构体定义

2、初始化

3、判断是否为空

4、取得栈顶值

5、入栈操作

6、出栈操作

7、打印栈的内容

1、结构体定义

  1. #define LENGTH 100  
  2. #include<stdio.h>  
  3. #include<stdlib.h>  
  4.   
  5. typedef char datatype;  
  6.   
  7. typedef struct sequence_stack{  
  8.     datatype data[LENGTH];  
  9.     int top;  
  10. }sequence_stack;  

2、初始化

  1. #include"stack.h"  
  2. void init_sequence_stack(sequence_stack *st){  
  3.     st->top = 0;  
  4. }  

3、判断是否为空

  1. #include"stack.h"  
  2. int is_empty_sequence_stack(sequence_stack *st){  
  3.     return (st->top? 0:1);  
  4. }  


4、取得栈顶值

  1. #include"stack.h"  
  2.   
  3. datatype get_top(sequence_stack *st){  
  4.     if(st->top == 0){  
  5.         printf("the stack is empty!\n");  
  6.         exit(1);  
  7.     }else{  
  8.         return st->data[st->top-1];  
  9.     }  
  10. }  



5、入栈操作

  1. #include"stack.h"  
  2.   
  3. void push(sequence_stack *st,datatype x){  
  4.     if(st->top == LENGTH){  
  5.         printf("the stack is full!\n");  
  6.         exit(1);  
  7.     }else{  
  8.         st->data[st->top] = x;  
  9.         st->top++;  
  10.     }  
  11. }  



6、出栈操作

  1. #include"stack.h"  
  2.   
  3. datatype pop(sequence_stack *st){  
  4.     if(st->top==0){  
  5.         printf("the stack is empty!\n");  
  6.         exit(1);  
  7.     }else{  
  8.         st->top--;  
  9.         return st->data[st->top];  
  10.     }  
  11. }  



7、打印栈的内容

  1. #include"stack.h"  
  2.   
  3. void display_sequcence_stack(sequence_stack *st){  
  4.     if(st->top == 0){  
  5.         printf("the stack is empty!\n");  
  6.         exit(1);  
  7.     }else{  
  8.         int i;  
  9.         for(i = 0;i<st->top;i++){  
  10.             printf("%c ",st->data[i]);  
  11.         }  
  12.     }  
  13. }  

 

三、栈的应用举例

括号匹配:检验表达式中的括号是否匹配

  1. #include<stdio.h>  
  2. #include"stack.h"  
  3.   
  4. void compare(sequence_stack *,datatype);  
  5.   
  6. int main(){  
  7.     datatype data='\0';  
  8.     printf("Please input the express: ");  
  9.     sequence_stack Mystack;  
  10.     sequence_stack *stack=&Mystack;  
  11.     init_sequence_stack(stack);  
  12.     while((data=getchar()) != '\n'){  
  13.         switch(data){  
  14.         case '{':  
  15.         case '[':  
  16.         case '(':  
  17.                 push(stack,data);  
  18.                 break;  
  19.         case '}':  
  20.                 compare(stack,'{');  
  21.                 break;  
  22.         case ']':  
  23.                 compare(stack,'[');  
  24.                 break;  
  25.         case ')':  
  26.                 compare(stack,'(');  
  27.                 break;  
  28.         defult : break;  
  29.         }  
  30.     }  
  31.     if(is_empty_sequence_stack(stack)){  
  32.         printf("The bracket compare!\n");  
  33.     }else {  
  34.         printf("The bracket doesn't compare!\n");  
  35.     }  
  36.     return 0;  
  37. }  
  38. void compare(sequence_stack *stack,datatype data){  
  39.     if(get_top(stack) != data){  
  40.         printf("The bracket doesn't compare!\n");  
  41.         exit(0);  
  42.     }else   
  43.         pop(stack);  
  44. }  


 调试运行:

  1. [fsy@localhost stack]$ gcc `ls stack*` bracket_match.c -o bracket_match -g  
  2. [fsy@localhost stack]$ ./bracket_match   
  3. Please input the express: abc{1[2(3)]4}aaa  
  4. The bracket compare!  
  5. [fsy@localhost stack]$ ./bracket_match   
  6. Please input the express: {s(s{}s]s}        
  7. The bracket doesn't compare!  
  8. [fsy@localhost stack]$ ./bracket_match   
  9. Please input the express: { [ ( ] ) }  
  10. The bracket doesn't compare!  
  11. [fsy@localhost stack]$   

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多