(数据结构)单链表及其应用实例——C语言实现

文章目录

  • 前言
  • 代码
  • 总结


前言

学习记录《数据结构——从概念到C实现》
part 2:单链表


代码

基于C语言实现链表的基本操作,并调用基本操作的函数来完成相应的功能。

#include 
#include 
#include 

typedef int DataType;
typedef struct Node
{
    DataType data;
    struct Node *next;
} Node;

//单链表的初始化
Node *InitList()
{
    Node *first = (Node *)malloc(sizeof(Node)); //生成头结点
    first->next = NULL;
    return first;
}

//判空操作(只需判断是否只有头结点)
int Empty(Node *first)
{
    if (first->next == NULL)
        return 1;
    else
        return 0;
}

//遍历操作
void PrintList(Node *first)
{
    Node *p = first->next;
    while (p != NULL)
    {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("\n");
}

//求单链表长度
int Length(Node *first)
{
    Node *p = first->next;
    int count = 0;
    while (p != NULL)
    {
        p = p->next;
        count++;
    }
    return count;
}

//按值查找
int Locate(Node *first, DataType x)
{
    Node *p = first->next;
    int count = 1;
    while (p != NULL)
    {
        if (p->data == x)
            return count;
        p = p->next;
        count++;
    }
    return 0;
}

//按位查找
int Get(Node *first, int i, DataType *ptr)
{
    Node *p = first->next;
    int count = 1;
    while (p != NULL)
    {
        if (count == i)
        {
            *ptr = p->data;
            return 1;
        }
        p = p->next;
        count++;
    }
    printf("位置错误,查找失败\n");
    return 0;
}

//插入操作
int Insert(Node *first, int i, DataType x)
{
    Node *p = first;
    Node *node = (Node *)malloc(sizeof(Node));
    int count = 1;
    while (p != NULL)
    {
        if (count == i)
        {
            node->data = x;
            node->next = p->next;
            p->next = node;
            return 1;
        }
        count++;
        p = p->next;
    }
    printf("位置错误,插入失败\n");
    return 0;
}

//头插法建立单链表
Node *HeadCreateList(DataType a[], int n)
{
    Node *s = NULL;
    Node *first = (Node *)malloc(sizeof(Node));
    first->next = NULL;
    for (int i = 0; i < n; i++)
    {
        s = (Node *)malloc(sizeof(Node));
        s->data = a[i];
        s->next = first->next;
        first->next = s;
    }
    return first;
}

//尾插法建立单链表
Node *TailCreateList(DataType a[], int n)
{
    Node *s = NULL, *r = NULL;
    Node *first = (Node *)malloc(sizeof(Node));
    first->next = NULL;
    r = first;
    for (int i = 0; i < n; i++)
    {
        s = (Node *)malloc(sizeof(Node));
        s->data = a[i];
        r->next = s;
        r = r->next;
    }
    r->next = NULL;
    return first;
}

//删除操作
int Delete(Node *first, int i, DataType *ptr)
{
    Node *p = first, *q = NULL; //需要额外定义一个q,表示要删除的结点,用于释放
    int count = 0;
    while (p != NULL)
    {
        if (count == i - 1 && p->next != NULL)
        {
            q = p->next;
            *ptr = q->data;
            p->next = q->next;
            free(q);
            return 1;
        }
        count++;
        p = p->next;
    }
    printf("位置错误,删除失败\n");
    return 0;
}

//销毁单链表
void DestroyList(Node *first)
{
    Node *p = first;
    while (first != NULL)
    {
        first = first->next;
        free(p);
        p = first;
    }
}

int main()
{
    int r[5] = {1, 2, 3, 4, 5}, i, x;
    Node *first = NULL;
    first = TailCreateList(r, 5);
    printf("当前线性表的数据为:");
    PrintList(first);
    Insert(first, 2, 8);
    printf("执行插入操作后数据为:");
    PrintList(first);
    printf("当前线性表的长度为:%d\n", Length(first));
    printf("请输入查找的元素值:");
    scanf("%d", &x);
    i = Locate(first, x);
    if (0 == i)
        printf("查找失败\n");
    else
        printf("元素%d的位置为:%d\n", x, i);
    printf("请输入查找第几个元素值:");
    scanf("%d", &i);
    if (1 == Get(first, i, &x))
        printf("第%d个元素值是%d\n", i, x);
    else
        printf("线性表中没有第%d个元素\n", i);
    printf("请输入要删除第几个元素:");
    scanf("%d", &i);
    if (Delete(first, i, &x) == 1)
    {
        printf("删除第%d个元素是%d,删除后数据为:", i, x);
        PrintList(first);
    }
    else
        printf("删除操作失败\n");
    DestroyList(first); //与顺序表不一样,静态分配的顺序表会自动被释放,而链表需要手动释放
    return 0;
}

总结

这个系列为我复习《数据结构》的学习记录过程,本篇介绍单链表的基本操作,范例程序基本和顺序表一致,不过需要注意的是静态分配存储的顺序表不需要手动释放内存空间,而单链表需要使用free进行释放。(理解指针程度++)

你可能感兴趣的:(数据结构,c语言,链表)