单链表的创建及输出(对刘霞同学的程序调试)

来源:互联网 发布:风驰网络加速器 编辑:程序博客网 时间:2024/05/02 18:00

#include"stdio.h"
#include"malloc.h"
typedef struct student
{
  int data;
  struct student *next;

}list;
list *creat(int m)
{
  list *head,*p,*q;
  int i;
  head=q=(list *)malloc(sizeof(struct student));
  printf("input q node:");
  scanf("%d",&q->data);
  head=q;
  for(i=m;i>1;i--)
  {
      p=(list *)malloc(sizeof(struct student));
      printf("input p node:");
      scanf("%d",&p->data);
      if(i!=2)
      {
      q->next=p;
      q=q->next;
      }
      else
      q->next=p;
      p->next=NULL;

  }
  free(p);
  free(q);

  return (head);

}
void print(list *head)
{
  list *p;
  p=head;
  if(p==NULL)
  printf("Is not here");
  while(p!=NULL)
  {
    printf("%d ",p->data) ;
    p=p->next;
  }
  free(p);
}
void main()
{  list *head;
   int m;
   printf("The numble of node :") ;
   scanf("%d",&m);
   head=creat(m);
   print(head);
 }
 

程序运行结果:

The numble of node:3

input q node:1

input p node:2

input p node:3

1 2 3