【数据结构】链表

【1】线性表的顺序存储结构有存储密度高及能够随机存取等优点,但存在以下不足:

    (1)要求系统提供一片较大的连续存储空间。
    (2)插入、删除等运算耗时,且存在元素在存储器中成片移动的现象;

【2】线性表的链式存储(单链表)的实现

#include <stdio.h>
#include <stdlib.h>
typedef int datatype_t;
struct node
{
    datatype_t data;
    struct node *next;
};


int linklist_empty(struct node *ll)
{
    return (ll->next==NULL)?1:0;
}

struct node *linklist_create()
{
    struct node *tmp=(struct node *)malloc(sizeof(struct node));
    tmp->next=NULL;
    tmp->data=-1;
    return tmp;
}

void linklist_insert_head(struct node *ll,datatype_t value)
{
    struct node *tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=value;
    tmp->next=ll->next;
    ll->next=tmp;
}

int linklist_insert_by_pos(struct node *ll,int pos,datatype_t value)
{
    int cnt=0;
    while(ll->next!=NULL)
    {
        if(cnt==pos)
        {
            struct node *tmp=(struct node *)malloc(sizeof(struct node));
            tmp->data=value;
            tmp->next=ll->next;
            ll->next=tmp;
            return 0;
        }
        cnt++;
        ll=ll->next;
    }
    return -1;
}

void linklist_insert_tail(struct node *ll,datatype_t value)
{
    while(ll->next!=NULL)
        ll=ll->next;
    struct node *tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=value;
    tmp->next=ll->next;
    ll->next=tmp;
}

void linklist_insert_bt_sort(struct node *ll,datatype_t value)
{
    while(ll->next!=NULL && ll->next->data<value)
        ll=ll->next;
    struct node *tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=value;
    tmp->next=ll->next;
    ll->next=tmp;
}

datatype_t linklist_delete_head(struct node *ll)
{
    if(linklist_empty(ll)==1)
    {
        return -1;
    }
    struct node *p=ll->next;
    ll->next=p->next;
    free(p);
    p=NULL;
}

void linklist_reverse(struct node *ll)
{
    struct node *p,*q;
    p=ll->next;
    ll->next=NULL;
    while(p!=NULL)
    {
        q=p;
        p=p->next;
        q->next=ll->next;
        ll->next=q;
    }
}

void linklist_modify_by_data(struct node *ll,datatype_t pre,datatype_t value)
{
    while(ll->next!=NULL)
    {
        if(ll->next->data==pre)
            ll->next->data=value;
        ll=ll->next;
    }
}

int linklist_search_by_data(struct node *ll,datatype_t value)
{
    int pos=0;
    while(ll->next!=NULL)
    {
        if(ll->next->data==value)
            return pos;
        ll=ll->next;
        pos++;
    }
    return -1;
}


void linklist_free(struct node *ll)
{
    struct node *p,*q;
    p=ll->next;
    ll->next=NULL;
    while(p!=NULL)
    {
        q=p;
        p=p->next;
        free(q);
    }
}





void linklist_show(struct node *ll)
{
    while(ll->next!=NULL)
    {
        printf("%d ",ll->next->data);
        ll=ll->next;
    }
    putchar('\n');
}





int main()
{
    int i;
    struct node *linklist;
    linklist=linklist_create();
    for(i=5;i>=0;i--)
    {
        linklist_insert_bt_sort(linklist,i);
    }

    linklist_show(linklist);

    datatype_t d=linklist_delete_head(linklist);
    linklist_show(linklist);

    linklist_modify_by_data(linklist,4,3);
    linklist_show(linklist);

    linklist_insert_by_pos(linklist,1,20);
    linklist_show(linklist);

    linklist_reverse(linklist);
    linklist_show(linklist);

    linklist_free(linklist);
    return 0;
}

【3】单向循环链表的实现

    //定义数据类型
    //定义结构体
    //创建一个空的链表(循环)
    //插入数据(头插法)
    //打印一下
    //去头结点
    //打印一下

#include <stdio.h>
#include <stdlib.h>
typedef int datatype_t;


struct node
{
    datatype_t data;
    struct node *next;
};


struct node *looplist_create()
{
    struct node *tmp=(struct node *)malloc(sizeof(struct node));
    tmp->next=tmp;
    return tmp;
}

void looplist_insert(struct node *ll,datatype_t value)
{
    struct node *tmp=(struct node *)malloc(sizeof(struct node));

    tmp->data=value;
    if(ll->next==ll)
    {
        //printf("0:%d\n",tmp->data);
        tmp->next=tmp;
        ll->next=tmp;
    }
    else
    {
        //printf("1:%d\n",tmp->data);
        struct node *p=ll->next;
        while(p->next!=ll->next)
        {
            p=p->next;
        }
        tmp->next=ll->next;
        ll->next=tmp;
        p->next=tmp;

    }
}

void looplist_show(struct node *ll)
{
    struct node *p=ll->next;
    if(ll->next==ll)
        return;
    else if(p->next==p)
    {
        printf("%d\n",p->data);
    }
    else
    {
        do
        {
            printf("%d ",p->data);
            p=p->next;
        }while(p!=ll->next);
    }
    putchar('\n');
}



int main()
{
    int i;
    struct node *looplist;
    looplist=looplist_create();

    for(i=5;i>0;i--)
    {
        looplist_insert(looplist,i);
    }

    looplist_show(looplist);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值