c语言如何实现单链表的就地逆置操作
时间: 2023-10-10 20:08:03 浏览: 71
单链表的就地逆置操作可以使用三个指针来实现,具体步骤如下:
1. 定义三个指针变量,分别指向当前节点(p)、当前节点的前驱节点(pre)和当前节点的后继节点(next)。
2. 将当前节点的后继节点保存到next指针中。
3. 将当前节点的后继指针指向它的前驱节点,即将pre指针赋值给p指针的next成员。
4. 将pre指针移动到当前节点位置,将p指针移动到next指针指向的位置。
5. 重复执行第2步至第4步,直至p指针指向链表的末尾节点。
以下是具体的C语言代码实现:
```
void reverseList(Node *head) {
Node *pre = NULL;
Node *p = head;
Node *next = NULL;
while (p != NULL) {
next = p->next;
p->next = pre;
pre = p;
p = next;
}
head = pre;
}
```
其中,head是单链表的头节点指针。该函数会将单链表就地逆置,并将头节点指针指向逆置后的链表的新头节点。
相关问题
数据结构c语言 单链表的就地逆置
单链表的就地逆置是指不引入新的节点,只调整原有节点的指针,将单链表逆序排列。
实现单链表的就地逆置可以使用三个指针:prev、curr和next。开始时,将prev指向NULL,curr指向链表的首节点,next指向curr的下一个节点。
接下来,通过循环遍历单链表,每次将curr的指针指向prev,然后将prev、curr和next指针都向后移动一个节点。直到curr指向NULL,表示已经遍历到链表的末尾。
最后,将链表的头节点指向prev,完成就地逆置。
以下是C语言实现的代码示例:
#include <stdio.h>
struct Node {
int data;
struct Node* next;
};
// 单链表就地逆置函数
void reverseList(struct Node** head){
struct Node* prev = NULL;
struct Node* curr = *head;
struct Node* next = NULL;
while(curr != NULL){
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head = prev;
}
// 打印单链表函数
void printList(struct Node* head){
struct Node* temp = head;
while(temp != NULL){
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main(){
// 创建一个示例单链表: 1 -> 2 -> 3 -> 4 -> 5
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
struct Node* fourth = NULL;
struct Node* fifth = NULL;
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
fourth = (struct Node*)malloc(sizeof(struct Node));
fifth = (struct Node*)malloc(sizeof(struct Node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = fourth;
fourth->data = 4;
fourth->next = fifth;
fifth->data = 5;
fifth->next = NULL;
printf("原始链表: ");
printList(head);
reverseList(&head);
printf("就地逆置后的链表: ");
printList(head);
return 0;
}
运行此程序,输出结果为:
原始链表: 1 2 3 4 5
就地逆置后的链表: 5 4 3 2 1
用c语言写单链表的就地逆置
好的,下面是用C语言实现单链表就地逆置的代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
Node *createList(int n) {
Node *head = NULL, *tail = NULL;
int i, val;
for (i = 0; i < n; i++) {
printf("请输入第%d个节点的值:", i + 1);
scanf("%d", &val);
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = val;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
return head;
}
void printList(Node *head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
void reverseList(Node **head) {
if (*head == NULL || (*head)->next == NULL) {
return;
}
Node *prev = NULL, *curr = *head, *next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head = prev;
}
int main() {
int n;
printf("请输入链表的长度:");
scanf("%d", &n);
Node *head = createList(n);
printf("原始链表:");
printList(head);
reverseList(&head);
printf("逆置后的链表:");
printList(head);
return 0;
}
```
该代码首先定义了一个`Node`结构体表示链表中的节点,包含了一个整数`data`和下一个节点的指针`next`。然后通过`createList`函数创建了一个长度为`n`的单链表,通过`printList`函数打印出了原始链表。接着通过`reverseList`函数实现了单链表的就地逆置,最后再次通过`printList`函数打印出了逆置后的链表。