您的位置:首页 > 其它

逆序建立链表

2016-10-02 15:09 211 查看


#include<stdio.h>

#include<malloc.h>

struct node

{

 int data;

 struct node *next;

};

struct node *creat(int n)

{

 struct node *head,*tail,*p;

 head=(struct node *)malloc(sizeof(struct node));

 head->next=NULL;

 

 int i;

 for(i=0;i<n;i++)

 {

  p=(struct node *)malloc(sizeof(struct node));

  scanf("%d",&p->data);

  p->next=head->next;

  head->next=p;

 }

 return head;

}

void print(struct node *head)

{

 struct node *p;

 p=head->next;

 while(p!=NULL)

 {

  if(p==NULL)

  printf("%d",p->data);

  else printf("%d ",p->data);

  p=p->next;

 }

}

int main()

{

 int n;

 struct node *head;

 scanf("%d",&n);

 head=creat(n);

 print(head);

 printf("\n");

 return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: