单链表

单链表:单链表是一种连式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象)+指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每一个结点的地址数据。

typedef struct Node
{
int data;
struct Node *next;
}Node,*List;
void InitList(List plist);//初始化单链表
bool Insert_head(List plist,int val);//头插法
bool Insert_tail(List plist,int val);//尾插法
bool Insert_pos(List plist,int pos,int val);//pos 位置插入
Node *Search_pre(List plist,int key);//查找 key 的前驱
bool Delete(List plist,int key);//删除 key 这个结点
bool IsEmpty(List plist);//是否为空
void Destroy(List plist);//摧毁函数(如果有动态开辟内存)
int GetLength(List plist);//得到单链表的长度
void Show(List plist);//打印单链表


代码:
void InitList(List plist)
{
    assert(plist != NULL);
    if(plist == NULL)
    {
        return;
    }
        plist->next = NULL;
static Node*GetNode(int val)
{
    Node *p = (Node*)malloc(sizeof(Node));
    assert( p != NULL);
    p->data = val;
    p->next = NULL;
    return p;
}
bool Insert_head(List plist,int val)
{
    assert(plist != NULL);
    Node *p = GetNode(val);
    p->next = plist->next;
    plist->next = p;
    return true;
}   
bool Insert_tail(List plist,int val)
{
    assert(plist != NULL);
    Node *p = plist;
    while(p->next != NULL)
    {
        p = p->next;
    }
    Node *pGet = GetNode(val);
    p->next = pGet;
    return true;
 }
bool Insert_pos(List plist,int pos,int val)
{
    assert(plist != NULL);
    Node *p = plist;
    if(pos < 0 || pos > GetLength(plist))
    {
        return false;
    }
    for(int i = 0;i <= pos - 1;i++)
    {
        p = p->next;
    }
    Node *pGet = GetNode(val);
    pGet->next = p->next;
    p->next = pGet;
    return true;
}
Node *Search_pre(List plist,int key)//查找key 的前驱
{
    assert(plist != NULL);
    Node *p;
    for(p = plist;p->next != NULL;p = p->next)
    {
        if(p->next->data == key)
        {
            return p;
        }
    }
    return NULL;
}
bool Delete(List plist,int key)
{
    assert(plist != NULL);
    Node *p = Search_pre(plist,key);
    assert(p != NULL);
    Node *q = p->next;
    p->next = q->next;
    free(q);
    q = NULL;
    return true;
}
bool isEmpty(List plist)
{
    if(plist->next == NULL)
    {
        return true;
    }
    return false;
}
void Destroy(List plist)
{
    assert(plist != NULL);
    Node *p;
    for(p = plist->next;p->next != NULL;p = p->next)
    {
        free(p);
        p = NULL;
    }
}
int GetLength(List plist)
{
    assert(plist != NULL);
    int count = 0;
    for(Node *p = plist;p->next != NULL;p = p->next)
    {
        count++;
    }
    return count;
}
void Show(List plist)
{
    assert(plist != NULL);
    Node *p;
    for(p = plist->next;p != NULL;p = p->next)
    {
        printf("%d ",p->data);
    }
    printf("\n");
}

测试:
#include<stdio.h>
#include"List.h"

int main()
{
    Node head;
    InitList(&head);
    for(int i = 0;i <= 9;i++)
    {
        Insert_tail(&head,i);
    }
    Show(&head);
    Insert_tail(&head,34);
    Show(&head);
    Insert_pos(&head,3,24);
    Show(&head);
    Node *tmp = Search_pre(&head,8);
    printf("%d\n ",tmp->data);
    Delete(&head,5);
    Show(&head);
    int len = GetLength(&head);
    printf("%d ",len);
    return 0;
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值