棧的基本操作(+實例)


棧:是限定僅在表尾進行插入或刪除操作的線性表,表尾段稱為棧頂,表頭段稱為棧底,棧有稱后進先出線性表。棧有順序棧和鏈棧。

一、棧的順序存儲

1、順序棧的結構定義

//順序棧的存儲結構
typedef struct{
    ElemType *base;//棧底指針變量 
    ElemType *top;//棧頂指針變量 
    int stackSize;//當前可使用的最大容量 
}sqStack;

2、創建一個空棧

//創建一個空棧
#define STACK_INIT_SIZE 100  //存儲空間初始分配量 
#define STACKINCREMENT 10 //存儲空間分配增量 
Status InitStack(SqStack &s){
    s.base = (ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
    if( !s.base )
        exit(OVERFLOW);
    s.top = s.base;
    s.stackSize = STACK_INIT_SIZE;
    return OK;
} 

3、元素入棧

//元素入棧
Status Push(SqStack &s, ElemType e){
    if( s.top-s.base >= s.stackSize ){//若棧滿則追加空間 
        s.base = (ElemType *)realloc(s.base,(s.stackSize+STACKINCREAMENT)*sizeof(ElemType));
        if( !s.base )
            exit(OVERFLOW);
        s.top = s.base+s.stackSize; //設置棧頂
        s.stackSize = s.stackSize+STACKINCERMENT; //設置棧的最大容量 
    }
    *s.top = e;
    s.top++; 
    //*s.top++ = e;
}

4、元素出棧

//元素出棧 
Status Pop(SqStack &s, ElemType &e){
    //若棧不為空,則用e返回s的棧頂元素
    if( s.top == s.base )
        return ERROR;
    e = *--s.top;  //s.top指針先向下移動一個位置,再取出其中的元素
    return OK; 
} 

5、清空一個棧(將棧頂指針指向棧底指針)

//清空一個棧
Status ClearStack(SqStack &s){
    s.top = s.base; //棧頂指針指向棧底指針
    return OK; 
} 

6、銷毀一個棧

//銷毀棧
Status DestroyStack(SqStack &s){
    int len = s.stackSize;
    for(int i =0; i < len; i++){
        free(s.base);
        s.base++;
    }
    s.base = s.top = NULL;
    s,stackSize = 0;
} 

7、返回棧的當前容量

//返回棧當前容量
Status StackLen(SqStack &s){
    return (s.yop-s.base);
} 

棧的應用(進制間的轉換):

 1 //二進制轉十進制
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<math.h>
 5 
 6 #define STACK_INIT_SIZE 20
 7 #define STACKINCREMENT 10
 8 
 9 typedef char ElemType;
10 
11 typedef struct{
12     ElemType *base;
13     ElemType *top;
14     int stackSize;
15 }sqStack;
16 
17 void InitStack(sqStack &s){
18     s.base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
19     if( !s.base )
20         exit(0);
21     s.top = s.base;
22     s.stackSize = STACK_INIT_SIZE;
23 }
24 
25 void Push(sqStack &s, ElemType e){
26     if( s.top-s.base >= s.stackSize ){
27         s.base = (ElemType *)realloc(s.base, (s.stackSize+STACKINCREMENT)*sizeof(ElemType));
28         if( !s.base )
29             exit(0);
30     }
31     *s.top = e;
32     s.top++;
33 }
34 
35 void Pop(sqStack &s, ElemType &e){
36     if( s.top == s.base )
37         return ;
38     e = *--s.top;
39 }
40 
41 int StackLen(sqStack s){
42     return (s.top-s.base);
43 } 
44 
45 int main(){
46     ElemType c;
47     int len,sum = 0;
48     sqStack s;
49     InitStack(s);
50     printf("請輸入一個二進制數,輸入#表示結束!\n");
51     scanf("%c",&c);
52     while( c != '#' ){
53         Push(s,c);
54         scanf("%c",&c);
55     }
56     getchar();
57     len = StackLen(s);
58     printf("棧的當前容量是:%d\n",len);
59     for(int i = 0; i <len; i++){
60         Pop(s,c);
61         sum = sum+(c-48)*pow(2,i);
62     }
63     printf("轉化為十進制數為:%d\n",sum);
64     return 0; 
65 } 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM