📄 shiyan1.cpp
字号:
#include <iostream.h>
#include"link_h.h"
template<class Elem> class LList
{
private:
Link<Elem>* head;
Link<Elem>* tail;
Link<Elem>* fence;
int leftcnt;
int rightcnt;
void init()
{
fence=tail=head=new Link<Elem>;
leftcnt=rightcnt=0;
}
public:
LList(int size=10){init();}
~LList() {removeall();}
void clear(){removeall();init();}
void removeall()
{
while(head!=NULL)
{
fence=head;
head=head->next;
delete fence;
}
}
bool insert(const Elem&);
bool append (const Elem&);
bool remove(Elem&,const int& );
bool find(const Elem&);
void setStart()
{fence =head;rightcnt+=leftcnt;leftcnt=0;}
void setEnd()
{fence=tail;leftcnt+=rightcnt;rightcnt=0;}
void prev();
void next(){
if(fence!=tail)
{fence=fence->next;righcnt--:leftcnt++;}
}
int leftLength() const {return leftcnt;}
int rightLength() const {return rightcnt;}
bool setPos(int pos);
bool getValue(Elem& it) const{
if(rightLength()==0)return false;
it=fence->next->element;
return ture;}
void print() const;
};
template <class Elem>
bool LList<Elem>::insert(const Elem& item){
fence->next=new Link<Elem>(item,fence->next);
if(tail==fence)tail=fence->next;
rightcnt++;
return true;
}
template <class Elem>
bool LList<Elem>::find(const Elem& item){
Link<Elem>* ltemp;
ltemp=head;
for(int i=1;i<leftcnt+rightcnt+1;i++)
{ltemp=ltemp->next;
if(ltemp->element==item)
cout<<"the position is:"<<i<<":"<<ltemp->element<<'\n';
}
return true;
}
template <class Elem>
bool LList<Elem>::append (const Elem& item){
tail=tail->next=new Link<Elem>(item,NULL);
rightcnt++;
return true;
}
template <class Elem>
bool LList<Elem>::remove(Elem& ele,const int& pos)
{
int length=leftcnt+rightcnt;
if(length<pos||pos<=0)
return false;
Link<Elem> *temp=fence;
for(int i=0;i<pos-1;i++)
temp=temp->next;
Link<Elem>* t=temp->next;
temp->next=temp->next->next;
ele=t->element;
delete t;
return true;
}
template<class Elem>
void LList<Elem>::prev(){
Link<Elem>* temp=head;
if(fence==head) return;
while(temp->next!=fence)temp=temp->next;
fence=temp;
leftcnt--;rightcnt++;
}
template<class Elem>
bool LList<Elem>::setPos(int pos){
if((pos<0)||(pos>rightcnt+leftcnt))return false;
fence=head;
for(int i=0;i<pos;i++)fence=fence->next;
return ture;
}
template<class Elem>
void LList<Elem>::print() const{
Link<Elem>*temp=head;
cout<<"<";
while(temp!=fence){
cout<<temp->next->element<<" ";
temp=temp->next;
}
cout<<"|";
while(temp->next!=NULL){
cout<<temp->next->element<<" ";
temp=temp->next;
}
cout<<">\n";
}
void main()
{
LList<int> intLList;
int ele;
cout<<"\n 1:清除";
cout<<"\n 2: 插入";
cout<<"\n 3: 追加";
cout<<"\n 4: 删除";
cout<<"\n 5: 查找";
cout<<"\n 0: 返回 \n";
char choice;
do{
cout <<"\n please input the choice:\n";
cin>>choice;
switch(choice)
{
case '1':
cout<<"要清除全部元素吗?Y or N\n";
char flag;
cin>>flag;
if(flag=='Y')
intLList.removeall();
intLList.print();
break;
case '2':
cout<<"请输入要插入的数据:\n";
int it;
cin>>it;
intLList.insert(it);
intLList.print();
break;
case '3':
cout<<"请输入你想追加的数据:\n";
cin>>it;
intLList.append(it);
intLList.print();
break;
case '4':
cout<<"请输入你要删除的数据的位置:\n";
cin>>it;
intLList.remove(ele,it);
intLList.print();
break;
case '5':
cout<<"请输入你要查找的数据:\n";
cin>>it;
intLList.find(it);
intLList.print();
break;
default:
cout<<"you input a invalid choice.";
}
}while(true);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -