I.普通栈

1.建立栈

#define max 50typedef struct{int data[max];int top;}Stack;

2.进栈

void InsertStack(Stack*L,int e){if(L->top==Max)return;//栈满了直接返回else{++L->top;//栈从下标1开始存取元素L->data[L->top]=e;}}

3.出栈

int PopStack(Stack*L,int*e){if(L->top==0)return;//空栈直接返回else{*e=L->data[top];--L->top;}return*e;}

4.打印栈中元素

void PrintStack(Stack*L){if(L->top==0)return;elsefor(int i=1;i<=L->top;i++)printf("%d ",L->data[i]);}

5.总代码

#include<stdio.h>#include<malloc.h>//1.建立栈#define Max 50typedef struct{  int data[Max];  int top;}Stack;//2.进栈void InsertStack(Stack *L,int e){  if (L->top == Max)    return;  else  {    ++L->top;    L->data[L->top] = e;  }}//3.出栈int GoStack(Stack* L, int* e){  if (L->top == 0)    return;  else  {    *e = L->data[L->top];    L->top--;  }  return *e;}//4.打印栈中元素void PrintStack(Stack* L){  if (L->top == 0)    return;  else    for (int i = 1; i <= L->top; i++)      printf("%d ", L->data[i]);}//5.代码实现int main(){  Stack* L = (Stack*)malloc(sizeof(Stack));  L->top = 0;//初始化  for (int i = 0; i < 10; i++)    InsertStack(L, i * 2);  PrintStack(L);  printf("\n");  int* e = malloc(sizeof(int));  GoStack(L, e);  PrintStack(L);  return 0;}

II.两栈共享空间

1.建立栈

#define Max 31typedef struct{  int data[Max];  int top1;  int top2;}Stack;

2.进栈

void InsertStack(Stack* L,int e, int Stacknum)//Stacknumber用来判断是否用top1还是top2{  if (L->top1 + 1 == L->top2)//栈满就直接退出    return;  else  {    if (Stacknum == 1)    {      ++L->top1;      L->data[L->top1] = e;    }    if (Stacknum == 2)    {      --L->top2;      L->data[L->top2] = e;    }  }}

3.出栈

int GoStack(Stack*L,int*e,int Stacknum){  if (L->top1==0&&L->top2==Max)//空栈就直接退出    return;  else  {    if (Stacknum == 1)    {      *e=L->data[L->top1];      --L->top1;    }    if (Stacknum == 2)    {      *e = L->data[L->top2];      ++L->top2;    }  }  return *e;}

4.打印栈

void PrintStack(Stack* L,int Stacknum){  if (L->top1 == 0 && L->top2 == Max)//空栈直接退出    return;  else  {    if (Stacknum == 1)      for (int i = 1; i <= L->top1;++i)        printf("%d ", L->data[i]);    if (Stacknum == 2)      for (int i =Max-1;i>=L->top2;--i)        printf("%d ", L->data[i]);  }}

5.总代码

#include<stdio.h>#include<malloc.h>#define Max 31typedef struct{  int data[Max];  int top1;  int top2;}Stack;//1.进栈void InsertStack(Stack* L,int e, int Stacknum)//Stacknumber用来判断是否用top1还是top2{  if (L->top1 + 1 == L->top2)//栈满就直接退出    return;  else  {    if (Stacknum == 1)    {      ++L->top1;      L->data[L->top1] = e;    }    if (Stacknum == 2)    {      --L->top2;      L->data[L->top2] = e;    }  }}//2.出栈int GoStack(Stack*L,int*e,int Stacknum){  if (L->top1==0&&L->top2==Max)//空栈就直接退出    return;  else  {    if (Stacknum == 1)    {      *e=L->data[L->top1];      --L->top1;    }    if (Stacknum == 2)    {      *e = L->data[L->top2];      ++L->top2;    }  }  return *e;}//3.打印元素void PrintStack(Stack* L,int Stacknum){  if (L->top1 == 0 && L->top2 == Max)//空栈直接退出    return;  else  {    if (Stacknum == 1)      for (int i = 1; i <= L->top1;++i)        printf("%d ", L->data[i]);    if (Stacknum == 2)      for (int i =Max-1;i>=L->top2;--i)        printf("%d ", L->data[i]);  }}int main(){  Stack* L = malloc(sizeof(Stack));  L->top1 = 0, L->top2 = Max;//初始化，一定要.  for (int i = 0; i < 10; i++)  {    InsertStack(L, i * 2, 1);    InsertStack(L, i * 2, 2);  }  PrintStack(L, 1);  PrintStack(L, 2);  printf("\n");  int* e = malloc(sizeof(int));  GoStack(L, e, 1);  PrintStack(L, 1);  PrintStack(L, 2);  return 0;}

III.链栈

#include<stdio.h>#include<malloc.h>typedef struct StackNode{  int data;  struct StackNode* next;  int count;}StackNode,*LinkListStack;void INit(StackNode** p, StackNode*** L){  (*p)->next =**L, ** L = *p;  ++(**L)->count;}//1.进栈void InsertStack(StackNode** L, int e){  StackNode* p = (StackNode*)malloc(sizeof(StackNode));  p->data = e; p->next = NULL; p->count = (*L)->count;//存取进来后栈中一共有多少个元素  INit(&p, &L);}//2.出栈void PopStack(StackNode** L){  StackNode* p = (StackNode*)malloc(sizeof(StackNode));  p = *L; *L = (*L)->next;  free(p);}//3.打印栈void PrintStack(StackNode* L){  StackNode* p = L;  for (int i = 0; i < L->count; i++)  {    printf("%d ", p->data);    p = p->next;  }  printf("\n");}int main(){  StackNode* L = malloc(sizeof(StackNode)); L->next = NULL; L->count = 0;  for (int i = 0; i < 10; i++)    InsertStack(&L, i * 2);  PrintStack(L);  //删除栈顶元素  PopStack(&L);  PrintStack(L);  return 0;}