zoukankan      html  css  js  c++  java
  • 0x00数据结构——C语言实现(单链表)

    0x00数据结构——C语言实现(单链表)

    /*filename:singly_linked_list.h*/
    /*
     单链表(singly linked list)是一种最简单的链表表示,也叫做线性链表。
     用它来表示线性表时,用指针表示结点间的逻辑关系。
    
     Functions:
     (在链表中增加附加头结点的版本)
        创建一个空线性表
        将链表置为空表
        计算表长度
        返回附加头结点的地址
        搜索函数:找x在表中的位置,返回表项位置
        定位函数:返回第i个表项在表中的位置
        取第i个表项的值
        用x修改第i个表项的内容
        插入x在表中第i个表项之后,函数返回成功标志
        删除表中第i个表项,通过x返回删除表项的值,函数返回成功标志
        判断表空,空返回真,否则返回假
        判断表满:满返回真,否则返回假
        输出
        对当前的表排序
    */
    
    #ifndef SINGLY_LINKED_LIST
    #define SINGLY_LINKED_LIST
    
    //假定每个表项的类型为T
    typedef int T;
    #define MAXLEN 100
    typedef enum {
        false = 0,
        true
    } BOOL;
    
    //单链表结点的数据结构。
    struct node;
    typedef struct node node;
    typedef struct node *to_node;
    typedef to_node link_list;
    typedef to_node pos;
    
    //创建一个空单链表
    link_list create_list(void);
    
    //将链表置为空表
    BOOL set_empty(link_list l);
    
    //计算表长度
    int calc_length(link_list l);
    
    //返回附加头结点的地址
    link_list head_addr(link_list l);
    
    //搜索函数:找x在表中的位置,返回表项位置
    pos search(link_list l, T x);
    
    //定位函数:返回第i个表项在表中的位置
    pos locate(link_list l, int i);
    
    //取第i个表项的值
    T get_val(link_list l, int i);
    
    //用x修改第i个表项的内容
    BOOL change_val(link_list l, int i, const T x);
    
    //插入x在表中第i个表项之后,函数返回成功标志
    BOOL insert_val(link_list l, int i, const T x);
    
    //删除表中第i个表项,通过x返回删除表项的值,函数返回成功标志
    BOOL delete_val(link_list l, int i, T *x);
    
    //判断表空,空返回真,否则返回假
    BOOL isempty(link_list l);
    
    //输出
    void output(link_list l);
    
    //对当前的表排序
    BOOL sort(link_list l);
    
    #endif
    /*filename:singly_linked_list.c*/
    #include <stdio.h>
    #include <stdlib.h>
    #include "singly_linked_list.h"
    
    
    /*
    单链表结点的数据结构
    */
    struct node{
        T val;
        struct node *next;
    };
    /*
    struct node;
    typedef struct node *to_node;
    typedef to_node link_list;
    typedef to_node pos;
    */
    
    //创建一个空单链表
    link_list create_list(void)
    {
        link_list l = (link_list)malloc(sizeof(node));
        l->next = NULL;
        l->val = 0;
        return l;
    }
    //该单链表带头节点,头节点的val域存储链表的长度
    
    //将链表置为空表
    BOOL set_empty(link_list l)
    {
        pos tmp = l->next, ttemp;
        while(tmp!=NULL){
            ttemp = tmp->next;
            free(tmp);
            tmp = ttemp;
            }
        l->next = NULL;
        l->val = 0;
        return true;
    }
    
    //计算表长度
    int calc_length(link_list l)
    {
        return l->val;
    }
    
    //返回附加头结点的地址
    link_list head_addr(link_list l)
    {
        return l;
    }
    
    //搜索函数:找x在表中的位置,返回表项位置
    pos search(link_list l, T x)
    {
        int i = 1;
        pos tmp;
        tmp = l->next;
        while(tmp != NULL && tmp->val != x && i<=l->val){
            tmp = tmp->next;
            i++;
        }
        if(i>l->val) return NULL;
        else return tmp;
    }
    
    //定位函数:返回第i个表项在表中的位置
    pos locate(link_list l, int i)
    {
        if(i<=l->val && i>0){
            pos tmp;
            tmp = l;
            while(--i>=0 && tmp!=NULL){
                tmp = tmp->next;
            }
            return tmp;
        } else {
            printf("can not access the %d'th element
    ", i);
            return NULL;
        }
    }
    
    //取第i个表项的值
    T get_val(link_list l, int i)
    {
        if(i<=l->val && i>0){
            pos tmp;
            tmp = l;
            while(--i>=0 && tmp!=NULL){
                tmp = tmp->next;
            }
            return tmp->val;
        } else {
            printf("can not access the %d'th element
    ", i);
            return -1;
        }
    }
    
    //用x修改第i个表项的内容
    BOOL change_val(link_list l, int i, const T x)
    {
        if(i<=l->val && i>0){
            pos tmp;
            tmp = l;
            while(--i>=0 && tmp!=NULL){
                tmp = tmp->next;
            }
            tmp->val = x;
            return true;
        } else {
            return false;
        }
    }
    
    //插入x在表中第i个表项之后,函数返回成功标志
    BOOL insert_val(link_list l, int i, const T x)
    {
        if(i<=l->val && i>=0){
            pos tmp;
            tmp = l;
            while(--i>=0 && tmp!=NULL){
                tmp = tmp->next;
            }
            pos t = (pos)malloc(sizeof(node));
            t->next = tmp->next;
            t->val = x;
            tmp->next = t;
            l->val++;
            return true;
        } else {
            return false;
        }
    }
    
    //删除表中第i个表项,通过x返回删除表项的值,函数返回成功标志
    BOOL delete_val(link_list l, int i, T *x)
    {
        if(i<=l->val && i>0){
            i--;
            pos tmp;
            tmp = l;
            while(--i>=0 && tmp!=NULL){
                tmp = tmp->next;
            }
            *x = (tmp->next)->val;
            tmp->next = (tmp->next)->next;
            l->val--;
            return true;
        } else {
            return false;
        }
    }
    
    //判断表空,空返回真,否则返回假
    BOOL isempty(const link_list l)
    {
        return (l->val == 0 ? true:false);
    }
    
    //输出
    void output(const link_list l)
    {
        pos tmp = NULL;
        int i = 1;
        tmp = l->next;
        while(tmp!=NULL){
            printf("the %dth element is %d
    ", i++, tmp->val);
            tmp = tmp->next;
        }
    }
    
    //对当前的表排序
    BOOL sort(const link_list l)
    {
        if(l->val < 2)
            return true;
        //冒泡排序
        pos tmp, tmp1, tmp2;
        int i;
        tmp = l->next;
        while(tmp->next != NULL) {
    
            tmp1 = l->next;
            tmp2 = tmp1->next;
            while(tmp2 != NULL) {
                if(tmp1->val > tmp2->val) {
                    i = tmp1->val;
                    tmp1->val = tmp2->val;
                    tmp2->val = i;
                }
                tmp1 = tmp1->next;
                tmp2 = tmp2->next;
            }
    
            tmp = tmp->next;
        }
        return true;
    
        //快速排序
    
        //归并排序
    
    }
  • 相关阅读:
    awk线程号
    std::string::substr函数
    计数器表的简单使用
    vim + oh-my-zsh + git搭建开发环境
    <<代码大全>>阅读笔记之二 变量名的力量
    <<代码大全>>阅读笔记之一 使用变量的一般事项
    压测工具ab的简单使用
    nginx配置文件详解
    numba初体验
    Linux查找文件内容小技巧
  • 原文地址:https://www.cnblogs.com/born2run/p/9581347.html
Copyright © 2011-2022 走看看