单链表

单链表

简而言之,用列表表示线性关系

基本结构

typedef int DataType;   /*数据类型*/
typedef int ElemType;   /*元素类型*/

//----------------线性列表存储结构----------------
typedef struct LNode
{
    ElemType data;
    struct LNode *next;
}LNode, *LinkList;

函数

  • 创建链表
void ListCreate(LinkList L, int n)
//创建含有n个结点的链表
{
    int i;
    LinkList p, q;
    p = L;
    for(i = 0 ; i < n; i++)
    {
        q = (LinkList)malloc(sizeof(LNode));    //生成新结点
        scanf("%d", &q->data);
        q->next = NULL;

        p->next = q;                            //前后结点链接
        p = q;
    }
}
  • 插入结点
void ListInsert(LinkList L, int i, ElemType e)
//在带头结点的单链表L中第i个位置之前插入元素e
{
    LinkList p = L;
    int j = 0;
    while(p && j < i-1)                         //寻找第i-1个结点
    {
        p = p->next;
        ++j;
    }

    LinkList s = (LinkList)malloc(sizeof(LNode));
    s->data = e;
    s->next = p->next;
    p->next = s;
}
  • 归并两个有序表
void ListMerge(LinkList La, LinkList Lb, LinkList Lc)
{
    LinkList pa = La->next, pb = Lb->next;
    LinkList pc = Lc;

    while(pa && pb)
    {
        if(pa->data <= pb->data)
        {
            pc ->next = pa;
            pc = pa;
            pa = pa->next;
        }
        else
        {
            pc->next = pb;
            pc = pb;
            pb = pb->next;
        }
    }
    pc->next = pa?pa:pb;                //插入剩余段
    free(La);
    free(Lb);
}
  • 删除结点
void ListDelete(LinkList L, int i, ElemType *e)
//在带头结点的单链表L中,删除第i个元素,并用e返回其值
{
    LinkList p = L;
    int j = 0;
    while(p->next && j < i-1)                   //寻找第i-1个结点
    {
        p = p->next;
        ++j;
    }

    LinkList q = p->next;
    p->next = q->next;
    *e = q->data;
    free(q);
}
  • 获取元素
void GetElem(LinkList L, int i, ElemType *e)
//返回第i个元素的值
{
    LinkList p = L;
    int j = 0;
    while(p && j < i)
    {
        p = p->next;
        ++j;
    }

    *e = p->data;
}

测试代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//函数结果状态代码
#define TRUE 1
#define FALUSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2

typedef int DataType;   /*数据类型*/
typedef int ElemType;   /*元素类型*/

//----------------线性列表存储结构----------------
typedef struct LNode
{
    ElemType data;
    struct LNode *next;
}LNode, *LinkList;

void ListCreate(LinkList L, int n)
//创建含有n个结点的链表
{
    int i;
    LinkList p, q;
    p = L;
    for(i = 0 ; i < n; i++)
    {
        q = (LinkList)malloc(sizeof(LNode));    //生成新结点
        scanf("%d", &q->data);
        q->next = NULL;

        p->next = q;                            //前后结点链接
        p = q;
    }
}

void ListInsert(LinkList L, int i, ElemType e)
//在带头结点的单链表L中第i个位置之前插入元素e
{
    LinkList p = L;
    int j = 0;
    while(p && j < i-1)                         //寻找第i-1个结点
    {
        p = p->next;
        ++j;
    }

    LinkList s = (LinkList)malloc(sizeof(LNode));
    s->data = e;
    s->next = p->next;
    p->next = s;
}

void ListMerge(LinkList La, LinkList Lb, LinkList Lc)
{
    LinkList pa = La->next, pb = Lb->next;
    LinkList pc = Lc;

    while(pa && pb)
    {
        if(pa->data <= pb->data)
        {
            pc ->next = pa;
            pc = pa;
            pa = pa->next;
        }
        else
        {
            pc->next = pb;
            pc = pb;
            pb = pb->next;
        }
    }
    pc->next = pa?pa:pb;                //插入剩余段
    free(La);
    free(Lb);
}

void ListDelete(LinkList L, int i, ElemType *e)
//在带头结点的单链表L中,删除第i个元素,并用e返回其值
{
    LinkList p = L;
    int j = 0;
    while(p->next && j < i-1)                   //寻找第i-1个结点
    {
        p = p->next;
        ++j;
    }

    LinkList q = p->next;
    p->next = q->next;
    *e = q->data;
    free(q);
}

void GetElem(LinkList L, int i, ElemType *e)
//返回第i个元素的值
{
    LinkList p = L;
    int j = 0;
    while(p && j < i)
    {
        p = p->next;
        ++j;
    }

    *e = p->data;
}

void ListTraverse(LinkList L)
{
    LinkList p = L;
    while(p->next)
    {
        p = p->next;
        printf("%d", p->data);
    }
    printf("\n");
}

int main()
{
    int a = 4;
    int *e = &a;
    LNode La, Lb, Lc;


    ListCreate(&La, 4);
    ListTraverse(&La);

    ListCreate(&Lb, 4);
    ListTraverse(&Lb);

    ListInsert(&La, 3, 4);
    ListDelete(&La, 4, e);
    ListTraverse(&La);
    printf("%d\n", *e);

    ListMerge(&La, &Lb, &Lc);
    ListTraverse(&Lc);

    return 0;
}

你可能感兴趣的:(单链表)