// Stack.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
//*
#define STACK_MAX_SIZE 7
int StackData[STACK_MAX_SIZE] = {'A','B','C','D','E','F','G'};
//*/
/*
#define STACK_MAX_SIZE 14
int StackData[STACK_MAX_SIZE] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N'};
*/
/*顺序栈类型定义*/
typedef struct
{
int data[STACK_MAX_SIZE];
int top;
}sqStack;
/*链栈类型定义*/
struct LinkStack
{
int data;
struct LinkStack *link;
};
typedef struct LinkStack lkStack;
/*顺序栈基本操作函数定义部分*/
void sqInitialize(sqStack *stack)
{
stack->top = 0;
}
int sqIsEmpty(sqStack *stack)
{
if(stack->top == 0)
return(1);
else
return(0);
}
int sqIsFull(sqStack *stack)
{
if(stack->top >= STACK_MAX_SIZE)
return(1);
else
return(0);
}
int sqPush(sqStack *stack,int data)
{
if(sqIsFull(stack))
{
printf("堆栈已満!/n");
return(0);
}
else
{
stack->data[stack->top++] = data;
return(1);
}
}
int sqPop(sqStack *stack,int *p)
{
if(sqIsEmpty(stack))
{
printf("堆栈已空!/n");
return(0);
}
else
{
*p = stack->data[--stack->top];
return(1);
}
}
int sqGetTop(sqStack *stack,int *p)
{
if(sqIsEmpty(stack))
{
printf("堆栈已空!/n");
return(0);
}
else
{
*p = stack->data[stack->top];
return(1);
}
}
void sqWriteValue(sqStack *stack,int array[],int n)
{
int i;
for(i=0;i<n;i++)
if(sqPush(stack,array[i])) printf("Stack[%d] = %c/t",stack->top-1,array[i]);
}
void sqPrintValue(sqStack *stack)
{
int StackData,i;
i=0;
while(sqPop(stack,&StackData))
printf("Stack[%d] = %c/t",stack->top,StackData);
printf("/n");
}
/*链栈基本操作函数定义部分*/
void lkInitialize(lkStack *top)
{
top = NULL;
}
int lkIsEmpty(lkStack *top)
{
if(top == NULL)
return(1);
else
return(0);
}
int lkPush(lkStack *top,int data)
{
lkStack *q;
q = (lkStack *)malloc(sizeof(lkStack));
if(q==NULL)
return(0);
else
{
q->data = data;
q->link = NULL;
top->link = q;
top = q;
return(1);
}
int lkPop(struct LinkStack *stack,int *p)
{
struct LinkStack *q;
if(lkIsEmpty(stack))
{
printf("堆栈已空!/n");
return(0);
}
else
{
q = stack;
*p = stack->data;
stack = stack->next;
//free(q);
return(1);
}
}
int lkGetTop(struct LinkStack *stack,int *p)
{
if(lkIsEmpty(stack))
{
printf("堆栈已空!/n");
return(0);
}
else
{
*p = stack->data;
return(1);
}
}
void lkWriteValue(struct LinkStack *stack,int n)
{
int i;
for(i=0;i<n;i++)
if(lkPush(stack,i*10)) printf("Stack[%d] = %d/t",i,i*10);
}
void lkPrintValue(struct LinkStack *stack,int n)
{
int StackData,i;
i=0;
for(i=0;i<n;i++)
{
lkPop(stack,&StackData);
printf("Stack[%d] = %d/t",i++,StackData);
}
printf("/n");
}
int main(int argc, char* argv[])
{
sqStack *stack1;
struct LinkStack *stack2;
//顺序栈处理部分
sqInitialize(stack1);
printf("/n顺序栈[入栈]数据........./n");
sqWriteValue(stack1,StackData,STACK_MAX_SIZE);
printf("/n顺序栈[出栈]数据........./n");
sqPrintValue(stack1);
/*
//链栈处理部分
lkInitialize(stack2);
printf("/n链栈入栈数据........./n");
lkWriteValue(stack2,10);
printf("/n链栈出栈数据........./n");
lkPrintValue(stack2,10);
*/
return 0;
}