資料結構 棧 c++ 原始碼實現

來源:互聯網
上載者:User

標籤:資料結構 棧 c/c++

#include <malloc.h>

#include <stdlib.h>

#define  STACK_INIT_SIZE 10

#define  STACK_INCREMENT_SIZE 10

#define Status bool

#define OK true

#define ERROR false

typedef int SElemType;


typedef struct SqStack{

SElemType* top;

SElemType* base;

int stackSize;

}SqStack,*pSqStack;


class Stack{

public:

Stack();

~Stack();

Status InitStack(SqStack &S);

Status DestroyStack(SqStack &S);

Status ClearStack(SqStack &S);

Status StackIsEmpty(SqStack &S);

Status StackLength(SqStack &S,int& len);

Status getTop(SqStack &S,SElemType& e);

Status push(SqStack &S,SElemType& e);

Status pop(SqStack &S,SElemType& e);

};

Stack::Stack(){


}

Stack::~Stack(){


}


/*

初始化棧

分配預設大小空間

*/

Status Stack::InitStack(SqStack &S){

S.base = (SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));

if (!S.base)

return ERROR;

S.top = S.base;

S.stackSize = STACK_INCREMENT_SIZE;

return OK;

}

/*

銷毀棧

預設分配的空間也會被回收

*/

Status Stack::DestroyStack(SqStack &S){

if (S.base)

       free(S.base);

S.base = S.top = NULL;

return true;

}

/*

清空棧

邏輯上的清空

*/

Status Stack::ClearStack(SqStack &S){

if (!S.base)

  return ERROR;

S.top = S.base;

}

/*

棧是否為空白

*/

Status Stack::StackIsEmpty(SqStack &S){

if (S.top == S.base) return OK;

return ERROR;

}

/*

棧的長度

*/

Status Stack::StackLength(SqStack &S,int& len){

if (S.top == S.base){

len = 0;

return ERROR;

}

len = (S.top - S.base)%(S.stackSize+1);

return OK;

}

/*

棧頂得值

*/

Status Stack::getTop(SqStack &S,SElemType& e){

if (S.top == S.base) return ERROR;

e = *(S.top-1);

return OK;

}

/*

壓棧

*/

Status Stack::push(SqStack &S,SElemType& e){

if (S.top - S.base >= S.stackSize)

{

//需要增加空間

S.base = (SElemType*)realloc(S.base,(S.stackSize+STACK_INCREMENT_SIZE)*sizeof(SElemType));

if (!S.base) return ERROR;

S.top = S.base+S.stackSize;

S.stackSize+=STACK_INCREMENT_SIZE;

}

*S.top++ = e;

return OK;

}

/*

出棧

*/

Status Stack::pop(SqStack &S,SElemType& e){

if (S.top == S.base)return ERROR;

e=*--S.top;

return OK;

}


/*

測試


*/

int _tmain(int argc, _TCHAR* argv[])

{

SqStack sq;

Stack* stack = new Stack();


stack->InitStack(sq);

if(stack->StackIsEmpty(sq))

printf("棧為空白,請push\n");

SElemType i = 0;

while(i++<30)

stack->push(sq,i);

if (stack->StackIsEmpty(sq))

printf("棧不為空白\n");

int len;

if (stack->StackLength(sq,len))

printf("棧的長度=%d\n",len);

SElemType e;

if (stack->getTop(sq,e))

printf("棧頂元素=%d\n",e);

i=0;

while (i++<len)

{

if (stack->pop(sq,e))

{

printf("棧頂元素%d=%d\n",i,e);

}

}

if (stack->StackIsEmpty(sq))

printf("棧為空白\n");   

stack->ClearStack(sq);

stack->DestroyStack(sq);

if (NULL == sq.base)

{

printf("棧已銷毀\n"); 

}

if (stack)

{

delete stack;

}

getchar();

return 0;

}



資料結構 棧 c++ 原始碼實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.