實驗1 輸入若干個學生的資訊(學號、姓名、成績),當輸入學號為0時結束

來源:互聯網
上載者:User

實驗1  
輸入若干個學生的資訊(學號、姓名、成績),當輸入學號為0時結束,用單向鏈表組織這些學生資訊後,再按順序輸出。
輸入:          輸出:
1 zhang 78          1 zhang 78
2 wang 80           2 wang 80
3 li 75              3 li 75
4 zhao 85           4 zhao 85

0

#include <stdio.h>#include <malloc.h>struct node{    char name[10];    int num,score;    struct node *next;};struct node *create(){    printf("請輸入學生的資訊,以輸入學號為0結束\n");    printf("\t學號\t姓名\t分數\n");    struct node *Head,*p,*tail;    int date;    Head = (struct node *)malloc(sizeof(struct node));    Head->next = NULL;    tail = Head;    p = (struct node *)malloc(sizeof(struct node));    p->next = NULL;    while(scanf("%d",&date) != EOF)
 /*控制當學號為零就結束停止時,
                                      不能直接通過控制結點,因為                                      那樣會開闢一個結點,所以當輸入為零                                      時,必須輸入姓名分數才行,所以,要設定一個                                      變數,來控制分數的輸入,當學號為零時,可                                      直接停止*/                           {        if(date == 0) break;        p->num = date;        scanf("%s %d",p->name,&p->score);        tail->next = p;        tail = p;        p = (struct node *)malloc(sizeof(struct node));        p->next = NULL;    }    return Head;}void print(struct node *Head){    printf("\t\t學生資訊輸出\n");    printf("\t學號\t姓名\t分數\n");    struct node *p;    p = Head->next;    while(p != NULL)    {        printf("\t%d\t%s\t%d\n",p->num,p->name,p->score);        p = p->next;    }}int main(){    struct node *head;    head = create();    print(head);    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.