版本1
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}Node;
Node *head,*p;
Node * ReverseLink(Node *head)
{
Node *p1, *p2, *p3;
if(head==NULL || head->next==NULL)
return head;
p1=head, p2=p1->next;
while(p2)
{
p3=p2->next;
p2->next=p1;
p1=p2;
p2=p3;
}
head->next=NULL;
head=p1;
return head;
}
void CreateList(int n)
{
Node *q;
int i;
printf("Input %2d data: ",n);
head=(Node *)malloc(sizeof(Node));
q=head;
scanf("%d",&q->data);
for(i=2;i<=n;i++)
{
q->next=(Node *)malloc(sizeof(Node));
q=q->next;
scanf("%d",&q->data);
}
q->next=NULL;
}
void PrintList()
{
Node *q;
q=head;
while (q!=NULL)
{
printf("%-8d",q->data);
q=q->next;
}
printf("\n");
}
void main()
{
CreateList(5);
PrintList();
head=ReverseLink(head);
PrintList();
}
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}Node;
Node *head,*p;
Node * ReverseLink(Node *head)
{
Node *p1, *p2, *p3;
if(head==NULL || head->next==NULL)
return head;
p1=head, p2=p1->next;
while(p2)
{
p3=p2->next;
p2->next=p1;
p1=p2;
p2=p3;
}
head->next=NULL;
head=p1;
return head;
}
void CreateList(int n)
{
Node *q;
int i;
printf("Input %2d data: ",n);
head=(Node *)malloc(sizeof(Node));
q=head;
scanf("%d",&q->data);
for(i=2;i<=n;i++)
{
q->next=(Node *)malloc(sizeof(Node));
q=q->next;
scanf("%d",&q->data);
}
q->next=NULL;
}
void PrintList()
{
Node *q;
q=head;
while (q!=NULL)
{
printf("%-8d",q->data);
q=q->next;
}
printf("\n");
}
void main()
{
CreateList(5);
PrintList();
head=ReverseLink(head);
PrintList();
}
版本2
#include <iostream>
#include <assert.h>
using namespace std;
struct LNode{
char data;
LNode * next;
};
LNode * initList()
{
LNode *head=new LNode;
LNode *curPtr, *newPtr;
curPtr=head;
int i=0;
char ch='A';
while(i++<10)
{
newPtr=new LNode;
newPtr->data=ch++;
curPtr->next=newPtr;
curPtr=newPtr;
}
newPtr->next=NULL;
return head;
}
void print(LNode *head)
{
LNode *ptr=head->next;
while(ptr != NULL)
{
cout << ptr->data << " ";
ptr=ptr->next;
}
cout << endl;
}
void reverse(LNode *head)
{
assert(head != NULL && head->next != NULL);
LNode *ptr=head->next->next;
head->next->next=NULL;
while(ptr != NULL)
{
LNode *tmp=ptr->next;
ptr->next=head->next;
head->next=ptr;
ptr=tmp;
}
}
int main()
{
LNode *head=initList();
print(head);
cout << "After reverse: " << endl;
reverse(head);
print(head);
system("PAUSE");
return 0;
}
#include <assert.h>
using namespace std;
struct LNode{
char data;
LNode * next;
};
LNode * initList()
{
LNode *head=new LNode;
LNode *curPtr, *newPtr;
curPtr=head;
int i=0;
char ch='A';
while(i++<10)
{
newPtr=new LNode;
newPtr->data=ch++;
curPtr->next=newPtr;
curPtr=newPtr;
}
newPtr->next=NULL;
return head;
}
void print(LNode *head)
{
LNode *ptr=head->next;
while(ptr != NULL)
{
cout << ptr->data << " ";
ptr=ptr->next;
}
cout << endl;
}
void reverse(LNode *head)
{
assert(head != NULL && head->next != NULL);
LNode *ptr=head->next->next;
head->next->next=NULL;
while(ptr != NULL)
{
LNode *tmp=ptr->next;
ptr->next=head->next;
head->next=ptr;
ptr=tmp;
}
}
int main()
{
LNode *head=initList();
print(head);
cout << "After reverse: " << endl;
reverse(head);
print(head);
system("PAUSE");
return 0;
}