国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

C語(yǔ)言實(shí)現(xiàn)動(dòng)態(tài)鏈表的創(chuàng)建、刪除、插入、輸出(CentOS6.5上調(diào)試成功)

2019-11-14 08:57:55
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

該代碼是基于網(wǎng)上其他代碼修改而來(lái),原來(lái)的代碼有錯(cuò)誤,以下是我修改后并在CentOS上調(diào)試成功的代碼:

#include  <stdio.h>#include <stdlib.h>#include <unistd.h>#define LEN sizeof(struct student)struct student{    int num;    char name[10];    float score;    struct student *next;};static unsigned inode = 1;void fill(char c[], int n, char ch);//填充數(shù)組void del(struct student *ph, int n);//刪除指定序號(hào)的節(jié)點(diǎn)void insert(struct student *ph, int n);//在指定序號(hào)后插入節(jié)點(diǎn)void output(struct student *ph);//輸出所有節(jié)點(diǎn)struct student *input(void);//輸入新節(jié)點(diǎn)void clearline(int n);//分割線(xiàn)int main(){        struct student *head,*ps,*pa,*pb;    int u;    char flag = 'y';    head = ps = pa = (struct student *)malloc(LEN);    fill(pa->name,10,'/0');    //輸入數(shù)據(jù),建立鏈表    while(flag != 'n'){                PRintf("please input student informations: /n");        printf("Num/tName/tScore/n");        scanf("%d %s %f", &pa->num, pa->name, &pa->score);        flag = getchar();        printf("Press any key input data continue, otherwise press n quit:");        flag = getchar();        if(flag != 'n'){            clearline(60);            pa->next = (struct student *)malloc(LEN);            pa = pa->next;            fill(pa->name,10,'/0');            inode++;        }    }            pa->next = NULL;    output(ps);          //刪除節(jié)點(diǎn)    printf("which student's num do you want to del?/n");    printf("please input the num that you want to del:");    scanf("%d", &u);    if(u > inode){        printf("input error!/n");        exit(1);    }    if(u == 1){        pb = head;        ps = head->next;        free(pb);        head = pb = pa = ps;    }else        del(ps,u);    output(ps);    //插入節(jié)點(diǎn)    printf("which student's num do you want to insert after:");    scanf("%d",&u);    if(u>inode){        printf("input error!/n");        exit(1);    }    if(u == 0){        pb = head;        head = ps = pa = input();        pa->next = pb;        inode++;    }else{        insert(ps, u);    }    output(ps);    return 0;}void fill(char c[], int n, char ch){    int i;    for(i=0;i<=n;i++){        *(c+i) = ch;    }}void del(struct student *ph, int n){    int i;    struct student *pb,*pf;    pb = pf = ph;    for(i=1;i<=n-2;i++){        ph = ph->next;    }    if(n == inode){        pb = ph->next;        ph->next = NULL;        free(pb);    }else{        pb = ph->next;        ph->next = (ph->next)->next;        free(pb);    }    inode--;}void insert(struct student *ph, int n){    int i;    struct student *pa,*pb,*p_new,*ps;    ps = pa = ph;    p_new = (struct student *)malloc(LEN);    fill(p_new->name,10,'/0');    printf("please input the new student informations:/n");    printf("Num/tName/tScore:/n");    scanf("%d %s %f",&p_new->num,p_new->name,&p_new->score);    for(i=1;i<n;i++){        pa = pa->next;    }    pb = pa->next;    pa->next = p_new;    p_new->next = pb;    inode++;    }void output(struct student *ph){    int i;    struct student *pa;    printf("/n*****students list*****/n");    printf("Order/tNum/tName/tScore/n");        pa = ph;    for(i=1;pa!=NULL;i++){        printf("%d/t%d/t%s/t%.2f/n", i,pa->num,pa->name,pa->score);        pa=pa->next;    }    printf("totol %d /n",inode);   }struct student *input(void){    struct student *pa;    pa = (struct student *)malloc(LEN);    printf("please the new student information/n");    printf("Num/tName/tScore/n");    scanf("%d %s %f", &pa->num, pa->name, &pa->score);    return pa;}void clearline(int n){    int space;    printf("/r");    for(space=0;space<n;space++){        printf("-");    }    printf("/r");}

運(yùn)行過(guò)程及結(jié)果:

please input student informations: Num     Name    Score1       aa      10Press any key input data continue, otherwise press n quit:yplease input student informations: -------------------------Num     Name    Score2       bb      20Press any key input data continue, otherwise press n quit:yplease input student informations: -------------------------Num     Name    Score3       cc      30Press any key input data continue, otherwise press n quit:n*****students list*****Order   Num     Name    Score1       1       aa      10.002       2       bb      20.003       3       cc      30.00totol 3 which student's num do you want to del?please input the num that you want to del:2*****students list*****Order   Num     Name    Score1       1       aa      10.002       3       cc      30.00totol 2 which student's num do you want to insert after:1please input the new student informations:Num     Name    Score:2       bb      20*****students list*****Order   Num     Name    Score1       1       aa      10.002       2       bb      20.003       3       cc      30.00totol 3 

結(jié)束


上一篇:Filter介紹

下一篇:Linux系統(tǒng)的安裝

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 潍坊市| 麻江县| 兴和县| 綦江县| 沅江市| 图木舒克市| 东阿县| 蚌埠市| 安福县| 泌阳县| 台北县| 金华市| 九江县| 东海县| 达日县| 剑阁县| 宾川县| 邳州市| 全州县| 板桥市| 湖州市| 孟村| 泌阳县| 蒲江县| 南京市| 永春县| 云安县| 禄丰县| 信阳市| 黄冈市| 乐业县| 丹东市| 镇坪县| 永川市| 东乡县| 宁蒗| 繁峙县| 厦门市| 大新县| 铅山县| 汾阳市|