a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C > 正文

c語言鏈表基本操作(帶有創建鏈表 刪除 打印 插入)

2020-01-26 15:39:50
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define LEN sizeof(struct Student)
struct Student
{
    long num;
    float score;
    struct Student*next;
};
int n;
int main()
{
    /*-----------------------------程序描述--------------------------------------------
        題目:寫出一個主函數,分別調用建立鏈表的函數create(),輸出鏈表的函數print(),
              刪除鏈表結點的函數del(),插入結點的函數insert(),一共5個函數。
        Author:KillerLegend
        Date:  2013.12.6
    ----------------------------------------------------------------------------------*/
//函數聲明
    struct Student* create();//創建動態鏈表的函數聲明,函數類型為student結構體類型,返回頭指針
    struct Student* del(struct  Student* ,long);//刪除指定位置結點的函數聲明,參數:鏈表頭結點+刪除結點位置+返回頭指針
    struct Student* insert(struct Student*,struct Student*);//插入一個Student類型數據的函數聲明
    void   print(struct Student*);//輸出鏈表中數據的函數聲明,參數為鏈表的頭指針
//定義變量
    struct Student *head,*stu;//定義動態鏈表的頭指針與新的結點
    long del_num;
//建立鏈表操作
    printf("Input records:/n");
    head = create();//建立鏈表并返回頭指針
    print(head);//輸出全部結點

//刪除結點操作
    printf("/nInput the deleted number:");
    scanf("%ld",&del_num);
    while(del_num!=0)//當輸入學號為0時結束循環
    {
        head = del(head,del_num);//刪除結點后返回鏈表的頭地址
        print(head);//輸出全部結點
        printf("Input the deleted number:");
        scanf("%ld",&del_num);
    }
//插入結點操作
    printf("/nInput the inserted number:");
    stu=(struct Student*)malloc(LEN);//每插入一個結點需要開辟一個新的結點
    scanf("%ld %f",&stu->num,&stu->score);
    while(stu->num!=0)//當輸入的學號為0時結束循環
    {
        head = insert(head,stu);//返回鏈表的頭地址
        print(head);
        printf("/nInput the inserted number:");
        stu = (struct Student*)malloc(LEN);
        scanf("%ld %f",&stu->num,&stu->score);
    }
    return 0;
}
//建立鏈表的函數
struct  Student* create()
{
    struct Student *head;
    struct Student *p1,*p2;
    n=0;
    p1=p2=(struct Student *)malloc(LEN);
    scanf("%ld %f",&p1->num,&p1->score);
    head=NULL;
    while(p1->num!=0)
    {
        n++;
        if(n==1)head=p1;
        else p2->next=p1;//第一次執行時,這一步是將頭指針指向自身,當n從2起,這一步用于使p2指向下一個元素
        p2=p1;//使p2和p1指向同一個存儲區
        p1=(struct Student*)malloc(LEN);//開辟動態存儲區,強制返回struct Student類型的指針
        scanf("%ld %f",&p1->num,&p1->score);
    }
    p2->next=NULL;
    return (head);
}

//刪除結點的函數
struct Student* del(struct  Student* head,long num)
{
    struct Student *p1,*p2;
    if(head==NULL)
    {
        printf("List null!/n");
        return (head);
    }
    p1=head;
    while(num!=p1->num && p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if(num==p1->num)
    {
        if(p1==head)
        {
            head=p1->next;
        }
        else
        {
            p2->next=p1->next;
        }
        printf("Delete:%ld/n",num);
        n=n-1;
    }
    else
    {
        printf("%ld not been found!",num);
    }
    return (head);
}

//插入結點的函數
struct   Student* insert(struct Student* head,struct Student * stud)
{
    struct Student *p0,*p1,*p2;
    p1=head;
    p0=stud;
    if(head==NULL)//原來的鏈表是空表
    {
        head=p0;p0->next=NULL;//空表時使插入的結點作為頭結點
    }
    else//如果不是空表,則遍歷尋找合適的插入位置
    {
        while((p0->num>p1->num)&&(p1->next!=NULL))//按學號順序插入,如果插入的學號數字比較大,則應該向后推移
        {
            p2=p1;
            p1=p1->next;//后移
        }
    }
    if(p0->num<=p1->num)//找到插入的位置,插入的位置是p1所指向的位置之前,也就是p2指向的位置
    {
        if(head==p1)head=p0;//如果插入的位置是頭位置之前,則使head指向p0
        else p2->next=p0;//如果不是頭位置之前,則使p2的next指針指向插入的數據地址即p0
        p0->next=p1;//使p0的next指針指向p1,完成了數據的加入
    }
    else//插入的學號位置在最后一個
    {
        p1->next=p0;
        p0->next=NULL;
    }
    n=n+1;//記錄數加一
    return(head);
}
//輸出鏈表的函數
void print(struct Student * head)
{
    struct Student * p;
    printf("Now,These %d records are:/n",n);
    p=head;
    if(head!=NULL)
    do
    {
        printf("%ld %5.1f/n",p->num,p->score);
        p=p->next;
    }while(p!=NULL);
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 日韩国产中文字幕 | 国产欧美在线视频 | 欧洲毛片基地 | 欧美在线视频网站 | 在线看免费黄色片 | 久久国产精品99久久久久久老狼 | 国产精品揄拍一区二区久久国内亚洲精 | 国产自在线| 在线国产欧美 | 欧美精品久久久 | 免费av在线播放 | 国产精品人成在线播放新网站 | 欧美亚洲国产一区二区三区 | 男人和女人上床网站 | 国产性色 | 在线观看精品自拍私拍 | 国产一区二区三区四区在线观看 | 成年人免费在线视频 | 久久精品色欧美aⅴ一区二区 | 亚洲欧洲精品一区二区 | 免费在线成人av | 亚洲88| 久久99精品久久久久久园产越南 | 国产免费拔擦拔擦8x高清 | 久久精品免费一区二区 | www.78av | 欧美视频在线观看免费 | 久久精品91久久久久久再现 | 国产精品人成在线播放新网站 | 在线色网 | 久久久久久久国产精品 | 日韩一区电影 | 国产精品美女久久久久aⅴ国产馆 | 国产不卡一区二区三区在线观看 | 久久这| 国产欧美精品区一区二区三区 | 欧美在线看片 | 日韩免费福利视频 | 欧美一级特黄aaaaaaa色戒 | 亚洲精品亚洲人成人网 | 在线看片日韩 |