不可变长的顺序栈,没有详细的讲解
主函数中的是测试代码,可以根据需要删改
C语言
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <time.h>
#include <malloc.h>
#define MAXSIZE 1024
typedef int elemtype;
typedef struct Stack
{
elemtype date[MAXSIZE];
int top;
}stack;
bool Stack_Empty(stack* S)//判空
{
if(S->top == -1)
return true;
else return false;
}
bool Stack_Full(stack * S)//判满
{
if(S->top == (MAXSIZE - 1))
return true;
else return false;
}
stack* Init_stack()//创建栈
{
stack * S;
S = (stack*)malloc(sizeof(stack));
if(S == NULL)
return S;
S->top = -1;
return S;
}
int Stack_Len(stack * S)//栈长
{
return (S->top+1);
}
bool Push_Stack(stack * S,elemtype val)//入栈
{
if(Stack_Full(S))
return false;
S->top++;
S->date[S->top] = val;
return true;
}
bool Pop_Stack(stack * S,elemtype val)//出栈
{
if(Stack_Empty(S))
return false;
else
{
S->top--;
val = S->date[S->top+1];
return true;
}
}
int GetTop_Stack(stack * S)//栈顶元素
{
if(Stack_Len(S) == -1)
return 0;
else return (S->date[S->top]);
}
void Show_Stack(stack * S)//打印
{
if(Stack_Empty(S))
printf("Empty");
else
{
for(int i = 0;i < S->top ;i++ )
{
printf("S->data[%d] = %d\n",i,S->date[i]);
}
}
}
int main()
{
stack * S = Init_stack();
if(S == NULL)
printf("S == NULL\n");
else printf("OK\n");
for(int i = 0;i < 10;i++)
{
Push_Stack(S,i);
}
Show_Stack(S);
printf("LEN = %d\n",Stack_Len(S));
printf("TOP = %d\n",GetTop_Stack(S));
int j = 0;
Pop_Stack(S,j);
//printf("POP = %d\n",j);
Show_Stack(S);
printf("LEN = %d\n",Stack_Len(S));
printf("TOP = %d\n",GetTop_Stack(S));
free(S);
}
C++
#include <iostream>
#include <string.h>
#include <malloc.h>
#define MAXSIZE 1024
typedef int elempty;
class Stack
{
public:
Stack();//构造函数
~Stack();//析构函数
/*Stack(const Stack& rhs)
{
data = new elempty[MAXSIZE]();
memcpy(data,rhs.data,sizeof(elempty)*top);
top = rhs.top;
stacksize = rhs.stacksize;
}*/
bool IsEmpty(); //判空
bool Push(elempty val);//入栈
bool Pop();//出栈
int GetTop();//栈顶元素
int GetLen();//栈长
bool Clear();//清空
void Show();//打印
private:
bool IsFull()
{
if(top == (MAXSIZE - 1))
return true;
else return false;
}
elempty *data;
int top;
//int stacksize;
};
void Stack::Show()
{
for(int i = 0;i<=top;i++)
{
std::cout<<"data["<<i<<"]="<<data[i]<<std::endl;
}
}
bool Stack::IsEmpty()
{
if(top == -1)
return true;
else return false;
}
bool Stack::Push(int val)
{
if(IsFull())
return false;
else
{
top++;
data[top] = val;
return true;
}
}
bool Stack::Pop()
{
if(IsEmpty())
return false;
top--;
return true;
}
int Stack::GetLen()
{
if(IsEmpty())
return -1;
return top + 1;
}
int Stack::GetTop()
{
if(IsEmpty())
throw std::exception("stack is empty!");
return data[top];
}
bool Stack::Clear()
{
top = -1;
return true;
}
Stack::Stack()
{
std::cout<<"Stack()"<<std::endl;
top = -1;
//stacksize = 20;
data = new elempty[MAXSIZE]();
}
Stack::~Stack()
{
std::cout <<"~Stack()"<< std::endl;
delete []data;
data = NULL;
}
int main()
{
Stack S;
int i = 0;
while(i < 10)
{
S.Push(i);
i++;
}
S.Show();
std::cout<<"StackLen="<<S.GetLen()<<std::endl;
std::cout<<"StackTop="<<S.GetTop()<<std::endl;
S.Pop();
S.Show();
std::cout<<"StackLen="<<S.GetLen()<<std::endl;
std::cout<<"StackTop="<<S.GetTop()<<std::endl;
S.Clear();
S.Show();
std::cout<<"StackLen="<<S.GetLen()<<std::endl;
//std::cout<<"StackTop="<<S.GetTop()<<std::endl;
}
如果是初次接触数据结构的话建议不要直接复制,先找找大佬们写的详解来看看。这个大哥写的博客就比较详细
要是老师让交作业比较急的话可以直接拿来用,但还是建议交完作业了再好好看看书多了解了解。