一个无头结点的单链表逆输出: typedef node * point …… veout(point h) { point p; p=h->next; printf("\n"); if(p==0)
printf("\n L -> "); else { veout(p); printf(" %d -> ",p->data); }
return ok;}
[此贴子已经被作者于2005-10-11 12:02:19编辑过]
#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:\n"); 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:\n"); 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\t",p->data); p=p->next; } } main() { LinkList head; CreateList(&head); Print(head); Reverse(head); printf("\n"); Print(head); }