当前位置:网站首页>Using bidirectional linked list to realize stack (c)
Using bidirectional linked list to realize stack (c)
2022-07-24 07:32:00 【And move forward with the high wind - & gt;】
Catalog
One . Structure definition :
1. Define the node , Including a precursor pointer , A successor pointer , A data field
2. Define stack , Including a stack top pointer , Point to the top element of the stack ; The number of elements in a stack size
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int STDataType;
// Define each node
typedef struct StackNode {
struct Stack* next;
struct Stack* prev;
STDataType data;
}STNode;
// Define a stack
typedef struct Stack {
STNode* top;
int size;
}Stack;Two . Structure operation
Contains initialization , Push , Out of the stack , Look at the top of the stack elements , Destroy the stack , Air judgment and other operations .
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);The specific implementation is as follows :
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);
}
}3、 ... and . Test code
The test code is as follows :

The test results are as follows :

Four . Application
Explain LeetCode20. Valid parenthesis :

Create a stack , Encountered left parenthesis in stack , Encounter the same right parenthesis out of the stack .
Code implementation :
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);
}
}
// The above is the stack structure and operation definition .
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)){// If first element is a right parenthesis
StackDestroy(&st);
return false;
}
if((StackTop(&st)=='('&&*s==')')||(StackTop(&st)=='['&&*s==']')||(StackTop(&st)=='{'&&*s=='}')){
StackPop(&st);// Out of the stack
}else{
StackDestroy(&st);
return false;
}
}
s++;
}
if((&st)->top!=NULL){// If there are elements at the top of the stack
StackDestroy(&st);
return false;
}
StackDestroy(&st);
return true;
}
边栏推荐
- 二维平面多段线Y轴最短距离
- Who can stand it when the project goes online
- numpy.arange
- 我的创作纪念日
- Unity中使用深度和法线纹理
- DOM operation of JS -- style operation
- [introduction to C language] zzulioj 1011-1015
- 从CIA看常见网络攻击(爆破,PE,流量攻击)
- Bookkeeping app: xiaoha bookkeeping 2 - production of registration page
- R language handwritten numeral recognition
猜你喜欢

Bookkeeping app: xiaoha bookkeeping 2 - production of registration page

爬虫学习-概述

Jenkins detailed deployment

Deep learning two or three things - review those classical convolutional neural networks

XSS漏洞学习

Injectfix principle learning (to realize the heat of repair addition)

Win10 sound icon has no sound

Influxdb未授权访问&CouchDB权限绕过

Laplace distribution

Development system selection route
随机推荐
Vulnhub DC1
requests-爬虫实现一个简易网页采集器
SPI - send 16 bit and 8-bit data
Seminar 2022.07.22 -- Comparative learning
The goal you specified requires a project to execute but there is no POM in this directory
Customization or GM, what is the future development trend of SaaS in China?
Kali安装pip以及pip换源
Who can stand it when the project goes online
From the perspective of CIA, common network attacks (blasting, PE, traffic attacks)
File "manage.py", line 14) from exc ^ syntaxerror: cause and solution of invalid syntax error
The goal you specified requires a project to execute but there is no POM in this directory
My creation anniversary
Reptile learning - Overview
Feature Selective Anchor-Free Module for Single-Shot Object Detection
Compilation and debugging (GCC, g++, GDB)
MITRE ATT&CK超详细学习笔记-01(背景,术语,案例)
25.消息订阅与发布——PubSub-js
Requests crawl page source code data
QoS quality of service three DiffServ Model message marking and PHB
Cloud version upgrade