求助:结果是一样的,但老师说要用单向链表逆序,请教方法
#import <Foundation/Foundation.h>typedef struct Node_ {
int value;
struct Node_ *next;
struct Node_ *pre;
} Node;
Node *createNode(int value,Node *pre,Node *next) {
Node* node = malloc(sizeof(Node));
node->value = value;
if (next!=NULL) {
next->pre = node;
}
node->next = next;
if (pre!=NULL) {
pre->next = node;
}
node->pre = pre;
return node;
}
void printList(Node *firstNode) {
for (Node *node=firstNode; node!=NULL; node=node->next) {
printf("%d\n",node->value);
}
}
void rprintList(Node *end) {
for (Node *node=end; node!=NULL; node=node->pre) {
printf("%d\n",node->value);
}
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
Node *first = createNode(0, NULL,NULL);
Node *end = first;
end = createNode(1, end,NULL);
end = createNode(2, end,NULL);
end = createNode(3, end,NULL);
end = createNode(4, end,NULL);
end = createNode(5, end,NULL);
end = createNode(6, end,NULL);
end = createNode(7, end,NULL);
end = createNode(8, end,NULL);
end = createNode(9, end,NULL);
printList(first);
char *str = "reversed";
printf("%s\n",str);
rprintList(end);
}
return 0;
}