博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
链表的相关操作
阅读量:2242 次
发布时间:2019-05-09

本文共 4495 字,大约阅读时间需要 14 分钟。

定义

#include 
#include
#include
typedef int DataType;

定义结构体

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

初始化

void SListInit(Node **ppfirst)    {        assert(ppfirst);        *ppfirst = NULL;    }

销毁

void SListDestory(Node **ppfirst)    {        assert(ppfirst);        Node *cur, *next;        for (cur = *ppfirst; cur != NULL; cur = next)        {            next = cur->next;            free(cur);        }    }

注:销毁链表时一定要把每一个结点都释放

插入

头插
static Node *CreateNewNode(DataType data)    {        Node *node = (Node *)malloc(sizeof(Node));        node->data = data;        node->next = NULL;        return node;    }    void SListPushFront(Node **ppfirst, DataType data)    {        assert(ppfirst);        Node *newNode = CreateNewNode(data);        newNode->next = *ppfirst;        *ppfirst = newNode;    }
尾插
void SListPushBack(Node **ppfirst, DataType data)    {        assert(ppfirst);        Node *newNode = CreateNewNode(data);        if (*ppfirst == NULL)        {            *ppfirst = newNode;            return;        }        Node *cur = *ppfirst;        while (cur->next != NULL)        {            cur = cur->next;        }        cur->next = newNode;    }
随机插入(在结点pos前面插入)
void SListInsert(Node **ppfirst, Node *pos, DataType data)    {        assert(ppfirst);        Node *newNode = CreateNewNode(data);        if (pos == *ppfirst)        {            SListPushFront(ppfirst, data);            return;        }        Node *cur = *ppfirst;        while (cur->next != pos)        {            cur = cur->next;        }        newNode->next = pos;        cur->next = newNode;    }

删除

头删
void SListPopFront(Node **ppfirst)    {        assert(ppfirst); //断言不是空指针        assert(*ppfirst); //断言不是空链表        Node *first = *ppfirst;        *ppfirst = (*ppfirst)->next;        free(first);    }
尾删
void SListPopBack(Node **ppfirst)    {        assert(ppfirst);         assert(*ppfirst);         if ((*ppfirst)->next == NULL)        {            free(*ppfirst);            *ppfirst = NULL;            return;        }        Node *cur = *ppfirst;        while (cur->next->next != NULL)        {            cur = cur->next;        }        free(cur->next);        cur->next = NULL;    }
随机删除(删除pos结点)
void SListRemove(Node **ppfirst, Node *pos)    {        assert(ppfirst);        assert(*ppfirst);        if (*ppfirst == pos)        {            SListPopFront(ppfirst);            return;        }        Node *cur = *ppfirst;        while (cur->next != pos)        {            cur = cur->next;        }        cur->next = pos->next;        free(pos);    }

查找

Node *SListFind(Node **ppfirst, DataType data)    {        assert(ppfirst);        Node *cur = *ppfirst;        while (cur->data != data || cur->next != NULL)        {            cur = cur->next;        }        if (cur->data == data)        {            return cur;        }        return NULL; //找不到返回NULL    }

删除(第一个数值为data的结点)

void SListRemoveFirst(Node **ppfirst, DataType data)    {        assert(ppfirst);        assert(*ppfirst);        Node *cur = *ppfirst;        Node *node = *ppfirst; //记录cur的前一个        while (cur->data != data && cur->next !=  NULL)        {            cur = cur->next;        }        if (cur->next == NULL)        {            printf("不存在\n");            return;        }        if (cur == *ppfirst)        {            SListPopFront(ppfirst);            return;        }        while (node->next != cur)        {            node = node->next;        }        node->next = cur->next;        free(cur);    }

删除(所有值为data的结点)

void SListRemoveAll(Node **ppfirst, DataType data)    {        assert(ppfirst);        assert(*ppfirst);        Node *cur = *ppfirst;        Node *node = *ppfirst;        while (cur->next != NULL)        {            node = *ppfirst;            if (cur->data == data)            {                if (cur == *ppfirst)                {                    cur = cur->next;                    SListPopFront(ppfirst);                    continue;                }                while (node->next != cur)                {                    node = node->next;                }                node->next = cur->next;                free(cur);                cur = node->next;                continue;            }            cur = cur->next;        }        if (cur == *ppfirst &&  cur->data == data)        {            SListPopFront(ppfirst);        }    }

注:在链表中,插入元素一定要先申请空间,删除元素一个要记得释放

转载地址:http://gbwdb.baihongyu.com/

你可能感兴趣的文章
SpringMVC学习笔记2
查看>>
Oracle知识点连载(一)
查看>>
Oracle知识点连载(二)
查看>>
Oracle知识点连载(三)
查看>>
Oracle知识点连载(五)
查看>>
关于三元运算符的类型转换问题
查看>>
笔记本怎么设置WIfi热点
查看>>
如何实现字符串的反转及替换?
查看>>
Java面试题全集(上)
查看>>
Java面试题全集(中)
查看>>
值传递和引用传递
查看>>
什么情况下用+运算符进行字符串连接比调用StringBuilder对象的append方法连接字符串性能更好?
查看>>
怎么根据Comparable方法中的compareTo方法的返回值的正负 判断升序 还是 降序?
查看>>
理解事务的4种隔离级别
查看>>
常用正则匹配符号
查看>>
建议42: 让工具类不可实例化
查看>>
Java 异步机制与同步机制的区别
查看>>
hibernate的对象三种状态说明
查看>>
什么是N+1查询?
查看>>
Spring 接管 Hibernate 配置 延迟加载
查看>>