以下是链表表示的栈实现:
//--Symbol。h
//--symbol。h - definitions/declarations for symbols used by other C Header files
#ifndef SYMBOL_H
#define SYMBOL_H
#define STACK_H
#define TRUE 1
#define OK 1
#define YES 1
#define FALSE 0
#define ERROR 0
#define NO 0
#define OVERFLOW -1
#ifndef NULL
#define NULL 0
#endif
typedef unsigned int UINT;
typedef int STATUS;
typedef int BOOL;
#endif /*SYMBOL_H*/
//--stack。
h - definitions/declarations for stack operation
#ifndef STACK_H
#define STACK_H
#include "Symbol。
h"
#include
#include
#include
#include
#include
#define S_CHAR 1
#define S_SHORT 2
#define S_INT 3
#define S_FLOAT 4
#define S_DOUBLE 5
//-- Stack。
h
typedef struct tagNode
{
void*pData;
struct tagNode*pNext;
}Node,*PNode;
typedef struct tagStack
{
UINT uType;
/*1 char
*2 short/short int
*3 int
*4 float
*5 double
*/
UINT uLength;
struct tagNode*pFirst;
struct tagNode*pTop;
}Stack,*PStack;
STATUS InitStack(PStack pStack,UINT uType)
{
pStack->uType=uType;
pStack->uLength=0;
pStack->pFirst=pStack->pTop=NULL;
return OK;
}
STATUS ShowStack(PStack pStack)
{
PNode pNode=pStack->pFirst;
while(pNode)
{
switch(pStack->uType)
{
case S_CHAR: // char
printf("%c ",*(char*)(pNode->pData));break;
case S_FLOAT: // float
printf("%-4。
1f ",*(float*)(pNode->pData));
}
pNode=pNode->pNext;
}
putchar(10);
return OK;
}
STATUS Push(PStack pStack,void*pData)
{
PNode pNode=(PNode)malloc(sizeof(Node));
if(!pNode)
{
printf("
malloc error!
");
fflush(stdin);
getch();
exit(ERROR);
}
if(pStack->uType==1)
{
pNode->pData=(char*)malloc(sizeof(char));
*(char*)(pNode->pData)=*(char*)pData;
}
else if(pStack->uType==3)
{
pNode->pData=(int*)malloc(sizeof(int));
pNode->pData=(int*)malloc(sizeof(int));
*(int*)(pNode->pData)=*(int*)pData;
}
else if(pStack->uType==4)
{
pNode->pData=(float*)malloc(sizeof(float));
pNode->pData=(float*)malloc(sizeof(float));
*(float*)(pNode->pData)=*(float*)pData;
}
else if(pStack->uType==5)
{
pNode->pData=(double*)malloc(sizeof(double));
pNode->pData=(double*)malloc(sizeof(double));
*(double*)(pNode->pData)=*(double*)pData;
}
pNode->pNext=NULL;
if(!pStack->pTop)
pStack->pTop=pStack->pFirst=pNode;
else
{
pStack->pTop->pNext=pNode;
pStack->pTop=pNode;
}
pStack->uLength ;
return OK;
}
STATUS Pop(PStack pStack,void*pData)
{
PNode pPre=pStack->pFirst;
if(pStack->pTop!=pStack->pFirst)
while(pPre->pNext!=pStack->pTop)
pPre=pPre->pNext;
else
pPre=NULL;
if(pStack->uType==1)
*(char*)(pData)=*(char*)(pStack->pTop->pData);
else if(pStack->uType==3)
*(int*)(pData)=*(int*)(pStack->pTop->pData);
else if(pStack->uType==4)
*(float*)(pData)=*(float*)(pStack->pTop->pData);
else if(pStack->uType==5)
*(double*)(pData)=*(double*)(pStack->pTop->pData);
free(pStack->pTop->pData);
free(pStack->pTop);
pStack->pTop=pPre;
if(pPre)
pStack->pTop->pNext=NULL;
else
pStack->pFirst=NULL;
pStack->uLength--;
return OK;
}
STATUS GetTop(PStack pStack,void*pData)
{
if(pStack->uType==1)
*(char*)(pData)=*(char*)(pStack->pTop->pData);
else if(pStack->uType==3)
*(int*)(pData)=*(int*)(pStack->pTop->pData);
else if(pStack->uType==4)
*(float*)(pData)=*(float*)(pStack->pTop->pData);
else if(pStack->uType==5)
*(double*)(pData)=*(double*)(pStack->pTop->pData);
return OK;
}
STATUS DestroyStack(PStack pStack)
{
PNode pPre1,pPre2;
pPre1=pPre2=pStack->pFirst;
while(pPre1)
{
pPre1=pPre1->pNext;
free(pPre2->pData);
free(pPre2);
pPre2=pPre1;
}
pStack->pFirst=pStack->pTop=NULL;
pStack->uLength=0;
return OK;
}
#endif /* STACK_H */。
全部