常见C语言面试题之九:链表逆序

 
  1. #include "stdafx.h"
  2. #include "stdio.h"
  3. #include "stdlib.h"
  4. typedef struct List
  5. {
  6.   int data;
  7.   struct List *next;
  8. }List;
  9. List* list_Create(void)
  10. {
  11.   List *head, *tail, *p;
  12.   int e;
  13.   head = (List*)malloc(sizeof(List));
  14.   tail = head;
  15.   printf("/nList Create, input Num(end of 0):");
  16.   scanf("%d",&e);
  17.   while(e)
  18.   {
  19.     p=(List*)malloc(sizeof(List));
  20.     p->data=e;
  21.     tail->next=p;
  22.     tail=p;
  23.     scanf("%d",&e);
  24.   }
  25.   tail->next = NULL;
  26.   return head;
  27. }
  28. List *list_reverse(List *head)
  29. {
  30.   List *p,*q,*r;
  31.   p=head;
  32.   q=p->next;
  33.   while(q!=NULL)
  34.   {
  35.     r = q->next;
  36.     q->next=p;
  37.     p=q;
  38.     q=r;
  39.   }
  40.   head->next = NULL;
  41.   head=p;
  42.   return head;
  43. }
  44. int _tmain(int argc, _TCHAR* argv[])
  45. {
  46.     List *head, *p;
  47.   int d;
  48.   head=list_Create();
  49.   printf("/n");
  50.   for(p=head->next;p!=NULL;p=p->next)
  51.     printf("--%d--",p->data);
  52.   head = list_reverse(head);
  53.   printf("/n");
  54.   for(p=head;p->next!=NULL;p=p->next)
  55.     printf("--%d--",p->data);
  56.   return 0;
  57. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值