记录一下自己学习过程中写的代码。以下是我看严蔚敏老师的数据结构教材后,结合教材所讲用C语言实现了关于栈的顺序表示及其实现的基本操作,供以后复习所用。本程序建立的栈是一个顺序栈,即该栈是利用一组地址连续的存储单元依次存放自栈底到栈顶的数据元素,同时附设指针Top指示栈顶元素在顺序栈中的位置。具体做法是,先给栈分配一个基本容量,然后在应用过程中个,当栈的空间不够使用时再逐渐扩大。在初始化栈的时候,按照设定的初始分配量进行第一次存储分配,Bottom是栈底指针,在本栈中,它始终指向栈底的位置,若其值为NULL,则表明栈不存在。Top是栈顶指针,其初值指向栈底,即Top=Bottom可以作为栈空的标志。每当插入新元素时,Top增1;删除栈顶元素时,Top减一,因此,非空栈中的栈顶指针始终指向栈顶元素的下一个位置(并非指向栈顶元素位置)。可以看到,我用栈的顺序存储结构得到的结果,和前面利用栈的链式存储结构时得到的结果,是一样的。
编译软件:VC++6.0
测试用例结果截图如下:
源代码如下:
/**********************************
栈的顺序表示和实现(完整代码,C实现)
Author:大地在我腳下
Date:2016-8-6
Email:jsrcdjcyy@163.com
**********************************/
#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100 //存储空间初始分配量
#define STACKINCREMENT 10 //存储空间分配增量
typedef struct Stack
{
int* Top;
int* Bottom;
int Stacksize;//栈的实际长度
int size;//当前已分配的存储空间,以元素为单位
}Stack, *SqStack;
bool CreateStack(SqStack);
bool Push(SqStack,int);
bool Empty(SqStack);
int Pop(SqStack);
void Traverse(SqStack);
void Destroy(SqStack);
void Clear(SqStack);
int Stacklength(SqStack);
int GetTop(SqStack);
void main()
{Stack S;int data;
//创建一个空的栈
if(CreateStack(&S)==1)
printf("Successfully build stack!\n");
//进行入栈操作,之后遍历输出栈内数据
Push(&S,6);
Push(&S,25);
Push(&S,89);
Push(&S,127);
Push(&S,888);
Traverse(&S);
//栈的长度
printf("Now the length of stack is:%d\n",Stacklength(&S));
//取出栈顶元素
if(Empty(&S))
printf("\nNow the stack is empty!");
else printf("\nNow the top of stack is:%d\n",GetTop(&S));
//进行出栈操作,之后遍历输出
if(Empty(&S))
printf("\nNow the stack is empty!");
else
{printf("pop succeed!the pop data is:%d\n",Pop(&S));
Traverse(&S);
}
//重新输出栈的长度
printf("Now the length of stack is:%d\n",Stacklength(&S));
//重新取出栈顶元素
if(Empty(&S))
printf("\nNow the stack is empty!");
else printf("\nNow the top of stack is:%d\n",GetTop(&S));
//清空栈,并输出清空后栈中的数据
Clear(&S);
printf("\ndata cleared!\n");
Traverse(&S);
}
//建立顺序栈
bool CreateStack(SqStack S)
{(*S).Bottom=(int*)malloc(STACK_INIT_SIZE*sizeof(int));
if(!(*S).Bottom)
{printf("Malloc failed!");
exit(-1);
}
(*S).Top=(*S).Bottom;
(*S).size=STACK_INIT_SIZE;
(*S).Stacksize=0;
return true;
}
//进栈
bool Push(SqStack S,int e)
{if((*S).Top-(*S).Bottom>=(*S).size)//若栈满,追加存储空间
{(*S).Bottom=(int*)realloc((*S).Bottom,((*S).size+STACKINCREMENT)*sizeof(int));
if(!(*S).Bottom)
{printf("Realloc failed!");
exit(-1);
}
(*S).Top=(*S).Bottom+(*S).size;
(*S).size+=STACKINCREMENT;
}
*(*S).Top++=e;
(*S).Stacksize++;
return true;
}
//判断栈是否为空栈
bool Empty(SqStack S)
{if((*S).Top==(*S).Bottom) return true;
else return false;
}
//出栈,将出栈得到的值保存在e中,避免丢失
int Pop(SqStack S)
{int e;
if(Empty(S)) printf("\nNow the stack is empty!");
e=*--(*S).Top;
*(*S).Top=0;
(*S).Stacksize--;
return e;
}
//遍历并输出栈内数据
void Traverse(SqStack S)
{int* p=(*S).Top;
if(Empty(S)) printf("\nNow the stack is empty!\n");
else
{printf("Now datas int the stack are:\n");
while(p!=(*S).Bottom)
{printf("%d",*--p);
putchar(32);
}
printf("\n");
}
}
//销毁栈,栈不再存在
void Destroy(SqStack S)
{
free((*S).Bottom);
(*S).Top=NULL;
(*S).Bottom=NULL;
(*S).Stacksize=(*S).size=0;
}
//清空栈,把栈置为空栈
void Clear(SqStack S)
{if(Empty(S)) printf("\nNow the stack is empty!");
else Destroy(S);
}
//栈的长度,即栈内元素个数
int Stacklength(SqStack S)
{return (*S).Stacksize;
}
//取栈顶元素
int GetTop(SqStack S)
{int e;
if(Empty(S)) printf("\nNow the stack is empty!");
e=*((*S).Top-1);
return e;
}