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

首頁 > 編程 > 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);
}

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

圖片精選

主站蜘蛛池模板: 万载县| 桓仁| 三门县| 枝江市| 柘荣县| 砀山县| 克拉玛依市| 资兴市| 孟津县| 三都| 信阳市| 宝鸡市| 弋阳县| 乡城县| 太保市| 阜新市| 五峰| 宣威市| 连城县| 慈溪市| 来凤县| 南和县| 元谋县| 梁山县| 曲阳县| 教育| 宁强县| 安远县| 噶尔县| 南安市| 涪陵区| 陵水| 万载县| 太湖县| 绥芬河市| 芜湖县| 西吉县| 宁夏| 藁城市| 韶关市| 林芝县|