栈的使用

1.这个是严蔚敏《数据结构》第三章的代码,写的是一个进制转换的函数,可以把默认十进制的数,转换为2、8、7等进制。唯一特殊的地方是栈是自己实现的。

 1 #include <stdio.h>
 2 #include <malloc.h>
 3 
 4 #define STACK_INIT_SIZE 100
 5 #define STACKINCREMENT 10
 6 typedef int SElemType;
 7 
 8 typedef struct {
 9     SElemType *base;
10     SElemType *top;
11     int stacksize; 
12 }SqStack;  
13 
14 //基本操作的函数原型说明
15 bool InitStack(SqStack &S);   //构造一个空栈
16 void DestroyStack(SqStack &S);//销毁栈S,S不再存在
17 void ClearStack(SqStack &S);  //把S置为空栈 
18 bool StackEmpty(SqStack S);   //若栈S为空,则返回TRUE,否则返回FALSE
19 int StackLength(SqStack S);   //返回S的元素的个数,即栈的长度
20 bool GetTop(SqStack S,SElemType &e ); //若栈S不空,则用e返回栈顶元素,并返回1,否则返回0; 
21 bool Pop(SqStack &S,SElemType &e);    //若栈不空,则删除S的栈顶元素,用e返回其值,并返回1;否则返回0;
22 bool Push(SqStack &S,SElemType e);    //插入元素e为新的栈顶元素 
23 
24 //基本操作的算法描述部分
25 bool InitStack(SqStack &S){   
26     //构造一个空栈
27     S.base = (SElemType * )malloc(STACK_INIT_SIZE * sizeof(SElemType)); 
28     if(!S.base) return  0;
29     S.top = S.base;   
30     S.stacksize = STACK_INIT_SIZE;
31     return 1;     
32 }
33 
34 bool GetTop(SqStack S,SElemType &e ){
35      //若栈S不空,则用e返回栈顶元素,并返回1,否则返回0;
36     if(S.top==S.base )  return 0;
37     e = *(S.top-1);
38     return 1;    
39 }
40  
41 bool Pop(SqStack &S,SElemType &e){
42     //若栈不空,则删除S的栈顶元素,用e返回其值,并返回1;否则返回0;
43     if(S.top==S.base) return 0;
44     e = *--S.top;
45     return 1;
46 }
47 
48 bool Push(SqStack &S, SElemType e){   
49     //插入元素e为新的栈顶元素 
50     if(S.top - S.base >= S.stacksize ){
51         S.base = (SElemType * ) realloc(S.base,(S.stacksize + STACKINCREMENT)*sizeof(SElemType )  );
52         if(!S.base) return 0;
53         S.top = S.base + S.stacksize;
54         S.stacksize += STACKINCREMENT;  
55     }
56     *S.top++ = e;
57     return 1;    
58 }
59 
60 bool StackEmpty(SqStack S){  //若栈S为空,则返回TRUE,否则返回FALSE
61     if( S.top==S.base ) return 1;
62     else return 0;    
63 }
64 
65 int main(){
66     //进制转换函数   
67     SqStack S;
68     SElemType e;
69     int N;
70     int jinzhi = 8 ;   //定义数制转化为几进制  ,例如可以为2进制、8进制、7进制等等   
71     
72     InitStack(S);
73     scanf("%d",&N );
74     while(N){
75         Push( S,N%jinzhi );
76         N=N/jinzhi; 
77     }    
78     while(!StackEmpty(S)){
79         Pop(S,e);
80         printf("%d",e);
81     } 
82         
83 }
进制转换

 2.关于括号匹配的一道题

http://ac.jobdu.com/problem.php?pid=1153

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <cstring>
 4 #include <stack>
 5 using namespace std;
 6 
 7 int main(){
 8     char data[101];
 9 
10     while(gets(data)){
11         stack<char> temp;
12         stack<int> num;    
13         int len = strlen(data);    
14         char res[len];
15         
16         for(int i=0;i<len;i++){
17         
18             if(data[i]=='('){
19                 temp.push(data[i]);  num.push(i);
20                 res[i] = '$';
21                                 
22             }
23             else if(data[i]==')' ){
24                 if(temp.empty()) {
25                     temp.push(data[i]);  
26                     num.push(i);  
27                     res[i]='?';
28                 } 
29                 else{
30                     if(temp.top()=='('){
31                         temp.pop();
32                         res[i] = ' ';    
33                         res[ num.top() ] = ' ';
34                         num.pop();  
35                     }
36                     else{
37                         temp.push(data[i]);
38                         num.push(i); 
39                         res[i]='?'; 
40                     }
41                 } 
42             } 
43             else if(data[i]>='a'&&data[i]<='z'||data[i]>='A'&&data[i]<='Z'){
44                 res[i] = ' ';  
45             }
46             
47         }
48         
49         for(int i=0;i<len;i++){
50             cout<<data[i];
51         }
52         cout<<endl;
53         for(int i=0;i<len;i++){
54             cout<<res[i];
55         }
56         cout<<endl;
57         
58         
59     }    
60 }         
括号匹配

 3. 迷宫问题

http://poj.org/problem?id=3984

 

posted on 2016-06-27 13:58  逸阳  阅读(230)  评论(0)    收藏  举报

导航