c语言使用指针完成逆序输出
时间: 2023-08-07 21:08:21 浏览: 72
可以使用指针来实现一个逆序输出的函数,具体实现如下:
```c
#include <stdio.h>
void reverse_print(char *str){
if(*str == '\0')
return;
reverse_print(str+1); // 递归调用
printf("%c", *str);
}
int main(){
char str[] = "hello world";
reverse_print(str);
return 0;
}
```
在这个例子中,使用递归的方式来实现逆序输出。函数 `reverse_print` 传入一个指向字符串的指针 `str`,如果当前指针指向的字符是字符串的结束符 `\0`,则返回。否则,递归调用 `reverse_print` 函数传入指针 `str+1`,也就是指针指向下一个字符的位置,直到指针指向字符串结束符。最后,再依次输出每个字符即可。
相关问题
C语言用指针实现逆序操作
下面是用指针实现逆序操作的C语言代码:
```c
#include <stdio.h>
void reverse(int *arr, int len) {
int *p1 = arr, *p2 = arr + len - 1;
while (p1 < p2) {
int temp = *p1;
*p1 = *p2;
*p2 = temp;
p1++;
p2--;
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int len = sizeof(arr) / sizeof(int);
printf("原数组:");
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
reverse(arr, len);
printf("\n逆序数组:");
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
在函数`reverse`中,我们定义了两个指针`p1`和`p2`,它们分别指向数组的头部和尾部。然后我们通过循环交换它们指向的元素,直到它们相遇为止,即完成了数组的逆序操作。在主函数中,我们调用了`reverse`函数,并输出了逆序后的数组。
c语言如何实现链表逆序输出
要实现链表逆序输出,可以使用递归或者迭代的方式。以下是分别使用这两种方法的示例代码。
递归方法:
```c
#include <stdio.h>
struct Node {
int data;
struct Node* next;
};
void reversePrint(struct Node* head) {
if (head == NULL) {
return;
}
reversePrint(head->next);
printf("%d ", head->data);
}
int main() {
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
reversePrint(head);
return 0;
}
```
迭代方法:
```c
#include <stdio.h>
struct Node {
int data;
struct Node* next;
};
void reversePrint(struct Node* head) {
struct Node* prev = NULL;
struct Node* current = head;
struct Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
}
int main() {
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
reversePrint(head);
return 0;
}
```
以上两种方法都能实现链表逆序输出,递归方法通过递归调用实现逆序输出,迭代方法则通过交换链表节点的指针实现逆序输出。
相关推荐
![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_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)
![](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)