数据结构:链表

#链表
链表是线性表的一种,已知线性表的存储方式为两种:顺序排列和链式排列。
这里说明一下什么是链式
1、链式就是数据元素在内存的随机地址,但是每个数据元素都有指向下一个元素的号码牌,就好比是解密类游戏,每个数据元素都有找到下一个元素的线索。
2、说到链表,就要说明链表的最小单位结点。结点就好比是数据元素,但是结点里面不仅有数据还有下一个结点的地址,结点分为:数据域和指针域。
数据域:自然就是存放数据元素的数据。
指针域:存放下一个结点的地址。

优点:1、链表的存储方式是随机存储,不需要考虑内存空间的是节约还是浪费的问题。
2、链表的删除和插入相对于顺序表而言在确定位置情况下时间复杂度为O(1)比顺序表O(n)明显来得高效
3、链表不需要考虑数据上溢

缺点:1、链表的获取元素需要从头指针开始依次遍历时间复杂为O(n),而顺序表直接可以找出元素,时间复杂度为O(1)。
2、链表比顺序需要更多存储空间

总结:在已知数据大小的情况下,需要频繁使用插入和删除的适合使用链表,需要频繁使用获取元素适合使用顺序表,需要考虑节约空间的也使用顺序表,在考虑未知数据元素情况下使用链表。

链表有单项链表和双向链表,这里代码举例为单项链表`

#创建结点

struct Node
{
    TYPE date;
    Node* next;
};

Node* Creat_Node(TYPE dat)
{
    Node* d = (Node*)malloc(sizeof(Node));
    d->date = dat;
    d->next = NULL;
    return d;

}


#创建链表

struct Linklist
{
    Node *head;
    int len;
};
    //创建
Linklist* Creat_Linklist(void)
{
    Linklist* list = (Linklist*)malloc(sizeof(Linklist));
    list->head = NULL;
    list->len = 0;
    return list;

}

#链表的基础功能

//头插入
bool add_head(Linklist* list,TYPE dat)
{
    Node* node = Creat_Node(dat);
    if(NULL == node) return false;
    if(list->head == NULL)
    {
        list->head = node;
        list->len++;
    }
    else
    {
        node->next = list->head;
        list->head = node;
        list->len++;
    }
    return true;
}
    //头删除
bool del_head(Linklist* list)
{
    if(list->head == NULL) return false;
    Node* node = list->head;
    list->head = node->next;
    list->len--;
    free(node);
    return true;

}

 //指定位置添加
bool add_behind_index(Linklist* list,TYPE val,int index)
{
    if(index<=0 || index>list->len) return false;
    Node* temp = list->head;
    Node* node = Creat_Node(val);
    while(--index)
    {
        temp = temp->next;
    }
    node->next = temp->next;
    temp->next = node;
    list->len++;
    return true;


}
    //指定位子删除
bool del_index(Linklist* list,int index)
{
    if(list->head == NULL || index<=0 || index>list->len) return false;
    if(1 == index) return del_head(list);
    Node* temp = list->head;
    index = index-1;
    while(--index)
    {
        temp = temp->next;
    }
    Node* node = temp->next;
    temp->next = node->next;
    free(node);
    list->len--;
    return true;

}
  //查找
int find_val_list(Linklist* list,TYPE val)
{
    if(list->head == NULL) return 0;
    int index = 0;
    for(Node* node = list->head;node != NULL;node=node->next)
    {
        index++;
        if(val == node->date)
        {
            return index;
        }
    }
    return 0;
}
    //遍历
bool show(Linklist* list)
{
    if(list->head == NULL) return false;
    for(Node* i = list->head;i !=NULL;i=i->next)
    {
        cout<<i->date<<" ";
    }
    cout<<endl;
    return true;
}
    //销毁
void delet_list(Linklist* list)
{
    while(list->head != NULL)
    {
        del_head(list);
    }
    free(list);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值