链表新体验

总是看别人些链表,今天自己尝试点简单的链表程序,简单的链表的增删查改

#include <QCoreApplication>
#include <QDebug>

struct node
{
    int value ;
    node *next ;
} ;

void printf(node *root)
{
    while (root!= nullptr)
    {
        qDebug() << root->value ;
        root = root->next ;
    }
}

void insert(node *root ,int i  ,int value )
{
    for (int i_t = 0 ; i_t < i ; i_t++)
    {
        root = root->next ;
    }
    node *node_tt = root->next ;
    node *node_t = new node ;
    node_t->value = value ;
    node_t->next =  nullptr ;
    root->next = node_t ;
    node_t->next = node_tt ;

}

void front_insert(node *root ,int i , int value)
{
    for (int i_t = 0 ; i_t < i-1 ; i_t++ )
    {
        root = root->next ;
    }

    node *node_t = new node ;
    node_t->value = value ;
    node_t->next = root->next ;
    root->next = node_t ;
}

void del (node *root , int i)
{
    for (int i_t = 0 ; i_t < i ; i_t++)
    {
        root = root->next ;
    }
    root->next = root->next->next ;
}

int find (node *root , int data)
{
    int index = 0 ;
    while (root)
    {
        if (root->value == data)
            return index ;
        root = root->next ;
        index ++ ;
    }
    return -1 ;
}

int node_size(node *root )
{
    int length = 0 ;
    while (root)
    {
        length ++ ;
        root = root->next ;
    }
    return length ;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    node *root = new node ;
    root->value = 0 ;
    root->next = nullptr ;
    node *no = root ;
    for (int i = 0 ; i < 15 ; i++ )
    {
        node *node_t = new node ;
        node_t->value = i ;
        node_t->next = nullptr ;
        no->next = node_t ;
        no = no->next ;
    }

    printf(root->next) ;
    insert(root ,10 ,11);
    printf(root->next) ;
    del(root ,10) ;
    printf(root->next) ;
    int index = find (root->next ,5) ;
    qDebug() << index ;
    int length = node_size(root->next) ;
    qDebug() << length ;
    front_insert(root->next,5,10) ;
    printf(root->next) ;
    return a.exec();
}
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
0
1
2
3
4
5
6
7
8
9
11
10
11
12
13
14
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
5
15
0
1
2
3
4
10
5
6
7
8
9
10
11
12
13
14

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值