64,649
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<algorithm>
using namespace std;
/*
注释:双端队列Deque的功能函数仿照队列queue的函数实现
以下列出与queue对应的Deque的功能函数
queue Deque
push(x) 将x入队 push_front(x) 将x压入队首
push_back(x) 将x压入队尾
front() 返回队首元素 front() 返回队首元素
back() 返回队尾元素 back() 返回队尾元素
pop() 将队首元素弹出 pop_front() 将队首元素弹出
pop_back() 将队尾元素弹出
empty() 判空 empty() 判空
size() 返回队列内元素个数 size() 返回队列内元素个数
因此此程序Deque类内的功能函数为:
push_front(x), push_back(x), front(), back(),pop_front(), pop_back(), empty(),size()
*/
template <typename T> class Node//结点类定义
{
public:
T data;
Node* next;
Node* pre;
Node(){next=pre=NULL;data=0;}
};
template <typename E> class Deque:public Node<E>//基于结点类的双端队列类定义
{
private:
Node<E>* head;
Node<E>* tail;
int length;
public:
Deque(){head->next=tail;tail->pre=head;head->pre=tail->next=NULL;length=0;}//创建空的单链表
~Deque(){};
void push_front(E x)//将元素x压入队首
{
Node<E>* head1=head->next;
Node<E>* newnode=new Node<E>;
newnode->data=x;
newnode->pre=head;
head1->pre=newnode;
newnode->next=head1;
head->next=newnode;
length++;
}
void push_back(E x)//将元素x压入队尾
{
Node<E>* tail1=tail->pre;
Node<E>* newnode=new Node<E>;
newnode->data=x;
newnode->next=tail;
tail1->next=newnode;
newnode->pre=tail1;
tail->pre=newnode;
length++;
}
E front()//返回队首元素
{
Node<E>* head1=head->next;
return head1->data;
}
E back()//返回队尾元素
{
Node<E>* tail1=tail->pre;
return tail1->data;
}
void pop_front()//队首元素出队
{
Node<E>* head1=head->next;
Node<E>* head2=head1->next;
head->next=head1->next;
head2->pre=head;
head1->next=head1->pre=NULL;
delete(head1);
length--;
}
void pop_back()//队尾元素出队
{
Node<E>* tail1=tail->pre;
Node<E>* tail2=tail1->pre;
tail->pre=tail1->pre;
tail2->next=tail;
tail1->pre=tail->next=NULL;
delete(tail1);
length--;
}
bool empty()//判空
{
return !length;
}
int size(){return length;}//返回当前双端队列长度
void output()
{
cout<<"当前双端队列为:";
Node<E>* head1=head->next;
while(head1!=tail)
{
cout<<head1->data<<" ";
head1=head1->next;
}
cout<<endl;
}
};
int main()
{
Deque<int> deque;
int n;
cout<<"输入初始时元素个数:"<<endl;
cin>>n;
int a[n];
cout<<"请按顺序输入各个元素:"<<endl;
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<n;i++)
deque.push_back(a[i]);
deque.output();
cout<<deque.front()<<endl;//输出队首元素
cout<<deque.back()<<endl;//输出队尾元素
int fr,ba;
cout<<"输入两个元素,分别压入队首、队尾";
cin>>fr;
deque.push_front(fr);//将fr压入队首
deque.output();
cin>>ba;
deque.push_back(ba);//将ba压入队尾
deque.output();
deque.pop_front();//将队首元素弹出
deque.output();
deque.pop_back();//将队尾元素弹出
deque.output();
cout<<"双端队列是否为空:"<<deque.empty();
cout<<"当前双端队列元素个数:"<<deque.size();
return 0;
}
#include<iostream>
#include<algorithm>
using namespace std;
template <typename T> class List//结点类定义
{
public:
T data;
List* next;
List(const T& data1){data=data1;next=NULL;}
List(){next=NULL;}
void changedata(const T& newdata){data=newdata;}
T getdata(){return data;}
};
template <typename E> class LList:public List<E>//单链表类定义
{
private:
List<E>* head;
int length;
public:
LList(){head->next=NULL;length=0;}//创建空的单链表
void lengthshort(){length--;}//长度-1
void lengthlong(){length++;}//长度+1
int getlength(){return length;}//返回当前单链表长度
void setlength(int x){length=x;}//设置单链表长度
List<E>* gethead(LList Llist){return Llist.head;}//返回头指针
List<E>* insert(int n,E list)//在位置n插入新结点list
{
int m=0;
List<E>* head1=head;
while(m!=n-1)
{
head=head->next;
m++;
}
List<E>* beinserted=head->next;
List<E>* getinsert=new List<E>;
getinsert->data=list;
getinsert->next=beinserted;
head->next=getinsert;
lengthlong();
head=head1;
return head;
}
List<E>* append(E list)//将新结点置于单链表结尾
{
List<E>* head1=head;
if(head->next==NULL)
{
List<E>* newone=new List<E>;
newone->data=list;
newone->next=NULL;
head->next=newone;
}
else
{
while(head->next!=NULL)
head=head->next;
List<E>* newone=new List<E>;
newone->data=list;
newone->next=NULL;
head->next=newone;
}
lengthlong();
head=head1;
return head;
}
List<E>* deleteposition(int m)//删除位置m的结点
{
if(m<1||m>getlength())
{
cout<<"删除位置有误,请重新输入:"<<endl;
int m;
cin>>m;
deleteposition(m);
}
if(m!=1)
{
int p=0;
List<E>* head1=head;
while(p!=m-1)
{
head1=head1->next;
p++;
}
List<E>* getdelete=head1->next;
head1->next=getdelete->next;
getdelete->next=NULL;
}
else
{
List<E>* newhead=head->next;
head->next=NULL;
head=newhead;
}
lengthshort();
return head;
}
List<E>* deletevalue(E m)//删除所有值为m的结点
{
int n=0;
List<E>* head1=head;
while(head1)
{
if(head1->data!=m)
n++;
else
{
deleteposition(n);
lengthshort();
}
head1=head1->next;
}
return head;
}
int getposition(E data1)//查找并返回第一个值为指定值data1结点的位置
{
int p=1;
List<E>* head1=this->head;
head=head->next;
while(head->data!=data1&&head!=NULL)
{
p++;
head=head->next;
}
head=head1;
if(p>getlength())
return -1;
else
return p;
}
E getvalue(int position,List<E>* head)//查找并返回位置position的值
{
int p=0;
List<E>* head1=head;
while(p!=position)
{
head1=head1->next;
p++;
}
return (head1->data);
}
List<E>* changeposition(int position,E newdata)//将位置position的元素的值修改为newdata
{
if(position<1||position>getlength())
{
cout<<"改值位置有误,请重新输入:"<<endl;
int position;
cin>>position;
changeposition(position,newdata);
}
int p=0;
List<E>* head1=head;
while(p!=position)
{
p++;
head1=head1->next;
}
head1->changedata(newdata);
return head;
}
List<E>* changevalue(E value,E newdata)//将单链表中所有值为value的结点的值修改为newdata
{
List<E>* head1=head;
while(head1!=NULL)
{
if(head1->data==value)
head1->changedata(newdata);
head1=head1->next;
}
return head;
}
void destroy(List<E>* head)//销毁单链表
{
if(head==NULL)
return ;
while(head)
{
List<E>* p=head->next;
delete(head);
head=p;
}
}
void clear(List<E>* head)//清空单链表
{
if(head==NULL);
else
{
List<E>* p=head->next;
while(p!=NULL)
{
List<E>* q=p->next;
delete(p);
p=q;
}
head->next=NULL;
}
setlength(0);
return ;
}
void output(List<E>* head)//输出链表元素
{
List<E>* head1=head->next;
while(head1->next!=NULL)
{
cout<<head1->data<<"->";
head1=head1->next;
}
cout<<head1->data<<endl;
return ;
}
List<E>* Reverse(List<E>* head)//反转链表
{
List<E>* last=head;
List<E>* pre=head;
List<E>* now=head->next;
List<E>* sub=now->next;
while(last->next!=NULL)
last=last->next;
now->next=NULL;
pre->next=last;
pre=now;
now=sub;
sub=sub->next;
while(sub!=NULL)
{
now->next=pre;
pre=now;
now=sub;
sub=sub->next;
}
now->next=pre;
return head;
}
};
int main()
{
LList<int> list;
int n;
cout<<"输入初始时结点个数:";
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cout<<"输入第"<<i+1<<"个元素: ";
cin>>a[i];
}
for(int i=0;i<n;i++)
list.append(a[i]);
list.output(list.gethead(list));
string inform;
while(1)
{
loop:
cout<<"查找请输入关键字search,修改请输入关键字change,插入请输入关键字insert,删除请输入关键字delete,输入关键字end则退出程序"<<endl;
cin>>inform;
if(inform=="end")
break;
if(inform!="search"&&inform!="change"&&inform!="insert"&&inform!="delete")
{
cout<<"wrong input,please input again:"<<endl;
goto loop;
}
else
{
if(inform=="search")
{
cout<<"查找某位置元素请输入1,查找某元素位置请输入2:"<<endl;
int j;
cin>>j;
if(j!=1&&j!=2)
{
cout<<"输入有误,不存在此操作!"<<endl;
goto loop;
}
else
{
if(j==1)
{
cout<<"您要查找第几个元素:"<<endl;
int k;
cin>>k;
if(k<1||k>list.getlength())
{
cout<<"不存在此位置,请重新输入:"<<endl;
}
else
cout<<"第"<<k<<"个元素为:"<<list.getvalue(k,list.gethead(list))<<endl;
}
else
{
cout<<"您要查找哪个元素的位置:"<<endl;
int k;
cin>>k;
int l=list.getposition(k);
if(l!=-1)
cout<<"此元素是第"<<l<<"个"<<endl;
else
cout<<"链表中无此元素"<<endl;
}
goto loop;
}
}
else if(inform=="change")
{
cout<<"想将某位置元素修改请输入1,想将某元素修改为另一元素请输入2:"<<endl;
int j;
cin>>j;
if(j!=1&&j!=2)
{
cout<<"输入有误,不存在此操作!"<<endl;
goto loop;
}
else
{
if(j==1)
{
cout<<"请输入你要修改的位置及修改后的值"<<endl;
int k,l;
cin>>k>>l;
list.changeposition(k,l);
cout<<"(提示信息:修改完成!)"<<endl;
}
else
{
cout<<"请输入你要修改的值及修改后的值"<<endl;
int k,l;
cin>>k>>l;
list.changevalue(k,l);
cout<<"(提示信息:修改完成!)"<<endl;
}
}
}
else if(inform=="insert")
{
cout<<"请输入您要插入的位置和将要插入的元素:"<<endl;
int k,l;
cin>>k>>l;
list.insert(k,l);
cout<<"(提示信息:插入完成)"<<endl;
}
else
{
cout<<"删除某位置元素请输入1及要删除的位置,删除某值的元素请输入2及要删除的元素的值"<<endl;
int k,l;
cin>>k>>l;
if(k!=1&&k!=2)
{
cout<<"输入有误,不存在此操作!";
goto loop;
}
else
{
if(k==1)
{
list.deleteposition(l);
cout<<"(提示信息:删除完成)"<<endl;
}
else
{
list.deletevalue(l);
cout<<"(提示信息:删除完成)"<<endl;
}
}
}
cout<<"当前链表为: ";
list.output(list.gethead(list));
cout<<endl;
goto loop;
}
}
return 0;
}
[/quote]
运气好没崩。[/quote]
好吧。
非常感谢#include<iostream>
#include<algorithm>
using namespace std;
template <typename T> class List//结点类定义
{
public:
T data;
List* next;
List(const T& data1){data=data1;next=NULL;}
List(){next=NULL;}
void changedata(const T& newdata){data=newdata;}
T getdata(){return data;}
};
template <typename E> class LList:public List<E>//单链表类定义
{
private:
List<E>* head;
int length;
public:
LList(){head->next=NULL;length=0;}//创建空的单链表
void lengthshort(){length--;}//长度-1
void lengthlong(){length++;}//长度+1
int getlength(){return length;}//返回当前单链表长度
void setlength(int x){length=x;}//设置单链表长度
List<E>* gethead(LList Llist){return Llist.head;}//返回头指针
List<E>* insert(int n,E list)//在位置n插入新结点list
{
int m=0;
List<E>* head1=head;
while(m!=n-1)
{
head=head->next;
m++;
}
List<E>* beinserted=head->next;
List<E>* getinsert=new List<E>;
getinsert->data=list;
getinsert->next=beinserted;
head->next=getinsert;
lengthlong();
head=head1;
return head;
}
List<E>* append(E list)//将新结点置于单链表结尾
{
List<E>* head1=head;
if(head->next==NULL)
{
List<E>* newone=new List<E>;
newone->data=list;
newone->next=NULL;
head->next=newone;
}
else
{
while(head->next!=NULL)
head=head->next;
List<E>* newone=new List<E>;
newone->data=list;
newone->next=NULL;
head->next=newone;
}
lengthlong();
head=head1;
return head;
}
List<E>* deleteposition(int m)//删除位置m的结点
{
if(m<1||m>getlength())
{
cout<<"删除位置有误,请重新输入:"<<endl;
int m;
cin>>m;
deleteposition(m);
}
if(m!=1)
{
int p=0;
List<E>* head1=head;
while(p!=m-1)
{
head1=head1->next;
p++;
}
List<E>* getdelete=head1->next;
head1->next=getdelete->next;
getdelete->next=NULL;
}
else
{
List<E>* newhead=head->next;
head->next=NULL;
head=newhead;
}
lengthshort();
return head;
}
List<E>* deletevalue(E m)//删除所有值为m的结点
{
int n=0;
List<E>* head1=head;
while(head1)
{
if(head1->data!=m)
n++;
else
{
deleteposition(n);
lengthshort();
}
head1=head1->next;
}
return head;
}
int getposition(E data1)//查找并返回第一个值为指定值data1结点的位置
{
int p=1;
List<E>* head1=this->head;
head=head->next;
while(head->data!=data1&&head!=NULL)
{
p++;
head=head->next;
}
head=head1;
if(p>getlength())
return -1;
else
return p;
}
E getvalue(int position,List<E>* head)//查找并返回位置position的值
{
int p=0;
List<E>* head1=head;
while(p!=position)
{
head1=head1->next;
p++;
}
return (head1->data);
}
List<E>* changeposition(int position,E newdata)//将位置position的元素的值修改为newdata
{
if(position<1||position>getlength())
{
cout<<"改值位置有误,请重新输入:"<<endl;
int position;
cin>>position;
changeposition(position,newdata);
}
int p=0;
List<E>* head1=head;
while(p!=position)
{
p++;
head1=head1->next;
}
head1->changedata(newdata);
return head;
}
List<E>* changevalue(E value,E newdata)//将单链表中所有值为value的结点的值修改为newdata
{
List<E>* head1=head;
while(head1!=NULL)
{
if(head1->data==value)
head1->changedata(newdata);
head1=head1->next;
}
return head;
}
void destroy(List<E>* head)//销毁单链表
{
if(head==NULL)
return ;
while(head)
{
List<E>* p=head->next;
delete(head);
head=p;
}
}
void clear(List<E>* head)//清空单链表
{
if(head==NULL);
else
{
List<E>* p=head->next;
while(p!=NULL)
{
List<E>* q=p->next;
delete(p);
p=q;
}
head->next=NULL;
}
setlength(0);
return ;
}
void output(List<E>* head)//输出链表元素
{
List<E>* head1=head->next;
while(head1->next!=NULL)
{
cout<<head1->data<<"->";
head1=head1->next;
}
cout<<head1->data<<endl;
return ;
}
List<E>* Reverse(List<E>* head)//反转链表
{
List<E>* last=head;
List<E>* pre=head;
List<E>* now=head->next;
List<E>* sub=now->next;
while(last->next!=NULL)
last=last->next;
now->next=NULL;
pre->next=last;
pre=now;
now=sub;
sub=sub->next;
while(sub!=NULL)
{
now->next=pre;
pre=now;
now=sub;
sub=sub->next;
}
now->next=pre;
return head;
}
};
int main()
{
LList<int> list;
int n;
cout<<"输入初始时结点个数:";
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cout<<"输入第"<<i+1<<"个元素: ";
cin>>a[i];
}
for(int i=0;i<n;i++)
list.append(a[i]);
list.output(list.gethead(list));
string inform;
while(1)
{
loop:
cout<<"查找请输入关键字search,修改请输入关键字change,插入请输入关键字insert,删除请输入关键字delete,输入关键字end则退出程序"<<endl;
cin>>inform;
if(inform=="end")
break;
if(inform!="search"&&inform!="change"&&inform!="insert"&&inform!="delete")
{
cout<<"wrong input,please input again:"<<endl;
goto loop;
}
else
{
if(inform=="search")
{
cout<<"查找某位置元素请输入1,查找某元素位置请输入2:"<<endl;
int j;
cin>>j;
if(j!=1&&j!=2)
{
cout<<"输入有误,不存在此操作!"<<endl;
goto loop;
}
else
{
if(j==1)
{
cout<<"您要查找第几个元素:"<<endl;
int k;
cin>>k;
if(k<1||k>list.getlength())
{
cout<<"不存在此位置,请重新输入:"<<endl;
}
else
cout<<"第"<<k<<"个元素为:"<<list.getvalue(k,list.gethead(list))<<endl;
}
else
{
cout<<"您要查找哪个元素的位置:"<<endl;
int k;
cin>>k;
int l=list.getposition(k);
if(l!=-1)
cout<<"此元素是第"<<l<<"个"<<endl;
else
cout<<"链表中无此元素"<<endl;
}
goto loop;
}
}
else if(inform=="change")
{
cout<<"想将某位置元素修改请输入1,想将某元素修改为另一元素请输入2:"<<endl;
int j;
cin>>j;
if(j!=1&&j!=2)
{
cout<<"输入有误,不存在此操作!"<<endl;
goto loop;
}
else
{
if(j==1)
{
cout<<"请输入你要修改的位置及修改后的值"<<endl;
int k,l;
cin>>k>>l;
list.changeposition(k,l);
cout<<"(提示信息:修改完成!)"<<endl;
}
else
{
cout<<"请输入你要修改的值及修改后的值"<<endl;
int k,l;
cin>>k>>l;
list.changevalue(k,l);
cout<<"(提示信息:修改完成!)"<<endl;
}
}
}
else if(inform=="insert")
{
cout<<"请输入您要插入的位置和将要插入的元素:"<<endl;
int k,l;
cin>>k>>l;
list.insert(k,l);
cout<<"(提示信息:插入完成)"<<endl;
}
else
{
cout<<"删除某位置元素请输入1及要删除的位置,删除某值的元素请输入2及要删除的元素的值"<<endl;
int k,l;
cin>>k>>l;
if(k!=1&&k!=2)
{
cout<<"输入有误,不存在此操作!";
goto loop;
}
else
{
if(k==1)
{
list.deleteposition(l);
cout<<"(提示信息:删除完成)"<<endl;
}
else
{
list.deletevalue(l);
cout<<"(提示信息:删除完成)"<<endl;
}
}
}
cout<<"当前链表为: ";
list.output(list.gethead(list));
cout<<endl;
goto loop;
}
}
return 0;
}
[/quote]
运气好没崩。#include<iostream>
#include<algorithm>
using namespace std;
template <typename T> class List//结点类定义
{
public:
T data;
List* next;
List(const T& data1){data=data1;next=NULL;}
List(){next=NULL;}
void changedata(const T& newdata){data=newdata;}
T getdata(){return data;}
};
template <typename E> class LList:public List<E>//单链表类定义
{
private:
List<E>* head;
int length;
public:
LList(){head->next=NULL;length=0;}//创建空的单链表
void lengthshort(){length--;}//长度-1
void lengthlong(){length++;}//长度+1
int getlength(){return length;}//返回当前单链表长度
void setlength(int x){length=x;}//设置单链表长度
List<E>* gethead(LList Llist){return Llist.head;}//返回头指针
List<E>* insert(int n,E list)//在位置n插入新结点list
{
int m=0;
List<E>* head1=head;
while(m!=n-1)
{
head=head->next;
m++;
}
List<E>* beinserted=head->next;
List<E>* getinsert=new List<E>;
getinsert->data=list;
getinsert->next=beinserted;
head->next=getinsert;
lengthlong();
head=head1;
return head;
}
List<E>* append(E list)//将新结点置于单链表结尾
{
List<E>* head1=head;
if(head->next==NULL)
{
List<E>* newone=new List<E>;
newone->data=list;
newone->next=NULL;
head->next=newone;
}
else
{
while(head->next!=NULL)
head=head->next;
List<E>* newone=new List<E>;
newone->data=list;
newone->next=NULL;
head->next=newone;
}
lengthlong();
head=head1;
return head;
}
List<E>* deleteposition(int m)//删除位置m的结点
{
if(m<1||m>getlength())
{
cout<<"删除位置有误,请重新输入:"<<endl;
int m;
cin>>m;
deleteposition(m);
}
if(m!=1)
{
int p=0;
List<E>* head1=head;
while(p!=m-1)
{
head1=head1->next;
p++;
}
List<E>* getdelete=head1->next;
head1->next=getdelete->next;
getdelete->next=NULL;
}
else
{
List<E>* newhead=head->next;
head->next=NULL;
head=newhead;
}
lengthshort();
return head;
}
List<E>* deletevalue(E m)//删除所有值为m的结点
{
int n=0;
List<E>* head1=head;
while(head1)
{
if(head1->data!=m)
n++;
else
{
deleteposition(n);
lengthshort();
}
head1=head1->next;
}
return head;
}
int getposition(E data1)//查找并返回第一个值为指定值data1结点的位置
{
int p=1;
List<E>* head1=this->head;
head=head->next;
while(head->data!=data1&&head!=NULL)
{
p++;
head=head->next;
}
head=head1;
if(p>getlength())
return -1;
else
return p;
}
E getvalue(int position,List<E>* head)//查找并返回位置position的值
{
int p=0;
List<E>* head1=head;
while(p!=position)
{
head1=head1->next;
p++;
}
return (head1->data);
}
List<E>* changeposition(int position,E newdata)//将位置position的元素的值修改为newdata
{
if(position<1||position>getlength())
{
cout<<"改值位置有误,请重新输入:"<<endl;
int position;
cin>>position;
changeposition(position,newdata);
}
int p=0;
List<E>* head1=head;
while(p!=position)
{
p++;
head1=head1->next;
}
head1->changedata(newdata);
return head;
}
List<E>* changevalue(E value,E newdata)//将单链表中所有值为value的结点的值修改为newdata
{
List<E>* head1=head;
while(head1!=NULL)
{
if(head1->data==value)
head1->changedata(newdata);
head1=head1->next;
}
return head;
}
void destroy(List<E>* head)//销毁单链表
{
if(head==NULL)
return ;
while(head)
{
List<E>* p=head->next;
delete(head);
head=p;
}
}
void clear(List<E>* head)//清空单链表
{
if(head==NULL);
else
{
List<E>* p=head->next;
while(p!=NULL)
{
List<E>* q=p->next;
delete(p);
p=q;
}
head->next=NULL;
}
setlength(0);
return ;
}
void output(List<E>* head)//输出链表元素
{
List<E>* head1=head->next;
while(head1->next!=NULL)
{
cout<<head1->data<<"->";
head1=head1->next;
}
cout<<head1->data<<endl;
return ;
}
List<E>* Reverse(List<E>* head)//反转链表
{
List<E>* last=head;
List<E>* pre=head;
List<E>* now=head->next;
List<E>* sub=now->next;
while(last->next!=NULL)
last=last->next;
now->next=NULL;
pre->next=last;
pre=now;
now=sub;
sub=sub->next;
while(sub!=NULL)
{
now->next=pre;
pre=now;
now=sub;
sub=sub->next;
}
now->next=pre;
return head;
}
};
int main()
{
LList<int> list;
int n;
cout<<"输入初始时结点个数:";
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cout<<"输入第"<<i+1<<"个元素: ";
cin>>a[i];
}
for(int i=0;i<n;i++)
list.append(a[i]);
list.output(list.gethead(list));
string inform;
while(1)
{
loop:
cout<<"查找请输入关键字search,修改请输入关键字change,插入请输入关键字insert,删除请输入关键字delete,输入关键字end则退出程序"<<endl;
cin>>inform;
if(inform=="end")
break;
if(inform!="search"&&inform!="change"&&inform!="insert"&&inform!="delete")
{
cout<<"wrong input,please input again:"<<endl;
goto loop;
}
else
{
if(inform=="search")
{
cout<<"查找某位置元素请输入1,查找某元素位置请输入2:"<<endl;
int j;
cin>>j;
if(j!=1&&j!=2)
{
cout<<"输入有误,不存在此操作!"<<endl;
goto loop;
}
else
{
if(j==1)
{
cout<<"您要查找第几个元素:"<<endl;
int k;
cin>>k;
if(k<1||k>list.getlength())
{
cout<<"不存在此位置,请重新输入:"<<endl;
}
else
cout<<"第"<<k<<"个元素为:"<<list.getvalue(k,list.gethead(list))<<endl;
}
else
{
cout<<"您要查找哪个元素的位置:"<<endl;
int k;
cin>>k;
int l=list.getposition(k);
if(l!=-1)
cout<<"此元素是第"<<l<<"个"<<endl;
else
cout<<"链表中无此元素"<<endl;
}
goto loop;
}
}
else if(inform=="change")
{
cout<<"想将某位置元素修改请输入1,想将某元素修改为另一元素请输入2:"<<endl;
int j;
cin>>j;
if(j!=1&&j!=2)
{
cout<<"输入有误,不存在此操作!"<<endl;
goto loop;
}
else
{
if(j==1)
{
cout<<"请输入你要修改的位置及修改后的值"<<endl;
int k,l;
cin>>k>>l;
list.changeposition(k,l);
cout<<"(提示信息:修改完成!)"<<endl;
}
else
{
cout<<"请输入你要修改的值及修改后的值"<<endl;
int k,l;
cin>>k>>l;
list.changevalue(k,l);
cout<<"(提示信息:修改完成!)"<<endl;
}
}
}
else if(inform=="insert")
{
cout<<"请输入您要插入的位置和将要插入的元素:"<<endl;
int k,l;
cin>>k>>l;
list.insert(k,l);
cout<<"(提示信息:插入完成)"<<endl;
}
else
{
cout<<"删除某位置元素请输入1及要删除的位置,删除某值的元素请输入2及要删除的元素的值"<<endl;
int k,l;
cin>>k>>l;
if(k!=1&&k!=2)
{
cout<<"输入有误,不存在此操作!";
goto loop;
}
else
{
if(k==1)
{
list.deleteposition(l);
cout<<"(提示信息:删除完成)"<<endl;
}
else
{
list.deletevalue(l);
cout<<"(提示信息:删除完成)"<<endl;
}
}
}
cout<<"当前链表为: ";
list.output(list.gethead(list));
cout<<endl;
goto loop;
}
}
return 0;
}