顺序栈和链式栈的时间复杂度均为O(1), 因此唯一可以比较的是空间性能。初始时顺序栈必须开辟一个固定的长度内存,所以存在可存储元素个数限制和浪费空间的问题。链式栈没有栈满的问题,只有当内存空间用完才会出现栈满的情况。但是每个元素都需要一个指针域,从而存在结构性开销。所以当栈的使用元素个数变化较大时,应采用链栈;反之应该采用顺序栈。
顺序存储结构及C语言实现
#include<iostream>
using namespace std;
#define StackSize 100
//定义数据类型
typedef int DataType;
//定义结构体
typedef struct
{
DataType data[StackSize];
int top;
}SeqStack;
//初始化栈
void InitStack(SeqStack *S) {
S->top = -1; //要注意处置状态时栈顶位置为-1
}
//入栈操作
int Push(SeqStack *S, DataType x) {
if(S->top == StackSize - 1) {
printf("Failed, because the stack is full!\n");
return 0;
}
S->data[++S->top] = x;
return 1;
}
//出栈操作
int Pop(SeqStack *S, DataType *ptr) {
if(S->top == -1) {
printf("Failed, because the stack is empty!\n");
return 0;
}
*ptr = S->data[S->top--];
return 1;
}
//获取栈顶元素
int GetTop(SeqStack *S, DataType *ptr) {
if(S->top == -1) {
printf("ERROR, the stack is empty, you can't get the top of the stack!\n");
return 0;
}
*ptr = S->data[S->top];
return 1;
}
//判断栈是否为空
int Empty(SeqStack *S) {
if(S->top == -1)
return 1;
else
return 0;
}
//主函数
int main() {
DataType x;
SeqStack S;
InitStack(&S); //初始化栈
printf("We want to insert 10 and 15. \n");
Push(&S, 10); //元素10入栈
Push(&S, 15); //元素15入栈
if(GetTop(&S, &x) == 1) //打印栈顶元素
printf("Now, the top element of the stack is %d \n", x);
if(Pop(&S, &x) == 1) //出栈
printf("Doing a Pop, the element you delete is : %d\n", x);
if(GetTop(&S, &x) == 1) //打印栈顶元素
printf("Now, the top element of the stack is %d \n", x);
printf("you can push a new element into stack.\n");
scanf("%d", &x); //自定义元素入栈
Push(&S, x); //然后输出栈顶元素
if(Empty(&S) == 1)
printf("your stack is empty!\n");
else {
printf("your stack isn't empty!\t");
if(GetTop(&S, &x) == 1)
printf("And the top element of the stack is %d \n", x);
}
system("pause");
return 0;
}
链式存储结构及C语言实现
#include<iostream>
using namespace std;
#define MaxSize 100
typedef int DataType;
//定义节点结构体
typedef struct Node
{
DataType data;
struct Node *next;
}Node;
//初始化
Node * InitStack() {
Node *top = NULL;
return top;
}
//销毁栈
void DestroyStack(Node *top) {
Node *p = top;
while (top != NULL) {
printf("%d\t", top->data);
top = top->next;
free(p);
p = top;
}
}
//入栈操作
void Push(Node **top, DataType x) {
Node *s = (Node *)malloc(sizeof(Node));
s->data = x;
s->next = *top;
*top = s;
}
//出栈操作
int Pop(Node **top, DataType *ptr) {
Node *p = *top;
if (top == NULL) {
printf("1ERROR, the stack is empty!\n");
return 0;
}
*ptr = (*top)->data;
*top = (*top)->next;
free(p);
return 1;
}
//获取栈顶元素
int GetTop(Node *top, DataType *ptr) {
if (top == NULL) {
printf("2ERROR, the stack is empty!\n");
return 0;
}
*ptr = top->data;
return 1;
}
//判断栈是否为空
int Empty(Node *top) {
if (top == NULL)
return 1;
else
return 0;
}
int main() {
DataType x;
Node *top = InitStack();
printf("We want to insert 10 and 15. \n");
Push(&top, 10); //元素10入栈
if (GetTop(top, &x) == 1) //打印栈顶元素
printf("Now, the top element of the stack is %d \n", x);
Push(&top, 15); //元素15入栈
if (GetTop(top, &x) == 1) //打印栈顶元素
printf("Now, the top element of the stack is %d \n", x);
if (Pop(&top, &x) == 1) //出栈
printf("Doing a Pop, the element you delete is : %d\n", x);
if (GetTop(top, &x) == 1) //打印栈顶元素
printf("Now, the top element of the stack is %d \n", x);
printf("you can push a new element into stack: \t");
scanf("%d", &x); //自定义元素入栈
Push(&top, x); //然后输出栈顶元素
if (Empty(top) == 1)
printf("your stack is empty!\n");
else {
printf("your stack isn't empty!\t");
if (GetTop(top, &x) == 1)
printf("And the top element of the stack is %d \n", x);
}
DestroyStack(top);
system("pause");
return 0;
}