C語言鏈棧
阿新 • • 發佈:2018-11-11
#include <stdio.h>
#include <stdlib.h>
typedef struct LineStack///定義結構體
{
char data;///定義資料域
struct LineStack *next;///定義頭指標
} LineStack;
LineStack *Push(LineStack *ls,char a)///入棧
{
LineStack *s = (LineStack *)malloc(sizeof(LineStack));///申請新結點空間
s->data = a;///賦值a;
s->next = ls;
ls = s;
return ls;
}
LineStack *Poo(LineStack *ls)
{
if (ls)
{
LineStack * p=ls;
ls = ls->next;
printf("彈棧元素:%c ",p->data);
if (ls)
{
printf("棧頂元素:%c\n",ls->data);
}
else
{
printf("棧已空\n");
}
free(p);
}
else
{
printf("棧內沒有元素");
}
return ls;
int main()
{
LineStack *ls = NULL;
ls = Push(ls,'a');
ls = Push(ls,'b');
ls = Push(ls,'c');
ls = Push(ls,'d');
ls = Poo(ls);
ls = Poo(ls);
ls = Poo(ls);
ls = Poo(ls);
ls = Poo(ls);
return 0;
}
#include <stdlib.h>
typedef struct LineStack///定義結構體
{
char data;///定義資料域
struct LineStack *next;///定義頭指標
} LineStack;
LineStack *Push(LineStack *ls,char a)///入棧
{
LineStack *s = (LineStack *)malloc(sizeof(LineStack));///申請新結點空間
s->data = a;///賦值a;
s->next = ls;
ls = s;
return ls;
}
LineStack *Poo(LineStack *ls)
{
if (ls)
{
LineStack * p=ls;
ls = ls->next;
printf("彈棧元素:%c ",p->data);
if (ls)
{
printf("棧頂元素:%c\n",ls->data);
}
else
{
printf("棧已空\n");
}
free(p);
}
else
{
printf("棧內沒有元素");
}
return ls;
}
{
LineStack *ls = NULL;
ls = Push(ls,'a');
ls = Push(ls,'b');
ls = Push(ls,'c');
ls = Push(ls,'d');
ls = Poo(ls);
ls = Poo(ls);
ls = Poo(ls);
ls = Poo(ls);
ls = Poo(ls);
return 0;
}