【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;
}

【数据结构】链表的更多相关文章

  1. canvas中普通动效与粒子动效的实现代码示例

    canvas用于在网页上绘制图像、动画,可以将其理解为画布,在这个画布上构建想要的效果。本文详细的介绍了粒子特效,和普通动效进行对比,非常具有实用价值,需要的朋友可以参考下

  2. H5混合开发app如何升级的方法

    本篇文章主要介绍了H5混合开发app如何升级的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  3. canvas学习和滤镜实现代码

    这篇文章主要介绍了canvas学习和滤镜实现代码,利用 canvas,前端人员可以很轻松地、进行图像处理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  4. localStorage的过期时间设置的方法详解

    这篇文章主要介绍了localStorage的过期时间设置的方法详解的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  5. 详解HTML5 data-* 自定义属性

    这篇文章主要介绍了详解HTML5 data-* 自定义属性的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  6. HTML5的postMessage的使用手册

    HTML5提出了一个新的用来跨域传值的方法,即postMessage,这篇文章主要介绍了HTML5的postMessage的使用手册的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  7. 教你使用Canvas处理图片的方法

    本篇文章主要介绍了教你使用Canvas处理图片的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  8. ios – Swift语言:如何调用SecRandomCopyBytes

    从Objective-C,我可以这样做:在Swift中尝试这个时,我有以下内容:但我得到这个编译器错误:data.mutableBytes参数被拒绝,因为类型不匹配,但我无法弄清楚如何强制参数.解决方法这似乎有效:

  9. 使用Firebase iOS Swift将特定设备的通知推送到特定设备

    我非常感谢PushNotifications的帮助.我的应用聊天,用户可以直接向对方发送短信.但是如果没有PushNotifications,它就没有多大意义.它全部设置在Firebase上.如何将推送通知从特定设备发送到特定设备?

  10. ios – NSData to Data swift 3

    如何将此代码转换为使用Swift3数据?

随机推荐

  1. 【数据结构】单调栈

    显然,每个发射站发来的能量有可能被0或1或2个其他发射站所接受,特别是为了安全,每个发射站接收到的能量总和是我们很关心的问题。由于数据很多,现只需要你帮忙计算出接收最多能量的发射站接收的能量是多少。输入输出格式输入格式:第1行:一个整数N;第2到N+1行:第i+1行有两个整数Hi和Vi,表示第i个人发射站的高度和发射的能量值。输入输出样例输入样例:34235610输出样例:7题解中有讲解代码实现

  2. BZOJ 1798 [Ahoi2009] Seq 维护序列seq [线段树+多重标记下传]【数据结构】

    有长为N的数列,不妨设为a1,a2,…Input第一行两个整数N和P。第二行含有N个非负整数,从左到右依次为a1,aN,。表示把所有满足t≤i≤g的ai改为ai×c。操作2:“2tgc”。同一行相邻两数之间用一个空格隔开,每行开头和末尾没有多余空格。Output对每个操作3,按照它在输入中出现的顺序,依次输出一行一个整数表示询问结果。SampleInput7431234567512553242379313347SampleOutput2358HINT初始时数列为。对第5次操作,和为29+34+15+16=

  3. 陈越《数据结构》第一讲 基本概念

    陈越《数据结构》第一讲基本概念1什么是数据结构1.1引子例子:如何在书架上摆放图书?数据结构是:1.数据对象在计算机中的组织方式;2.数据对象必定与一系列加在其上的操作相关联;3.完成这些操作所用的方法就是算法。抽象数据类型数据类型-数据对象集;-数据集合相关联的操作集。抽象-与存放数据的机器无关;-与数据存储的物理结构无关;-与实现操作的算法和编程语言均无关。

  4. 陈越《数据结构》第二章 线性结构

    表中元素个数称为线性表的长度;线性表没有元素时,称为空表;表起始位置称表头,表结束位置称表尾。插入和删除操作只能在链栈的栈顶进行。

  5. 【数据结构】

    非线性结构:线性结构的元素之间具有线性关系,非线性结构中的元素之间不再是序列的关系,他们呈现的是更复杂的层次关系,即一个数据元素有且仅有一个直接前驱,但可有另个或者多个直接后继,显然比序列关系复杂常见非线性结构:树,图散列表PHP中的hashtable就是哈希表就是由数组和链表组成,一个长度为16的数组中,每个元素存储的是一个链表的头结点。

  6. 【数据结构】【C++STL】FIFO队列&amp;优先队列

    首先都需要打头文件queueFIFO队列是先进先出的就好像排队一样STL定义FIFO队列优先队列的话是有优先级存在的STL定义优先队列定义方式都是大根堆FIFO队列和优先队列都有一些操作COYG

  7. 【数据结构】 堆

    自底向上://增加/减少已有节点值Heap_Increase_Key//向堆插入新的节点HeapInsert自顶向下://替换堆顶后,维持堆函数KeepHeap//弹出堆顶函数Pop

  8. 【数据结构】链表

    线性表的顺序存储结构有存储密度高及能够随机存取等优点,但存在以下不足:线性表的链式存储(单链表)的实现单向循环链表的实现

  9. 伸展树(SPLAY)个人总结+模板 [平衡树]【数据结构】【模板】

    前言最近3个月内,无论是现场赛还线上赛中SPLAY出现的概率大的惊人啊啊啊!!!然而不会的我就GG了,同时发现大家都会SPLAY,,,,然后就学习了一波。——————————————————————————-附上整体代码-md贴上来太卡了,去题解里看吧维护序列的维护一堆数的

  10. BZOJ 1895 &amp; POJ 3580 supermemo [SPLAY]【数据结构】

    Ay}Ttimes.Forexample,performing“REVOLVE242”on{1,5}INSERTxP:insertPafterAx.Forexample,performing“INSERT24”on{1,5}DELETEx:deleteAx.Forexample,performing“DELETE2”on{1,5}MINxy:querytheparticipantwhatistheminimumnumberinsub-sequence{Ax…Ay}.Forexample,thecorrec

返回
顶部