数据结构实践(一)—— 栈
使用语言C/C++
关于栈的各种基本操作写了下,包括严蔚敏的《数据结构》中没有详细写出的。
C++用的不是很熟。
Stack.h
/**************************************
// Claim of Function in Sequence Stack
***************************************/
#ifndef __STACK_H__
#define __STACK_H__
typedef int SElemType; // Change the definition of SElemType for various data types
typedef class SqStack{
private:
SElemType* top;
SElemType* base;
int stacksize;
public:
SqStack();
bool InitStack();
bool DestroyStack();
bool ClearStack();
bool IsEmpty();
bool Push(SElemType p);
bool Pop(SElemType &e);
bool show();
int StackLen();
SElemType* Base();
SElemType* Top();
protected:
void Resetbase(SElemType* sbase);
void Resettop(SElemType* stop);
}SqStack;
#endif
Stack.cpp
/********************************************
// Definition of Function in Sequence Stack
********************************************/
#include <stdio.h>
#include <stdlib.h>
#include "Stack.h"
#define STACK_SIZE_INIT 100
#define STACK_SIZE_EXTRA 10
// Initiate a stack
bool SqStack::InitStack(){
if (base)
DestroyStack();
base = (SElemType *)malloc(STACK_SIZE_INIT * sizeof(SElemType));
if (!base)
return false;
top = base;
stacksize = STACK_SIZE_INIT;
return true;
}
// Destroy an existed stack
bool SqStack::DestroyStack(){
if (!base)
return false;
free(base);
top = NULL;
base = NULL;
stacksize = 0;
return true;
}
// Clear the content of the stack
bool SqStack::ClearStack(){
if (!base)
return false;
top = base;
stacksize = 0;
return true;
}
// Judge if the stack is empty
bool SqStack::IsEmpty(){
if (top == base)
return true;
return false;
}
// Add an element to the stack
bool SqStack::Push(SElemType p){
if (!base)
return false;
while (top - base >= stacksize)
base = (SElemType *)realloc(base, (stacksize += STACK_SIZE_EXTRA) * sizeof(SElemType));
if (!base)
return false;
*top++ = p;
if (*top != p)
return false;
return true;
}
// Pop out an element
bool SqStack::Pop(SElemType &e){
if (!base)
return false;
if (top == base)
return false;
e = *--top;
return true;
}
// Reset the base point of the stack (Advanced Function)
void SqStack::Resetbase(SElemType* sbase){
base = sbase;
}
// Reset the top point of the stack (Advanced Function)
void SqStack::Resettop(SElemType* stop){
top = stop;
}
// Get the base point
SElemType* SqStack::Base(){
return base;
}
// Get the top point
SElemType* SqStack::Top(){
return top;
}
// Get the length of the stack
int SqStack::StackLen(){
return stacksize;
}
// Show all the element of the stack
bool SqStack::show(){
if (!base)
return false;
if (top == base){
printf("Empty!");
return true;
}
SElemType* p;
printf("The Stack contain element include:\n");
for (p = base; p < top; p++){
printf("%d\n",*p);
}
return true;
}
// Initiate the parameter using initiating list
SqStack::SqStack():stacksize(0){
base = (SElemType *)malloc(STACK_SIZE_INIT * sizeof(SElemType));
top = base;
stacksize = STACK_SIZE_INIT;
}
main.cpp(测试用)
#include <stdio.h>
#include "Stack.h"
// Change the definition of SElemType for various data types
#define SElemType int
void main(){
SqStack S;
for (int i = 1; i <= 10; i++){
S.Push(i);
}
S.show();
}