一、栈的数组实现
#include <stdio.h>
#include <malloc.h>
#define MAX_SIZE_STACK 1024 /*栈的大小*/
#define IS_EMPTY_STACK 1;
#define IS_NOT_EMPTY_STACK 0;
typedef struct stack_node
{
int data[MAX_SIZE_STACK];
int top;/*指向栈顶*/
}SeqStack;
SeqStack *Init_SeqStack(); /*初始化栈操作*/
void Free_SeqStack(SeqStack *Stack); /*栈的释放操作*/
int Empty_SeqStack(SeqStack*Stack); /*栈的判空操作*/
int Push_SeqStack(SeqStack *Stack,int data); /*压栈操作*/
int Pop_SeqStack(SeqStack *Stack,int *pstdata); /*出栈操作*/
int Top_SeqStack(SeqStack *Stack); /*取栈顶元素*/
int Length_SeqStack(SeqStack *Stack); /*获取栈的元素个数*/
SeqStack *Init_SeqStack()
{
SeqStack *Stack = NULL;
Stack = (SeqStack *) malloc(sizeof(SeqStack));
if(NULL == Stack)
{
printf("\r\n Malloc stack memory failed!\r\n");
return NULL;
}
else
{
Stack->top= -1; /*初始化-1*/
return Stack;
}
}
void Free_SeqStack(SeqStack *Stack)
{
if (Stack == NULL)
{
return;
}
free(Stack);
return;
}
int Empty_SeqStack(SeqStack*Stack)/*空栈判断*/
{
if(Stack->top == -1)
{
return IS_EMPTY_STACK;
}
else
{
return IS_NOT_EMPTY_STACK;
}
}
int Push_SeqStack(SeqStack *Stack,int data)/*压栈操作*/
{
if(Stack->top == MAX_SIZE_STACK -1)
{
return 0;/*栈已满无法入栈*/
}
else
{
Stack->top++;
Stack->data[Stack->top] = data;
return 1;/*将数据压入栈内*/
}
}
int Pop_SeqStack(SeqStack *Stack,int *pstdata)
{
if(Empty_SeqStack(Stack) || NULL == pstdata)
{
return 0;/*空栈没数据可取*/
}
else
{
*pstdata = Stack->data[Stack->top];
Stack->top--;
return 1;/*从栈中取出数据*/
}
}
int Top_SeqStack(SeqStack *Stack)/*取栈顶元素*/
{
if(Empty_SeqStack(Stack))
{
return 0;/*空栈无数据可取*/
}
else
{
return Stack->data[Stack->top];
}
}
int Length_SeqStack(SeqStack *Stack)/*获取栈的元素个数*/
{
int len =0;
while(Stack->top >= 0)
{
len++;
Stack->top--;
}
return len;
}
int Print_SeqStack(SeqStack *Stack)
{
int i;
printf("\r\n 打印栈的元素:\n");
for(i=Stack->top;i>=0;i--)
printf("%-5d",Stack->data[i]);
printf("\r\n");
return 0;
}
int main(void) {
SeqStack *L;
int n,num,m;
int i;
L = Init_SeqStack();
if(NULL == L)
{
printf("栈初始化失败,直接返回\n");
return 0;
}
printf("栈初始化完成\n");
printf("栈空:%d\n",Empty_SeqStack(L));
printf("请输入入栈元素个数:\n");
scanf("%d",&n);
printf("请输入要入栈的%d个元素:\n",n);
for(i = 0;i < n; i++)
{
scanf("%d",&num);
Push_SeqStack(L,num);
}
Print_SeqStack(L);
printf("栈顶元素:%d\n",Top_SeqStack(L));
printf("请输入要出栈的元素个数(不能超过%d个):\n",n);
scanf("%d",&n);
printf("依次出栈的%d个元素:\n",n);
for(i=0;i<n;i++)
{
Pop_SeqStack(L,&m);
printf("%-5d",m);
}
printf("\n");
Print_SeqStack(L);
printf("栈顶元素:%d\n",Top_SeqStack(L));
printf("\r\n栈的元素个数:%d\r\n",Length_SeqStack(L));
return 0;
}
二、栈的链表实现
#include <stdio.h>
#include <malloc.h>
typedef int ElementType;
typedef struct stack_node
{
ElementType Element;
struct stack_node *Next;
}StackNode;
ElementType TopStackElement(StackNode *S);
void DestroyStack(StackNode *S);
void PushStack(StackNode *S,ElementType data);
void PopStack(StackNode *S,ElementType *pstdata);
int IsEmptyStack( StackNode *S );
StackNode * CreatStack( void);
void PrintStack(StackNode *S);
int Length_SeqStack(StackNode *S);
/************************************************************************/
/* 获取栈顶元素 */
/************************************************************************/
ElementType TopStackElement(StackNode *S)
{
if(IsEmptyStack(S))
{
printf("\r\n The stack is empty.\r\n");
return 0;
}
else
{
return S->Next->Element;
}
}
/************************************************************************/
/* 销毁栈操作 */ */
/************************************************************************/
void DestroyStack(StackNode *S)
{
ElementType data;
while (!IsEmptyStack(S))
{
PopStack(S,&data);
}
free(S);//删掉栈的头结点
printf("\r\n Destroy the stack.\r\n");
}
/************************************************************************/
/*压栈操作 */
/************************************************************************/
void PushStack(StackNode *S,ElementType data)
{
StackNode *pNewElement;
pNewElement = (StackNode *)malloc(sizeof(StackNode));
if (NULL == pNewElement)
{
printf("\r\n Failed to malloc new stack node.\r\n");
}
else
{
pNewElement->Element = data;
pNewElement->Next = S->Next;
S->Next = pNewElement;
}
}
/************************************************************************/
/* 出栈操作 */
/************************************************************************/
void PopStack(StackNode *S,ElementType *pstdata)
{
StackNode *pTopElement;
if(IsEmptyStack(S) || (NULL == pstdata))
{
printf("\r\n The stack is empty.\r\n");
}
else
{
pTopElement = S->Next;
*pstdata = pTopElement->Element;
S->Next = pTopElement->Next;
free(pTopElement);
}
}
/************************************************************************/
/* 栈的判空,如果栈为空,则返回1,若栈不为空,则返回0 */
/************************************************************************/
int IsEmptyStack( StackNode *S )
{
if(S->Next == NULL)
{
return 1;
}
return 0;
}
/************************************************************************/
/*创建一个新栈,返回栈指针 */
/************************************************************************/
StackNode * CreatStack( void)
{
StackNode *Stack;
Stack = (StackNode *)malloc(sizeof(StackNode));
if (NULL == Stack)
{
printf("\r\n Malloc stack node error.");
return NULL;
}
Stack->Next = NULL; //S结点代表栈头结点,其Next指向存储数据的栈结点
return Stack;
}
/************************************************************************/
/* 打印栈 */
/************************************************************************/
void PrintStack(StackNode *S)
{
StackNode *ptemp = NULL;
ptemp = S->Next;
while(ptemp)
{
printf("%-5d",ptemp->Element);
ptemp = ptemp->Next;
}
printf("\r\n");
}
/************************************************************************/
/*计算栈中元素个数 */
/************************************************************************/
int Length_SeqStack(StackNode *S)
{
StackNode *ptemp = NULL;
int len = 0;
ptemp = S->Next;
while(ptemp)
{
len++;
ptemp = ptemp->Next;
}
return len;
}
int _tmain(int argc, _TCHAR* argv[])
{
ElementType m;
StackNode *Stack;
int i,n,num;
Stack = CreatStack();
if (NULL == Stack)
{
printf("\r\n Creat stack failed.");
return 0;
}
else
{
printf("\r\n Creat stack success.");
}
printf("\r\nStack is empty:%d\n",IsEmptyStack(Stack));
printf("请输入入栈元素个数:\n");
scanf("%d",&n);
printf("请输入要入栈的%d个元素:\n",n);
for(i = 0;i < n; i++)
{
scanf("%d",&num);
PushStack(Stack,num);
}
PrintStack(Stack);
printf("栈顶元素:%d\n",TopStackElement(Stack));
printf("请输入要出栈的元素个数(不能超过%d个):\n",n);
scanf("%d",&n);
printf("依次出栈的%d个元素:\n",n);
for(i=0;i<n;i++)
{
PopStack(Stack,&m);
printf("%-5d",m);
}
printf("\n");
PrintStack(Stack);
printf("栈顶元素:%d\n",TopStackElement(Stack));
printf("\r\n栈的元素个数:%d\r\n",Length_SeqStack(Stack));
DestroyStack(Stack);
return 0;
}