C语言 表、栈和队列详解及实例代码
什么是表、栈和队列
- 表
表是一种动态的数据结构,它的每个元素都包含一个指向下一个元素的指针。表常用于构建链表,提供了动态插入和删除元素的能力。
- 栈
栈是一种先进后出的数据结构,它具有压入和弹出操作,插入和删除元素均在栈顶执行。栈在编程中可用于实现函数的调用、表达式求值等。
- 队列
队列是一种先进先出的数据结构,它具有入队和出队操作,插入和删除元素均在队尾和队头执行。
C语言表的实例代码
#include <stdio.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
void print_list(struct node *head) {
struct node *current = head;
while (current != NULL) {
printf("%d ", current->value);
current = current->next;
}
printf("\n");
}
void insert_node(struct node **head, int num) {
struct node *new_node = (struct node *)malloc(sizeof(struct node));
new_node->value = num;
new_node->next = NULL;
if (*head == NULL) {
*head = new_node;
} else {
struct node *current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = new_node;
}
}
void delete_node(struct node **head, int num) {
if (*head == NULL) {
return;
}
struct node *current = *head;
if (current->value == num) {
*head = current->next;
free(current);
return;
}
while (current->next != NULL && current->next->value != num) {
current = current->next;
}
if (current->next != NULL) {
struct node *to_delete = current->next;
current->next = to_delete->next;
free(to_delete);
}
}
int main() {
struct node *head = NULL;
insert_node(&head, 5);
insert_node(&head, 10);
insert_node(&head, 15);
insert_node(&head, 20);
print_list(head);
delete_node(&head, 15);
print_list(head);
return 0;
}
上面这段代码展示了如何使用C语言来为链表实现动态插入、删除和遍历。
C语言栈和队列的实例代码
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
int stack[MAX_SIZE];
int top = -1;
void push(int num) {
if (top >= MAX_SIZE - 1) {
printf("Stack Overflow\n");
return;
}
top++;
stack[top] = num;
}
void pop() {
if (top == -1) {
printf("Stack Underflow\n");
return;
}
top--;
}
int peek() {
if (top == -1) {
return -1;
}
return stack[top];
}
int is_empty() {
return top == -1;
}
int is_full() {
return top == MAX_SIZE - 1;
}
void print_stack() {
int i;
printf("Stack: ");
for (i = 0; i <= top; i++) {
printf("%d ", stack[i]);
}
printf("\n");
}
struct queue {
int front;
int rear;
int arr[MAX_SIZE];
};
struct queue *create_queue() {
struct queue *q = (struct queue *)malloc(sizeof(struct queue));
q->front = -1;
q->rear = -1;
return q;
}
void enqueue(struct queue *q, int num) {
if (q->rear >= MAX_SIZE - 1) {
printf("Queue Overflow\n");
return;
}
if (q->front == -1) {
q->front = 0;
}
q->rear++;
q->arr[q->rear] = num;
}
void dequeue(struct queue *q) {
if (q->front == -1 || q->front > q->rear) {
printf("Queue Underflow\n");
return;
}
q->front++;
}
int is_queue_empty(struct queue *q) {
return q->front == -1 || q->front > q->rear;
}
int peek_queue(struct queue *q) {
if (q->front == -1 || q->front > q->rear) {
return -1;
}
return q->arr[q->front];
}
void print_queue(struct queue *q) {
int i;
printf("Queue: ");
for (i = q->front; i <= q->rear; i++) {
printf("%d ", q->arr[i]);
}
printf("\n");
}
int main() {
push(1);
push(2);
push(3);
print_stack();
pop();
print_stack();
printf("Top element: %d\n", peek());
printf("Stack is empty: %d\n", is_empty());
printf("Stack is full: %d\n", is_full());
struct queue *q = create_queue();
enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);
print_queue(q);
dequeue(q);
print_queue(q);
printf("Front element: %d\n", peek_queue(q));
printf("Queue is empty: %d\n", is_queue_empty(q));
return 0;
}
这段代码演示了如何使用C语言为栈和队列实现动态插入、删除和查询操作。两个示例中使用了数值型数据,用户可以根据实际需求修改数据类型和数据数量。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C语言 表、栈和队列详解及实例代码 - Python技术站