//.h
class List
{
public:
List(){}
virtual ~List(){}
// clear contents from the list , to make it empty
virtual void clear()=0;
//insert an element at the current location
//item:the element to be insert
virtual void insert(const int& item)=0;
//append an element at the end of the list.
//item:the element to be insert
virtual void append(const int& item)=0;
//remove and return the current element
//return:the element that was removed
virtual int remove()=0;
//set the current position to the start of the list
virtual void movetostart()=0;
//set the current position to the end of the list
virtual void movetoend()=0;
//move the current position one step left
virtual void prev()=0;
//move the current position one step right
virtual void next()=0;
//return the number of elements in the list
virtual int length() const=0;
//return the position of the current element
virtual int currpos() const=0;
//set current position
//pos: the position to make current
virtual void movetopos(int pos)=0;
//return the current element
virtual const int& getvalue() const=0;
};
class Alist : public List
{
private:
int maxsize;
int listsize;
int curr;
int* listarray;
public:
Alist(int size)
{
maxsize=size;
listsize=curr=0;
listarray=new int[maxsize];
}
~Alist()
{
delete [] listarray;
}
void clear();
void insert(const int& it);
void append(const int& it);
int remove();
void movetostart();
void movetoend();
void prev();
void next();
int length() const;
int currpos() const;
void movetopos(int pos);
const int & getvalue() const;
};
基于顺序表
//.cpp
#include<iostream>
#include"alistADT.h"
using namespace std;
void Alist::clear()
{
delete [] listarray;
listsize=curr=0;
listarray= new int[maxsize];
}
//insert element in the current position
void Alist::insert(const int& it)
{
for(int i=listsize;i>curr;i--)
listarray[i]=listarray[i-1];
listarray[curr]=it;
listsize++;
}
//append element in the end of this list
void Alist::append(const int& it)
{
listarray[listsize++]=it;
}
int Alist::remove()
{
int it=listarray[curr];
for(int i=curr;i<listsize-1;i++)
listarray[i]=listarray[i+1];
listsize--;
return it;
}
void Alist::movetostart()
{
curr=0;
}
void Alist::movetoend()
{
curr=listsize;
}
void Alist::prev()
{
if(curr!=0)
curr--;
}
void Alist::next()
{
if(curr<listsize)
curr++;
}
int Alist::length() const
{
return listsize;
}
int Alist::currpos() const
{
return curr;
}
void Alist::movetopos(int pos)
{
curr=pos;
}
const int& Alist::getvalue() const
{
return listarray[curr];
}
基于链表
//.cpp
#include<iostream>
#include"llistADT.h"
using namespace std;
void Llist::clear()
{
removeall();
init();
}
//insert element in the current position
void Llist::insert(const int& it)
{
curr->next=new Link(it,curr->next);
if(tail==curr) tail=curr->next;
cnt++;
}
//append element in the end of this list
void Llist::append(const int& it)
{
tail=tail->next=new Link(it,NULL);
cnt++;
}
//remove the current element
int Llist::remove()
{
int it=curr->next->element;
Link *ltemp=curr->next;
if(tail==curr->next) tail=curr;
curr->next=curr->next->next;
delete ltemp;
cnt--;
return it;
}
void Llist::movetostart()
{
curr=head;
}
void Llist::movetoend()
{
curr=tail;
}
void Llist::prev()
{
if(curr==head) return;
Link *temp=head;
while(temp->next!=curr) temp=temp->next;
curr=temp;
}
void Llist::next()
{
if(curr!=tail) curr=curr->next;
}
int Llist::length() const
{
return cnt;
}
int Llist::currpos() const
{
Link *temp=head;
int i;
for(i=0;curr!=temp;i++)
temp=temp->next;
return i;
}
void Llist::movetopos(int pos)
{
curr=head;
for(int i=0;i<pos;i++)
curr=curr->next;
}
const int& Llist::getvalue() const
{
return curr->next->element;
}
//原创,请勿转载!!!