链表基础功能应用
/*链表基础功能应用*/
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>
using namespace std;
//构建数据收纳盒(存储数据)
struct data
{
int number;
string name;
string sex;
};
//构建抽屉(链表节点)
struct listpoint
{
data * infor;
listpoint * last;
listpoint * next;
};
//创建一个基础链表
listpoint * creat_normal_list(int n)
{
listpoint * head , * normal , * end; /*创建 头、中间、尾 节点 */
//分配内存
head = new listpoint;
head -> infor = new data;
end = head; /*刚开始头节点 与 尾节点 相同,⚠️ 头节点是不存放东西的*/
for(int i = 0; i < n; i ++)
{
normal = new listpoint;
normal -> infor = new data;
//往新申请的节点输入数据
cout<<"input the number"<<endl;
cin>>normal -> infor -> number;
cout<<"input the name"<<endl;
cin>>normal -> infor -> name;
cout<<"input the sex"<<endl;
cin>>normal -> infor -> sex;
//中间节点与尾节点 关系转换
end -> next = normal; //往end后添加新节点
normal -> last = end; //新节点的上一个节点就是 end
end = normal; //最后一个节点变成新节点,end 只是用来记录链表的最后一个节点的
}
head -> last = NULL; //链表的头节点 head 之前已经没有节点了
end -> next = NULL; //链表的尾节点 end 之后也没有节点了
return head;
}
//创建环形双向链表(与基础链表相同只不过 头尾指针 相等)
listpoint * creat_loop_list(int n)
{
listpoint * head, * normal , * end;
head = new listpoint;
head -> infor = new data;
end = head;
for(int i = 0; i < n; i ++)
{
normal = new listpoint;
normal -> infor = new data;
cout<<"input the number"<<endl;
cin>>normal -> infor -> number;
cout<<"input the name"<<endl;
cin>>normal -> infor -> name;
cout<<"input the sex"<<endl;
cin>>normal -> infor -> sex;
end -> next = normal;
normal -> last = end;
end = normal; //记录尾节点
}
//把链表首尾相连
head -> last = end;
end -> next = head;
return head;
}
//查找第n个节点
listpoint * search_point(listpoint * list,int n)
{
listpoint * p = list;
for(int i = 0; i < n; i ++)
p = p -> next;
return p;
}
//修改节点存储信息
void change_point(listpoint * list ,int n,data * information)
{
listpoint * p = list;
for(int i = 0; i < n; i ++)
{
p = p -> next;
}
p -> infor = information;
}
//删除某个节点
void delete_point(listpoint * list , int n)
{
listpoint * p = list;
for(int i = 0; i < n; i ++)
{
p = p -> next;
}
p -> last -> next = p -> next;
p -> next -> last = p -> last;
delete p;
}
//插入某个节点
void insert_point(listpoint * list,int n,data * information)
{
listpoint * p = list;
for(int i = 0; i < n; i ++)
p = p -> next;
listpoint * insertpoint = new listpoint;
insertpoint -> infor = new data;
insertpoint -> infor = information;
//在第n个节点这个位置插入新节点,而原来n这个节点及以后的节点,均往后退一位
p -> last -> next = insertpoint;
insertpoint -> last = p -> last;
insertpoint -> next = p;
p -> last = insertpoint;
}
//输出单个节点的数据
void output_point(listpoint * point)
{
cout<<"the number is :"<<point->infor->number<<endl;
cout<<"the name is :"<<point->infor->name<<endl;
cout<<"the sex is :"<<point->infor->sex<<endl;
cout<<"----------------------------------"<<endl;
}
//输出整个链表的数据(该函数不适合 环链表)
void output_list(listpoint * list)
{
listpoint * p = list;
while(p -> next != NULL)
{
output_point(p -> next);
p = p -> next;
}
}
//输出链表 m 到 n节点 之间的数据
void output_part_list(listpoint * list,int m,int n)
{
listpoint * p = list;
for(int i = 1;i <= n; i ++)
{
p = p -> next;
if(i >= m)
{
output_point(p);
}
}
}
int main()
{
//freopen("test.txt","r",stdin);
int n;
cin>>n;
listpoint * list;
list = creat_normal_list(n);
output_list(list);
return 0;
}
如看不懂可参考别人的博客