当前位置: 首页 > news >正文

[Leetcode]单链表回顾

一.链表结构

二 .初始化链表

三.建立链表

3.1头插法建立单链表(输入的顺序为倒序)

void createListInsertionMethod(LinkList& head)
{
    node* s;
    int flag = 1;
    char ch;
    while(flag)
    {
        c = getchar();
        if(c != '$')
        {
            s = new node;
            s->ch = c;
            s->next = head->next;
            head->next = s;
        }
        else
        {
            flag = 0;
        }
    }
}   

3.2尾插法建立单链表(输入的顺序为顺序)

//尾插法建立单链表
void creatListTailInsertionMethod(LinkList& head)
{
    char ch;
    node* p,*q;
    int flag = 1;
    p = head;
    q = NULL;
    while (flag)
    {
        ch = getchar();        
        if (ch != '$')
        {
            q = new node;
            p->next = q;
            q->ch = ch;
            p = q;
            q->next = NULL;
        }
        else
        {
            flag = 0;
        }
    }
}

四.求链表的长度

//求出链表的长度
int sizeofList(LinkList head)
{
    if (head != NULL)
    {
        int size = 1;
        node* p = head->next;
        while (p != NULL)
        {
            size++;
            p = p->next;
        }
        return size - 1;
    }
    return 0;
}

五.输出链表的内容

//输出链表的内容
void outputListContent(LinkList head) 
{
    cout << "The output of List is: ";
    node* p = head->next; // 跳过头节点,从第一个实际节点开始
    if (p == NULL) 
    {
        cout << "The list is empty." << endl;
        return;
    }

    while (p != NULL) 
    {
        cout << p->ch << " ";
        p = p->next;
    }
    cout << endl; // 在输出结束后换行
}

六.查找链表中的第i个内容

//查找单链表中的第i个节点
void searchByIndex(LinkList head, int index)
{
    if (head != NULL)
    {
        int size = sizeofList(head);
        if (index > size)
        {
            cout << "输入的index不合法。" << endl;
            return;
        }
        node* p = new node;
        p = head->next;
        for (int i = 1;i < index;i++)
        {
            p = p->next;
        }
        cout << "The content of " << index << " is :" << p->ch << endl;
    }
    else
    {
        cout << "链表为NULL" << endl;
        return;
    }
    
}


七.按照值查找

//按照值查找
void searchByContent(const LinkList head, char ch,node*& aim)
{
    if (head != NULL)
    {
        node* p = head->next;
        while (p->ch != ch)
        {
            if (p == NULL)
            {
                cout << "找不到ch" << endl;
                return;
            }
            p = p->next;
        }
        aim = p;
    }
    else
    {
        cout << "链表为NULL" << endl;
        return;
    }
}

八.单链表插入操作

//单链表插入操作
void insert(LinkList& head, int index, char ch)
{
    if (head != NULL)
    {
        int len = sizeofList(head);
        if (index > len)
        {
            cout << "插入操作的index输入不合法" << endl;
            return;
        }
        else
        {
            int i = 1;
            node* p = head->next;
            while (i != index - 1)
            {
                p = p->next;
                i++;
            }

            node* s = new node;
            s->next = p->next;
            p->next = s;
            s->ch = ch;
        }
    }
}

九.单链表删除操作

//单链表删除操作
bool delList(LinkList& head,int index)
{
    if (head != NULL)
    {
        int len = sizeofList(head);
        if (len < index)
        {
            cout << "删除操作的Index输入不合法" << endl;
            return false;
        }
        int i = 1;
        node* p = head->next;
        while (i != index - 1)
        {
            p = p->next;
            i++;
        }
        node* q = p->next;
        p->next = q->next;
        return true;
    }
    else
    {
        cout << "删除操作的输入链表为NULL" << endl;
        return false;
    }
}

十.Coding

#include<iostream>

using namespace std;

typedef struct node
{
    char ch;
    node* next;
}*LinkList,Node;

//初始化单链表
void initList(LinkList& head)
{
    head = new node;
    head->next = NULL;
}

/*头插法建立单链表
void createListInsertionMethod(LinkList& head)
{
    node* s;
    int flag = 1;
    char ch;
    while(flag)
    {
        c = getchar();
        if(c != '$')
        {
            s = new node;
            s->ch = ch;
            s->next = head->next;
            head->next = s;
        }
        else
        {
            flag = 0;
        }
    }
}   
*/

//尾插法建立单链表
void creatListTailInsertionMethod(LinkList& head)
{
    char ch;
    node* p,*q;
    int flag = 1;
    p = head;
    q = NULL;
    while (flag)
    {
        ch = getchar();        
        if (ch != '$')
        {
            q = new node;
            p->next = q;
            q->ch = ch;
            p = q;
            q->next = NULL;
        }
        else
        {
            flag = 0;
        }
    }
}

//输出链表的内容
void outputListContent(LinkList head) 
{
    cout << "The output of List is: ";
    node* p = head->next; // 跳过头节点,从第一个实际节点开始
    if (p == NULL) 
    {
        cout << "The list is empty." << endl;
        return;
    }

    while (p != NULL) 
    {
        cout << p->ch << " ";
        p = p->next;
    }
    cout << endl; // 在输出结束后换行
}

//求出链表的长度
int sizeofList(LinkList head)
{
    if (head != NULL)
    {
        int size = 1;
        node* p = head->next;
        while (p != NULL)
        {
            size++;
            p = p->next;
        }
        return size - 1;
    }
    return 0;
}

//查找单链表中的第i个节点
void searchByIndex(LinkList head, int index)
{
    if (head != NULL)
    {
        int size = sizeofList(head);
        if (index > size)
        {
            cout << "输入的index不合法。" << endl;
            return;
        }
        node* p = new node;
        p = head->next;
        for (int i = 1;i < index;i++)
        {
            p = p->next;
        }
        cout << "The content of " << index << " is :" << p->ch << endl;
    }
    else
    {
        cout << "链表为NULL" << endl;
        return;
    }
    
}

//按照值查找
void searchByContent(const LinkList head, char ch,node*& aim)
{
    if (head != NULL)
    {
        node* p = head->next;
        while (p->ch != ch)
        {
            if (p == NULL)
            {
                cout << "找不到ch" << endl;
                return;
            }
            p = p->next;
        }
        aim = p;
    }
    else
    {
        cout << "链表为NULL" << endl;
        return;
    }
}

//单链表插入操作
void insert(LinkList& head, int index, char ch)
{
    if (head != NULL)
    {
        int len = sizeofList(head);
        if (index > len)
        {
            cout << "插入操作的index输入不合法" << endl;
            return;
        }
        else
        {
            int i = 1;
            node* p = head->next;
            while (i != index - 1)
            {
                p = p->next;
                i++;
            }

            node* s = new node;
            s->next = p->next;
            p->next = s;
            s->ch = ch;
        }
    }
}

//单链表删除操作
bool delList(LinkList& head,int index)
{
    if (head != NULL)
    {
        int len = sizeofList(head);
        if (len < index)
        {
            cout << "删除操作的Index输入不合法" << endl;
            return false;
        }
        int i = 1;
        node* p = head->next;
        while (i != index - 1)
        {
            p = p->next;
            i++;
        }
        node* q = p->next;
        p->next = q->next;
        return true;
    }
    else
    {
        cout << "删除操作的输入链表为NULL" << endl;
        return false;
    }
}

int main()
{
    //input:
    //dsaf$
    LinkList head;
    initList(head);
    creatListTailInsertionMethod(head);
    outputListContent(head);
    cout << "The size of List is : " << sizeofList(head) << endl;
    int index = 3;
    cout << "链表第" << index << "个的内容是 : " << endl;
    searchByIndex(head, index);
    node* aim = NULL;
    searchByContent(head, 'a', aim);
    cout << "The aimed content of node is : " << aim->ch << endl;
    insert(head, 3, '%');
    outputListContent(head);
    cout << "The result of delete function is : " << delList(head, 3) << endl;
    outputListContent(head);
    return 0;
}

http://www.dtcms.com/a/92137.html

相关文章:

  • Linux系统中-cp命令/mv命令/rename命令/rm命令
  • JavaScript基础-BOM 概述
  • Rust vs. Go: 性能测试(2025)
  • 多态的原理
  • 个人学习编程(3-26) leetcode刷题
  • 三个串口同时打开并指定数据包控制指令思想
  • 高效内存管理:x86-64架构中的分页机制
  • RK3568 驱动和设备匹配的几种方法
  • 小区团购管理设计与实现(代码+数据库+LW)
  • Rust 与 FFmpeg 实现视频水印添加:技术解析与应用实践
  • AI作为学术评审专家有哪些优缺点?
  • Redis 常用数据结构及其对应的业务场景(总结)
  • R --- Error in library(***) : there is no package called ‘***’ (服务器非root用户)
  • 接口自动化进阶 —— Pytest全局配置pytest.ini文件详解!
  • 浏览器存储 IndexedDB
  • 蓝桥杯算法实战分享
  • CDN节点对网络安全扫描的影响:挑战与应对策略
  • 【Tauri2】004——run函数的简单介绍(2)
  • 【leetcode hot 100 84】柱状图中最大的矩形
  • LeetCode热题100题|1.两数之和,49.字母异位词分组
  • [WEB开发] Mybatis
  • CSP历年题解
  • Android 启动流程详解:从上电到桌面的全流程解析
  • Netty源码—7.ByteBuf原理四
  • K8s证书--运维之最佳选择(K8s Certificate - the best Choice for Operation and Maintenance)
  • 主键id设计
  • 华为OD机试A卷 - 积木最远距离(C++ Java JavaScript Python )
  • 文件描述符,它在哪里存的,exec()后还存在吗
  • 【STM32】对stm32F103VET6指南者原理图详解(超详细)
  • 支付页面安全与E-Skimming防护----浅谈PCI DSS v4.0.1要求6.4.3与11.6.1的实施