王道數(shù)據(jù)結(jié)構(gòu)強(qiáng)化課——【“棧、隊(duì)列”的應(yīng)用】代碼,持續(xù)更新
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-728857.html
鏈?zhǔn)酱鎯?chǔ)棧(單鏈表實(shí)現(xiàn)),并基于上述定義,棧頂在鏈頭,實(shí)現(xiàn)“出棧、入棧、判空、判滿”四個(gè)基本操作
#include <stdio.h>
#include <stdlib.h>
// 定義鏈表節(jié)點(diǎn)
struct Node {
int data;
struct Node* next;
};
// 定義棧結(jié)構(gòu)
struct Stack {
struct Node* top; // 棧頂指針
};
// 初始化棧
void initStack(struct Stack* stack) {
stack->top = NULL;
}
// 入棧操作
void push(struct Stack* stack, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("內(nèi)存分配失敗,無(wú)法執(zhí)行入棧操作\n");
return;
}
newNode->data = value;
newNode->next = stack->top;
stack->top = newNode;
}
// 出棧操作
int pop(struct Stack* stack) {
if (stack->top == NULL) {
printf("棧為空,無(wú)法執(zhí)行出棧操作\n");
return -1; // 返回一個(gè)錯(cuò)誤值
}
struct Node* temp = stack->top;
int poppedValue = temp->data;
stack->top = temp->next;
free(temp);
return poppedValue;
}
// 判空操作
int isEmpty(struct Stack* stack) {
return (stack->top == NULL);
}
// 判滿操作(對(duì)于鏈?zhǔn)酱鎯?chǔ)的棧,通常不會(huì)滿,所以返回0表示不滿)
int isFull(struct Stack* stack) {
return 0;
}
// 釋放棧內(nèi)存
void freeStack(struct Stack* stack) {
while (stack->top != NULL) {
struct Node* temp = stack->top;
stack->top = temp->next;
free(temp);
}
}
int main() {
struct Stack stack;
initStack(&stack);
// 入棧操作
push(&stack, 1);
push(&stack, 2);
push(&stack, 3);
// 出棧操作
printf("出棧操作: %d\n", pop(&stack));
// 判空操作
printf("棧是否為空: %s\n", isEmpty(&stack) ? "是" : "否");
// 判滿操作
printf("棧是否滿: %s\n", isFull(&stack) ? "是" : "否");
// 釋放棧內(nèi)存
freeStack(&stack);
return 0;
}
鏈?zhǔn)酱鎯?chǔ)棧(雙向鏈表實(shí)現(xiàn))
#include <stdio.h>
#include <stdlib.h>
// 定義鏈表節(jié)點(diǎn)
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
// 定義棧結(jié)構(gòu)
struct Stack {
struct Node* top; // 棧頂指針,鏈尾
};
// 初始化棧
void initStack(struct Stack* stack) {
stack->top = NULL;
}
// 入棧操作
void push(struct Stack* stack, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("內(nèi)存分配失敗,無(wú)法執(zhí)行入棧操作\n");
return;
}
newNode->data = value;
newNode->next = NULL;
if (stack->top == NULL) {
newNode->prev = NULL;
stack->top = newNode;
} else {
newNode->prev = stack->top;
stack->top->next = newNode;
stack->top = newNode;
}
}
// 出棧操作
int pop(struct Stack* stack) {
if (stack->top == NULL) {
printf("棧為空,無(wú)法執(zhí)行出棧操作\n");
return -1; // 返回一個(gè)錯(cuò)誤值
}
struct Node* temp = stack->top;
int poppedValue = temp->data;
if (stack->top->prev != NULL) {
stack->top = stack->top->prev;
stack->top->next = NULL;
} else {
stack->top = NULL;
}
free(temp);
return poppedValue;
}
// 判空操作
int isEmpty(struct Stack* stack) {
return (stack->top == NULL);
}
// 判滿操作(對(duì)于鏈?zhǔn)酱鎯?chǔ)的棧,通常不會(huì)滿,所以返回0表示不滿)
int isFull(struct Stack* stack) {
return 0;
}
// 釋放棧內(nèi)存
void freeStack(struct Stack* stack) {
while (stack->top != NULL) {
struct Node* temp = stack->top;
stack->top = temp->prev;
free(temp);
}
}
int main() {
struct Stack stack;
initStack(&stack);
// 入棧操作
push(&stack, 1);
push(&stack, 2);
push(&stack, 3);
// 出棧操作
printf("出棧操作: %d\n", pop(&stack));
// 判空操作
printf("棧是否為空: %s\n", isEmpty(&stack) ? "是" : "否");
// 判滿操作
printf("棧是否滿: %s\n", isFull(&stack) ? "是" : "否");
// 釋放棧內(nèi)存
freeStack(&stack);
return 0;
}
順序存儲(chǔ)的隊(duì)列(數(shù)組實(shí)現(xiàn))
#include <stdio.h>
#include <stdlib.h>
#define MAX_QUEUE_SIZE 10 // 隊(duì)列的最大容量
// 定義隊(duì)列結(jié)構(gòu)
struct Queue {
int front, rear; // 前后指針
int data[MAX_QUEUE_SIZE];
};
// 初始化隊(duì)列
void initQueue(struct Queue* queue) {
queue->front = -1;
queue->rear = -1;
}
// 判空操作
int isEmpty(struct Queue* queue) {
return (queue->front == -1);
}
// 判滿操作
int isFull(struct Queue* queue) {
return ((queue->rear + 1) % MAX_QUEUE_SIZE == queue->front);
}
// 入隊(duì)操作
void enqueue(struct Queue* queue, int value) {
if (isFull(queue)) {
printf("隊(duì)列已滿,無(wú)法執(zhí)行入隊(duì)操作\n");
return;
}
if (isEmpty(queue)) {
queue->front = 0;
}
queue->rear = (queue->rear + 1) % MAX_QUEUE_SIZE;
queue->data[queue->rear] = value;
}
// 出隊(duì)操作
int dequeue(struct Queue* queue) {
if (isEmpty(queue)) {
printf("隊(duì)列為空,無(wú)法執(zhí)行出隊(duì)操作\n");
return -1; // 返回一個(gè)錯(cuò)誤值
}
int dequeuedValue = queue->data[queue->front];
if (queue->front == queue->rear) {
// 隊(duì)列中只有一個(gè)元素,出隊(duì)后隊(duì)列為空
queue->front = -1;
queue->rear = -1;
} else {
queue->front = (queue->front + 1) % MAX_QUEUE_SIZE;
}
return dequeuedValue;
}
int main() {
struct Queue queue;
initQueue(&queue);
// 入隊(duì)操作
enqueue(&queue, 1);
enqueue(&queue, 2);
enqueue(&queue, 3);
// 出隊(duì)操作
printf("出隊(duì)操作: %d\n", dequeue(&queue));
// 判空操作
printf("隊(duì)列是否為空: %s\n", isEmpty(&queue) ? "是" : "否");
// 判滿操作
printf("隊(duì)列是否滿: %s\n", isFull(&queue) ? "是" : "否");
return 0;
}
鏈?zhǔn)酱鎯?chǔ)隊(duì)列(單鏈表實(shí)現(xiàn))
#include <stdio.h>
#include <stdlib.h>
// 定義鏈表節(jié)點(diǎn)
struct Node {
int data;
struct Node* next;
};
// 定義隊(duì)列結(jié)構(gòu)
struct Queue {
struct Node* front; // 隊(duì)列前端
struct Node* rear; // 隊(duì)列后端
};
// 初始化隊(duì)列
void initQueue(struct Queue* queue) {
queue->front = NULL;
queue->rear = NULL;
}
// 判空操作
int isEmpty(struct Queue* queue) {
return (queue->front == NULL);
}
// 判滿操作(對(duì)于鏈?zhǔn)酱鎯?chǔ)的隊(duì)列,通常不會(huì)滿,所以返回0表示不滿)
int isFull(struct Queue* queue) {
return 0;
}
// 入隊(duì)操作
void enqueue(struct Queue* queue, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("內(nèi)存分配失敗,無(wú)法執(zhí)行入隊(duì)操作\n");
return;
}
newNode->data = value;
newNode->next = NULL;
if (isEmpty(queue)) {
queue->front = newNode;
} else {
queue->rear->next = newNode;
}
queue->rear = newNode;
}
// 出隊(duì)操作
int dequeue(struct Queue* queue) {
if (isEmpty(queue)) {
printf("隊(duì)列為空,無(wú)法執(zhí)行出隊(duì)操作\n");
return -1; // 返回一個(gè)錯(cuò)誤值
}
struct Node* temp = queue->front;
int dequeuedValue = temp->data;
queue->front = temp->next;
free(temp);
if (queue->front == NULL) {
// 如果出隊(duì)后隊(duì)列為空,需要更新rear指針
queue->rear = NULL;
}
return dequeuedValue;
}
// 釋放隊(duì)列內(nèi)存
void freeQueue(struct Queue* queue) {
while (queue->front != NULL) {
struct Node* temp = queue->front;
queue->front = temp->next;
free(temp);
}
}
int main() {
struct Queue queue;
initQueue(&queue);
// 入隊(duì)操作
enqueue(&queue, 1);
enqueue(&queue, 2);
enqueue(&queue, 3);
// 出隊(duì)操作
printf("出隊(duì)操作: %d\n", dequeue(&queue));
// 判空操作
printf("隊(duì)列是否為空: %s\n", isEmpty(&queue) ? "是" : "否");
// 判滿操作
printf("隊(duì)列是否滿: %s\n", isFull(&queue) ? "是" : "否");
// 釋放隊(duì)列內(nèi)存
freeQueue(&queue);
return 0;
}
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-728857.html
到了這里,關(guān)于【“棧、隊(duì)列”的應(yīng)用】408數(shù)據(jù)結(jié)構(gòu)代碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!