用C语言反向输出一个链表。
时间: 2024-05-09 18:17:26 浏览: 18
可以使用递归或者栈来实现链表的反向输出。以下是使用递归的方式:
```c
struct ListNode {
int val;
struct ListNode *next;
};
void reversePrint(struct ListNode* head){
if(head == NULL){
return;
}
reversePrint(head->next); // 递归输出下一个节点
printf("%d ", head->val); // 输出当前节点
}
```
或者可以使用栈的方式实现:
```c
struct ListNode {
int val;
struct ListNode *next;
};
void reversePrint(struct ListNode* head){
if(head == NULL){
return;
}
// 创建栈
struct ListNode* stack[10000];
int top = -1;
// 把节点压入栈中
while(head){
stack[++top] = head;
head = head->next;
}
// 依次弹出节点并输出
while(top >= 0){
printf("%d ", stack[top--]->val);
}
}
```
相关问题
用C语言编写一i个完整程序反向输出一个链表。
好的,以下是代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义链表结构体
typedef struct Node {
int data;
struct Node* next;
} Node;
// 反转链表函数
Node* reverseList(Node* head) {
Node* prev = NULL;
Node* curr = head;
Node* next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
// 输出链表函数
void printList(Node* head) {
Node* curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
int main() {
// 初始化链表
Node* head = malloc(sizeof(Node));
head->data = 1;
head->next = malloc(sizeof(Node));
head->next->data = 2;
head->next->next = malloc(sizeof(Node));
head->next->next->data = 3;
head->next->next->next = NULL;
// 输出原始链表
printf("Original list: ");
printList(head);
// 反转链表
Node* reversed = reverseList(head);
// 输出反转后的链表
printf("Reversed list: ");
printList(reversed);
return 0;
}
```
希望这个程序可以帮助你!
请用C语言实现反向reverse一个单向链表编程
好的,以下是用C语言实现反向reverse一个单向链表的代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义单向链表结构体
typedef struct Node {
int data;
struct Node* next;
} Node;
// 创建节点函数
Node* createNode(int data) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = NULL;
return node;
}
// 反向函数
Node* reverse(Node* head) {
Node* prev = NULL;
Node* current = head;
Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
// 打印函数
void printList(Node* head) {
Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
// 创建链表
Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
head->next->next->next->next = createNode(5);
printf("原链表:");
printList(head);
// 反向链表
head = reverse(head);
printf("反向链表:");
printList(head);
return 0;
}
```
运行结果如下:
```
原链表:1 2 3 4 5
反向链表:5 4 3 2 1
```
相关推荐
![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)