c语言栈的pop函数编写 栈pop函数怎么用

怎样用C语言写出对栈进行的五种运算:push()、pop()、top()、empty()、makempty()

这是我用链表写的:

成都创新互联长期为成百上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为江口企业提供专业的成都做网站、网站设计,江口网站改版等技术服务。拥有10年丰富建站经验和众多成功案例,为您定制开发。

#include stdio.h

#include stdlib.h

typedef struct node

{

int x;

struct node *next;

}Node;

typedef struct stack

{

Node *top;

}Stack;

void InitStack(Stack *s)

{

s-top=NULL;

}

int IsEmpty(Stack *s)

{

if(s-top==NULL)

return 1;

else

return 0;

}

void PushStack(Stack *s,int x)

{

Node *p;

p=(Node*)malloc(sizeof(Node));

p-x=x;

// p-next=NULL;

p-next=s-top;

s-top=p;

}

int PopStack(Stack *s)

{

int data;

Node *p;

p=(Node *)malloc(sizeof(Node));

if(IsEmpty(s))

{

printf("the stack is empty!\n");

free(p);

return -1;

}

else

{

p=s-top;

data=p-x;

s-top=p-next;

free(p);

return data;

}

}

int main (int argc,char **argv)

{

int i;

Stack s;

InitStack(s);

for(i=0;i1000;i++)

{

PushStack(s,i);

}

for(i=0;i1000;i++)

{

printf("%d\n",PopStack(s));

}

}

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

#include stdio.h

#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;

}

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

利用堆栈指针PUSH和POP编写一个C语言程序

#include stdio.h

#include stdlib.h

struct Node;

typedef struct Node *PtrToNode;

typedef PtrToNode Stack;

struct Node

{

int x;

PtrToNode Next;

};

int IsEmpty( Stack s )

{

return s-Next == NULL;

}

void Push( int x, Stack s )//压栈

{

PtrToNode TmpCell;

TmpCell = malloc( sizeof( struct Node ) );

if( TmpCell == NULL)

{

printf("Out of Space!!");

exit(0);

}

else

{

TmpCell-x = x;

TmpCell-Next = s-Next;

s-Next = TmpCell;

printf("%d has been pushed!\n",x);

}

}

int Top( Stack s )//返回栈顶元素

{

if( !IsEmpty( s ))

{

return s-x;

}

printf("The stack is Empty!");

return -1;

}

void Pop( Stack s )//出栈

{

PtrToNode FirstCell;

if( IsEmpty( s ) )

{

printf("The stack is Empty!");

return;

}

else

{

FirstCell = s-Next;

s-Next = s-Next-Next;

printf("%d has been poped!\n",FirstCell-x);

free(FirstCell);

}

}

void MakeEmpty( Stack s )//清空栈

{

if( s == NULL )

{

printf( "Must use CreateStack first" );

return;

}

else

{

while( !IsEmpty( s ) )

{

Pop(s);

}

}

}

Stack CreateStack()//创建新栈

{

Stack s = malloc( sizeof( struct Node ) );

if( s == NULL )

{

printf( "Out of space!" );

exit(0);

}

s-Next = NULL;

MakeEmpty( s );

return s;

}

void main()

{

int i;

Stack s;

s = CreateStack();

for(i=1;i=20;++i)//将1~20压入栈

{

Push(i,s);

}

for(i=1;i=20;++i)

{

Pop(s);

}

}

希望能帮到你,回答有点晚,希望来得及~

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语言栈的pop函数编写 栈pop函数怎么用
路径分享:http://scyanting.com/article/docseps.html