在VC环境中可调试:
#include
<
stdio.h
>
#include < malloc.h >
typedef int elemtype;
typedef struct linknode
... { elemtype data;
struct linknode *next;
} nodetype;
nodetype * createlist()
... { elemtype d;
nodetype *h=NULL,*s,*t;
int i=1;
printf("Create a single Link list. ");
while(1)
...{
printf("Pls Enter the elements of the list(0:Exit)");
scanf("%d",&d);
if(d==0)
break;
if(i==1)
...{
h=(nodetype*)malloc(sizeof(nodetype));
h->data=d;
h->next=NULL;
t=h;
}
else
...{
s=(nodetype*)malloc(sizeof(nodetype));
s->data=d;
s->next=NULL;
t->next=s;
t=s;
}
i++;
}
return h;
}
void disp(nodetype * h)
... {
nodetype *p=h;
printf("Print the single link list: ");
if(p==NULL)
printf("The list has no elements! ");
while(p!=NULL)
...{
printf("%d ",p->data);
p=p->next;
}
printf(" ");
}
nodetype * reverse(nodetype * h)
... {
nodetype *p,*q,*r;
p=h;
if(p->next=NULL)
...{
printf("The reversed link list is: ");
return NULL;
}
else
...{
p=h;
q=p->next;
while(q!=NULL)
...{
r=q->next;
q->next=p;
p=q;
q=r;
}
h->next=NULL;
h=p;
return h;
}
}
void main()
... {
nodetype *head;
head=createlist();
disp(head);
head=reverse(head);
disp(head);
}
#include < malloc.h >
typedef int elemtype;
typedef struct linknode
... { elemtype data;
struct linknode *next;
} nodetype;
nodetype * createlist()
... { elemtype d;
nodetype *h=NULL,*s,*t;
int i=1;
printf("Create a single Link list. ");
while(1)
...{
printf("Pls Enter the elements of the list(0:Exit)");
scanf("%d",&d);
if(d==0)
break;
if(i==1)
...{
h=(nodetype*)malloc(sizeof(nodetype));
h->data=d;
h->next=NULL;
t=h;
}
else
...{
s=(nodetype*)malloc(sizeof(nodetype));
s->data=d;
s->next=NULL;
t->next=s;
t=s;
}
i++;
}
return h;
}
void disp(nodetype * h)
... {
nodetype *p=h;
printf("Print the single link list: ");
if(p==NULL)
printf("The list has no elements! ");
while(p!=NULL)
...{
printf("%d ",p->data);
p=p->next;
}
printf(" ");
}
nodetype * reverse(nodetype * h)
... {
nodetype *p,*q,*r;
p=h;
if(p->next=NULL)
...{
printf("The reversed link list is: ");
return NULL;
}
else
...{
p=h;
q=p->next;
while(q!=NULL)
...{
r=q->next;
q->next=p;
p=q;
q=r;
}
h->next=NULL;
h=p;
return h;
}
}
void main()
... {
nodetype *head;
head=createlist();
disp(head);
head=reverse(head);
disp(head);
}
第二个,未编译:
#include
<
stdio.h
>
#include < stdlib.h >
#include < malloc.h >
typedef struct node ... {
int data;
struct node *next;
} Node, * LinkList;
void CreateList(LinkList * head)
... { LinkList p,q;
int x;
if((*head=(Node *)malloc(sizeof(Node)))==NULL) exit(1);
(*head)->next=NULL;
q=(*head);
printf("please input data: ");
scanf("%d",&x);
do...{
if((p=(Node *)malloc(sizeof(Node)))==NULL) exit(1);
p->data=x;
p->next=NULL;
q->next=p;
q=p;
p=NULL;
printf("please input number: ");
fflush(stdin);
scanf("%d",&x);
}while(x!=-1);
} void Reverse(LinkList head)
... {LinkList p,q,s=NULL,Head;
int count=0,i;
p=head->next;
while(p)
...{ count++;
p=p->next;
}
p=head->next;
if((Head=(Node *)malloc(sizeof(Node)))==NULL) exit(1);
Head->next=p;
head->next=NULL;
for(i=0;i<count;i++)
...{ p=Head->next;
Head->next=p->next;
p->next=NULL;
if(s==NULL)
s=p;
else
...{ p->next=s;
s=p;
}
}
head->next=s;
free(Head);
} void Print(LinkList head)
... { LinkList p;
p=head->next;
while(p)
...{ printf("%d ",p->data);
p=p->next;
}
} main()
... { LinkList head;
CreateList(&head);
Print(head);
Reverse(head);
printf(" ");
Print(head);
}
#include < stdlib.h >
#include < malloc.h >
typedef struct node ... {
int data;
struct node *next;
} Node, * LinkList;
void CreateList(LinkList * head)
... { LinkList p,q;
int x;
if((*head=(Node *)malloc(sizeof(Node)))==NULL) exit(1);
(*head)->next=NULL;
q=(*head);
printf("please input data: ");
scanf("%d",&x);
do...{
if((p=(Node *)malloc(sizeof(Node)))==NULL) exit(1);
p->data=x;
p->next=NULL;
q->next=p;
q=p;
p=NULL;
printf("please input number: ");
fflush(stdin);
scanf("%d",&x);
}while(x!=-1);
} void Reverse(LinkList head)
... {LinkList p,q,s=NULL,Head;
int count=0,i;
p=head->next;
while(p)
...{ count++;
p=p->next;
}
p=head->next;
if((Head=(Node *)malloc(sizeof(Node)))==NULL) exit(1);
Head->next=p;
head->next=NULL;
for(i=0;i<count;i++)
...{ p=Head->next;
Head->next=p->next;
p->next=NULL;
if(s==NULL)
s=p;
else
...{ p->next=s;
s=p;
}
}
head->next=s;
free(Head);
} void Print(LinkList head)
... { LinkList p;
p=head->next;
while(p)
...{ printf("%d ",p->data);
p=p->next;
}
} main()
... { LinkList head;
CreateList(&head);
Print(head);
Reverse(head);
printf(" ");
Print(head);
}