c語言鏈表操作


鏈表的創(chuàng)建

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define LEN sizeof(struct Student)

//鏈表
//結(jié)構(gòu)體
struct Student{
    long num;
    float score;
    struct Student *next;
};
//全局變量,記錄student的個(gè)數(shù)
int n;

//創(chuàng)建鏈表
struct Student* create(){
    struct Student *head;
    struct Student *p1,*p2;
    n = 0;
    p1=p2=(struct Student *)malloc(LEN);
    scanf("%d %f",&p1->num,&p1->score);
    head = NULL;
    //輸入0,退出,尾插法
    while(p1->num != 0){
        ++n;
        if(n==1) head = p1;
        else p2->next = p1;
        //p2永遠(yuǎn)指向鏈表最后一個(gè)元素
        p2 = p1;
        //p1不斷生成新的結(jié)點(diǎn)
        p1 = (struct Student*)malloc(LEN);
        scanf("%ld %f",&p1->num,&p1->score);
    }
    p2->next = NULL;
    return head;
}
//打印鏈表元素
void print(struct Student* head){
    struct Student *p;
    printf("The numbers is %d\n",n);
    p = head;
    if(head != NULL){
        do{
            printf("num:%d,score:%5.1f\n",p->num,p->score);
            p = p->next;
        }while(p != NULL);
    }
}
int main(int argc, char* argv[])
{

    struct Student *pt;
    pt = create();
    print(pt);
    system("pause");
    return 0;
}

鏈表的建立.png

鏈表原地翻轉(zhuǎn)

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define LEN sizeof(struct Student)

//鏈表
struct Student{
    long num;
    float score;
    struct Student *next;
};
int n;

//創(chuàng)建鏈表
struct Student* create(){
    struct Student *head;
    struct Student *p1,*p2;
    n = 0;
    p1=p2=(struct Student *)malloc(LEN);
    scanf("%d %f",&p1->num,&p1->score);
    head = NULL;
    while(p1->num != 0){
        ++n;
        if(n==1) head = p1;
        else p2->next = p1;
        p2 = p1;
        //p1不斷生成新的結(jié)點(diǎn)
        p1 = (struct Student*)malloc(LEN);
        scanf("%ld %f",&p1->num,&p1->score);
    }
    p2->next = NULL;
    return head;
}
//逆序
struct Student* reverseOrder(struct Student* head){
    struct Student *p,*temp,*r,*q;
    p = head;
    q = p;
    r = p->next;
    while (r != NULL){
        //temp指針指向r
        temp = r;
        //r指針指向下一個(gè)
        r = r->next;
        temp->next = p;
        head = temp;
        p = head;
    }
    q->next = NULL;
    return head;
}
//打印鏈表元素
void print(struct Student* head){
    struct Student *p;
    printf("The numbers is %d\n",n);
    p = head;
    if(head != NULL){
        do{
            printf("num:%d,score:%5.1f\n",p->num,p->score);
            p = p->next;
        }while(p != NULL);
    }
}
int main(int argc, char* argv[])
{

    struct Student *pt;
    pt = create();
    print(pt);
    pt = reverseOrder(pt);
    print(pt);

    system("pause");
    return 0;
}

Snipaste_2021-03-12_22-45-42.png

鏈表結(jié)點(diǎn)刪除

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define LEN sizeof(struct Student)

//鏈表
struct Student{
    long num;
    float score;
    struct Student *next;
};
int n;

//創(chuàng)建鏈表
struct Student* create(){
    struct Student *head;
    struct Student *p1,*p2;
    n = 0;
    p1=p2=(struct Student *)malloc(LEN);
    scanf("%d %f",&p1->num,&p1->score);
    head = NULL;
    while(p1->num != 0){
        ++n;
        if(n==1) head = p1;
        else p2->next = p1;
        p2 = p1;
        //p1不斷生成新的結(jié)點(diǎn)
        p1 = (struct Student*)malloc(LEN);
        scanf("%ld %f",&p1->num,&p1->score);
    }
    p2->next = NULL;
    return head;
}

//刪除鏈表結(jié)點(diǎn)
struct Student* del(struct Student *head,long num){
    struct Student *p1,*p2;
    int count = 0;
    if(head == NULL){
        printf("empty");
        return head;
    }
    p1 = head;
    while(p1 != NULL){
        struct Student *temp;
        if(num == p1->num){
            if(p1 == head){
                temp = p1;
                head = p1->next;
                p1 = p1->next;

            }else{
                temp = p1;
                p2->next = p1->next;

            }
            free(temp);
            printf("delete:%d\n",num);
            n--;
            count++;
        }else{
            p2 = p1;
            p1 = p1->next;
        }

    }
    if(count == 0){
        printf("not found!\n");
    }
    return head;
}

//打印鏈表元素
void print(struct Student* head){
    struct Student *p;
    printf("The numbers is %d\n",n);
    p = head;
    if(head != NULL){
        do{
            printf("num:%d,score:%5.1f\n",p->num,p->score);
            p = p->next;
        }while(p != NULL);
    }
}
int main(int argc, char* argv[])
{

    struct Student *pt;
    pt = create();
    print(pt);
    pt = del(pt,1001);
    print(pt);
    
    system("pause");
    return 0;
}

結(jié)點(diǎn)刪除.png

頭插法添加結(jié)點(diǎn)

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define LEN sizeof(struct Student)

//鏈表
struct Student{
    long num;
    float score;
    struct Student *next;
};
int n;

//創(chuàng)建鏈表
struct Student* create(){
    struct Student *head;
    struct Student *p1,*p2;
    n = 0;
    p1=p2=(struct Student *)malloc(LEN);
    scanf("%d %f",&p1->num,&p1->score);
    head = NULL;
    while(p1->num != 0){
        ++n;
        if(n==1) head = p1;
        else p2->next = p1;
        p2 = p1;
        //p1不斷生成新的結(jié)點(diǎn)
        p1 = (struct Student*)malloc(LEN);
        scanf("%ld %f",&p1->num,&p1->score);
    }
    p2->next = NULL;
    return head;
}

//頭插法
struct Student* ins(struct Student *head,struct Student *stud){
    struct Student *p0,*p1,*p2;
    p0 = head;
    p1 = stud;
    if(head == NULL){
        head = p1;
        p1->next = NULL;
    }else{
        head = p1;
        p1->next = p0;
    }
    n++;
    return head;

}

//打印鏈表元素
void print(struct Student* head){
    struct Student *p;
    printf("The numbers is %d\n",n);
    p = head;
    if(head != NULL){
        do{
            printf("num:%d,score:%5.1f\n",p->num,p->score);
            p = p->next;
        }while(p != NULL);
    }
}
int main(int argc, char* argv[])
{

    struct Student *pt;
    pt = create();
    print(pt);

    struct Student s1;
    s1.score = 90;
    s1.num = 1004;
    pt = ins(pt,&s1);
    print(pt);
    
    system("pause");
    return 0;
}

頭插法.png

修改鏈表某個(gè)結(jié)點(diǎn)的值

  • 相當(dāng)于查找元素,修改其關(guān)聯(lián)元素的值
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define LEN sizeof(struct Student)

//鏈表
struct Student{
    long num;
    float score;
    struct Student *next;
};
int n;

//創(chuàng)建鏈表
struct Student* create(){
    struct Student *head;
    struct Student *p1,*p2;
    n = 0;
    p1=p2=(struct Student *)malloc(LEN);
    scanf("%d %f",&p1->num,&p1->score);
    head = NULL;
    while(p1->num != 0){
        ++n;
        if(n==1) head = p1;
        else p2->next = p1;
        p2 = p1;
        //p1不斷生成新的結(jié)點(diǎn)
        p1 = (struct Student*)malloc(LEN);
        scanf("%ld %f",&p1->num,&p1->score);
    }
    p2->next = NULL;
    return head;
}

struct Student *change(struct Student *head,long num,float score){
    struct Student *p;
    p = head;
    while (p != NULL){
        if(num == p->num){
            p->score = score;
        }
        p = p->next;
    }
    return head;
}

//打印鏈表元素
void print(struct Student* head){
    struct Student *p;
    printf("The numbers is %d\n",n);
    p = head;
    if(head != NULL){
        do{
            printf("num:%d,score:%5.1f\n",p->num,p->score);
            p = p->next;
        }while(p != NULL);
    }
}
int main(int argc, char* argv[])
{

    struct Student *pt;
    pt = create();
    print(pt);

    pt = change(pt,1002,99);
    print(pt);

    system("pause");
    return 0;
}

結(jié)點(diǎn)刪除.png

鏈表元素排序

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define LEN sizeof(struct Student)

//鏈表
struct Student{
    long num;
    float score;
    struct Student *next;
};
int n;

//創(chuàng)建鏈表
struct Student* create(){
    struct Student *head;
    struct Student *p1,*p2;
    n = 0;
    p1=p2=(struct Student *)malloc(LEN);
    scanf("%d %f",&p1->num,&p1->score);
    head = NULL;
    while(p1->num != 0){
        ++n;
        if(n==1) head = p1;
        else p2->next = p1;
        p2 = p1;
        //p1不斷生成新的結(jié)點(diǎn)
        p1 = (struct Student*)malloc(LEN);
        scanf("%ld %f",&p1->num,&p1->score);
    }
    p2->next = NULL;
    return head;
}

//排序(按成績(jī)),選擇排序
struct Student* sortStudent(struct Student *head){
    struct Student *p,*min,*ptr;
    long tempNum;
    float tempScore;
    p = head;
    while (p->next != NULL){
        min = p;
        ptr = p->next;
        while (ptr != NULL){
            if(min->score > ptr->score){
                min = ptr;
            }
            //交換ptr指針和p的值
            tempNum = min->num;
            min->num = p->num;
            p->num = tempNum;
            
            tempScore = min->score;
            min->score = p->score;
            p->score = tempScore;
            
            ptr = ptr->next;
        }
        p = p->next;
    }
    return head;

}
//打印鏈表元素
void print(struct Student* head){
    struct Student *p;
    printf("The numbers is %d\n",n);
    p = head;
    if(head != NULL){
        do{
            printf("num:%d,score:%5.1f\n",p->num,p->score);
            p = p->next;
        }while(p != NULL);
    }
}
int main(int argc, char* argv[])
{

    struct Student *pt;
    pt = create();
    print(pt);

    pt = sortStudent(pt);
    print(pt);
    
    system("pause");
    return 0;
}

鏈表排序.png

兩個(gè)有序鏈表的合并

  • 先將兩個(gè)鏈表分別排序再合并
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define LEN sizeof(struct Student)

//鏈表
struct Student{
    long num;
    float score;

    struct Student *next;
};
int n;

//創(chuàng)建鏈表
struct Student* create(){
    struct Student *head;
    struct Student *p1,*p2;
    n = 0;
    p1=p2=(struct Student *)malloc(LEN);
    scanf("%d %f",&p1->num,&p1->score);
    head = NULL;
    while(p1->num != 0){
        ++n;
        if(n==1) head = p1;
        else p2->next = p1;
        p2 = p1;
        //p1不斷生成新的結(jié)點(diǎn)
        p1 = (struct Student*)malloc(LEN);
        scanf("%ld %f",&p1->num,&p1->score);
    }
    p2->next = NULL;
    return head;
}

//合并兩個(gè)鏈表
struct Student* merge(struct Student *head1,struct Student *head2){
    struct Student *p1,*p2,*pre,*temp;
    p1 = head1;
    p2 = head2;
    while (p1 != NULL && p2 != NULL){
        if(p1->score <= p2->score){
            pre = p1;
            p1 = p1->next;
        }else{
            if(p1 == head1){
                p2 = p2->next;
                head1 = head2;
                head2->next = p1;
            }else{
                temp = p2;
                p2 = p2->next;
                pre->next = temp;
                temp->next = p1;
                pre = temp;

            }
        }
    }
    if(p1 == NULL){
        pre->next = p2;
    }

    return head1;
}

//排序(按成績(jī)),選擇排序
struct Student* sortStudent(struct Student *head){
    struct Student *p,*min,*ptr;
    long tempNum;
    float tempScore;
    p = head;
    while (p->next != NULL){
        min = p;
        ptr = p->next;
        while (ptr != NULL){
            if(min->score > ptr->score){
                min = ptr;
            }
            tempNum = min->num;
            min->num = p->num;
            p->num = tempNum;
            tempScore = min->score;
            min->score = p->score;
            p->score = tempScore;
            ptr = ptr->next;
        }
        p = p->next;
    }
    return head;

}
//打印鏈表元素
void print(struct Student* head){
    struct Student *p;
    printf("The numbers is %d\n",n);
    p = head;
    if(head != NULL){
        do{
            printf("num:%d,score:%5.1f\n",p->num,p->score);
            p = p->next;
        }while(p != NULL);
    }
}
int main(int argc, char* argv[])
{

    struct Student *pt;
    pt = create();
    pt = sortStudent(pt);
    print(pt);

    struct Student *pt1;
    pt1 = create();
    pt1 = sortStudent(pt1);
    print(pt1);

    pt = merge(pt,pt1);
    print(pt);
    
    system("pause");
    return 0;
}

鏈表合并.png

按序插入

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define LEN sizeof(struct Student)

//鏈表
struct Student{
    long num;
    float score;

    struct Student *next;
};
int n;

//創(chuàng)建鏈表
struct Student* create(){
    struct Student *head;
    struct Student *p1,*p2;
    n = 0;
    p1=p2=(struct Student *)malloc(LEN);
    scanf("%d %f",&p1->num,&p1->score);
    head = NULL;
    while(p1->num != 0){
        ++n;
        if(n==1) head = p1;
        else p2->next = p1;
        p2 = p1;
        //p1不斷生成新的結(jié)點(diǎn)
        p1 = (struct Student*)malloc(LEN);
        scanf("%ld %f",&p1->num,&p1->score);
    }
    p2->next = NULL;
    return head;
}



//按序插入,按成績(jī)
struct Student* insSort(struct Student *head,struct Student *stud){
    struct Student *p,*pre,*temp;
    p = head;
    temp = stud;
    if(head->score >= temp->score){
        head = temp;
        temp->next = p;
        return head;
    }
    while (p != NULL){
        if(p->score > temp->score){
            pre->next = temp;
            temp->next = p;
            break;
        }
        pre = p;
        p = p->next;
    }
    return head;
}


//排序(按成績(jī)),選擇排序
struct Student* sortStudent(struct Student *head){
    struct Student *p,*min,*ptr;
    long tempNum;
    float tempScore;
    p = head;
    while (p->next != NULL){
        min = p;
        ptr = p->next;
        while (ptr != NULL){
            if(min->score > ptr->score){
                min = ptr;
            }
            tempNum = min->num;
            min->num = p->num;
            p->num = tempNum;
            tempScore = min->score;
            min->score = p->score;
            p->score = tempScore;
            ptr = ptr->next;
        }
        p = p->next;
    }
    return head;

}
//打印鏈表元素
void print(struct Student* head){
    struct Student *p;
    printf("The numbers is %d\n",n);
    p = head;
    if(head != NULL){
        do{
            printf("num:%d,score:%5.1f\n",p->num,p->score);
            p = p->next;
        }while(p != NULL);
    }
}
int main(int argc, char* argv[])
{

    struct Student *pt;
    pt = create();
    pt = sortStudent(pt);
    print(pt);

    struct Student *s2;
    //必須申請(qǐng)空間
    s2=(struct Student *)malloc(LEN);
    s2->score = 78;
    s2->num = 1005;
    pt = insSort(pt,s2);
    print(pt);
    system("pause");
    return 0;
}

有序添加.png

釋放內(nèi)存

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define LEN sizeof(struct Student)

//鏈表
struct Student{
    long num;
    float score;

    struct Student *next;
};
int n;

//創(chuàng)建鏈表
struct Student* create(){
    struct Student *head;
    struct Student *p1,*p2;
    n = 0;
    p1=p2=(struct Student *)malloc(LEN);
    scanf("%d %f",&p1->num,&p1->score);
    head = NULL;
    while(p1->num != 0){
        ++n;
        if(n==1) head = p1;
        else p2->next = p1;
        p2 = p1;
        //p1不斷生成新的結(jié)點(diǎn)
        p1 = (struct Student*)malloc(LEN);
        scanf("%ld %f",&p1->num,&p1->score);
    }
    p2->next = NULL;
    return head;
}

//打印鏈表元素
void print(struct Student* head){
    struct Student *p;
    printf("The numbers is %d\n",n);
    p = head;
    if(head != NULL){
        do{
            printf("num:%d,score:%5.1f\n",p->num,p->score);
            p = p->next;
        }while(p != NULL);
    }
}
void deleteAll(struct Student *head){
    struct Student *p,*temp;
    p = head;
    while (p != NULL){
        n--;
        temp = p;
        p = p->next;
        free(temp);
    }
    printf("%d numbers\n",n);
}
int main(int argc, char* argv[])
{

    struct Student *pt;
    pt = create();
    print(pt);

    deleteAll(pt);
    system("pause");
    return 0;
}

delete.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容