#include "12.h"
int main(int argc, const char *argv[])
{ int i;
Pstack L=get_head();
int a[6]={10,20,30,40,50,60};
for(i=0;i<6;i++)
{
insert_stack(L,a[i]);
}
output(L);
out_stack(L);
output(L);
des(L);
output(L);
return 0;
}
#include"12.h"
Pstack get_head()
{
Pstack L=malloc(sizeof(Stack));
if(L==NULL)
{
printf("申请失败\n");
return NULL;
}
L->top=NULL;
L->len=0;
return L;
}
void insert_stack(Pstack L,int e)
{
if(L==NULL)
{
printf("链栈为空或不存在\n");
return;
}
Node *p=malloc(sizeof(Node));
p->data=e;
p->next=L->top;
L->top=p;
L->len++;
printf("入栈成功\n");
}
void output(Pstack L)
{
if(L==NULL||L->len==0)
{
printf("链栈为空或不存在\n");
return;
}
int i;
Node *t=L->top;
for(i=0;i<L->len;i++)
{
printf("%d\t",t->data);
t=t->next;
}
printf("\n");
}
void out_stack(Pstack L)
{if(L==NULL||L->len==0)
{
printf("链栈为空或不存在\n");
return;
}
Node *p=L->top;
printf("出栈的节点是%d\n",p->data);
L->top=p->next;
free(p);
p=NULL;
L->len--;
}
void des(Pstack L)
{if(L==NULL||L->len==0)
{
printf("链栈为空或不存在\n");
return;
}
Node *p;
while(L->top!=NULL)
{
p=L->top;
L->top=L->top->next;
free(p);
L->len--;
}
free(L);
L=NULL;
printf("释放节点成功\n");
}
#ifndef _12_H_
#define _12_H_
#include<myhead.h>
#define MAX 6
typedef struct node
{
int data;
struct node *next;
}Node;
typedef struct
{
int len;
Node *top;
}Stack,*Pstack;
Pstack get_head();
void insert_stack(Pstack L,int e);
void output(Pstack L);
void out_stack(Pstack L);
void des(Pstack L);
#endif