c语言出栈pop函数 出栈函数pops返回的是什么值

C语言 出栈操作Pop(struct SqStack * MyStack, ElemType *e

#include stdio.h

10年积累的成都网站建设、网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计制作后付款的网站建设流程,更有永兴免费网站建设让你可以放心的选择与我们合作。

#include conio.h

#include stdlib.h

#define elemType int /* 链栈元素数据类型 */

#define SNODE_SIZE sizeof (struct sNode) /* 链栈结点空间大小 */

#define status int /* 状态型变量 */

#define OVERFLOW -1 /* 内存溢出状态码 */

#define ERROR 0 /* 错误状态码 */

#define OK 1 /* 正确状态码 */

/* 链栈结点存储结构 */

typedef struct sNode {

elemType data;

struct sNode *next;

} sNode, *sNodePtr;

/* 链栈存储结构 */

typedef struct linkStack {

sNodePtr top; /* 栈顶指针 */

} linkStack;

/* 初始化 */

/* 操作结果:构造一个带头结点的空链栈S */

void initStack (linkStack *S) {

S-top = (sNodePtr) malloc (SNODE_SIZE); /* 产生头结点,栈顶指针指向此头结点 */

if (!S-top) /* 内存分配失败 */

exit (OVERFLOW);

S-top-next = NULL;

}

/* 销毁 */

/* 初始条件:链栈S已存在。操作结果:销毁链栈S */

void destroyStack (linkStack *S) {

sNodePtr p, q;

p = S-top; /* p指向S的头结点 */

while (p) {

q = p-next; /* q指向p的下一个结点 */

free (p); /* 回收p指向的结点 */

p = q; /* p移动到下一个结点 */

} /* 直到没有下一个结点 */

}

/* 判断链栈是否为空 */

/* 初始条件:链栈S已存在。操作结果:若S为空链栈,则返回TRUE,否则返回FALSE */

status stackIsEmpty (linkStack *S) {

return S-top-next == NULL;

}

/* 入栈 */

/* 操作结果:在S的栈顶插入新的元素e */

status push (linkStack *S, elemType e) {

sNodePtr p;

p = (sNodePtr) malloc (SNODE_SIZE); /* 产生新结点 */

if (!p) /* 内存分配失败 */

exit (OVERFLOW);

p-data = e;

p-next = S-top-next; /* 将新结点链接到原栈顶 */

S-top-next = p; /* 栈顶指向新结点 */

}

/* 出栈 */

/* 操作结果:删除S的栈顶元素,并由e返回其值 */

status pop (linkStack *S, elemType *e) {

sNodePtr p;

if (stackIsEmpty (S))

return ERROR;

p = S-top-next; /* p指向链栈的第一个结点 */

*e = p-data; /* 取出数据 */

S-top-next = p-next;

free (p); /* 删除该结点 */

if (S-top == p) /* 栈为空 */

S-top-next = NULL;

return OK;

}

/* 打印栈内容 */

/* 初始条件:链栈S已存在。操作结果:当栈不为空时,打印栈内容并返回OK,否则返回ERROR */

status printStack (linkStack *S) {

sNodePtr p;

if (stackIsEmpty (S)) {

puts ("The stack is empty! ");

return ERROR;

}

p = S-top-next;

while (p) {

printf ("%d\t", p-data);

p = p-next;

}

putchar ('\n');

return OK;

}

int main (void) {

linkStack S;

elemType e; 

elemType a, b, c, d;

a = 1; b = 2; c = 3; d = 4;

initStack (S);

push (S, a);

push (S, b);

push (S, c);

push (S, d);

puts ("Push 4 elements");

printf ("S:\t");

printStack (S);

putchar ('\n');

pop (S, e);

puts ("Pop 1 element");

printf ("S:\t");

printStack (S);

destroyStack (S);

getch (); /* 屏幕暂留 */

return 0;

}

如有问题,可以点击头像联系我

C语言写Pop函数取栈的出错

Pop函数改成这样:

int Pop (Stack * pstack, int * pname)

{

if(pstack-top=0)

{

return 0;

}

pstack-top--;

* pname = pstack-data[pstack-top];

return 1;

}

Push函数改成这样:

int Push (Stack * pstack, int num)

{

if(pstack-top=Stack_size)

{

printf("Push Error!");

return 0;

}

pstack-data[pstack-top]=num;

pstack-top++;

return 0;

}

试试(原来那样当元素达到最大数目时pstack-top就越界了)。

C语言 push和pop函数可以直接用吗?

#include stdio.h

#include stdlib.h

#define MAXSIZE 32

typedef struct{

int *elem;/* 栈的存储区 */

  int max;   /* 栈的容量,即找中最多能存放的元素个数 */

  int top;   /* 栈顶指针 */ 

}Stack;

int InitStack(Stack *S, int n) /*创建容量为n的空栈*/

{

S-elem = (int *)malloc(n * sizeof(int));

if(S-elem==NULL) return -1;

S-max=n;

S-top =0; //栈顶初值0

return 0;

}

int Push(Stack *S, int item) /*将整数item压入栈顶*/

{

if(S-top==S-max) {

printf("Stack is full! \n");

return -1;

}

S-elem[S-top++] = item; //压栈,栈顶加1

return 0;

}

int StackEmpty(Stack S)

{

return (!S.top)?1:0; /*判断栈是否为空*/

}

int Pop(Stack *S) /*栈顶元素出栈*/

{

if(!S-top) {

printf("Pop an empty stack!\n");

return -1;

}

return S-elem[--S-top]  ; //弹出栈,栈顶减1

}

void MultibaseOutput(long n,int B)

{

int m; Stack S;

if(InitStack(S,MAXSIZE)){

printf("Failure!\n");

return;

}

do {

if (Push(S,B )) //------

{

printf("Failure!\n");

return;

}

n= n-1 ; //--------

}while(n!=0);

while(!StackEmpty(S)) { /*输出B进制的数*/

m=Pop(S);

if(m10) printf("%d",m); /*小于10,输出数字*/

else printf("%c", m+55); /*大于或等于10,输出相应的字符*/

}

printf("\n");

}

网站标题:c语言出栈pop函数 出栈函数pops返回的是什么值
本文路径:https://www.cdcxhl.com/article30/ddgcjpo.html

成都网站建设公司_创新互联,为您提供云服务器域名注册动态网站外贸网站建设做网站用户体验

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

手机网站建设