📄 enhancedlinkedlist.h
字号:
#ifndef _ENHANCEDLINKEDLIST_H_
#define _ENHANCEDLINKEDLIST_H_
#include <stdexcept>
using namespace std;
#include "LinkedList.h"
class ListItemNotFoundException:public logic_error{
public:
ListItemNotFoundException(const string& what_arg)throw():
logic_error("Empty list exception:"+what_arg){}
};
template<class T>
class EnhancedLinkedList:public LinkedList<T>{
public:
EnhancedLinkedList(void):LinkedList<T>(){}
T& find_first(const T& key);
EnhancedLinkedList<T> find_all(const T& key);
void remove_first(const T& key);
void remove_all(const T& key);
};
template <class T>
T& EnhancedLinkedList<T>::find_first(const T& key)
{
if(head==NULL)
throw ListItemNotFoundException("The EnhancedLinkedList object is empty.");
Node<T>*current=this->head;
while(current!=NULL&¤t->getData()!=key)
current = current->getNext();
if(current!=NULL)
return current->getData();
else
throw ListItemNotFoundException("No item is found that matches the parameter");
}
template<class T>
EnhancedLinkedList<T> EnhancedLinkedList<T>::find_all (const T& key)
{
EnhancedLinkedList<T> newList;
Node<T>*current=this->head;
while(current!=NULL)
{
if(current->getData()==key)
{
newList.push_back(key);
}
current=current->getNext();
}
return newList;
}
template<class T>
void EnhancedLinkedList<T>::remove_first(const T& key)
{
if(head==NULL)
return;
Node<T>*current=this->head,*del;
if(current->getData()==key)
{
head=current->getNext();
count--;
return;
}
for(int i=0;i<size()-1;i++)
{
if(current->getNext()->getData()==key)
{
del=current->getNext();
current->getNext()=del->getNext();
delete del;
count--;
return;
}
if(current->getNext==NULL&¤t->getData()==key)
{
delete current;
count--;
return;
}
current=current->getNext();
}
}
template <class T>
void EnhancedLinkedList<T>::remove_all(const T& key)
{
if(head==NULL)
return;
Node<T>*current,*del;
while(1)
{
current=this->head;
if(head->getData()==key)
{
head=head->getNext();
count--;
}
else
break;
}
current=this->head;
for(int i=0;i<size();i++)
{
if(current->getNext()->getData()==key)
{
del=current->getNext();
current->getNext()=del->getNext();
delete del;
count--;
}
if(current->getNext==NULL&¤t->getData()==key)
{
delete current;
count--;
}
current=current->getNext();
}
return;
}
#endif*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -