基本单向链表的操作

 #include<stdio.h> 

 #include<stdlib.h>
 #define L sizeof(struct node) 
 typedef struct node{ 
  int info;
  struct node *next;
 }*PN;
 int n=0;//指代节点个数
 PN Create_List(void)
 {
  PN head,ptail,pnew;
  int val;
  head=NULL;
  printf("以输入零作为结束:\n");
  while (1)
  {
 
  if(scanf("%d",&val)==EOF||val<0)
  {printf("input error\n");
  continue;}
  if (val==0)
  {
  break;
  }
  pnew=(PN)malloc(L);
  pnew->info=val;
  pnew->next=NULL;
  if (head==NULL)
  {
  head=pnew;
  }else
  {
  ptail->next=pnew;
  }
  ptail=pnew;
  n++;
  }
  return head;
 }
 PN Reverse_Copy(PN head)
 {
  PN phead,q,r;
  phead=NULL;
  for (q=head;q!=NULL;q=q->next)
  {
  r=(PN)malloc(L);
  r->info=q->info;
  r->next=phead;
  phead=r;
  }
  return phead;
 }
 void Travel_Print(PN head)
 {
  PN tail=head;
  if (head==NULL)
  {
  printf("为空链表!") ;
  return ;
  }
  while (tail!=NULL)
  {
  printf("%d->",tail->info);
  tail=tail->next;
  }
  printf("NULL");
  putchar('\n');
 
 }
 void Free_List(PN head)
 {
  PN tail=head;
  while (tail!=NULL)
  {
  tail=tail->next;
  free(head);
  head=tail;
  }
 }
 int main()
 {
  PN head,head1;
  head=Create_List();
  Travel_Print(head);
  head1=Reverse_Copy(head);
  Travel_Print(head1);
 
  printf("节点个数为:%d\n",n);
  //释放内存
  Free_List(head);
  Free_List(head1);
 
 
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值