C语言 链栈

链栈

链栈:栈的链接存储结构。
和单链表相似,结点结构相同,
不同点是单链表从头结点指向下一个结点
链栈则相反,是栈顶结点指向栈底结点
只能在栈顶执行插入和删除操作。

入栈示意图
在这里插入图片描述

#include "stdafx.h"
#include "malloc.h"

const int STACKSIZE = 5;
void LinkStack();
void Push(int x);
int getTop();
int Pop();
int Empty();
void DeleLinkStack();
typedef int ElemType;

typedef struct Node
{
	ElemType data;
	Node* next;
}NODE; 

NODE* top = (struct Node*)malloc(sizeof(struct Node));

int main()
{
	ElemType x;
	LinkStack();
	int i=1;
	while (i<=STACKSIZE)
	{
		printf("输入需入栈的整数:");
		scanf_s("%d", &x);
		Push(x);
		printf("入栈成功\n");
		i++;
	}
	printf("返回栈顶元素:%d\n", getTop());
	//调用析构函数
	DeleLinkStack();
	while (true)
	{
		if (Empty()!=1)
		{
			printf("%d出栈成功\n", Pop());
		}
		else {
			printf("栈空\n");
			break;
		}
	}
	
    return 0;
}
//构造函数,初始化一个空栈链
void LinkStack()
{
	top = NULL;
	printf("空栈链初始化成功。\n");
}

//入栈
void Push(int x)
{
	NODE* s = (struct Node*)malloc(sizeof(struct Node));
	s->data = x;
	s->next = top;
	top = s;

}

//取栈顶元素(并不删除)
ElemType getTop()
{
	return top->data;
}

//出栈操作,将栈顶元素弹出
ElemType Pop()
{
	NODE* p = NULL;
	ElemType x;
	if (top == NULL) throw "下溢";
	x = top->data;
	p = top;
	top = top->next;
	delete p;
	return x;
}

//判断栈是否为空
int Empty()
{
	if (top == NULL) return 1;
	else return 0;
}

//析构函数,释放链栈各结点的存储空间
void DeleLinkStack()
{
	NODE *p = top;
	while (top != NULL) {
		top = top->next;	//top指向被释放结点的下一个结点
		printf("即将被删除的栈链结点为%d\n", p->data);
		delete p;
		p = top;	//工作指针p后移,到更新后的top
	}
}

运行结果
1、初始化空栈->入栈->返回栈顶元素->出栈
在这里插入图片描述

2、初始化空栈->入栈->返回栈顶元素->析构栈
在这里插入图片描述
有问题的,望评论区指正。
可以帮助到你的话,麻烦点赞支持一下。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是C语言的基本操作代码展示: ```c #include <stdio.h> #include <stdlib.h> // 定义链结构体 struct node { int data; struct node *next; }; // 初始化链 void initStack(struct node **top) { *top = NULL; } // 判断链是否为空 int isEmpty(struct node *top) { return top == NULL; } // 入操作 void push(struct node **top, int data) { struct node *newNode = (struct node*)malloc(sizeof(struct node)); if (newNode == NULL) { printf("内存分配失败"); exit(1); } newNode->data = data; newNode->next = *top; *top = newNode; } // 出操作 int pop(struct node **top) { if (isEmpty(*top)) { printf("链为空"); exit(1); } int data = (*top)->data; struct node *temp = *top; *top = (*top)->next; free(temp); return data; } // 获取顶元素 int peek(struct node *top) { if (isEmpty(top)) { printf("链为空"); exit(1); } return top->data; } // 输出链 void display(struct node *top) { if (isEmpty(top)) { printf("链为空"); return; } struct node *temp = top; while (temp != NULL) { printf("%d->", temp->data); temp = temp->next; } printf("NULL"); } int main() { struct node *top; initStack(&top); push(&top, 1); push(&top, 2); push(&top, 3); printf("链元素为:"); display(top); printf("\n"); printf("顶元素为:%d\n", peek(top)); printf("出元素为:%d\n", pop(&top)); printf("链元素为:"); display(top); printf("\n"); return 0; } ``` 注:上述代码中,`struct node` 表示链的节点,包含 `data` 和 `next` 两个成员变量。`top` 表示链顶节点。`initStack` 函数用于初始化链,将顶节点设置为 `NULL`。`isEmpty` 函数用于判断链是否为空。`push` 函数用于入操作,将新的节点插入链表头部。`pop` 函数用于出操作,从链表头部取出节点并删除。`peek` 函数用于获取顶元素。`display` 函数用于输出链。`main` 函数中展示了链的基本操作,包括初始化、入、获取顶元素、出和输出链等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值