栈的三种实现方式(双向链表,链表,数组)

数组

#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;
    
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值