单链表的基本用法

单链表的基本用法

1.结构体定义

typedef struct LIST
{
    ElemType data;     // 每个节点存放的数据
    struct LIST *next; // 指针指向下一个节点
} LNode, *LinkList;    // LNode 偏向节点,LinkList偏向链表

2.基本函数

// 1.单链表的初始化
bool InitList(LinkList &list);
// 2.判断链表是否为空
bool IsEmpty(LinkList list);
// 3.头插法建立单链表
LinkList List_HeadInsert(LinkList &L);
// 4.尾插法建立单链表
LinkList List_TailInsert(LinkList &L);
// 5.按序号查找节点
LNode *GetElem(LinkList list, int Index);
// 6.按值查找表节点
LNode *LocateElem(LinkList list, ElemType Value);
// 7.插入节点
bool LNodeInsert(LinkList &list, ElemType Value, int Index);
// 8.删除节点
bool LNodeDelete(LinkList &list, int Index, ElemType &Value);
// 9.求单链表表长度
int ListLength(LinkList list);
// 10.单链表打印
void ListPrint(LinkList list);
// 11.销毁单链表
bool DestoryList(LinkList &list);

3.完整代码

linklist.h

#ifndef __LINKLIST_H__
#define __LINKLIST_H__

#include <stdio.h>

typedef int ElemType;

typedef struct LIST
{
    ElemType data;     // 每个节点存放的数据
    struct LIST *next; // 指针指向下一个节点
} LNode, *LinkList;    // LNode 偏向节点,LinkList偏向链表

// 1.单链表的初始化
bool InitList(LinkList &list);
// 2.判断链表是否为空
bool IsEmpty(LinkList list);
// 3.头插法建立单链表
LinkList List_HeadInsert(LinkList &L);
// 4.尾插法建立单链表
LinkList List_TailInsert(LinkList &L);
// 5.按序号查找节点
LNode *GetElem(LinkList list, int Index);
// 6.按值查找表节点
LNode *LocateElem(LinkList list, ElemType Value);
// 7.插入节点
bool LNodeInsert(LinkList &list, ElemType Value, int Index);
// 8.删除节点
bool LNodeDelete(LinkList &list, int Index, ElemType &Value);
// 9.求单链表表长度
int ListLength(LinkList list);
// 10.单链表打印
void ListPrint(LinkList list);
// 11.销毁单链表
bool DestoryList(LinkList &list);

#endif

linklist.cc

#include <stdlib.h>
#include "linklist.h"

// 1.单链表的初始化
bool InitList(LinkList &list)
{
    list = (LNode *)malloc(sizeof(LNode));
    if (list == NULL)
        return false;
    list->next = NULL;
    return true;
}

// 2.判断链表是否为空
bool IsEmpty(LinkList list)
{
    if (list->next == NULL)
        return true;
    return false;
}

// 3.头插法建立单链表(L就是头指针)
LinkList List_HeadInsert(LinkList &L)
{
    LNode *s = L;
    ElemType value = 0;
    printf("请输入值:(当输入为-1时停止)\n");
    scanf("%d", &value);
    while (value != EOF)
    {
        s = (LNode *)malloc(sizeof(LNode));
        s->next = L->next;
        L->next = s;
        s->data = value;
        scanf("%d", &value);
    }
    return L;
}

// 4.尾插法建立单链表
LinkList List_TailInsert(LinkList &L)
{
    LNode *tail = L;
    LNode *s = L;
    ElemType value = 0;
    printf("请输入值:(当输入为-1时停止)\n");
    scanf("%d", &value);
    while (value != EOF)
    {
        s = (LNode *)malloc(sizeof(LNode));
        s->data = value;
        tail->next = s;
        tail = s;
        scanf("%d", &value);
    }
    tail->next = NULL;
    return L;
}
// 5.按序号查找节点
LNode *GetElem(LinkList list, int Index)
{
    if (Index == 0)
        return list;
    if (list == NULL || Index < 1)
        return NULL;
    LNode *p = list->next;
    int i = 1;
    while (p && i < Index)
    {
        i++;
        p = p->next;
    }
    return p;
}

// 6.按值查找表节点
LNode *LocateElem(LinkList list, ElemType Value)
{
    if (list == NULL)
        return NULL;
    LNode *p = list->next;
    while (p && p->data != Value)
    {
        p = p->next;
    }
    return p;
}

// 7.插入节点
bool LNodeInsert(LinkList &list, ElemType Value, int Index)
{

    // 后插法
    // 1)找到第index-1个节点
    LNode *p = GetElem(list, Index - 1);
    if (NULL == p)
        return false;
    // 创建新节点
    LNode *s = (LNode *)malloc(sizeof(LNode));
    if (NULL == s)
        return false;
    s->data = Value;
    // 2)在后面插入节点
    s->next = p->next;
    p->next = s;
    return true;
    // 前插法
    // 1)找到第index个节点
    // 2)在后面插入节点
    // 3)交换两个节点的值
}

// 8.删除节点
bool LNodeDelete(LinkList &list, int Index, ElemType &Value)
{
    // 找到第Index-1个节点
    // 删除
    LNode *p = GetElem(list, Index - 1);
    if (p == NULL || p->next == NULL)
        return false;
    LNode *s = p->next;
    p->next = s->next;
    Value = s->data;
    free(s);
    return true;
}
// 9.求单链表表长度
int ListLength(LinkList list)
{
    LNode *p = list;
    int length = 0;
    while (p->next != NULL)
    {
        p = p->next;
        length++;
    }
    return length;
}

// 10.单链表打印
void ListPrint(LinkList list)
{
    if (NULL == list->next)
        printf("NULL!\n");
    LNode *p = list;
    int i = 0;
    while (p->next != NULL)
    {
        p = p->next;
        i++;
        printf("第%d个节点的值为%d!\n", i, p->data);
    }
}

// 11.销毁单链表
bool DestoryList(LinkList &list)
{
    int length = ListLength(list);
    LNode *p = NULL;
    for (int i = length; i >= 0; i--)
    {
        p = GetElem(list, i);
        free(p);
    }
    list = NULL;
    return true;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值