总是看别人些链表,今天自己尝试点简单的链表程序,简单的链表的增删查改
#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
|