// stack.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<stdio.h>
#include<iostream>
typedef struct stack{
int data;
struct stack *next;
}stack;
void initStack(stack *&s){
s=(stack *)malloc(sizeof(stack));
s->next=NULL;
}
//入栈操作
//该操作中注意空间的开辟位置,本人就曾在for循环外开辟导致一直有错。。。。
void PushStack(stack *&s,int a){
stack *p;
printf("请输入要入栈的元素:\n");
for(int i=0;i<6;i++){
p=(stack *)malloc(sizeof(stack));
scanf_s("%d",&a);
p->data=a;
p->next=s->next;
s->next=p;
}
}
//出栈操作
void PullStack(stack *&s){
stack *p;
int a=0;
if(s->next!=NULL){
p=s->next;
a=s->data;
s->next=s->next->next;
//printf("%d \n",a);
free(p);
}
}
//取出栈中元素
void GetEle(stack *s){
int a=0;
stack *p;
p=s->next;
printf("栈中元素为:");
while(p!=NULL){
a=p->data;
printf("%d \n",a);
p=p->next;
}
}
void _tmain(int argc, _TCHAR* argv[])
{
stack *s;
int a=0;
initStack(s);
PushStack(s,a);
GetEle(s);
//PullStack(s);
system("PAUSE");
}