|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- //逆向单链表
- #include <stdio.h>
- #include <stdlib.h>
- #define N 5
- typedef struct node {
- int data;
- struct node *next;
- } NODE;
- NODE *fun(NODE *h)
- {
- int i;
- NODE *p,*q,*r,*head;
-
- head = q = NULL;
- p=r=h;
- for(i = 0; i < N-1 ; i++)
- {
- p=r=h;
- do
- {
- q = p;
- p = p->next;
-
- if(p->next != NULL) r = q;
-
- }while(p->next != NULL);
- q->next = r;
- r->next = NULL;
- while(head = NULL)
- {
- head = q;
- }
- }
- return head;
- }
- NODE *creatlist(int a[])
- { NODE *h,*p,*q;
- int i;
- h=NULL;
- for(i=0; i<N; i++)
- { q=(NODE *)malloc(sizeof(NODE));
- q->data=a[i];
- q->next = NULL;
- if (h == NULL) h = p = q;
- else { p->next = q; p = q; }
- }
- return h;
- }
- void outlist(NODE *h)
- { NODE *p;
- p=h;
- if (p==NULL) printf("The list is NULL!\n");
- else
- { printf("\nHead ");
- do
- { printf("->%d", p->data); p=p->next; }
- while(p!=NULL);
- printf("->End\n");
- }
- }
- void main()
- { NODE *head;
- head = NULL;
- int a[N]={2,4,6,8,10};
- head=creatlist(a);
- printf("\nThe original list:\n");
- outlist(head);
- head=fun(head);
- printf("\nThe list after inverting :\n");
- outlist(head);
- }
复制代码 请问应该怎样做才可以实现逆向单链表
|
|