2020 7 14 每日总结

今天完成了下学期的的第九个项目,链表实在没有学过,只能自学,一天下来确实没学到什么东西。明天一早坐火车,做一天多,不敲代码。

#include<bits/stdc++.h>
using namespace std;
list<int> a;
bool flag = 0;
class link
{
public:
void input()
{
cin>>xx;
}
void ergodic();
void menu();
void inset();
void del();
void query();
private:
int xx;
};
void link::ergodic()//创建
{
link aa;
int n;
cout<<"请输入创建的整数链表的数据个数:"<<endl;
begin2:
cin>>n;
if(n<=0)
{
cout<<"输入有误,请重新输入!"<<endl;
goto begin2;
}
else
{
cout<<"请依次输入这几个数,用空格隔开!"<<endl;
for(int i =0;i<n;++i)
{
aa.input();
a.push_back(aa.xx);
}
flag=1;
cout<<"链表创建成功!"<<endl<<"内容如下:"<<endl;

list<int>::iterator it = a.begin();
while(it != a.end())
{
cout<<*it<< " ";
it++;
}
cout<<endl;
cout<<"返回主菜单成功!请继续操作!"<<endl;
}
}
void link::inset()//插入
{
if(flag)
{
link aaa;
int p;
begin3:
cout<<"请选择你要插入的位置"<<endl;
cout<<"1 链表头部"<<endl;
cout<<"2 链表尾部"<<endl;
cout<<"3 链表中间某个位置"<<endl;
cin>>p;
if(p==1)
{
cout<<"请输入你要插入的元素的值"<<endl;
aaa.input();
a.push_front(aaa.xx);
}
if(p==2)
{
cout<<"请输入你要插入的元素的值"<<endl;
aaa.input();
a.push_back(aaa.xx);
}
if(p==3)
{
int n,i;
cout<<"请输入你要插入的位置:"<<endl;
cin>>n;
n--;
cout<<"请输入你要插入的元素的值"<<endl;
aaa.input();
list<int>::iterator j = a.begin();
for(i = 0; i < n && j!= a.end(); ++i)
++j;
a.insert(j,1,aaa.xx);
}
if(p!=3&&p!=2&&p!=1)
{
cout<<"输入有误,请重新输入!"<<endl;
goto begin3;
}
cout<<"插入链表成功!"<<endl<<"内容如下:"<<endl;

list<int>::iterator it = a.begin();
while(it != a.end())
{
cout<<*it<< " ";
it++;
}
cout<<endl;
cout<<"返回主菜单成功!请继续操作!"<<endl;
}
else
{
cout<<"您还没有创建链表!"<<"已返回主菜单!"<<endl;
}
}
void link::del()//删除
{
if(flag)
{
int x;
begin4:
cout<<"请选择:"<<endl;
cout<<"1 清空全部"<<endl;
cout<<"2 删除一个"<<endl;
cin>>x;
if(x==1)
{
a.clear();
//flag=0;
cout<<"清空成功!";
}
else if(x==2)
{
int w;
cout<<"请输入你要删除的元素的值:"<<endl;
cin>>w;
a.remove(w);
cout<<"删除成功!";
}
else if(x!=1&&x!=2)
{
cout<<"输入有误,请重新输入!"<<endl;
goto begin4;
}
cout<<"内容如下:"<<endl;

list<int>::iterator it = a.begin();
while(it != a.end())
{
cout<<*it<< " ";
it++;
}
cout<<endl;
cout<<"返回主菜单成功!请继续操作!"<<endl;
}
else
{
cout<<"您还没有创建链表!"<<"已返回主菜单!"<<endl;
}
}
void link::query()
{
if(flag)
{
int q,z=0;
cout<<"请输入您要查找的元素的值:"<<endl;
cin>>q;
list<int>::iterator it1 = a.begin();
while(it1 != a.end())
{
z++;
if(*it1==q)
{
cout<<*it1<< " "<<"在链表第"<<z<<"位";
goto begin6;
}

it1++;
}
cout<<"没有找到该元素!"<<endl;
begin6:
cout<<" 具体内容如下:"<<endl;

list<int>::iterator it = a.begin();
while(it != a.end())
{
cout<<*it<< " ";
it++;
}
cout<<endl;
cout<<"返回主菜单成功!请继续操作!"<<endl;
}
else
{
cout<<"您还没有创建链表!"<<"已返回主菜单!"<<endl;
}

}
void menu()
{
cout<<"1 创建"<<endl;
cout<<"2 插入"<<endl;
cout<<"3 删除"<<endl;
cout<<"4 查询和遍历"<<endl;
cout<<"5 退出"<<endl;
}
int main()
{
link p;
int xx;
cout<<"欢迎使用整数链表!"<<endl;
for(;;)
{
begin1:
menu();
cin>>xx;
if(xx==1)
{
p.ergodic();

}
if(xx==2)
{
p.inset();
}
if(xx==3)
{
p.del();
}
if(xx==4)
{
p.query();
}
if(xx==5)
{
cout<<"感谢使用!"<<endl;
break;
}
if(xx!=1&&xx!=2&&xx!=3&&xx!=4&&xx!=5)
{
cout<<"输入有误,请重新输入!"<<endl;
goto begin1;
}
}

return 0;
}

原文地址:https://www.cnblogs.com/fuxw4971/p/13337244.html