c语言菜单程序链表形式,C语言里面链表的具体形式是什么

本文详细介绍了使用链表实现的栈数据结构,包括初始化、显示、压栈、弹栈、获取栈顶元素和销毁栈等操作。栈支持不同数据类型,如char、int、float和double,并通过typedef定义了Node和Stack结构体,实现了动态内存分配和释放。
摘要由CSDN通过智能技术生成

以下是链表表示的栈实现:

//--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 */。

全部

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值