您的位置:首页 > 编程语言 > C语言/C++

C/C++ 知识回顾 栈的入栈与出栈

2017-03-06 22:17 316 查看
#include<iostream>
using namespace std;
typedef struct student
{
int data;
struct student * next;
}node;

typedef struct stackqueque
{
node * top, *zhandi;
} Stack;

Stack * push(Stack * stack,int num)
{
node *s = (node *)malloc(sizeof(node));
s->data = num;
s->next = NULL;
if (stack->top==NULL)
{
stack->top = s;
stack->zhandi = s;
}
else
{
stack->top->next = s;
stack->top = s;
}
return stack;
}
int  pop(Stack *stack)
{
if (stack->top==NULL)
{
cout << "栈为空" << endl;
return -1;
}
if (stack->top==stack->zhandi)
{
int x = stack->top->data;
stack->top = NULL;
stack->zhandi = NULL;
return x;
}
else
{

node*	p = stack->zhandi;
while (p->next!=stack->top)
{
p = p->next;
}
node *q = stack->top;
int x = stack->top->data;
p->next = NULL;
stack->top = p;
free(q);
return x;

}

}
int main()
{

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: