实现链表的增删改查
ownlinklist.cpp
#include<iostream>
#include "include/ownlinklist.h"
#include <cstdlib>
using namespace std;
node* init()
{
return new node;
}
node* get_elment(node *_list,int index)
{
int i = 0;
node *p = _list;
while(i<=index && p->next)
{
p = p->next;
i++;
}
if(i!=index+1 || index<0)
{
cout<<"index out of bounds while get element !"<<endl;
exit(0);
}
return p;
}
void add_node_to_tail(node *_list,node *_node)
{
node *p = _list;
if(!p->next)
{
p->next = _node;
return;
}
while(p->next)
{
p = p->next;
}
p->next = _node;
}
void insert_node(node *_list,node *_node,int index)
{
node *p = _list;
if(index < 0)
{
cout<<"insert failed,index must begins with 0"<<endl;
return;
}
int i = 0;
while(i<index && p->next)
{
p = p->next;
i++;
}
if(i>index)
{
cout<<"index out of bounds !"<<endl;
exit(0);
}
node *temp = p->next;
p->next = _node;
_node->next = temp;
}
void delete_node(node *_list,int index)
{
if(index < 0)
{
cout<<"delete failed,index must begins with 0"<<endl;
return;
}
node *p = _list;
if(!p->next)
{
cout<<"there's no element more !"<<endl;
return;
}
int i = 0;
while(i<index && p->next)
{
p = p->next;
i++;
}
if(!p->next->next && index != 0)
{
p->next = nullptr;
return;
}
p->next = p->next->next;
}
void update_node(node *node,int data)
{
node->data = data;
}
void update_node(node *_list,int index,int data)
{
node *p = _list;
if(!p->next)
{
cout<<"there's no element to update !"<<endl;
return;
}
int i = 0;
while(i<=index && p->next)
{
p = p->next;
i++;
}
p->data = data;
}
int len_of_list(node *_list)
{
node *p = _list;
int i = 0;
while(p->next)
{
p = p->next;
i++;
}
return i;
}
void print_list(node *_list)
{
cout<<"------\tbegin print the list\t------ "<<endl;
node *p = _list;
if(!p->next)
{
cout<<"the list is null !"<<endl;
}
while(p->next)
{
p = p->next;
cout<<p->data<<endl;
}
cout<<"------\tprint the list end\t------"<<endl;
}
ownlinklist.h
#ifndef OWNLINKLIST_H_INCLUDED
#define OWNLINKLIST_H_INCLUDED
struct node
{
int data;
node *next;
};
node* init();//返回头指针
node* get_elment(node *_list,int index);//获取某个节点
void add_node_to_tail(node *_list,node *_node);//添加一个节点到链表尾
void insert_node(node *_list,node *_node,int index);//插入节点
void delete_node(node *_list,int index);//删除节点
void update_node(node *node,int data);//根据节点修改值
void update_node(node *_list,int index,int data);//根据序号修改值
int len_of_list(node *_list);//链表长度
void print_list(node *_list);//打印链表
#endif // OWNLINKLIST_H_INCLUDED
main.cpp
#include <iostream>
#include "include/ownlinklist.h"
using namespace std;
int main()
{
node *_list = init();
for(int i=1;i<=10;i++)
{
node *n = new node{i,nullptr};
add_node_to_tail(_list,n);
}
print_list(_list);
node *_node = get_elment(_list,1);
cout<<"this node is "<<_node->data<<endl;
update_node(_node,33);
print_list(_list);
node *n1 = new node{22,nullptr};
insert_node(_list,n1,1);
print_list(_list);
delete_node(_list,-1);
print_list(_list);
update_node(_list,1,44);
print_list(_list);
cout<<"the list length is "<<len_of_list(_list)<<endl;
return 0;
}
result
------ begin print the list ------
1
2
3
4
5
6
7
8
9
10
------ print the list end ------
this node is 2
------ begin print the list ------
1
33
3
4
5
6
7
8
9
10
------ print the list end ------
------ begin print the list ------
1
22
33
3
4
5
6
7
8
9
10
------ print the list end ------
delete failed,index must begins with 0
------ begin print the list ------
1
22
33
3
4
5
6
7
8
9
10
------ print the list end ------
------ begin print the list ------
1
44
33
3
4
5
6
7
8
9
10
------ print the list end ------
the list length is 11
Process returned 0 (0x0) execution time : 0.004 s
Press ENTER to continue.