栈-C语言指针实现,请高手指点一下

angryagent 2012-12-26 12:51:03
自学C语言数据结构,书上用的是引用,由于VC不支持,我改成了指针。请高手指点一下



/***********************************************************************
*
***********************************************************************/

#include <stdio.h>
#include <stdlib.h>

struct SequentialStack
{
int* date; /*数据域*/
int top; /*date[top]指向栈顶元素,栈为空时,top=-1*/
int stacksize; /*栈空间的大小*/
};

typedef struct SequentialStack SqStack;
typedef SqStack* SSp;

/***********************************************************************
*0.错误函数
***********************************************************************/
void Error(char* s)
{
printf("%s\n",s);
}

/***********************************************************************
*1.初始化栈
***********************************************************************/
void InitSequentialStack(SSp p,int n)
{
p->date=(int*)malloc(n*sizeof(int));
p->top=-1;
p->stacksize=n;
}

/***********************************************************************
*2.销毁栈
***********************************************************************/
void DestroySequentialStack(SSp p)
{
p->top=-1;
p->stacksize=0;
free(p->date);
}


/***********************************************************************
*3.求栈长
***********************************************************************/
int LengthSequentialStack(SSp p)
{
return p->top + 1;
}


/***********************************************************************
*4.取栈顶元素
***********************************************************************/
int GetTopLengthSequentialStack(SSp p)
{
if(p->top != -1)
{
return p->date[p->top];
}
else
{
Error("Stack Empty!");
return 0;
}
}

/***********************************************************************
*5.入栈
***********************************************************************/
void PushSequentialStack(SSp p,int x)
{
if((p->top + 1) >= (p->stacksize))
{
Error("Stack Overflow!");
}
else
{
p->top = p->top + 1;
p->date[p->top] = x;
}
}


/***********************************************************************
*6.出栈
***********************************************************************/
int PopSequentialStack(SSp p)
{
if((p->top) >= 0)
{
int x = p->date[p->top];
p->top = p->top - 1;
return x;
}
else
{
Error("Stack Empty!");
return 0;
}
}


/***********************************************************************
*7.依次弹出栈中所有元素
***********************************************************************/
void PrintPopSequentialStack(SSp p)
{
int i = p->top;
printf("栈内元素如下:");
while(i >= 0)
{
printf("%d ",p->date[i]);
i = i -1;
}
printf("\n输出完毕\n\n");
}
...全文
241 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2012-12-27
  • 打赏
  • 举报
回复
引用 11 楼 angryagent 的回复:
引用 6 楼 zhao4zhong1 的回复:小心驶得万年船。 请检查所有函数的返回值。 如果输入值合法,那么函数就不会出现问题。不知道在哪些情况下应该信赖函数输入值?
在任何情况下都不要信赖函数输入值。
angryagent 2012-12-27
  • 打赏
  • 举报
回复
引用 6 楼 zhao4zhong1 的回复:
小心驶得万年船。 请检查所有函数的返回值。
如果输入值合法,那么函数就不会出现问题。不知道在哪些情况下应该信赖函数输入值?
angryagent 2012-12-27
  • 打赏
  • 举报
回复
引用 3 楼 turingo 的回复:
写得还是相当不错的,从代码中看出是个很适合编程的家伙,别让VC耽误了成长,建议看看我的博文(http://blog.csdn.net/turingo/article/details/8115042)。 引用 楼主 angryagent 的回复:自学C语言数据结构,书上用的是引用,由于VC不支持,我改成了指针。请高手指点一下 C/C++ code?12345……
看了你的博客,觉得你的博客不错
angryagent 2012-12-27
  • 打赏
  • 举报
回复
引用 4 楼 zhcosin 的回复:
内存分配成功失败都没检查......
确实有这个问题,谢谢提醒
赵4老师 2012-12-26
  • 打赏
  • 举报
回复
小心驶得万年船。 请检查所有函数的返回值。
BadPattern 2012-12-26
  • 打赏
  • 举报
回复
引用 3 楼 turingo 的回复:
写得还是相当不错的,从代码中看出是个很适合编程的家伙,别让VC耽误了成长,建议看看我的博文(http://blog.csdn.net/turingo/article/details/8115042)。 引用 楼主 angryagent 的回复:自学C语言数据结构,书上用的是引用,由于VC不支持,我改成了指针。请高手指点一下 C/C++ code?12345……
是的,Windows下开发一点也不爽,Linux我觉得是程序员最好的开发环境,看一些国外比较牛的书籍,除非说的就是windows的知识,否则大多基于linux环境
zhcosin 2012-12-26
  • 打赏
  • 举报
回复
内存分配成功失败都没检查......
图灵狗 2012-12-26
  • 打赏
  • 举报
回复
写得还是相当不错的,从代码中看出是个很适合编程的家伙,别让VC耽误了成长,建议看看我的博文(http://blog.csdn.net/turingo/article/details/8115042)。
引用 楼主 angryagent 的回复:
自学C语言数据结构,书上用的是引用,由于VC不支持,我改成了指针。请高手指点一下 C/C++ code?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697……
zhouxf_cn 2012-12-26
  • 打赏
  • 举报
回复
是C不支持引用,请用#include<iostream> using namespace std;
xxb249 2012-12-26
  • 打赏
  • 举报
回复
首先引用是C++的语法不是VC不支持 是C语言不支持 VC只是开发工具
proorck6 2012-12-26
  • 打赏
  • 举报
回复
yulitingfeng 2012-12-26
  • 打赏
  • 举报
回复
malloc ,free 如果能放在一起就尽量放在一起,尽量避免错误使用。

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧