YTU 2430: C語言習題 鏈表建立,插入,刪除,輸出

來源:互聯網
上載者:User
2430: C語言習題 鏈表建立,插入,刪除,輸出 時間限制: 1 Sec   記憶體限制: 128 MB
提交: 576   解決: 280
題目描述

編寫一個函數creatlink,用來建立一個動態鏈表。(包含學號和成績)
編寫一個函數printlink,用來輸出一個鏈表。
編寫一個函數dellink,用來刪除動態鏈表中一個指定的結點(由實參指定某一學號,表示要刪除該學生結點)。
編寫一個函數insertlink,用來向動態鏈表插入一個結點。
編寫一個函數freelink,用來釋放一個動態鏈表。 輸入

輸入多個學生的學號和成績,建立動態鏈表,以0 0 結束
輸入學號,刪除鏈表中的對應結點
插入兩個鏈表結點 輸出

輸出的鏈表 範例輸入

1001 1001002 951005 901008 760 010051006 981009 99
範例輸出
1001 100.001002 95.001006 98.001008 76.001009 99.00
提示

主函數已給定如下,提交時不需要包含下述主函數



/* C代碼 */

int main()

{

    struct student *creatlink(void);

    struct student *dellink(struct student *,long);

    struct student *insertlink(struct student *,struct student *);

    void printlink(struct student *);

    void freelink(struct student *);

    struct student *head,stu;

    long del_num;

    head=creatlink();

    scanf("%ld",&del_num);

    head=dellink(head,del_num);

    scanf("%ld%f",&stu.num,&stu.score);

    head=insertlink(head,&stu);

    scanf("%ld%f",&stu.num,&stu.score);

    head=insertlink(head,&stu);

    printlink(head);

    freelink(head);

    return 0;

}



/* C++代碼 */



int main()

{

    student *creatlink(void);

    student *dellink(student *,long);

    student *insertlink(student *,student *);

    void printlink(student *);

    void freelink(student *);

    student *head,stu;

    long del_num;

    head=creatlink();

    cin>>del_num;

    head=dellink(head,del_num);

    cin>>stu.num>>stu.score;

    head=insertlink(head,&stu);

    cin>>stu.num>>stu.score;

    head=insertlink(head,&stu);

    cout<<setiosflags(ios::fixed);

    cout<<setprecision(2);

    printlink(head);

    freelink(head);

    return 0;

}

迷失在幽穀中的鳥兒,獨自飛翔在這偌大的天地間,卻不知自己該飛往何方……

#include<stdio.h>#include<iostream>#include<stdlib.h>#include<string.h>#include<iomanip>using namespace std;struct student{    long int num;    float score;    struct student *next;};struct student *creatlink(){    struct student *p1,*p2,*head=NULL;    p1=p2=(struct student*)malloc(sizeof(struct student));    while(~scanf("%ld%f",&p1->num,&p1->score)&&(p1->score||p1->num))    {        if(head==NULL)head=p1;        else p2->next=p1;        p2=p1;        p1=p1->next=(struct student*)malloc(sizeof(struct student));    }    p2->next=NULL;    return head;};struct student *dellink(struct student *a,long b){    struct student *head=a,*p=a,*p2=a;    while(p!=NULL)    {        if(p->num==b)            p2->next=p->next;        p2=p;        p=p->next;    }    return head;};struct student *insertlink(struct student *a,struct student *b){    struct student *head=a,*p=a,*p2=a,*k;    k=(struct student*)malloc(sizeof(struct student));    k->num=b->num,k->score=b->score;    int n=0;    while(p!=NULL)    {        if(p->num>b->num)        {            p2->next=k;            k->next=p;            n=1;        }        p2=p;        p=p->next;    }    if(n==0)p2->next=k,k->next=NULL;    return head;};void printlink(struct student *a){    struct student *p=a;    while(p!=NULL)    {        printf("%ld %.2f\n",p->num,p->score);        p=p->next;    }}void freelink(struct student *a){    while(a!=NULL)    {        delete(a);        a=a->next;    }}int main(){    student *creatlink(void);    student *dellink(student *,long);    student *insertlink(student *,student *);    void printlink(student *);    void freelink(student *);    student *head,stu;    long del_num;    head=creatlink();    cin>>del_num;    head=dellink(head,del_num);    cin>>stu.num>>stu.score;    head=insertlink(head,&stu);    cin>>stu.num>>stu.score;    head=insertlink(head,&stu);    cout<<setiosflags(ios::fixed);    cout<<setprecision(2);    printlink(head);    freelink(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.