赞
踩
数组:简单易实现,空间固定,可能造成空间浪费或溢出。
- 1#include <stdio.h>
- 2#define MAX_SIZE 100
- 3
- 4typedef struct {
- 5 int data[MAX_SIZE];//静态数组
- 6 int top;
- 7} Stack;
- 8
- 9void initStack(Stack* s) {
- 10 s->top = -1; // 初始化栈顶指针为-1,表示空栈
- 11}
- 12
- 13void pushStack(Stack* s, int item) {
- 14 if (s->top == MAX_SIZE - 1) {
- 15 printf("Stack Overflow\n");
- 16 return;
- 17 }
- 18 s->data[++(s->top)] = item;
- 19}
- 20
- 21int popStack(Stack* s) {
- 22 if (s->top == -1) {
- 23 printf("Stack Underflow\n");
- 24 return -1;
- 25 }
- 26 return s->data[s->top--];
- 27}
- 28
- 29int peek(Stack* s) {
- 30 if (s->top != -1)
- 31 return s->data[s->top];
- 32 else
- 33 return -1; // 或其他错误码
- 34}
- 35
- 36int is_empty(Stack* s) {
- 37 return s->top == -1;
- 38}
判断空(is_empty):检查栈顶指针是否为NULL。
- #include <stdio.h>
- #include <stdlib.h>
-
- typedef struct Node {
- int data;
- struct Node* next;
- } Node;
-
- typedef struct Stack {
- Node* top;
- } Stack;
-
- void initStack(Stack* s) {
- s->top = NULL;
- }
-
- void push(Stack* s, int item) {
- Node* newNode = (Node*)malloc(sizeof(Node));
- if (newNode == NULL) {
- printf("Memory allocation failed\n");
- return;
- }
- newNode->data = item;
- newNode->next = s->top;
- s->top = newNode;
- }
-
- int pop(Stack* s) {
- if (s->top == NULL) {
- printf("Stack Underflow\n");
- return -1;
- }
- Node* temp = s->top;
- int data = temp->data;
- s->top = temp->next;
- free(temp);
- return data;
- }
-
- int peek(Stack* s) {
- if (s->top != NULL)
- return s->top->data;
- else
- return -1;
- }
-
- int isEmpty(Stack* s) {
- return s->top == NULL;
- }
-
- // 注意:记得在程序结束时释放栈中所有节点的内存
- 1#include <stdio.h>
- 2#define MAX_SIZE 100
- 3
- 4typedef struct {
- 5 int data[MAX_SIZE];
- 6 int front, rear;
- 7} Queue;
- 8
- 9void initQueue(Queue* q) {
- 10 q->front = q->rear = 0;
- 11}
- 12
- 13void enqueue(Queue* q, int item) {
- 14 if ((q->rear + 1) % MAX_SIZE == q->front) {
- 15 printf("Queue Overflow\n");
- 16 return;
- 17 }
- 18 q->data[q->rear] = item;
- 19 q->rear = (q->rear + 1) % MAX_SIZE;
- 20}
- 21
- 22int dequeue(Queue* q) {
- 23 if (q->front == q->rear) {
- 24 printf("Queue Underflow\n");
- 25 return -1;
- 26 }
- 27 int item = q->data[q->front];
- 28 q->front = (q->front + 1) % MAX_SIZE;
- 29 return item;
- 30}
- 31
- 32int front(Queue* q) {
- 33 if (!is_empty(q))
- 34 return q->data[q->front];
- 35 else
- 36 return -1; // 或其他错误码
- 37}
- 38
- 39int is_empty(Queue* q) {
- 40 return q->front == q->rear;
- 41}
- #include <stdio.h>
- #include <stdlib.h>
-
- typedef struct Node {
- int data;
- struct Node* next;
- } Node;
-
- typedef struct Queue {
- Node* front;
- Node* rear;
- } Queue;
-
- void initQueue(Queue* q) {
- q->front = q->rear = NULL;
- }
-
- void enqueue(Queue* q, int item) {
- Node* newNode = (Node*)malloc(sizeof(Node));
- if (newNode == NULL) {
- printf("Memory allocation failed\n");
- return;
- }
- newNode->data = item;
- newNode->next = NULL;
- if (q->rear == NULL)
- q->front = q->rear = newNode;
- else {
- q->rear->next = newNode;
- q->rear = newNode;
- }
- }
-
- int dequeue(Queue* q) {
- if (q->front == NULL) {
- printf("Queue Underflow\n");
- return -1;
- }
- Node* temp = q->front;
- int data = temp->data;
- q->front = temp->next;
- if (q->front == NULL)
- q->rear = NULL;
- free(temp);
- return data;
- }
-
- int front(Queue* q) {
- if (q->front != NULL)
- return q->front->data;
- else
- return -1;
- }
-
- int isEmpty(Queue* q) {
- return q->front == NULL;
- }
-
- // 同样,确保在程序结束时释放队列中所有节点的内存
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。