Ah Wei has recently fallen for some time, and now awakening, Wei have to adhere to write a blog!
Recent data structure to learn confused, Ah Wei to start from scratch to learn the data structure of the QAQ
Firstly, the ADT table linear (abstract)
(template for category here )
template <class T>
class linearList
{
public:
virtual ~linearList(){};
virtual bool empty() const=0;//返回true,当且仅当线性表为空
virtual int size() const=0;//返回线性表的元素个数
virtual T& get(int theIndex) const=0;//返回索引为theIndex的元素
virtual int indexOf(const T& theElement) const=0;//返回元素theElement第一次出现的索引
virtual void erase(int theIndex)=0;
virtual void insert(int theIndex,const T& theElement)=0;
virtual void output(ostream& out) const=0;//把线性表插入输出流out
};
Next is a linked list of nodes defined by the structure
template <class T>
struct chainNode
{
T element;
chainNode<T> *next;
chainNode(){}
chainNode(const T& element)
{
this->element=element;
}
chainNode(const T& element,chainNode<T>* next)
{
this->element=element;
this->next=next;
}
};
Next is the definition of a linked list
template <class T>
class chain:public linearList<T>
{
public:
chain(int initialCapacity=10);//构造函数
chain(const chain<T>&);//复制构造函数
~chain();//析构函数
//ADT方法
bool empty() const
{
return listSize==0;
}
int size() const
{
return listSize;
}
T& get(int theIndex)const;
int indexOf(const T& theElement) const;
void erase(int theIndex);
void insert(int theIndex,const T& theElement);
void output(ostream& out) const;
protected:
void checkIndex(int theIndex) const;//如果索引无效,抛出异常
chainNode<T>* firstNode;//指向链表第一个结点的指针,注意并不是头结点
int listSize;//线性表的元素个数
};
1. checkIndex functions to achieve:
template<class T>
void chain<T>::checkIndex(int theIndex)const
{//确定索引在0和listSize-1之间
if(theIndex<0||theIndex>=listSize)
{
ostringstream s;
s<<"index="<<theIndex<<"size= "<<listSize;
throw illegalIndex(s.str());
}
}
2. The list constructor (time complexity [theta] (. 1) )
template<class T>
chain<T>::chain(int initialCapacity)
{
if(initialCapacity<1)
{
ostringstream s;
s<<"Initial capacity = "<<initialCapacity<<"Must be > 0";
throw illegalParameterValue(s.str());
}
firstNode=NULL;
listSize=0;
}
3. A list of the copy constructor (time complexity * O (max {listSize, theList.listSize}))
template<class T>
chain<T>::chain(const chain<T>& theList)
{
listSize=theList.listSize;
if(listSize==0)//链表theList为空
{
firstNode=NULL;
return;
}
//若不为空
chainNode<T>* sourceNode=theList.firstNode;//复制链表theList的节点
firstNode=new chainNode<T>(sourceNode->element);//复制链表theList的首元素
sourceNode=sourceNode->next;
chainNode<T>* targetNode=firstNode;//当前链表的最后一个节点
//复制剩余元素
while(sourceNode!=NULL)
{
targetNode->next=new chainNode<T>(sourceNode->element);
targetNode=targetNode->next;
sourceNode=sourceNode->next;
}
targetNode->next=NULL;//链表结束
}
For the copy constructor to explain:
4. The list destructor (time complexity O (listSize))
destructor is to delete all the nodes, the method starts from the head node to delete
template<class T>
chain<T>::~chain()//删除链表的所有节点
{
while(firstNode!=NULL)//删除首节点
{
chainNode<T>* nextNode=firstNode->next;
delete firstNode;
firstNode=nextNode;
}
}
Implementation (time complexity O (theIndex)) 5. get Function
This function is to find the elements of the specified index
template<class T>
T& chain<T>::get(int theIndex)const
{
checkIndex(theIndex);//如果索引不存在,则抛出异常
chainNode<T>* currentNode=firstNode;
//移向所需要的节点
for(int i=0;i<theIndex;i++)
currentNode=currentNode->next;
return currentNode->element;
}
Implementation (time complexity O (listSize)) 6. indexOf function
This function is an index to find the first occurrence of the element theElement
If not found, returns -1, returns the index to find
template<class T>
int chain<T>::indexOf(const T& theElement)const
{
chainNode<T>* currentNode=firstNode;
int index=0;
//退出循环的条件:①没有找到,找到了最后,currentNode为空;②找到了
while(currentNode!=NULL&¤tNode->element!=theElement)
{
currentNode=currentNode->next;
index++;
}
//如果是由于没有找到退出的循环
if(currentNode==NULL)
return -1;
//否则
return index;
}
Implementation (time complexity O (theIndex)) 7. erase function is
divided into three cases:
①: theIndex.setdefault <0 or theIndex> = listSize. At this operation is not valid
②: delete non-empty table 0th element node (the first element)
③: Delete other element node
template<class T>
void chain<T>::erase(int theIndex)
{
checkIndex(theIndex);
if(theIndex==0)//如果要删除首节点
{
firstNode=firstNode->next;
}
else
{
chainNode<T>* currentNode=firstNode;
for(int i=0;i<theIndex-1;i++)//找到要删除的节点的前驱节点
{
currentNode=currentNode->next;
}
currentNode->next=currentNode->next->next;
}
listSize--;//将链表的个数减一
}
Realization (time complexity O (theIndex)) 8. insert a function of
insertion and deletion function similar to the function
below is Wei wrote the code (see the textbook written by their own dish I cry)
template<class T>
void chain<T>::insert(int theIndex,const T& theElement)
{
chainNode<T>* tmpNode=new chainNode<T>(theElement);
if(theIndex==0)
{
tmpNode->next=firstNode;
firstNode=tmpNode;
}
else
{
chainNode<T>* p=firstNode;
for(int i=0;i<theIndex-1;i++)
{
p=p->next;
}
tmpNode->next=p->next;
p->next=tmpNode;//这两行代码位置不能换,因为如果先将p指向tmpNode,那之前p的后继就找不到了
}
listSize++;
}
Please enjoy textbooks Code:
template<class T>
void chain<T>::insert(int theIndex,const T& theElement)
{
if(theIndex<0||theIndex>listSize)
{
ostringstream s;
s<<"index="<<theIndex<<"size="<<listSize;
throw illegalIndex(s.str());
}
if(theIndex==0)
firstNode=new chainNode<T>(theElement,firstNode);
else
{
chainNode<T>* p=firstNode;
for(int i=0;i<theIndex-1;i++)
p=p->next;
p->next=new chainNode<T>(theElement,p->next);//太强了!
}
listSize++;
}
9. Extended abstract class linear form
required of the linear expansion ADT table contains a number of other operations, such as clear Clear table, the table element into the tail push_back
template<class T>
class extendedLinearList::linearList<T>
{
public:
virtual ~extendedLinearList(){}
virtual void clear()=0;//清表
virtual void push_back(const T& theElement)=0;//将元素theElement插到表尾
};
10. Achieving clear function
we need to develop a class extendedChain, as described extendedLinearList chain abstract class, developed by the list chain derived from a shortcut, and the tail pointer to increase lastNode nodes
Here is the clear function
template<class T>
void extendedChain<T>::clear()
{
//从首节点开始删除
while(firstNode!=NULL)
{
chainNode<T>* nextNode=firstNode->next;
delete firstNode;
firstNode=nextNode;
}
listSize=0;
}
11. push_back function to achieve
template<class T>
void extendedChain<T>::push_back(const T& theElement)
{
chainNode<T>* newNode=new chainNode<T>(theElement,NULL);
if(firstNode==NULL)
firstNode=lastNode=newNode;//extendedChain中声明了尾结点lastNode
else
{
lastNode->next=newNode;
lastNode=newNode;
}
listSize++;
}