链表

头文件

#ifndef _NODE_H
#define _NODE_H

typedef struct _node{
    int value;
    struct _node *next;
} Node; 

#endif

主程序

#include"node.h"
#include
#include

//typedef struct _node{
//  int value;
//  struct _node *next;
//} Node; 

int main(int argc, char const *argv[])
{
    Node *head=NULL;
    int number;
    do{
        scanf("%d",&number);
        if(number!=-1){
            //add to linked-list
            Node *p=(Node*)malloc(sizeof(Node));
            p->value=number;
            p->next=NULL;
            //find the last
            Node *last=head;
            if(last)//last不为空
            {
                while(last->next)
                {
                    last=last->next;
                }
                    //attach
                 last->next=p;
                
             } 
             else
             {
                head=p; 
             }
             
        }
        
        
    }while(number!=-1);
    
    //printf("%d",head->value);
    return 0;
}

你可能感兴趣的:(链表)