#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
} *ListNode;
ListNode CreateList(int n) {
ListNode head,p,q;
head = p = (ListNode)malloc(sizeof(node));
p->data = 0;
for(int i = 0;i < n;i++) {
q = (ListNode)malloc(sizeof(node));
q->data = 2*i + 1;
p->next = q;
p = q;
}
p->next = head;
return head;
}
void PrintList(ListNode head) {
ListNode p = head->next;
while(p != head) {
printf("%d ",p->data);
p = p->next;
}
printf("\n\n");
}
void FreeHeap(ListNode head) {
ListNode p,q;
p = head;
q = p->next;
while(q != head) {
p = q;
q = p->next;
free(p);
}
free(head);
}
ListNode ReverseList(ListNode head) {
ListNode p,q,s;
//链表为空,或是单结点链表直接返回头结点
if (head == NULL || head->next == NULL) return head;
p = head;
q = head->next;
while (q != head) {
s = q->next;
q->next = p;
p = q;
q = s;
}
head->next = p;
return head;
}
int main() {
int n = 10;
ListNode head = CreateList(n);
PrintList(head);
PrintList(ReverseList(head));
FreeHeap(head);
return 0;
}
用程序运行有许多错误,你用的是C语言吗?
运行截图都发出来了,没有错误。