在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/C/ 鏈表中為什么少輸出一個(gè)數(shù)?

鏈表中為什么少輸出一個(gè)數(shù)?

#include<stdio.h>
#include<stdlib.h>  
 typedef struct node
 {     int data; 
    struct node *next;
 }node,*list;  
list create(int n) 
 {  
    int i;  
    node *p,*q;node *head;  
    p=q= (node*) malloc (sizeof(node));      
    scanf("%d",&p->data); 
    for(i=0;i<n;i++) 
         {        
           if(i==0)    
             {     
                  head=p;     
                    }         
        else    
           {       
               q->next=p;    
            q=p;    
           p=(node*)malloc(sizeof(node));                  
           scanf("%d",&p->data);        
          }    
       }
           q->next=NULL;    
        return head; 
    }   
void print(node *head) 
        {  
            node *p;  
            p=head;  
            while(p!=NULL)    
          {      
             printf("%d ",p->data);          
            p=p->next;     
           }
           } 
            int main() 
             {  
                 int n;      
                scanf("%d",&n);  
                 node *head,*p;     
                head=create(n);      
                print(head); 
             }
回答
編輯回答
不討喜
#include <stdio.h>
#include <stdlib.h>

typedef struct node {
  int data;
  struct node *next;
} node, *list;

list create(int n) {
  int i;
  node *p, *q;
  node *head;
  p = q = (node *)malloc(sizeof(node));
  scanf("%d", &p->data);
  for (i = 0; i < n; i++) {
    if (i == 0) {
      head = p;
    } else {
      p = (node *)malloc(sizeof(node));
      q->next = p;
      q = p;
      scanf("%d", &p->data);
    }
  }
  q->next = NULL;
  return head;
}

void print(node *head) {
  node *p;
  p = head;
  while (p != NULL) {
    printf("%d ", p->data);
    p = p->next;
  }
}

int main() {
  int n;
  scanf("%d", &n);
  node *head, *p;
  head = create(n);
  print(head);
}
2018年9月13日 14:25