怎么在线性表,栈和队列中进行基本操作,给出C语言的实现代码
时间: 2024-11-22 14:45:23 浏览: 21
在线性表、栈(Stack)和队列(Queue)中,基础操作主要包括插入(push)、删除(pop)、查看顶部元素(top)、查看队首元素(front)以及队尾插入和删除(enqueue/dequeue)。以下是C语言对这些操作的一些简单实现:
1. **线性表(数组实现)**:
```c
#include <stdio.h>
#define MAX_SIZE 10
int arr[MAX_SIZE], size = 0;
// 插入到末尾
void insert(int value) {
if (size == MAX_SIZE) {
printf("List is full.\n");
} else {
arr[size++] = value;
printf("Inserted %d at index %d\n", value, size - 1);
}
}
// 删除第一个元素
void remove() {
if (size > 0) {
arr[0] = arr[size - 1];
size--;
printf("Removed element from front: %d\n", arr[0]);
} else {
printf("List is empty.\n");
}
}
// 查看末尾元素
int view_last() {
return size ? arr[size - 1] : -1; // 如果非空返回最后一个元素,否则返回-1
}
```
2. **栈(数组实现)**:
```c
#include <stdio.h>
#define STACK_SIZE 5
int stack[STACK_SIZE], top = -1;
// 入栈
void push(int value) {
if (top == STACK_SIZE - 1) {
printf("Stack is full.\n");
} else {
stack[++top] = value;
printf("Pushed %d onto the stack.\n", value);
}
}
// 出栈
int pop() {
if (top >= 0) {
int value = stack[top--];
printf("Popped %d from the stack.\n", value);
return value;
} else {
printf("Stack is empty.\n");
return -1; // 返回特殊值表示栈空
}
}
// 查看栈顶元素
int peek() {
return top >= 0 ? stack[top] : -1; // 非空则返回栈顶元素,否则返回-1
}
```
3. **队列(数组实现)**:
```c
#include <stdio.h>
#define QUEUE_SIZE 5
int queue[QUEUE_SIZE], front = -1, rear = -1;
// 入队
void enqueue(int value) {
if (rear == QUEUE_SIZE - 1) {
printf("Queue is full.\n");
} else {
rear = (rear + 1) % QUEUE_SIZE;
queue[rear] = value;
printf("Enqueued %d to the queue.\n", value);
}
}
// 出队
int dequeue() {
if (front == rear) {
printf("Queue is empty.\n");
return -1; // 返回特殊值表示队列空
} else {
int value = queue[front];
front = (front + 1) % QUEUE_SIZE;
printf("Dequeued %d from the queue.\n", value);
return value;
}
}
// 查看队首元素
int view_front() {
return front >= 0 ? queue[front] : -1; // 非空则返回队首元素,否则返回-1
}
```
注意,这里只是简单的数组实现,实际应用中可能需要更复杂的数据结构如链表。此外,对于队列,如果经常需要动态调整大小,通常会使用链表数据结构。
阅读全文
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)