栈(stack)是限制插入与删除只能在一个位置上进行的表,该位置叫做栈的顶(top)。栈的基本操作包括进栈(push)即插入、出栈(pop)即删除。不允许对空栈进行pop操作。
栈与队列不同,队列是先进先出(FIFO),栈是后进先出(LIFO),就是说只有栈顶的元素是可以被访问的。
与队列相似,栈也可以通过链表或者数组的形式实现。在单链表的顶端插入可以实现push,删除顶端元素实现pop,Top操作考察表顶端元素并返回它的值。
1.栈的链表实现:
#include <iostream>
using namespace std;
typedef struct stacknode
{
int data;
struct stacknode *next;
} Stacknode, *LinkStack;
int StackEmpty(LinkStack top)
{
if(top->next == NULL)
return 1;
else
return 0;
}
LinkStack Push(LinkStack top, int value) //入栈
{
Stacknode *newp = (Stacknode *) malloc( sizeof(Stacknode));
if (newp != NULL)
{
newp->data = value;
newp->next = top->next;
top->next = newp;
}
else
cout << "No memory available!\n";
return top;
}
int Pop(LinkStack top) //出栈
{
Stacknode *temp;
int t;
if(StackEmpty(top))
cout << "This stack is empty!\n";
else
{
temp = top->next;
t = temp->data;
top ->next = temp->next;
free(temp);
}
return t;
}
void PrintfStack(LinkStack top)
{
if(top->next == NULL)
cout << "stack is empty\n";
else
{
while (top->next != NULL)
{
cout << top->data << " ";
top = top->next;
}
}
}
int StackTop(LinkStack top)
{
if(StackEmpty(top))
cout << "the stack is empty\n";
else
return top->next->data;
}
int StackLenght(LinkStack top)
{
int len = 0;
while (top->next!= NULL)
{
len++;
top = top->next;
}
return len;
}
void DestoryStack(LinkStack top)
{
LinkStack q;
while (top)
{
q = top->next;
delete top;
top = q;
}
cout<<"the stack is destroied\n";
}
void InitStack(LinkStack top)
{
top->next = NULL;
top->data = NULL; //表头数据赋为 0
}
int main()
{
LinkStack stack;
stack = (LinkStack) malloc(sizeof(Stacknode));
InitStack(stack);
for (int i = 0; i < 10; i++)
{
Push(stack, i);
}
cout<<"the stack is:\n";
PrintfStack(stack);
int len = StackLenght(stack);
cout << "\nthe lenght of stack is : " << len << endl;
cout << "top value od stack is: " << StackTop(stack)<<endl;
cout <<"try to clear stack\n";
DestoryStack(stack);
system("pause");
return 0;
}
2.栈的数组实现
#include <stdio.h>
#include <stdlib.h>
#define overflow -2
#define STACK_INTT_SIZE 100
#define STACK_INTT_INCREMENT 20
#define ElemType int
struct SqStack
{
ElemType *base, *top;
int stackSize;
};
/*
int InitStack(SqStack &s) 初始化栈
int DestoryStack(SqStack &s) 消毁栈
int clearStack(SqStack &s) 清除栈
bool StackEmpty(SqStack s) 栈是否为空
int StackLength(SqStack s) 栈的长度
int GetTop(SqStack s, ElemType &e) 得到栈顶
int Push(SqStack &s, ElemType e) 压栈
int Pop(SqStack &s, ElemType &e) 出栈
void DisplayStack(SqStack s) 显示栈内元素
*/
int InitStack(SqStack &s) //&表示参数是栈的引用,C++里的
{
s.base = (ElemType *) malloc(STACK_INTT_SIZE * (sizeof(ElemType)));
if(!s.base)
return false;
else
s.top = s.base;
s.stackSize = STACK_INTT_SIZE;
}
int DestoryStack(SqStack &s)
{
s.top = s.base;
free(s.base);
s.base = NULL;
s.top = NULL;
return true;
}
bool StackEmpty(SqStack s)
{
if(s.base == s.top)
return true;
else
return false;
}
int StackLenght(SqStack s)
{
if(s.base = s.top)
return false;
else
{
return (s.top - s.base);
}
}
int GetTop(SqStack s, ElemType &e)
{
if (StackEmpty(s))
{
printf("This stack is empty.\n");
return false;
}
else
{
s.top--;
e = *s.top;
return true;
}
}
int Push(SqStack &s, ElemType e)
{
if (StackLenght(s) == STACK_INTT_SIZE)
{
ElemType *temp = (ElemType*)realloc(s.base, (STACK_INTT_INCREMENT)*(sizeof(ElemType)));
if(!temp)
return false;
s.base = temp;
s.top = s.base + STACK_INTT_SIZE;
s.stackSize = STACK_INTT_SIZE + STACK_INTT_INCREMENT;
*(s.top++) = e;
return true;
}
else
{
*s.top = e;
s.top++;
return true;
}
}
int Pop(SqStack &s, ElemType &e)
{
if (StackEmpty(s))
{
printf("this stack is empty\n");
return false;
}
else
{
e = *(--s.top);
return true;
}
}
int ClearStack(SqStack &s)
{
s.top = s.base;
s.stackSize = 0;
return true;
}
void DisplayStack(SqStack s)
{
if(StackEmpty(s))
{
printf("this stack is empty\n");
exit(-1);
}
while (s.top != s.base)
{
printf("%d\t",*(--s.top));
}
printf("\n");
}
int main()
{
SqStack stack;
InitStack(stack);
for (int i = 0; i < 5; i++)
{
if(Push(stack, i))
printf("%d is push in this stack successfully\n", i);
else
{
printf("/n/that happen an error\n");
}
}
DisplayStack(stack);
int temp;
printf("now I will print this top of stack! \n");
GetTop(stack, temp);
printf("%d is top of this stack\n", temp);
DestoryStack(stack);
system("pause");
return 0;
}
参考:http://blog.csdn.net/sszgg2006/article/details/7555974
http://www.cnblogs.com/xmaomao/archive/2013/08/28/3288218.html