C语言反向输出不带头结点单链表
时间: 2023-03-30 09:04:40 浏览: 229
可以使用递归或者栈来实现反向输出不带头结点单链表。具体实现方法可以参考以下代码:
```c
void reversePrint(Node* head) {
if (head == NULL) {
return;
}
reversePrint(head->next);
printf("%d ", head->data);
}
```
其中,Node 是单链表节点的结构体,包含一个 int 类型的 data 和一个指向下一个节点的指针 next。reversePrint 函数使用递归实现反向输出单链表,先递归输出后面的节点,再输出当前节点的数据。
相关问题
用C语言写一个带头结点单链表
C语言写一个带头结点的单链表,需要先定义一个结构体来存储每一个结点的数据。一般的结构体定义如下:
```
struct Node {
int data;
struct Node *next;
};
```
然后,需要定义一个指针变量来指向链表的头结点:
```
struct Node *head = NULL;
```
最后,可以实现一些基本的操作,如插入、删除、查询等。例如,插入操作如下:
```
void insert(int value) {
struct Node *new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = value;
new_node->next = head;
head = new_node;
}
```
带头结点的单链表有很多应用,在实际开发中会经常用到。带头结点的单链表比不带头结点的链表更加方便和实用,同时也更加容易实现。
用c语言实现编写实现带头结点单链表就地逆置的成员函数
下面是用C语言实现带头结点单链表就地逆置的成员函数的代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义单链表的结点结构体
typedef struct ListNode {
int data;
struct ListNode* next;
} ListNode;
// 初始化带头结点的单链表
ListNode* initList() {
ListNode* head = (ListNode*)malloc(sizeof(ListNode));
head->next = NULL;
return head;
}
// 尾插法建立单链表
void createList(ListNode* head) {
int data;
ListNode* tail = head;
printf("请输入链表中的元素(以-1结束):");
while (1) {
scanf("%d", &data);
if (data == -1) {
break;
}
ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
newNode->data = data;
newNode->next = NULL;
tail->next = newNode;
tail = newNode;
}
}
// 打印带头结点的单链表
void printList(ListNode* head) {
ListNode* p = head->next;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
// 就地逆置带头结点的单链表
void reverseList(ListNode* head) {
ListNode* p = head->next;
ListNode* q = p->next;
p->next = NULL; // 将第一个结点的指针域赋值为NULL
while (q != NULL) {
ListNode* r = q->next;
q->next = p;
p = q;
q = r;
}
head->next = p; // 将头结点的指针域指向原链表的最后一个结点
}
int main() {
ListNode* head = initList();
createList(head);
printf("原链表:");
printList(head);
reverseList(head);
printf("逆置后的链表:");
printList(head);
return 0;
}
```
运行结果:
```
请输入链表中的元素(以-1结束):1 2 3 4 5 -1
原链表:1 2 3 4 5
逆置后的链表:5 4 3 2 1
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)