C编程连表倒置逆序的问题 潭练习

来源:百度知道 编辑:UC知道 时间:2024/05/29 20:56:30
谁给我讲讲啊...
55555555
我看晕;饿`
谢谢了..你的和书上一样把??
呵呵。.我这个会

对!吧原来指向后接点,改成指向前接点!(只有一个指针)
如果有两个指针,就一个指前,一个指后

由于链表在内存中本身是不连续的,靠的就是链表结点指针来指向下一个结点,因此要将链表倒置,就只需要重新分配链表结点指针的指向,如:
有链表(C指向NULL说明C是最后一个结点,NULL表示空结点)
A->B->C->NULL
要将其倒置,就是
C->next = B;
B->next = A;
A->next = NULL;
倒置后就是:NULL<-A<-B<-C(按正常顺序写就是C->B->A->NULL)

下面是一个例子:

#include <stdio.h>
#include <malloc.h>
#define NULL 0

//定义结点的结构体
struct Node
{
int data;
struct Node* next;
};

int main()
{
//首先构造一个链表,有元素A,B,C,对应的值分别为1,2,3
struct Node* A = (struct Node*)malloc( sizeof(struct Node) );
struct Node* B = (struct Node*)malloc( sizeof(struct Node) );
struct Node* C = (struct Node*)malloc( sizeof(struct Node) );
A->data = 1;
B->data = 2;
C->data = 3;
A->next = B;
B->next = C;
C->next = NULL;

//链表的头结点为A
struct Node* head = A;<