建立动态链表

来源:互联网 发布:海淘网站推荐 知乎 编辑:程序博客网 时间:2024/04/29 09:49

3  建立动态链表

  学自徐洪波c语言教程

 

#include<stdio.h>

#include<malloc.h>

 

 

// 链表结构体

struct  node

{

       int value;

       struct node * next;

 

}

 

void creat()

{

     struct node * root;

     struct node * tail;

    struct node *p;

 

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

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

  

    if(NULL!=p)

     {

        root =p;

        tail   =p;

     }

 

   while(p->value!=0)

   {

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

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

     if(p!=NULL)

     {

           tail->next=p;

           tail=p;

     }

   }

   

}

 

void main()
{
 
 struct std *q= creat_link();
 

 while(q->num != 0)
 {
  printf("节点%d /n",q->num);
  q=q->next;
 }
 /*
 while(q != NULL)
 {
  printf("节点%d /n",q->num);
  q=q->next;
 }
 */

}

原创粉丝点击