当前位置: 首页> 游戏> 单机 > 吉林省干部网络培训_桂林网丫网业管理有限公司_排名优化方案_个人怎么接外贸订单

吉林省干部网络培训_桂林网丫网业管理有限公司_排名优化方案_个人怎么接外贸订单

时间:2025/7/9 8:20:51来源:https://blog.csdn.net/qq_74776071/article/details/146536068 浏览次数:0次
吉林省干部网络培训_桂林网丫网业管理有限公司_排名优化方案_个人怎么接外贸订单

一.链表结构

二 .初始化链表

三.建立链表

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;
}

关键字:吉林省干部网络培训_桂林网丫网业管理有限公司_排名优化方案_个人怎么接外贸订单

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: