データ構造(C)

814 ワード

#include
//        
struct node{
    int data;
    struct node *next;
};
int main(){
    int a,i,n;
    struct node *head,*current,*p;
//head,current,p          
    scanf("%d",&n);
    head = NULL;
    for(i = 0;i < n;i++){
       scanf("%d",&a);
       p = (struct node *)malloc(sizeof(struct node)); //p            
       p -> data = a;
       p -> next = NULL;
       if(head == NULL){
          head = p;
       }else{
          current -> next = p; //current         ,current     ,    , 
       }
       current = p;
    }

    struct node *t;
    t = head; //              ,      &  ,           (     ) 
    do{
       printf("%d ",t->data);
       t = t -> next; 
    }while(t != NULL);
    
    return 0;
}