1. 程式人生 > >數據結構--順序棧的實現

數據結構--順序棧的實現

數制轉換 struct while 轉換 ktr -- eas 進制數 spa

最近在看嚴蔚敏的數據結構,以下是參照

http://blog.csdn.net/WLxinliang/article/details/52894338

手寫的順序棧的實現代碼:

1.頭文件定義了常數項

//constant.h
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define
OVERFLOW -2 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef int ElemType; typedef int Status;

2.九種基本操作

//header.h
#include "constant.h"
typedef struct{
    ElemType *base;
    ElemType *top;
    int stacksize;
}SqStack;


//構造一個空棧
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; } //銷毀棧 Status DestroyStack(SqStack &S){ S.top=NULL; S.stacksize=0; free(S.base); return OK; } //請空棧 Status ClearStack(SqStack &S){ S.top
=S.base; return OK; } //判斷是否為空棧 Status StackEmpty(SqStack S){ if(S.base==S.top) return ERROR; else return TRUE; } //求棧的長度 Status StackLength(SqStack S){ if(S.top==S.base) return FALSE; else return (S.top-S.base); } //求棧頂元素 Status GetTop(SqStack S,ElemType &e){ if(S.top==S.base) return FALSE; else e=*(S.top-1); return e; } //棧頂插入元素 Status Push(SqStack&S,ElemType &e){ //如果長度超出,增加分配面積 if(S.top-S.base>=STACK_INIT_SIZE){ S.base=(ElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(ElemType)); if(!S.base) return false; S.top=S.base+STACK_INIT_SIZE;//棧的地址可能發生改變,重新定位棧頂元素 S.stacksize=S.stacksize+STACKINCREMENT; } *S.top=e; S.top++; return OK; } //刪除棧頂元素 Status Pop(SqStack &S,ElemType &e){ if(S.top==S.base) return ERROR; else{ S.top--; e=*S.top; return e; } } //遍歷棧 Status StackTraverse(SqStack S){ if(S.base==NULL) return ERROR; if(S.top==S.base) cout<<"棧中沒有元素"<<endl; ElemType *p; p=S.top; while(p>S.base){ p--; cout<<*p<<" "; } return OK; }

3.操作例子

//main.cpp
#include <iostream>
#include "header.h"
using namespace std;

int main()
{
    SqStack S;
    cout<<"構造一個空棧......"<<endl;
    InitStack(S);
    int i,n;
    cout<<"輸入棧的長度:    "<<endl;
    cin>>n;
    for(i=1;i<=n;i++){
        cout<<"輸入棧的第"<<i<<"個元素"<<endl;
        ++S.top;
        cin>>*(S.top-1);
    }
    cout << "……本棧是空棧嗎??……" << endl;
    if (StackEmpty(S) == 1)
        cout << "NO !!!" << endl;
    else
        cout << "YES !!!" << endl;
         cout << "……求出棧的長度……" << endl;
    int m;
    m = StackLength(S);
    cout << "棧的長度是:" << endl;
    cout << m << endl;
    cout << "遍歷輸出棧中的所有元素:" << endl;
    StackTraverse(S);
    cout << endl;
    cout << "……輸出棧頂元素……" << endl;
    int e;
    e = GetTop(S, e);
    cout << "棧頂元素是:" << endl;
    cout << e << endl;
    cout << "……棧頂插入元素……" << endl;
    cout << "請輸入要插入的元素的數值:" << endl;
    cin >> e;
    Push(S,e);
    cout << "現在棧中的元素是:" << endl;
    StackTraverse(S);
    cout << endl;
    cout << "……棧頂刪除元素……" << endl;
    e = Pop(S,e);
    cout << "被刪除的元素是:" << endl;
    cout << e << endl;
    cout << "現在棧中的元素是:" << endl;
    StackTraverse(S);
    cout << endl;
    cout << "……清空棧……" << endl;
    ClearStack(S);
    cout << "現在棧中的元素是:" << endl;
    StackTraverse(S);
    cout << "……銷毀棧……" << endl;
    if(DestroyStack(S)==1)
        cout << "銷毀棧成功" << endl;
    else
        cout << "銷毀棧失敗" << endl;
    cout << "恭喜您成功完成所有的功能,畢竟您那麽帥!!!" << endl;
    return 0;

}

4.案例--數制轉換

#include <iostream>
#include "header.h"
using namespace std;

//對於輸入的一個非負十進制整數,打印輸出與其等值的八進制數
int main()
{
    SqStack S;
    InitStack(S);
    int n;
    cout<<"輸入一個非負十進制整數"<<endl;
    cin>>n;
    while(n){
        int m=n%8;
        Push(S,m);
        n=n/8;
    }
    cout<<"轉化後的八進制為:"<<endl;
    StackTraverse(S);
    return 0;

}

數據結構--順序棧的實現