当前位置:网站首页>用双向链表实现栈(C)
用双向链表实现栈(C)
2022-07-24 05:16:00 【且随疾风前行->】
目录
一.结构定义:
1.定义节点,包括一个前驱指针,一个后继指针,一个数据域
2.定义栈,包括一个栈顶指针,指向栈顶元素;一个栈所含元素个数size
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int STDataType;
//定义各个节点
typedef struct StackNode {
struct Stack* next;
struct Stack* prev;
STDataType data;
}STNode;
//定义一个栈
typedef struct Stack {
STNode* top;
int size;
}Stack;二.结构操作
包含初始化,入栈,出栈,查看栈顶元素,销毁栈,判空等操作。
void StackInit(Stack* ps);
void StackDestroy(Stack* ps);
void StackPush(Stack* ps, STDataType x);
void StackPop(Stack* ps);
STDataType StackTop(Stack* ps);
bool StackEmpty(Stack* ps);
int StackSize(Stack* ps);具体实现如下:
void StackInit(Stack* ps) {
assert(ps);
ps->size = 0;
ps->top = NULL;
}
void StackPush(Stack* ps, STDataType x) {
assert(ps);
STNode*newnode= (STNode*)malloc(sizeof(STNode));
assert(newnode);
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
if (ps->top == NULL)ps->top = newnode;
else {
ps->top->next = newnode;
newnode->prev = ps->top;
ps->top = newnode;
}
ps->size++;
}
void StackPop(Stack* ps) {
assert(ps&&ps->top);
if (ps->top->prev== NULL) {
free(ps->top);
ps->top = NULL;
}
else {
STNode* prev = ps->top->prev;
free(ps->top);
ps->top = prev;
}
ps->size--;
}
STDataType StackTop(Stack* ps) {
assert(ps && ps->top);
return ps->top->data;
}
bool StackEmpty(Stack* ps){
assert(ps);
return ps->top == NULL;
}
int StackSize(Stack* ps) {
return ps->size;
}
void StackDestroy(Stack* ps) {
assert(ps);
while (ps->top) {
StackPop(ps);
}
}三.测试代码
测试代码如下:

测试结果如下:

四.运用
解LeetCode20.有效的括号:

创建一个栈,遇到左括号入栈,遇到相同右括号出栈。
代码实现:
typedef char STDataType;
typedef struct StackNode {
struct Stack* next;
struct Stack* prev;
STDataType data;
}STNode;
typedef struct Stack {
STNode* top;
int size;
}Stack;
void StackInit(Stack* ps) {
assert(ps);
ps->size = 0;
ps->top = NULL;
}
void StackPush(Stack* ps, STDataType x) {
assert(ps);
STNode*newnode= (STNode*)malloc(sizeof(STNode));
assert(newnode);
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
if (ps->top == NULL)ps->top = newnode;
else {
ps->top->next = newnode;
newnode->prev = ps->top;
ps->top = newnode;
}
ps->size++;
}
void StackPop(Stack* ps) {
assert(ps&&ps->top);
if (ps->top->prev== NULL) {
free(ps->top);
ps->top = NULL;
}
else {
STNode* prev = ps->top->prev;
free(ps->top);
ps->top = prev;
}
ps->size--;
}
STDataType StackTop(Stack* ps) {
assert(ps && ps->top);
return ps->top->data;
}
bool StackEmpty(Stack* ps){
assert(ps);
return ps->top == NULL;
}
int StackSize(Stack* ps) {
return ps->size;
}
void StackDestroy(Stack* ps) {
assert(ps);
while (ps->top) {
StackPop(ps);
}
}
//以上是栈结构和操作定义。
bool isValid(char * s){
if(s==NULL)return false;
Stack st;
StackInit(&st);
while(*s!='\0'){
if(*s=='('||*s=='{'||*s=='['){
StackPush(&st,*s);
}else{
if(StackEmpty(&st)){//若第一个元素是右括号
StackDestroy(&st);
return false;
}
if((StackTop(&st)=='('&&*s==')')||(StackTop(&st)=='['&&*s==']')||(StackTop(&st)=='{'&&*s=='}')){
StackPop(&st);//出栈
}else{
StackDestroy(&st);
return false;
}
}
s++;
}
if((&st)->top!=NULL){//若栈顶还有元素
StackDestroy(&st);
return false;
}
StackDestroy(&st);
return true;
}
边栏推荐
- finally和return的执行顺序
- Embedded system transplantation [3] - uboot burning and use
- ssm的整合
- Read the summary of "machine learning - Zhou Zhihua"
- 7. Find the sum of numbers between 100 and 300 that can be divided by 3.
- Support complex T4 file systems such as model group monitoring and real-time alarm. e
- 关于numpy基础用法的一个整理
- c2-随机产生函数种子seed、numpy.random.seed()、tf.random.set_seed学习+转载整理
- Jsp+dao integration
- Tips for using the built-in variable props of BeanShell
猜你喜欢

Binary SCA fingerprint extraction black Technology: go language Reverse Technology

Hotel IPTV digital TV system solution

View progress!!! RPM installation!!!

DHCP principle and configuration

AttributeError: ‘NoneType‘ object has no attribute ‘shape‘

Use of fiddler packet capturing tool

T 11-20

Wang Qing, director of cloud infrastructure software research and development of Intel (China): Intel's technology development and prospects in cloud native

T 1-5

DNS domain name resolution service
随机推荐
MySQL connection
The difference between compiled language and interpreted language
安装Pytorch+anaconda+cuda+cudnn
1. Input a 100 point score from the keyboard and output its grade according to the following principles: score ≥ 90, Grade A; 80 ≤ score < 90, grade B; 70 ≤ score < 80, grade C; 60 ≤ score < 70, grade
九大排序实现与比较(万字总结)
scikit-learn笔记
Knowledge record of College Physics C in advance in summer [update]
Machine vision learning summary
PPPoE网关模拟环境搭建
Echo speaker pairing and operation method
C#表格数据去重
FRP intranet penetration service usage
Wang Qing, director of cloud infrastructure software research and development of Intel (China): Intel's technology development and prospects in cloud native
DNS domain name resolution service
浅谈不可转让的声誉积分NFT SBTs面临的困境
The fourth job: about the usage of cat, grep, cut, sort, uniq, VIM, TR and other commands
Beginners' preparation for the Blue Bridge Cup (University Programming learning history records, topic ideas for students who need to prepare for the Blue Bridge Cup)
Problems encountered in configuring Yum source
Do you want to have a robot that can make cartoon avatars in three steps?
jdbc封装一个父类减少代码重复