单链表实现逆置
#include <iostream>
#include<stdio.h>
int SIZE = 5;
typedef struct Node
{
int data;
struct Node *next;
}Node, * Hnode;
int main() {
//构建
Node
//输入值
for (int i = 0; i < SIZE;i++) {
q = (Node*)malloc(sizeof(Node));
scanf_s("%d",&q->data);
p->next = q;
p = q;
}
p->next = NULL;//一定要让指针指向NULL (报错)
//逆置
p = head->next;
head->next = NULL;
while (p != NULL) {
q = p;
p = p->next;
q->next = head->next;
head->next = q;
}
p = head->next;
while (p != NULL) {
printf("%d ",p->data);
p = p->next;
}
return 0;

浙公网安备 33010602011771号