struct intNode
{
int data;
struct intNode * next;
}
以上结果构造了链表T ,调用如下revers函数后将链表颠倒,完成该函数功能
[code]
//不知道是不是这个意思:) 欢迎大家指正
struct intNode * revers( struct intNode *t)
{
struct intNode *temp,*head;
if(t==null){
return t;
}
head=t;
t=t->next;
head->next=null;
while(t!=null){
temp=t;
t=t->next;
temp->next=head;
head=temp;
}
return head;
}
#include "../my.h"
typedef struct intNode
{
int data;
struct intNode * next;
} intNode;
struct intNode *revers(struct intNode *t)
{
struct intNode *next_bak = NULL, *current = t, *nn_bak = NULL;
if (t) next_bak = t->next;
if (next_bak) nn_bak = next_bak->next;
while(current) {
if (next_bak) next_bak->next = current;//do it
current = next_bak;
next_bak = nn_bak;
if (nn_bak)
nn_bak = next_bak->next;
else
nn_bak = NULL;
//printf("%p\n%p\n%p\n\n",current,next_bak,nn_bak);
}
if (t) t->next = NULL;
}
int main()
{
intNode k1,k2,k3,k4,k5, *p;
bzero(&k1,sizeof(intNode));
bzero(&k2,sizeof(intNode));
bzero(&k3,sizeof(intNode));
bzero(&k4,sizeof(intNode));
bzero(&k5,sizeof(intNode));
k1.data = 11;
k1.next = &k2;
k2.data = 22;
k2.next = &k3;
k3.data = 33;
k3.next = &k4;
k4.data = 44;
k4.next = &k5;
k5.data = 55;
k5.next = NULL;
p = &k1;
while(p) {
printf("[%p][%d][-->%p]\n", p, p->data, p->next);
p = p->next;
}
revers(&k1);
printf("After revers\n");
p=&k5;
while(p) {
printf("[%p][%d][-->%p]\n", p,p->data, p->next);
p = p->next;
}
}