数组
#include <stdio.h>
#include <stdbool.h>
#define MAXSIZE 100
#define ERROR 0
#define OK 1
typedef int Status;
typedef int SElemType;
typedef struct{
SElemType data[MAXSIZE];
int top;
}SqStack;
Status push(SqStack* S, SElemType e){
if(S->top == MAXSIZE-1){
return ERROR;
}
S->data[++S->top] = e;
return OK;
}
Status pop(SqStack* S, SElemType *e){
if(S->top == -1){
return ERROR;
}
*e = S->data[S->top--];
return OK;
}
int StackLength(SqStack* S){
return S->top+1;
}
bool StackEmpty(SqStack* S){
if(S->top == -1)
return true;
return false;
}
bool StackFull(SqStack* S){
if(S->top == MAXSIZE-1)
return true;
return false;
}
Status getTop(SqStack* S, SElemType* e){
if(S->top == -1)
return ERROR;
*e = S->data[S->top];
return OK;
}
void printStack(SqStack* S){
if(StackEmpty(S)){
printf("栈空!\n");
return;
}
int i = 0;
printf("栈中元素遍历:\n");
while(i<=S->top)
printf("%d\n", S->data[i++]);
}
int main(){
bool continueFlag = true;
int functionNum;
SqStack stack, *S = &stack;
S->top = -1;
SElemType pushNum, popNum;
while(continueFlag){
printf("-----------菜单------------\n");
printf("-----------1.入栈----------\n");
printf("-----------2.出栈----------\n");
printf("-----------3.返回栈顶元素--\n");
printf("-----------4.栈元素遍历----\n");
printf("-----------5.栈元素个数----\n");
printf("-----------6.判断栈是否为空\n");
printf("-----------7.判断栈是否已满\n");
printf("-----------8.退出----------\n");
printf("输入功能号:\n");
scanf("%d",&functionNum);
printf("-----------------------\n");
switch(functionNum){
case 1:
printf("请输入要入栈的元素:\n");
scanf("%d", &pushNum);
if(push(S, pushNum))
printf("入栈成功!\n");
else
printf("入栈失败!\n");
break;
case 2:
if(pop(S, &popNum))
printf("出栈元素:%d\n",popNum);
else
printf("栈空!\n");
break;
case 3:
if(getTop(S, &popNum))
printf("栈顶元素:%d!\n",popNum);
else
printf("栈空!\n");
break;
case 4:
printStack(S);
break;
case 5:
printf("栈元素个数:%d\n",StackLength(S));
break;
case 6:
if(StackEmpty(S))
printf("栈空!\n",popNum);
else
printf("栈非空!\n");
break;
case 7:
if(StackFull(S))
printf("栈满!\n",popNum);
else
printf("栈不满!\n");
break;
case 8:
continueFlag = false;
printf("欢迎使用!\n");
break;
default:
printf("功能号不存在!\n");
break;
}
printf("-----------------------\n");
}
return 0;
}
单向链表
typedef int elemtype;
typedef struct LinkedStackNode
{
elemtype data;
LinkedStackNode *next;
}LinkedStackNode,*LinkedStack;
LinkedStack Init_LinkedStack()
{
LinkedStack top = (LinkedStackNode *)malloc(sizeof(LinkedStackNode));
if(top != NULL)
{
top->next = NULL;
}
return top;
}
int LinkedStack_Empty(LinkedStack top)
{
if(top->next == NULL)
{
return 1;
}
else
{
return 0;
}
}
int Push_LinkedStack(LinkedStack top,elemtype x)
{
LinkedStackNode * node = (LinkedStackNode *)malloc(sizeof(LinkedStackNode));
if(node == NULL)
{
return 0;
}
else
{
node->data = x;
node->next = top->next;
top->next = node;
return 1;
}
}
int Pop_LinkedStack(LinkedStack top,elemtype *x)
{
LinkedStackNode *node;
if(top->next == NULL)
{
return 0;
}
else
{
node = top->next;
*x = node->data;
top->next = node->next;
free(node);
return 1;
}
}
int Get_LinkedStack(LinkedStack top,elemtype *x)
{
if(top->next == NULL)
{
return 0;
}
else
{
*x = top->next->data;
return 1;
}
}
双向链表
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* next;
struct node* prev;
} *top;
void push(int e)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
if (!temp)
{
printf("We cannot allocate memory");
return;
}
printf("Enter element which you want to push\n");
scanf("%d", &e);
temp->data = e;
if (top == NULL)
{
temp->next = NULL;
temp->prev = NULL;
top = temp;
}
else
{
top->next = temp;
temp->prev = top;
top = temp;
}
}
int pop()
{
if (top == NULL)
{
printf("stack is empty. We have nothing to pop\n");
return 0;
}
if(top->next == NULL && top->prev ==NULL){
int a = top->data;
struct node *temp;
temp = top;
top= NULL;
free(temp);
return a;
}
struct node *temp;
temp = top;
top = top->prev;
top->next = NULL;
free(temp);
}
void display()
{
struct node *temp = top;
if (temp==NULL)
{
printf("Stack is empty\n");
return;
}
while(temp!= NULL)
{
printf("%d ",temp->data);
temp = temp->prev;
}
printf("\n");
}
int main()
{
int choose_number, e;
while(1)
{
printf("1. push element\n");
printf("2. pop element\n");
printf("3. Display elements\n");
printf("choose a number\n");
scanf("%d", &choose_number);
switch (choose_number)
{
case 1: push(e);
break;
case 2: e = pop();
printf("e => %d\n", e);
break;
case 3: display();
break;
default: printf("invalid choice\n");
}
}
return 0;
}