主题:[讨论]关于栈的错误
// 栈.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
typedef int SElemType;
typedef struct
{
SElemType *top;
SElemType *base;
int StackLength; /*The number of element*/
int StackSize;
}SqStack;
void InitStack(SqStack *pSqStack)
{
pSqStack->base=(SElemType *)malloc(sizeof(SElemType));
pSqStack->top=pSqStack->base;
pSqStack->StackLength=0;
pSqStack->StackSize=1;
}
void DestroyStack()
{
}
void ClearStack(SqStack *pSqStack)
{
pSqStack->top=pSqStack->base;
pSqStack->StackLength=0;
}
bool StackEmpty(SqStack *pSqStack)
{
if(pSqStack->base==pSqStack->top)
{
return true;
}
else
{
return false;
}
}
bool GetTop(SqStack *pSqStack,SElemType *pElem)
{
if(StackEmpty(pSqStack))
{
return false;
}
else
{
*pElem=*(pSqStack->top);
return true;
}
return 0;
}
void Push(SqStack *pSqStack,SElemType elem )
{
if(pSqStack->StackLength==pSqStack->StackSize)
{
pSqStack->base=(SElemType *)realloc(pSqStack->base,(2*pSqStack->StackSize+1)*sizeof(SElemType));
pSqStack->StackSize=2*pSqStack->StackSize+1;
*(++pSqStack->top)=elem;
}
else
{
*(++pSqStack->top)=elem;
}
pSqStack->StackLength++;
}
void Pop(SqStack *pSqStack,SElemType *pElem)
{
*pElem=*pSqStack->top--;
pSqStack->StackLength--;
}
void Show(SqStack *pSqStack)
{
SElemType elem;
while(!StackEmpty(pSqStack))
{
Pop(pSqStack,&elem);
printf("%d\t",elem);
}
printf("\n\n");
}
int _tmain(int argc, _TCHAR* argv[])
{
SqStack MyStack;
InitStack(&MyStack);
SElemType elem;
Push(&MyStack,5);
Push(&MyStack,6);
Push(&MyStack,7);
//Pop(&MyStack,&elem);
//Pop(&MyStack,&elem);
//Push(&MyStack,8);
Push(&MyStack,9);
/*Push(&MyStack,10);
Push(&MyStack,11);
Push(&MyStack,12);*/
GetTop(&MyStack,&elem);
printf("%d\n",elem);
MyStack.top--;
GetTop(&MyStack,&elem);
printf("%d\n",elem);
MyStack.top--;
GetTop(&MyStack,&elem);
printf("%d\n",elem);
/*MyStack.top--;
GetTop(&MyStack,&elem);
printf("%d\n",elem);
MyStack.top--;
GetTop(&MyStack,&elem);
printf("%d\n",elem);
MyStack.top--;
GetTop(&MyStack,&elem);
printf("%d\n",elem);
//Show(&MyStack);
/*printf("%d\n",MyStack.StackLength);
printf("%d\n",MyStack.StackSize);*/
/*SElemType num=1;
SElemType temp;
scanf("%d",&num);
while(num!=0)
{
temp=num;
num=num%2;
Push(&MyStack,temp-num*2);
}*/
return 0;
}
//
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
typedef int SElemType;
typedef struct
{
SElemType *top;
SElemType *base;
int StackLength; /*The number of element*/
int StackSize;
}SqStack;
void InitStack(SqStack *pSqStack)
{
pSqStack->base=(SElemType *)malloc(sizeof(SElemType));
pSqStack->top=pSqStack->base;
pSqStack->StackLength=0;
pSqStack->StackSize=1;
}
void DestroyStack()
{
}
void ClearStack(SqStack *pSqStack)
{
pSqStack->top=pSqStack->base;
pSqStack->StackLength=0;
}
bool StackEmpty(SqStack *pSqStack)
{
if(pSqStack->base==pSqStack->top)
{
return true;
}
else
{
return false;
}
}
bool GetTop(SqStack *pSqStack,SElemType *pElem)
{
if(StackEmpty(pSqStack))
{
return false;
}
else
{
*pElem=*(pSqStack->top);
return true;
}
return 0;
}
void Push(SqStack *pSqStack,SElemType elem )
{
if(pSqStack->StackLength==pSqStack->StackSize)
{
pSqStack->base=(SElemType *)realloc(pSqStack->base,(2*pSqStack->StackSize+1)*sizeof(SElemType));
pSqStack->StackSize=2*pSqStack->StackSize+1;
*(++pSqStack->top)=elem;
}
else
{
*(++pSqStack->top)=elem;
}
pSqStack->StackLength++;
}
void Pop(SqStack *pSqStack,SElemType *pElem)
{
*pElem=*pSqStack->top--;
pSqStack->StackLength--;
}
void Show(SqStack *pSqStack)
{
SElemType elem;
while(!StackEmpty(pSqStack))
{
Pop(pSqStack,&elem);
printf("%d\t",elem);
}
printf("\n\n");
}
int _tmain(int argc, _TCHAR* argv[])
{
SqStack MyStack;
InitStack(&MyStack);
SElemType elem;
Push(&MyStack,5);
Push(&MyStack,6);
Push(&MyStack,7);
//Pop(&MyStack,&elem);
//Pop(&MyStack,&elem);
//Push(&MyStack,8);
Push(&MyStack,9);
/*Push(&MyStack,10);
Push(&MyStack,11);
Push(&MyStack,12);*/
GetTop(&MyStack,&elem);
printf("%d\n",elem);
MyStack.top--;
GetTop(&MyStack,&elem);
printf("%d\n",elem);
MyStack.top--;
GetTop(&MyStack,&elem);
printf("%d\n",elem);
/*MyStack.top--;
GetTop(&MyStack,&elem);
printf("%d\n",elem);
MyStack.top--;
GetTop(&MyStack,&elem);
printf("%d\n",elem);
MyStack.top--;
GetTop(&MyStack,&elem);
printf("%d\n",elem);
//Show(&MyStack);
/*printf("%d\n",MyStack.StackLength);
printf("%d\n",MyStack.StackSize);*/
/*SElemType num=1;
SElemType temp;
scanf("%d",&num);
while(num!=0)
{
temp=num;
num=num%2;
Push(&MyStack,temp-num*2);
}*/
return 0;
}