#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100001
struct tNode{
int taddr;
int tdata;
int tnext;
};
struct Node{
int addr;
int data;
int next;
struct Node* pNext;
};
typedef struct Node* RLList;
typedef struct Node RLNode;
typedef struct tNode TNode;
TNode nodes[MAXSIZE];
RLList Reverse(RLList L,int K){
int cnt=1;
RLList new,old,temp;
new=L->pNext;
old=new->pNext;
while (cnt<K) {
temp=old->pNext;
old->pNext=new;
new=old;
old=temp;
cnt++;
}
L->pNext->pNext=old;
L->pNext=new;
return L;
}
int main(int argc, char *argv[]) {
int flag=1;
RLList L,node,p;
L= (RLList)malloc(sizeof(struct Node));
L->pNext=NULL;
p=L;
RLList new,old;
int firAddr,N,s;
scanf("%d%d%d",&firAddr,&N,&s);
for (int i=0;i<N;i++) {
TNode tn;
scanf("%d%d%d",&tn.taddr,&tn.tdata,&tn.tnext);
nodes[tn.taddr]=tn;
}
for (int i=0;i<N;i++) {
node=(RLList)malloc(sizeof(struct Node));
node->pNext=NULL;
node->addr=firAddr;
node->data=nodes[firAddr].tdata;
node->next=nodes[firAddr].tnext;
firAddr=nodes[firAddr].tnext;
p->pNext=node;
p=node;
}
if (N>2) {
L=Reverse(L, s);
}
if (N==2&&s==2) {
new=L->pNext;
old=new->pNext;
L->pNext=old;
old->pNext=new;
new->pNext=NULL;
}
L=L->pNext;
while (L) {
if (!flag) {
printf("\n");
}
printf("%05d %d %05d",L->addr,L->data,L->next);
L=L->pNext;
if (!(L->pNext)) {
break;
}
flag=0;
}
printf("\n");
printf("%05d %d %d",L->addr,L->data,L->next);
return 0;
}
代码过不了,但是找不出错在哪里。

本文探讨了一种链表逆序算法的实现细节,通过具体的C语言代码示例,展示了如何对链表进行部分逆序操作。特别关注了逆序过程中指针的操作,并讨论了不同链表长度下算法的具体实现方式。
412

被折叠的 条评论
为什么被折叠?



