数据结构例程——单链表应用举例

  本文针对数据结构基础系列网络课程(2):线性表中第11课时单链表应用举例

例:拆分单链表 (linklist.h是单链表“算法库”中的头文件,详情单击链接…

#include <stdio.h>
#include <malloc.h>
#include "linklist.h"
void split(LinkList *&L,LinkList *&L1,LinkList *&L2)
{
    LinkList *p=L->next,*q,*r1; //p指向第1个数据节点
    L1=L;       //L1利用原来L的头节点
    r1=L1;                  //r1始终指向L1的尾节点
    L2=(LinkList *)malloc(sizeof(LinkList));    //创建L2的头节点
    L2->next=NULL;          //置L2的指针域为NULL
    while (p!=NULL)
    {
        r1->next=p;         //采用尾插法将*p(data值为ai)插入L1中
        r1=p;
        p=p->next;          //p移向下一个节点(data值为bi)
        q=p->next;          //由于头插法修改p的next域,故用q保存*p的后继节点
        p->next=L2->next;   //采用头插法将*p插入L2中
        L2->next=p;
        p=q;                //p重新指向ai+1的节点
    }
    r1->next=NULL;          //尾节点next置空
}
int main()
{
    LinkList *L,*L1,*L2;
    int i;
    ElemType a[]= {1,2,3,4,5,6,7,8,9,10};
    InitList(L);
    InitList(L1);
    InitList(L2);
    for(i=9; i>=0; i--)
        ListInsert(L, 1, a[i]);
    printf("L:");
    DispList(L);
    printf("L->L1,L2\n");
    split(L,L1,L2);
    printf("L1:");
    DispList(L1);
    printf("L2:");
    DispList(L2);
    DestroyList(L1);
    DestroyList(L2);
    return 0;
}

例:删除元素最大的节点(linklist.h是单链表“算法库”中的头文件,详情单击链接…

#include <stdio.h>
#include <malloc.h>
#include "linklist.h"

void delmaxnode(LinkList *&L)
{
    LinkList *p=L->next,*pre=L,*maxp=p,*maxpre=pre;
    while (p!=NULL) //用p扫描整个单链表,pre始终指向其前驱节点
    {
        if (maxp->data<p->data)  //若找到一个更大的节点
        {
            maxp=p; //更改maxp
            maxpre=pre; //更改maxpre
        }
        pre=p;      //p、pre同步后移一个节点
        p=p->next;
    }
    maxpre->next=maxp->next;    //删除*maxp节点
    free(maxp);         //释放*maxp节点
}

int main()
{
    LinkList *L;
    int i;
    ElemType a[]= {1,3,2,9,0,4,7,6,5,8};
    InitList(L);
    for(i=9; i>=0; i--)
        ListInsert(L, 1, a[i]);
    printf("L:");
    DispList(L);
    printf("删除最大值节点\n");
    delmaxnode(L);
    printf("L:");
    DispList(L);
    DestroyList(L);
    return 0;
}

例:增序排列节点(linklist.h是单链表“算法库”中的头文件,详情单击链接…

#include <stdio.h>
#include <malloc.h>
#include "linklist.h"

void sort(LinkList *&L)
{
    LinkList *p,*pre,*q;
    p=L->next->next;        //p指向L的第2个数据节点
    L->next->next=NULL;     //构造只含一个数据节点的有序表
    while (p!=NULL)
    {
        q=p->next;          //q保存*p节点后继节点的指针
        pre=L;              //从有序表开头进行比较,pre指向插入*p的前驱节点
        while (pre->next!=NULL && pre->next->data<p->data)
            pre=pre->next;  //在有序表中找插入*p的前驱节点*pre
        p->next=pre->next;  //将*pre之后插入*p
        pre->next=p;
        p=q;                //扫描原单链表余下的节点
    }
}
int main()
{
    LinkList *L;
    int i;
    ElemType a[]= {1,3,2,9,0,4,7,6,5,8};
    InitList(L);
    for(i=9; i>=0; i--)
        ListInsert(L, 1, a[i]);
    printf("L:");
    DispList(L);
    printf("排序\n");
    sort(L);
    printf("L:");
    DispList(L);
    DestroyList(L);
    return 0;
}
时间: 2024-08-02 16:50:11

数据结构例程——单链表应用举例的相关文章

数据结构例程——单链表的建立

本文是数据结构基础系列网络课程(2):线性表中第9课时建立单链表中所讲的例程. [例程] 定义单链表存储结构,用头插法和尾插法建立单链表,并显示建立好以后的结果. #include <stdio.h> #include <malloc.h> typedef int ElemType; typedef struct LNode //定义单链表结点类型 { ElemType data; struct LNode *next; //指向后继结点 } LinkList; void Crea

数据结构模版----单链表实现方式总结

数据结构模版----单链表实现方式总结 前面我们提供了四种方式实现的单链表,有带头结点的不带头结点的,而单链表的结构体定义也有两种方式,那么这些实现方式,到底有什么区别呢,为什么会出现这么多种实现方式呢,下面我们就来细细体会 一 单链表结构体的实现区别 首先我们对比一下,单链表结构体 不同方式的单链表实现时,链表结点的实现是相同的,不同之处在于单链表结构体的实现上 单链表结构体的实现 [cpp] view plain copy print? typedef int ElemType;      

数据结构模版----单链表SimpleLinkList[带头结点&amp;&amp;面向对象设计思想](C语言实现)

链表中的数据是以节点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据.以"结点的序列"表示线性表称作线性链表(单链表) 单链表是链式存取的结构,为找第 i 个数据元素,必须先找到第 i-1 个数据元素. [cpp] view plain copy print? #include <stdio.h>   #include <stdlib.h>   #include <

数据结构实践——单链表:逆置、连接与递增判断

本文针对数据结构基础系列网络课程(2):线性表的实践项目. [项目 - 单链表算法](程序中利用了已经实现的单链表算法,头文件LinkList.h及其中函数的实现见单链表算法库) 1.设计一个算法,将一个带头结点的数据域依次为a1,a2,-,an(n≥3)的单链表的所有结点逆置,即第一个结点的数据域变为an,-,最后一个结点的数据域为a1.实现这个算法,并完成测试. [参考解答] (程序中利用了已经实现的单链表算法,头文件LinkList.h及其中函数的实现见单链表算法库) #include <

数据结构模版----单链表SimpleLinkList[带头结点](C语言实现)

前面写的单链表结构体是重新设计的.包含头结点(或者头指针)以及链表长度的结构体,而我们通常实现的链表是直接把单链表结点结构体作为单链表来使用的,下面我们给出z这种实现方式,让我们一起来细细体会他们实现之间的区别 [cpp] view plain copy print? #include <stdio.h>   #include <stdlib.h>   #include <stdbool.h>   #include <assert.h>      //#de

数据结构模版----单链表SimpleLinkList[不带头结点&amp;&amp;伪OO](C语言实现)

上一篇写单链表是带头结点的,但是其他这种写法的单链表中,头结点其实就不是那么必要了,因为我们的单链表结构体中增加了一项m_length 下面的不加头结点的单链表奉上 不带头结点的单链表结构体 [cpp] view plain copy print? #include <stdio.h>   #include <stdlib.h>   #include <stdbool.h>   #include <assert.h>            ///*/////

数据结构模版----单链表SimpleLinkList[不带头结点](C语言实现)

下面给出的是单链表不带头结点的另一种实现方式,也是最复杂的一种方式 [cpp] view plain copy print? #include <stdio.h>   #include <stdlib.h>   #include <stdbool.h>   #include <assert.h>      //#define DEBUG             // 调试插桩信息宏      ///*/////////////////////////////

算法与数据结构之单链表

单链表:#include<stdio.h>#include<malloc.h>#include<windows.h>typedef int elemtype; typedef struct LNode //定义单链表存储类型{elemtype data;struct LNode *next;}linklist; void creatlistf(linklist *&L ) //建立链表(头插法){linklist *s;int i;elemtype a[10];

单链表 数据结构 c++-单链表具体实现实现reverse

问题描述 单链表具体实现实现reverse 用c++语言写,需要完整的程序,能运行的 将链表的顺序颠倒过来,最好写清楚指针的具体实现的过程 谢谢谢谢 解决方案 #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct node{ int data; struct node *next; }NODE; void insert(NODE **head,NODE *node){ if((*he