#include<iostream.h>
const int max_list=10;
enum error_code{success,underflow,overflow,range_error,not_present};
/*********************************************************************************************************/
template<class list_entry>
class list {
public:
list() {count=0;}
int size()const;
bool full()const;
bool empty()const;
void clear();
error_code retrieve(int position,list_entry &x)const;
error_code replace(int position,const list_entry &x);
error_code remove(int position,list_entry &x);
error_code insert(int position,const list_entry &x);
protected:
int count;
list_entry entry[max_list];
};
/*********************************************************************************************************/
template<class list_entry>
int list<list_entry>::size()const
{
return count;
}
/*********************************************************************************************************/
template<class list_entry>
bool list<list_entry>::full()const
{
if(count==max_list-1) return true;
else return false;
}
/*********************************************************************************************************/
template<class list_entry>
bool list<list_entry>::empty()const
{
if(count==0) return true;
else return false;
}
/*********************************************************************************************************/
template<class list_entry>
void list<list_entry>::clear()
{
count=0;
}
/*********************************************************************************************************/
template<class list_entry>
error_code list<list_entry>::insert(int position,const list_entry &x)
{
if(full())
return overflow;
if(position<0||position>count)
return range_error;
for(int i=count-1;i>=position;i--)
entry[i+1]=entry;
entry[position]=x;
count++;
return success;
}
/*********************************************************************************************************/
template<class list_entry>
error_code list<list_entry>::remove(int position,list_entry &x)
{
if(empty())
return underflow;
if(position<0||position>count)
return range_error;
x=entry[position];
for(int i=count-1;i>=position;i--)
entry[i-1]=entry;
count--;
return success;
}
/*********************************************************************************************************/
template<class list_entry>
error_code list<list_entry>::replace(int position,const list_entry &x)
{
if(empty())
return underflow;
if(position<0||position>count)
return range_error;
entry[position]=x;
return success;
}
/*********************************************************************************************************/
template<class list_entry>
error_code list<list_entry>::retrieve(int position,list_entry &x)const
{
if(empty())
return underflow;
if(position<0||position>count)
return range_error;
x=entry[position];
return success;
}
/*********************************************************************************************************/
template<class list_entry>
error_code binary_search_1(const list<list_entry>&the_list,
const int &target,int &position)
{
int data;
int bottom=0,top=the_list.size()-1;
while(bottom<top){
int mid=(bottom+top)/2;
the_list.retrieve(mid,data);
if(data<target)
bottom=mid+1;
else
top=mid;
}
if(top<bottom) return not_present;
else {
position=bottom;
the_list.retrieve(bottom,data);
if(data==target) return success;
else return not_present;
}
}
/*********************************************************************************************************/
void introduction() {
cout<<endl
<<"this program allows the user to enter one command"<<endl
<<"(but only one) on each input line."<<endl
<<"for example,if the command I is entered,then "<<endl
<<"the program will insert an element to the list."<<endl
<<endl
<<"the valid commands are:"<<endl
<<"I-insert a position element to the list"<<endl
<<"V-remove a position element to the list"<<endl
<<"#-the current size of the list"<<endl
<<"T-retrieve a position element from the list"<<endl
<<"S-search for an element"<<endl
<<"Q-Quit"<<endl
<<"press <Enter> to continue."<<flush;
}
/*********************************************************************************************************/
char get_command()
{
char c;
cin>> c;
return c;
}
/*********************************************************************************************************/
template<class list_entry>
bool do_command(char c,list<list_entry> &the_list)
{ list_entry x,target;
int p,position;
bool continue_input=true;
switch©{
case'i':
cout<<endl
<<"Enter your element :"
<<endl;
cin>>x;
cout<<endl
<<"Enter its position:"
<<endl;
cin>>p;
if(the_list.insert(p,x)==(overflow||range_error))
cout<<"list is full or range error"<<endl;
break;
case'v':
cout<<endl
<<"Enter the position of an element to be removed"
<<endl;
cin>>p;
if(the_list.remove(p,x)==(underflow||range_error))
cout<<"list is empty or range_error.... "<<endl;
else
cout<<"element [ "<<x<<" ] has been removed";
break;
case't':
if(the_list.retrieve(p,x)==underflow)
cout<<"list is empty"<<endl;
else {
cout<<endl
<<"Enter the position of an element to be displayed"
<<endl;
cin>>p;
the_list.retrieve(p,x);
cout<<"the element is: "<<x
<<endl;
}
break;
case'c':
the_list.clear();
cout<<endl
<<"list has been cleared"
<<endl;
break;
case '#':cout<<"the current size of the list"<<endl
<<the_list.size()<<endl;
break;
/* case's':cout<<"enter your element to check if it is exist"<<endl;
cin>>target;
if(sequential_search(the_list,target,position)==not_present)
cout<<"this element is not found in the list"<<endl;
else
cout<<"Element "<<target<<" is found at position "<<position<<" "<<endl;
break;*/
case'q':
cout<<"Extended queue demonstration finished."<<endl<<endl<<endl;
continue_input=false;
break;
}
return continue_input;
}
/*********************************************************************************************************/
int main()
{
list<int>the_list;
introduction();
while(do_command(get_command(),the_list));
return 0;
}
/*********************************************************************************************************/
