浮云间
//LStack.h//堆栈的链式存储结构#include<malloc.h>#include<process.h>//定义结点结构体typedef struct StackNode{ElemType data; //数据域struct StackNode *next; //指针域}StackNode;//定义栈顶指针typedef struct{StackNode *top; //栈顶指示器}LinkStack;//初始化void InitStack(LinkStack &S){if(NULL==(S.top=(StackNode *)malloc(sizeof(StackNode)))) //申请栈顶结点空间exit(0); //申请失败推出程序S.top->next=NULL; //申请成功栈顶结点指针域初始化为空S.top->data=NULL; //数据域初始化为NULL}//判断栈是否为空int isEmpty(LinkStack &S){if(NULL==S.top->next&&NULL==S.top->data) //栈顶没有后继结点表明为空栈return 0; //返回0return 1; //非空时返回1}//入栈int Push(const ElemType &item,LinkStack &S) //此处最好把item定义为抽象数据类型ElemType{StackNode *snp;if(NULL==(snp=(StackNode *)malloc(sizeof(StackNode)))) //申请新结点空间return 0;snp->data=item;snp->next=S.top;S.top=snp;return 1;}//出栈int Pop(LinkStack &S){ElemType reData;StackNode *indexp;if(!isEmpty(S)) //栈空退出return 0;reData=S.top->data; //取出数据域存储的数据indexp=S.top; //保存当前栈顶结点S.top=S.top->next; //栈顶变为top的后继结点free(indexp); //释放原栈顶结点空间return reData; //返回栈顶数据}//取栈顶数据int GetTop(LinkStack &S){ElemType reData;if(!isEmpty(S)) //栈空退出return 0;reData=S.top->data; //取出数据return reData;}//清空栈void MakeEmpty(LinkStack &S){StackNode *p,*pp;p=S.top;while(p!=NULL){pp=p;p=p->next;free(pp);}}LStack.cpp#include<stdio.h>typedef int ElemType;#include "LStack.h"int main() //测试堆栈的链式存储结构{LinkStack T;int a[10]={1,3,5,7,9,11,13,15,17,19};InitStack(T); //初始化栈for(int i=0;i<10;i++)Push(a[i],T); // 入栈printf("栈顶数据=%d\n",GetTop(T)); //取栈顶数据printf("出栈:");while(isEmpty(T)) //栈不空时继续出栈{printf("%-4d",Pop(T)); //出栈}MakeEmpty(T); //清空栈}
宝慕林4294392
/// 按照你的要求写出来了,并且改了两个你的错误,,/// 给分吧,,呵呵,祝你学习愉快// 刚刚把2个C++的内存分配改成c的了#include <stdio.h>#include <malloc.h>#define ElemType inttypedef struct StackNode{ElemType data; //存放数据struct StackNode * next; //指向下一个结点}StackNode;typedef struct LinkStack{struct StackNode * top; // 栈顶指针}LinkStack;void InitStack(LinkStack &S); // 栈的初始化void Push(const int &item,LinkStack &S); // 入栈int Pop(LinkStack &S); // 出栈int GetTop(LinkStack &S); // 取栈顶元素int IsEmpty(LinkStack &S); // 判断栈是否为空void MakeEmpty(LinkStack &S); // 清空栈// 打印堆栈内容void PrintStack(LinkStack &S){StackNode *pSN = S.top;while (pSN->next != 0){printf("%d " , pSN->data);pSN = pSN->next;}printf("\n");}void main(){LinkStack Stack; // 栈顶int iItem = 0 ,i = 0;/// 初始化InitStack(Stack);// 压入内容for (i = 0 ; i < 10 ; i ++){Push(i , Stack);}printf("栈内内容为:");PrintStack(Stack);// 弹出iItem = Pop(Stack);printf("\n弹出%d:" , iItem);printf("\n栈内内容为:");PrintStack(Stack);// 弹出iItem = Pop(Stack);printf("\n弹出%d:" , iItem);printf("\n栈内内容为:");PrintStack(Stack);//清空栈MakeEmpty(Stack);}// 栈的初始化void InitStack(LinkStack &S){S.top = (struct StackNode*)malloc(sizeof(StackNode));S.top->data = 0;S.top->next = 0;}// 入栈void Push(const int &item , LinkStack &S){StackNode *pSN = (struct StackNode*)malloc(sizeof(StackNode));pSN->data = item;pSN->next = S.top;S.top = pSN;}// 出栈int Pop(LinkStack &S){if (IsEmpty(S)){return 0;}StackNode *pSN = S.top;int iRet = pSN->data;S.top = pSN->next;delete pSN;return iRet;}// 取栈顶元素int GetTop(LinkStack &S){if (IsEmpty(S)){return 0;}return S.top->data;}// 判断栈是否为空int IsEmpty(LinkStack &S){StackNode *pSN = S.top->next;if (0 == pSN->next){return 1;}return 0;}// 清空栈void MakeEmpty(LinkStack &S){while (!IsEmpty(S)){Pop(S);}if (S.top != 0){delete S.top;}S.top = 0;}