单向链表浅析(小学生都能看懂)

本文详细介绍了链表的基础知识,包括如何创建链表、向链表中添加节点以及删除节点。通过示例代码展示了C++实现链表的操作,包括初始化头结点、动态添加节点到链表尾部以及输出链表内容。此外,还提供了一个锯齿矩阵问题的解决方案,进一步说明链表在实际问题中的应用。
摘要由CSDN通过智能技术生成

个人理解:

某种程度上可以说,链表就是结构体。所以学链表之前先去学结构体

这个结构体包含我们想记录的数据,还包含一个指针,指向下一个结构体,如此反复套娃,直到这个指针最终指向空,这一堆结构体便被称为链表。

一.创建链表

我们只需要把它的头搞出来就行。

首先是创建一个结构体

#include<bits/stdc++.h>
using namespace std;
typedef struct listnode{
    int num;
    struct listnode *next;
}mynode;

然后创个头

int main()
{
    mynode *head;
    mynode *list;
    mynode *temp;
	head=(mynode*)malloc(sizeof(mynode));
    head->next=NULL;//创建头结点
    list=(mynode*)malloc(sizeof(mynode));
    list=head;//给咱的链表装个头上去
}

二.链表添加节点 

for(int i=0;i<5;i++)
{
    int x;
    cin>>x;
    temp=(mynode*)malloc(sizeof(mynode));
    temp->num=x;
    temp->next=NULL;
    list->next=temp;
    list=temp;
}

三.链表输出

temp=head->next;
    if(temp->next==NULL)
    	cout<<endl;
    else
        {
            while(temp!=NULL)
            {
                cout<<temp->num<<" ";
                temp=temp->next;
            }
            cout<<endl;
    	}

完整代码

#include<bits/stdc++.h>
using namespace std;
typedef struct listnode{
    int num;
    struct listnode *next;
}mynode;
int main()
{
    mynode *head;
    mynode *list;
    mynode *temp;
	head=(mynode*)malloc(sizeof(mynode));
    head->next=NULL;//创建头结点
    list=(mynode*)malloc(sizeof(mynode));
    list=head;//给咱的链表装个头上去
    for(int i=0;i<5;i++)
    {
    int x;
    cin>>x;
    temp=(mynode*)malloc(sizeof(mynode));
    temp->num=x;
    temp->next=NULL;
    list->next=temp;
    list=temp;
	}
    temp=head->next;
    if(temp->next==NULL)
    	cout<<endl;
    else
        {
            while(temp!=NULL)
            {
                cout<<temp->num<<" ";
                temp=temp->next;
            }
            cout<<endl;
    	}
}

四.删除节点

 让指向要删除节点的指针指向要删除节点的下一个节点

例题:

锯齿矩阵是指每一行包含的元素个数不相同的矩阵,比如:

1

3 5 2 6 1

2

2 3 4

3

1 6 2 7

初始时矩阵为空,读入 mm 对整数 (x,y)(x,y),表示在第 xx 行的末尾加上一个元素 yy。

输出最终的锯齿矩阵。

输入格式

第一行输入两个整数 n,m\ (1 \leq n,m \leq 10000)n,m (1≤n,m≤10000),其中 nn 表示锯齿数组的行数,mm 表示插入的元素总数。

接下来一共 mm 行,每行两个整数 x,y\ (1 \leq x \leq n, 0 \leq y \leq 10000)x,y (1≤x≤n,0≤y≤10000),表示在第 xx 行的末尾插入一个元素 yy。

输出格式

一共输出 nn 行,每行若干个用空格分隔的整数。如果某行没有任何元素,则输出一个空行

输出时每行末尾的多余空格,不影响答案正确性

样例输入复制

3 12
1 3
2 2
2 3
2 4
3 1
3 6
1 5
1 2
1 6
3 2
3 7
1 1

样例输出复制

3 5 2 6 1
2 3 4
1 6 2 7
#include<bits/stdc++.h>
using namespace std;
typedef struct ANode{
    int num;
    struct ANode *next;
}ArcNode;
int main()
{
    int n,m;
    cin>>n>>m;
    ArcNode *head[n];
    ArcNode *list[n];
    ArcNode *temp;
    for(int i=0;i<n;i++)
    {
        head[i]=(ArcNode*)malloc(sizeof(ArcNode));
        head[i]->next=NULL;//创建头结点
        list[i]=(ArcNode*)malloc(sizeof(ArcNode));
        list[i]=head[i];//给咱的链表装个头上去
    }
    for(int i=0;i<m;i++)
    {
        int x,y;
        cin>>x>>y;
        temp=(ArcNode*)malloc(sizeof(ArcNode));
        temp->num=y;
        temp->next=NULL;
        list[x-1]->next=temp;
        list[x-1]=temp;
    }
    for(int i=0;i<n;i++)
    {
        temp=head[i]->next;
        if(temp->next==NULL)
            cout<<endl;
        else
        {
            while(temp!=NULL)
            {
                cout<<temp->num<<" ";
                temp=temp->next;
            }
            cout<<endl;
        }
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嗯嗯你说的对

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值