• C语言实现单链表


    大二学习数据结构和算法啦,因为之前用Java语言实现过,所以理解起来还是比较轻松,就是理解指针与结构体的运用。废话不多说,上代码!

    初始化

    typedef struct Node {
        int data;
        struct Node *next;
    } ListNode; 
    
    ListNode* InitLink() { //初始化,创建头结点
        ListNode *Head = (ListNode*)malloc(sizeof(ListNode));
        Head->next = NULL;
        return Head;
    }

    添加

    void AddLink(ListNode *Head, int msg) { //添加
        ListNode *Tmp = Head, *New;
        while(Tmp->next != NULL) {
            Tmp = Tmp->next;
        }
        New = (ListNode*)malloc(sizeof(ListNode));
        New->data = msg;
        New->next = NULL;
        Tmp->next = New;
    }

    删除(可以按值,也可以按位置)

    void Delete(ListNode *Head,int data) {//按值删除 
        if(Head->next == NULL) {
            printf("链表无元素可以删除!");
            return;
        }
        
        ListNode *pre = Head; //保存上一个结点 
        ListNode *cur = Head->next;//保存当前结点 
        while(cur != NULL) {
            if(cur->data == data) {
                pre->next = cur->next;//上一个结点的下一个等于当前结点的下一个结点 
                free(cur);//释放结点 
                return;//结束 
            }
            pre = cur;//前一个结点等于当前结点 
            cur = cur->next;//当前结点等于下一个结点 
        }
    }
    
    void DeleteMethod(ListNode *head, int element){//按值删除结点
        ListNode *pre = head;
        ListNode *cur = head->next;
        while (cur != NULL){
            if (cur->data == element){
                pre->next = cur->next;
                free(cur);
                return;//结束程序 
            }
            pre = cur;
            cur = cur->next;
        }
    }
    
    //index代表删除第几个结点 
    void DeleteLink(ListNode *Head,int index){//按下标删除 
        ListNode *Tmp = Head;
        ListNode *Old;
        int mark = 0;
        if(index < 0){
            return;
        }
        while(mark < index-1 && Tmp->next != NULL){
            Old = Tmp;
            Tmp = Tmp->next;
            mark++;
        }
        while(Tmp->next == NULL){
            return; 
        }
        Old = Tmp->next;
        Tmp->next = Old->next; 
        free(Old);
    }

    修改(可以按值,也可以按位置)

    void UpdataLink(ListNode *head, int oldata, int newdata){//按值修改数据
        ListNode *pre = head;
        ListNode *cur = head->next;
        while (cur != NULL){
            if (cur->data == oldata){
                cur->data = newdata;
                return;//结束程序 
            }
            pre = cur;
            cur = cur->next;
        }
    }
    
    void UpLink(ListNode *Head,int index,int element){//按下标修改 
        ListNode *Tmp = Head;
        int mark = 0;
        if(index < 0){
            return;
        }
        while(mark < index - 1 && Tmp->next != NULL){
            Tmp = Tmp->next;
            mark++;
        }    
        while(Tmp->next == NULL){
            return; 
        }
        Tmp->next->data = element; 
    }

    插入(可以按值,也可以按位置)

    void InsetrLink(ListNode *head, int oldata, int newdata){//按值输入之前插入 
        ListNode *New = (ListNode*)malloc(sizeof(ListNode));
        New->data = newdata;
        New->next = NULL;
        ListNode *pre = head;
        ListNode *cur = head->next;
        while (cur != NULL){
            if (cur->data == oldata){
                pre->next = New;
                New->next = cur;
                return;//结束程序 
            }
            pre = cur;
            cur = cur->next;
        }
    }
    void InsertLink(ListNode *Head,int index,int element){//按下标插入 
        ListNode *Tmp = Head;
        ListNode *New;
        int mark = 0;
        if(index < 0){
            return;
        }
        while(mark < index - 1 && Tmp->next != NULL){
            Tmp = Tmp->next;
            mark++;
        }    
        while(Tmp->next == NULL){
            return; 
        }
        New = (ListNode*)malloc(sizeof(ListNode));
        New->data = element;
        New->next = Tmp->next;
        Tmp->next = New;
    }

    查询(返回一个结点地址)

    //提出查找结点方法,其它方法可以调用 
    //在单链表中查找第index个结点 
    ListNode* GetNode(ListNode *Head,int index){//查找结点方法 
        int mark = 0;
        ListNode *Tmp = Head;
        while(Tmp->next && mark < index){//Tmp->next为NULL 或者 mark==index结束 
            Tmp = Tmp->next;
            mark++;
        }
        if(index == mark){
            return Tmp;
        }else{
            return NULL;
        }
    }

    销毁

    void DestoryLink(ListNode *Head) { //销毁链表
        ListNode *Tmp;
        while(Head->next){
            Tmp = Head->next;
            Head->next = Tmp->next;
            free(Tmp);
        }
        free(Head);//头都删掉 
    }

    显示

    void Display(ListNode *Head) {//显示
        ListNode *Tmp = Head->next;
        while(Tmp != NULL) {
            printf("%d ",Tmp->data);
            Tmp = Tmp->next;
        }
    }

    甩上完整代码

    #include<stdio.h>
    #include<stdlib.h>
    
    //Upon开发————2019.9.26 
    typedef struct Node {
        int data;
        struct Node *next;
    } ListNode; 
    
    ListNode* InitLink() { //初始化,创建头结点
        ListNode *Head = (ListNode*)malloc(sizeof(ListNode));
        Head->next = NULL;
        return Head;
    }
    
    void AddLink(ListNode *Head, int msg) { //添加
        ListNode *Tmp = Head, *New;
        while(Tmp->next != NULL) {
            Tmp = Tmp->next;
        }
        New = (ListNode*)malloc(sizeof(ListNode));
        New->data = msg;
        New->next = NULL;
        Tmp->next = New;
    }
    
    void Delete(ListNode *Head,int data) {//按值删除 
        if(Head->next == NULL) {
            printf("链表无元素可以删除!");
            return;
        }
        
        ListNode *pre = Head; //保存上一个结点 
        ListNode *cur = Head->next;//保存当前结点 
        while(cur != NULL) {
            if(cur->data == data) {
                pre->next = cur->next;//上一个结点的下一个等于当前结点的下一个结点 
                free(cur);//释放结点 
                return;//结束 
            }
            pre = cur;//前一个结点等于当前结点 
            cur = cur->next;//当前结点等于下一个结点 
        }
    }
    
    void DeleteMethod(ListNode *head, int element){//按值删除结点
        ListNode *pre = head;
        ListNode *cur = head->next;
        while (cur != NULL){
            if (cur->data == element){
                pre->next = cur->next;
                free(cur);
                return;//结束程序 
            }
            pre = cur;
            cur = cur->next;
        }
    }
    void InsetrLink(ListNode *head, int oldata, int newdata){//按值输入之前插入 
        ListNode *New = (ListNode*)malloc(sizeof(ListNode));
        New->data = newdata;
        New->next = NULL;
        ListNode *pre = head;
        ListNode *cur = head->next;
        while (cur != NULL){
            if (cur->data == oldata){
                pre->next = New;
                New->next = cur;
                return;//结束程序 
            }
            pre = cur;
            cur = cur->next;
        }
    }
    
    void UpdataLink(ListNode *head, int oldata, int newdata){//修改数据
        ListNode *pre = head;
        ListNode *cur = head->next;
        while (cur != NULL){
            if (cur->data == oldata){
                cur->data = newdata;
                return;//结束程序 
            }
            pre = cur;
            cur = cur->next;
        }
    }
    void InsertLink(ListNode *Head,int index,int element){//按下标插入 
        ListNode *Tmp = Head;
        ListNode *New;
        int mark = 0;
        if(index < 0){
            return;
        }
        while(mark < index - 1 && Tmp->next != NULL){
            Tmp = Tmp->next;
            mark++;
        }    
        while(Tmp->next == NULL){
            return; 
        }
        New = (ListNode*)malloc(sizeof(ListNode));
        New->data = element;
        New->next = Tmp->next;
        Tmp->next = New;
    }
    void UpLink(ListNode *Head,int index,int element){//按下标修改 
        ListNode *Tmp = Head;
        int mark = 0;
        if(index < 0){
            return;
        }
        while(mark < index - 1 && Tmp->next != NULL){
            Tmp = Tmp->next;
            mark++;
        }    
        while(Tmp->next == NULL){
            return; 
        }
        Tmp->next->data = element; 
    }
    //index代表删除第几个结点 
    void DeleteLink(ListNode *Head,int index){//按下标删除 
        ListNode *Tmp = Head;
        ListNode *Old;
        int mark = 0;
        if(index < 0){
            return;
        }
        while(mark < index-1 && Tmp->next != NULL){
            Old = Tmp;
            Tmp = Tmp->next;
            mark++;
        }
        while(Tmp->next == NULL){
            return; 
        }
        Old = Tmp->next;
        Tmp->next = Old->next; 
        free(Old);
    }
    //提出查找结点方法,其它方法可以调用 
    //在单链表中查找第index个结点 
    ListNode* GetNode(ListNode *Head,int index){//查找结点方法 
        int mark = 0;
        ListNode *Tmp = Head;
        while(Tmp->next && mark < index){//Tmp->next为NULL 或者 mark==index结束 
            Tmp = Tmp->next;
            mark++;
        }
        if(index == mark){
            return Tmp;
        }else{
            return NULL;
        }
    }
    void InsertList(ListNode *Head,int data,int index){//按下标插入 
        ListNode *Tmp = GetNode(Head,index - 1),*New;
        if(Tmp == NULL){
            return;
        }
        New = (ListNode*)malloc(sizeof(ListNode));
        New->data = data;
        New->next = Tmp->next;
        Tmp->next = New;
    }
    void DeleteList(ListNode *Head,int index){//按下标删除 
        ListNode *Tmp = GetNode(Head,index - 1),*New;
        if(Tmp == NULL || Tmp->next == NULL){
            return;
        }
        New = Tmp->next;
        Tmp->next = New->next;
        free(New);
    }
    void Display(ListNode *Head) {//显示
        ListNode *Tmp = Head->next;
        while(Tmp != NULL) {
            printf("%d ",Tmp->data);
            Tmp = Tmp->next;
        }
    }
    
    void DestoryLink(ListNode *Head) { //销毁链表
        ListNode *Tmp;
        while(Head->next){
            Tmp = Head->next;
            Head->next = Tmp->next;
            free(Tmp);
        }
        free(Head);//头都删掉 
    }
    
    int main() {
        ListNode *Head = InitLink();
        AddLink(Head,1);
        AddLink(Head,2);
        AddLink(Head,3);
        AddLink(Head,4);
        AddLink(Head,5);
    
        UpdataLink(Head,5,4);
        Display(Head);
        return 0;
    }
    View Code
  • 相关阅读:
    NYOJ 158 省赛来了(变相组合数)
    NYOJ 111 分数加减法
    NYOJ 14 会场安排问题 (贪心)
    POJ 3903 Stock Exchange(LIS)
    NYOJ 456 邮票分你一半(01背包)
    HDU 4521 小明系列问题——小明序列 (LIS加强版)
    CSU 1120 病毒(经典模板例题:最长公共递增子序列)
    挑战程序设计竞赛里面的几道深度优先搜索
    2009 Multi-University Training Contest 4
    USACO sec1.1
  • 原文地址:https://www.cnblogs.com/hardhp74520/p/11594475.html
Copyright © 2020-2023  润新知