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

鍍金池/ 問(wèn)答/C  HTML/ C語(yǔ)言鏈表,哪里錯(cuò)了?

C語(yǔ)言鏈表,哪里錯(cuò)了?

哪里錯(cuò)了

#include<stdio.h>
#include<stdlib.h>
struct student {
    int num;
    char name[20];
    struct student *next;
};
struct student *crea(int n)
{
    int i;
    struct student *head, *p_end, *p_new;//p_new指向鏈表新的結(jié)點(diǎn),end指向最后一個(gè)結(jié)點(diǎn),head是頭節(jié)點(diǎn)
    head = NULL;
    for (i = 0; i < n; i++)
    {
        p_new = (struct student*)malloc(sizeof(struct student));//循環(huán)分配內(nèi)存空間
        if (p_new == NULL)
        {
            printf("第%d個(gè)學(xué)生分配內(nèi)存空間失敗!", i + 1);
            break;
        }
    }
    printf("輸入第%d個(gè)學(xué)生的學(xué)號(hào):",i+1);
    scanf_s("%d", &p_new->num);
    printf("輸入第%d個(gè)學(xué)生的姓名:", i + 1);
    scanf_s("%s", p_new->name,sizeof(p_new->name));
    return head;
}
int main()
{
    struct student*head = crea(0);
    system("pause");
    return 0;
}

圖片描述

回答
編輯回答
使勁操

C4101 警告指示變量未使用。

以下是參考代碼,望仔細(xì)對(duì)比,親手練習(xí)才有益

// 使用 c11 標(biāo)準(zhǔn)編譯。
#include <stdio.h>
#include <stdlib.h>

struct student {
  int num;
  char name[20];
  struct student *next;
};

struct student *crea(int n) {
  struct student *head = NULL, *end = NULL;
  for (int i = 0; i < n; i++) {
    struct student *p_new =
        (struct student *)malloc(sizeof(struct student)); //循環(huán)分配內(nèi)存空間
    if (p_new == NULL) {
      printf("第%d個(gè)學(xué)生分配內(nèi)存空間失敗!", i + 1);
      break;
    }
    printf("輸入第%d個(gè)學(xué)生的學(xué)號(hào):", i + 1);
    scanf("%d", &p_new->num);
    printf("輸入第%d個(gè)學(xué)生的姓名:", i + 1);
    scanf("%s", p_new->name);

    p_new->next = NULL;
    if (!head)
      head = p_new;
    if (end)
      end->next = p_new;
    else
      end = p_new;
  }
  return head;
}

void print(const struct student *link) {
    const struct student *curr = link;
    int i = 0;
    while (curr) {
        printf("#%d: %s, %d\n", ++i, curr->name, curr->num);
        curr = curr->next;
    }
}

int main() {
  struct student *link = crea(2);
  print(link);
  system("pause");
  return 0;
}
2018年2月11日 15:00