帮我写一个c语言 循环地址存储
时间: 2024-02-06 19:03:40 浏览: 31
循环地址存储是一种常见的技巧,可以用来优化某些算法。以下是一个简单的示例,展示如何使用循环地址存储来反转一个数组:
```c
#include <stdio.h>
void reverse(int arr[], int n) {
int i, j, tmp;
for (i = 0, j = n - 1; i < j; i++, j--) {
tmp = *(arr + i);
*(arr + i) = *(arr + j);
*(arr + j) = tmp;
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
reverse(arr, n);
int i;
for (i = 0; i < n; i++) {
printf("%d ", *(arr + i));
}
return 0;
}
```
在反转函数 `reverse` 中,我们使用了指针算术运算来访问数组中的元素。通过使用 `arr + i` 和 `arr + j` 来访问数组中的元素,我们可以避免使用 `arr[i]` 和 `arr[j]` 来访问数组中的元素,从而提高代码的效率。
相关问题
写一个C语言双向循环链表
以下是一个简单的双向循环链表的C语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义双向循环链表节点的结构体
typedef struct Node {
int data; // 节点存储的数据
struct Node* prev; // 指向前一个节点的指针
struct Node* next; // 指向后一个节点的指针
} Node;
// 初始化双向循环链表
Node* initList() {
// 创建一个头节点并初始化
Node* head = (Node*)malloc(sizeof(Node));
head->data = 0;
head->prev = head;
head->next = head;
return head;
}
// 在链表末尾插入一个节点
void insert(Node* head, int data) {
// 创建一个新节点并初始化
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->prev = head->prev;
node->next = head;
// 更新链表节点的指针
head->prev->next = node;
head->prev = node;
}
// 删除指定节点
void delete(Node* head, Node* node) {
// 更新链表节点的指针
node->prev->next = node->next;
node->next->prev = node->prev;
// 释放被删除节点的内存
free(node);
}
// 打印链表
void printList(Node* head) {
Node* p = head->next;
while (p != head) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main() {
Node* head = initList();
insert(head, 1);
insert(head, 2);
insert(head, 3);
printList(head);
delete(head, head->next);
printList(head);
return 0;
}
```
这个实现中,我们定义了一个双向循环链表节点的结构体,并在initList函数中创建了一个头节点并初始化。在insert函数中,我们创建一个新节点并插入到链表末尾。在delete函数中,我们删除指定节点并释放其内存。最后,在printList函数中,我们遍历链表并打印每个节点的数据。
用C语言写一个循环存储的策略
当使用C语言编写循环存储策略时,我们可以使用数组来实现。以下是一个示例代码,演示如何使用循环存储策略来实现一个简单的队列。
```c
#include <stdio.h>
#define MAX_SIZE 10
typedef struct {
int data[MAX_SIZE];
int front;
int rear;
} CircularQueue;
void initQueue(CircularQueue *queue) {
queue->front = -1;
queue->rear = -1;
}
int isQueueEmpty(CircularQueue *queue) {
return (queue->front == -1 && queue->rear == -1);
}
int isQueueFull(CircularQueue *queue) {
return ((queue->rear + 1) % MAX_SIZE == queue->front);
}
void enqueue(CircularQueue *queue, int item) {
if (isQueueFull(queue)) {
printf("Queue is full. Cannot enqueue.\n");
return;
}
if (isQueueEmpty(queue)) {
queue->front = 0;
queue->rear = 0;
} else {
queue->rear = (queue->rear + 1) % MAX_SIZE;
}
queue->data[queue->rear] = item;
}
int dequeue(CircularQueue *queue) {
if (isQueueEmpty(queue)) {
printf("Queue is empty. Cannot dequeue.\n");
return -1;
}
int item = queue->data[queue->front];
if (queue->front == queue->rear) {
// If there was only one element in the queue
queue->front = -1;
queue->rear = -1;
} else {
queue->front = (queue->front + 1) % MAX_SIZE;
}
return item;
}
int main() {
CircularQueue queue;
initQueue(&queue);
enqueue(&queue, 5);
enqueue(&queue, 10);
enqueue(&queue, 15);
printf("Dequeued item: %d\n", dequeue(&queue));
printf("Dequeued item: %d\n", dequeue(&queue));
printf("Dequeued item: %d\n", dequeue(&queue));
return 0;
}
```
这段代码实现了一个循环队列,利用数组的循环存储特性来实现入队和出队操作。你可以根据需要修改 `MAX_SIZE` 宏定义来调整队列的最大容量。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)