題庫網(wǎng)站建設(shè)seo快速排名是什么
文章目錄
- 前言
- 一、棧
- 1.1 棧的概念結(jié)構(gòu)
- 1.2棧的實(shí)現(xiàn)
- 二、隊(duì)列
- 2.1隊(duì)列的概念及結(jié)構(gòu)
- 2.2隊(duì)列的實(shí)現(xiàn)
- 三、棧和隊(duì)列面試題
- 總結(jié)
前言
一、棧
1.1 棧的概念結(jié)構(gòu)
棧也是一種線性表,數(shù)據(jù)在邏輯上挨著存儲(chǔ)。只允許在固定的一端進(jìn)行插入和刪除元素。進(jìn)行插入和刪除操作的一端叫棧頂,另一端叫棧底。符合LIFO 先進(jìn)后出。
壓棧:插入操作。
出棧:刪除操作。
1.2棧的實(shí)現(xiàn)
棧的實(shí)現(xiàn)用數(shù)組實(shí)現(xiàn)更好,因?yàn)橥昝婪蠑?shù)組的尾插尾刪。
數(shù)組的緩存利用率高一點(diǎn)。
小練習(xí):
支持動(dòng)態(tài)增長的棧:
typedef int STDataType;
typedef struct Stack
{STDataType* _a;int _top; // 棧頂int _capacity; // 容量
}Stack;
初始化棧:
void StackInit(ST* ps)
{assert(ps);ps->a = NULL;ps->top = ps->capacity = 0;
}
入棧:進(jìn)棧(棧的插入操作),若棧未滿,則將x加入使之成為新棧頂。
void StackPush(ST* ps, STDataType x)
{assert(ps);if (ps->top == ps->capacity){int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;STDataType* tmp = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));if (tmp == NULL){perror("realloc fail");exit(-1);}ps->a = tmp;ps->capacity = newCapacity;}ps->a[ps->top] = x;ps->top++;
}
出棧:出棧(棧的刪除操作),若棧非空,則彈出棧頂元素,并用x返回。
void StackPop(ST* ps)
{assert(ps);assert(!StackEmpty(ps));--ps->top;
}
獲取棧頂元素:
STDataType StackTop(ST* ps)
{assert(ps);assert(!StackEmpty(ps));return ps->a[ps->top - 1];
}
獲取棧中有效元素個(gè)數(shù):
int StackSize(ST* ps)
{assert(ps);return ps->top;
}
銷毀棧:棧銷毀,并釋放占用的存儲(chǔ)空間
void StackDestroy(ST* ps)
{assert(ps);free(ps->a);ps->a = NULL;ps->capacity = ps->top = 0;
}
檢測棧是否為空,如果為空返回非零結(jié)果,如果不為空返回0:
bool StackEmpty(ST* ps)
{assert(ps);return ps->top == 0;
}
Stack.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int STDataType;
typedef struct Stack
{STDataType* a;int top;int capacity;
}ST;void StackInit(ST* ps);
void StackDestroy(ST* ps);
void StackPush(ST* ps, STDataType x);
void StackPop(ST* ps);
STDataType StackTop(ST* ps);
bool StackEmpty(ST* ps);
int StackSize(ST* ps);
Stack.c
#include "Stack.h"void StackInit(ST* ps)
{assert(ps);ps->a = NULL;ps->top = ps->capacity = 0;
}void StackDestroy(ST* ps)
{assert(ps);free(ps->a);ps->a = NULL;ps->capacity = ps->top = 0;
}void StackPush(ST* ps, STDataType x)
{assert(ps);if (ps->top == ps->capacity){int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;STDataType* tmp = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));if (tmp == NULL){perror("realloc fail");exit(-1);}ps->a = tmp;ps->capacity = newCapacity;}ps->a[ps->top] = x;ps->top++;
}void StackPop(ST* ps)
{assert(ps);assert(!StackEmpty(ps));--ps->top;
}STDataType StackTop(ST* ps)
{assert(ps);assert(!StackEmpty(ps));return ps->a[ps->top - 1];
}bool StackEmpty(ST* ps)
{assert(ps);return ps->top == 0;
}int StackSize(ST* ps)
{assert(ps);return ps->top;
}
二、隊(duì)列
2.1隊(duì)列的概念及結(jié)構(gòu)
隊(duì)列:只允許在一端進(jìn)行插入數(shù)據(jù)操作,在另一端進(jìn)行刪除數(shù)據(jù)操作的特殊線性表,隊(duì)列具有先進(jìn)先出FIFO(First In First Out)
入隊(duì)列:進(jìn)行插入操作的一端稱為隊(duì)尾。
出隊(duì)列:進(jìn)行刪除操作的一端稱為隊(duì)頭。
2.2隊(duì)列的實(shí)現(xiàn)
隊(duì)列用鏈表的結(jié)構(gòu)實(shí)現(xiàn)更優(yōu)
鏈?zhǔn)浇Y(jié)構(gòu):表示隊(duì)列
typedef struct QListNode
{ struct QListNode* _pNext; QDataType _data;
}QNode;
隊(duì)列的結(jié)構(gòu):
typedef struct Queue
{QNode* head;QNode* tail;int size;
}Queue;
初始化隊(duì)列:
void QueueInit(Queue* pq)
{assert(pq);pq->head = pq->tail = NULL;pq->size = 0;
}
隊(duì)尾入隊(duì)列:
void QueuePush(Queue* pq, QDataType x)
{assert(pq);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail");exit(-1);}else{newnode->data = x;newnode->next = NULL;}if (pq->tail == NULL){pq->head = pq->tail = newnode;}else{pq->tail->next = newnode;pq->tail = newnode;}pq->size++;
}
隊(duì)頭出隊(duì)列:
void QueuePop(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));if (pq->head->next == NULL){free(pq->head);pq->head = pq->tail = NULL;}else{QNode* del = pq->head;pq->head = pq->head->next;free(del);del = NULL;}pq->size--;
}
獲取隊(duì)列頭部元素:
QDataType QueueFront(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->head->data;
}
獲取隊(duì)列隊(duì)尾元素:
QDataType QueueBack(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->tail->data;
}
獲取隊(duì)列中有效元素個(gè)數(shù):
int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}
檢測隊(duì)列是否為空,如果為空返回非零結(jié)果,如果非空返回0:
bool QueueEmpty(Queue* pq)
{assert(pq);return pq->head == NULL && pq->tail == NULL;
}
銷毀隊(duì)列:
void QueueDestroy(Queue* pq)
{assert(pq);QNode* cur = pq->head;while (cur){QNode* del = cur;cur = cur->next;free(del);}pq->head = pq->tail = NULL;
}
Queue.h
#pragma once
#include <stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>typedef int QDataType;
typedef struct QueueNode
{struct QueueNode* next;QDataType data;
}QNode;typedef struct Queue
{QNode* head;QNode* tail;int size;
}Queue;void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
void QueuePush(Queue* pq, QDataType x);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
bool QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);
Queue.c
#include"Queue.h"void QueueInit(Queue* pq)
{assert(pq);pq->head = pq->tail = NULL;pq->size = 0;
}void QueueDestroy(Queue* pq)
{assert(pq);QNode* cur = pq->head;while (cur){QNode* del = cur;cur = cur->next;free(del);}pq->head = pq->tail = NULL;
}void QueuePush(Queue* pq, QDataType x)
{assert(pq);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail");exit(-1);}else{newnode->data = x;newnode->next = NULL;}if (pq->tail == NULL){pq->head = pq->tail = newnode;}else{pq->tail->next = newnode;pq->tail = newnode;}pq->size++;
}void QueuePop(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));if (pq->head->next == NULL){free(pq->head);pq->head = pq->tail = NULL;}else{QNode* del = pq->head;pq->head = pq->head->next;free(del);del = NULL;}pq->size--;
}QDataType QueueFront(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->head->data;
}QDataType QueueBack(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->tail->data;
}bool QueueEmpty(Queue* pq)
{assert(pq);return pq->head == NULL && pq->tail == NULL;
}int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}
三、棧和隊(duì)列面試題
題目鏈接:
1.https://leetcode.cn/problems/valid-parentheses/
題目鏈接:
2.https://leetcode.cn/problems/implement-stack-using-queues/