1567 字
8 分钟
链表&&栈&&队列(顺序/链式)
链表
#include <iostream>#include <cstdlib> // 用于 malloc 和 freeusing namespace std;
// 定义链表节点typedef struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} // 构造函数} Node;
// 创建新节点Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); if (!newNode) { // 确保分配成功 cout << "内存分配失败!" << endl; exit(1); } newNode->val = data; newNode->next = NULL; return newNode;}
// 在链表头部插入节点void insertAtHead(Node** head, int data) { Node* newNode = createNode(data); newNode->next = *head; *head = newNode;}
// 删除指定值的节点void deleteNode(Node** head, int key) { if (*head == NULL) return; // 避免访问空指针
Node* temp = *head, *prev = NULL;
if (temp != NULL && temp->val == key) { *head = temp->next; // 确保头指针更新 free(temp); return; }
while (temp != NULL && temp->val != key) { prev = temp; temp = temp->next; }
if (temp == NULL) return;
prev->next = temp->next; free(temp);}
// 更新指定值的节点void updateNode(Node* head, int key, int newData) { Node* temp = head; while (temp != NULL) { if (temp->val == key) { temp->val = newData; break; } temp = temp->next; }}
// 查找指定值的节点Node* searchNode(Node* head, int key) { Node* temp = head; while (temp != NULL) { if (temp->val == key) { return temp; } temp = temp->next; } return NULL;}
// 反转链表(迭代)void reverseList(Node** head) { if (*head == NULL) return; // 避免访问空指针
Node* prev = NULL; Node* current = *head; Node* next = NULL;
while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; }
*head = prev;}
// 反转链表(递归)Node* reverse(Node* head) { if (head == NULL || head->next == NULL) { return head; } Node* newHead = reverse(head->next); head->next->next = head; head->next = NULL; return newHead;}
// 打印链表void printList(Node* head) { Node* temp = head; while (temp != NULL) { cout << temp->val << " "; temp = temp->next; } cout << endl;}
int main() { Node* head = NULL; // 初始化链表为空
// 插入节点 insertAtHead(&head, 3); insertAtHead(&head, 2); insertAtHead(&head, 1); cout << "插入后的链表: "; printList(head);
// 删除节点 deleteNode(&head, 2); cout << "删除后的链表: "; printList(head);
// 更新节点 updateNode(head, 3, 30); cout << "更新后的链表: "; printList(head);
// 查找节点 Node* found = searchNode(head, 30); if (found) { cout << "找到节点: " << found->val << endl; } else { cout << "未找到节点" << endl; }
// 反转链表(迭代) reverseList(&head); cout << "反转后的链表: "; printList(head);
return 0;}栈
顺序栈
#include <stdio.h>#include <stdbool.h>
#define MAXSIZE 100 // 定义栈的最大大小
typedef struct { int data[MAXSIZE]; int top;} stack;
void init(stack *s) { s->top = -1;}
// 判断栈是否为空bool isEmpty(stack *s) { return s->top == -1;}
// 判断栈是否满bool isFull(stack *s) { return s->top == MAXSIZE - 1;}
// 入栈void push(stack* s, int x) { if (isFull(s)) { printf("栈满\n"); return; } s->data[++s->top] = x;}
// 出栈void pop(stack *s, int *x) { if (isEmpty(s)) { printf("栈为空\n"); } *x = s->data[s->top--]; // 先取值,再减少 top}
// 打印栈void printStack(stack *s) { if (isEmpty(s)) { printf("栈为空\n"); return; } printf("栈内容: "); for (int i = 0; i <= s->top; i++) { printf("%d ", s->data[i]); } printf("\n");}
int main() { stack s; init(&s);
push(&s, 10); push(&s, 20); push(&s, 30); printStack(&s);
int val; if (pop(&s, &val)) { printf("出栈: %d\n", val); } printStack(&s);
return 0;}#### 链栈
```cpp#include <stdio.h>#include <stdlib.h>#include <stdbool.h>
// 链栈结点typedef struct StackNode { int data; struct StackNode *next;} StackNode;
// 链栈结构typedef struct { StackNode *top; int count;} LinkStack;
// 初始化链栈void InitStack(LinkStack *stack) { stack->top = NULL; stack->count = 0;}
// 入栈操作void push(LinkStack *s, int x) { StackNode *p = (StackNode *)malloc(sizeof(StackNode)); if (p == NULL) { // 确保内存分配成功 printf("内存分配失败!\n"); return; } p->data = x; p->next = s->top; s->top = p; s->count++; printf("入栈: %d\n", x);}
// 出栈操作bool pop(LinkStack *s, int *x) { if (s->top == NULL) { printf("栈为空,无法出栈!\n"); return false; // 失败返回 false } StackNode *p = s->top; if (x != NULL) { *x = p->data; } s->top = s->top->next; free(p); s->count--;
if (x != NULL) { printf("出栈: %d\n", *x); } return true; // 成功返回 true}
// 打印栈void printStack(LinkStack *s) { StackNode *p = s->top; printf("当前栈内容: "); while (p) { printf("%d ", p->data); p = p->next; } printf("\n");}
// 测试代码int main() { LinkStack stack; InitStack(&stack);
push(&stack, 10); push(&stack, 20); push(&stack, 30); printStack(&stack);
int val; if (pop(&stack, &val)) { printf("成功出栈: %d\n", val); } printStack(&stack);
return 0;}队列
顺序队列
#include <stdio.h>#include <stdbool.h>
#define MAXSIZE 5 // 定义队列最大长度
typedef struct { int data[MAXSIZE]; int front; // 头指针,指向队首元素 int rear; // 尾指针,指向队尾元素的下一个位置} Queue;
// 初始化队列void init(Queue *q) { q->front = q->rear = 0;}
// 判断队列是否为空bool isEmpty(Queue *q) { return q->front == q->rear;}
// 判断队列是否已满bool isFull(Queue *q) { return (q->rear + 1) % MAXSIZE == q->front;}
// 入队操作void enqueue(Queue *q, int x) { if (isFull(q)) { printf("队列已满\n"); return; } q->data[q->rear] = x; q->rear = (q->rear + 1) % MAXSIZE; printf("入队: %d\n", x);}
// 出队操作bool dequeue(Queue *q, int *x) { if (isEmpty(q)) { printf("队列为空\n"); return false; } if (x != NULL) { *x = q->data[q->front]; } q->front = (q->front + 1) % MAXSIZE; printf("出队: %d\n", *x); return true;}
// 打印队列void printQueue(Queue *q) { if (isEmpty(q)) { printf("队列为空\n"); return; } printf("队列内容: "); for (int i = q->front; i != q->rear; i = (i + 1) % MAXSIZE) { printf("%d ", q->data[i]); } printf("\n");}
// 测试代码int main() { Queue q; init(&q);
enqueue(&q, 10); enqueue(&q, 20); enqueue(&q, 30); enqueue(&q, 40); printQueue(&q);
int val; if (dequeue(&q, &val)) { printf("成功出队: %d\n", val); } printQueue(&q);
enqueue(&q, 50); enqueue(&q, 60); printQueue(&q);
return 0;}#### 链队列
```cpp#include <stdio.h>#include <stdlib.h>#include <stdbool.h>
// 链表节点定义typedef struct Node { int data; struct Node *next;} Node;
// 队列定义typedef struct { Node *front; Node *rear;} Queue;
// 初始化队列void initQueue(Queue *q) { q->front = q->rear = NULL;}
// 判断队列是否为空bool isEmpty(Queue *q) { return q->front == NULL;}
// 入队操作void enqueue(Queue *q, int x) { Node *newNode = (Node *)malloc(sizeof(Node)); if (newNode == NULL) { printf("内存分配失败!\n"); return; } newNode->data = x; newNode->next = NULL;
if (q->rear == NULL) { q->front = newNode; q->rear = newNode; } else { q->rear->next = newNode; q->rear = newNode; } printf("入队: %d\n", x);}
// 出队操作bool dequeue(Queue *q, int *x) { if (isEmpty(q)) { printf("队列为空,无法出队!\n"); return false; }
Node *p = q->front; *x = p->data; q->front = p->next; if (q->front == NULL) { q->rear = NULL; // 如果队列空了,更新队尾指针 } free(p); printf("出队: %d\n", *x); return true;}
// 打印队列void printQueue(Queue *q) { if (isEmpty(q)) { printf("队列为空\n"); return; } Node *p = q->front; printf("队列内容: "); while (p != NULL) { printf("%d ", p->data); p = p->next; } printf("\n");}
// 测试代码int main() { Queue q; initQueue(&q);
enqueue(&q, 10); enqueue(&q, 20); enqueue(&q, 30); printQueue(&q);
int val; if (dequeue(&q, &val)) { printf("成功出队: %d\n", val); } printQueue(&q);
enqueue(&q, 40); printQueue(&q);
return 0;}原文发布于 CSDN:链表&&栈&&队列(顺序/链式)
链表&&栈&&队列(顺序/链式)
https://www.tommywutong.cn/posts/csdn-import/csdn-146613136--/