单链表的实现
想自己独立写一写有关单链表的实现,解果发现水平不高,不能不看书来写,但在敲的过程中还是领会到了实现各种函数的思想,以下是我在“书本”的帮助下敲的代码。求大神勿喷!
LinkedList_H.h(源代码):
#ifndef LinkedList_H
#define LinkedList_H
#include <stdexcept>
using namespace std;
template<typename T>
class LinkedList
{
public:
struct Node
{
T data;
Node * next;
Node(const T & x,Node * p = NULL):data(x),next(p){};
};
typedef Node* Ptr;
LinkedList(); //构造一个空表
~LinkedList();
LinkedList(const List &l);
const List & operator=(const List &l);
int size() const {return theSize;};
bool empty() const { return (theSize==0);};
void clear();
Ptr insert(int position,const T &x); //在position处插入x,并返回指向插入元素的指针
void push_front(const T &x); //在第一个元素前插入x
void push_back(const T &x); //将x插入尾部
T pop_back();
T pop_front();
T & front(); //若表不空,返回第一个元素的引用
T & back();
const T & front() const; //将front()重载
const T & back() const;
Ptr begin() { return head;};
Ptr end() { return NULL;}
void traverse(void (*visit)(T &));
Ptr advance(int p);
Ptr erase(int position);
private:
Ptr previous(Ptr p);
Ptr head;
Ptr tail;
int theSize;
};
template<typename T>
LinkedList<T>::LinkedList()
{
head = tail = NULL;
theSize = 0;
}
template<typename T>
void LinkedList<T>::push_front(const T &x)
{
Ptr newNode = new Node(x);
newNode ->next = head;
theSize++;
}
template<typename T>
void LinkedList<T>::push_back(const T &x)
{
if(tail == NULL)
{
head = tail = new Node(x);
}
else
{
tail->next = new Node(x);
tail = tail->next;
}
theSize++;
}
template<typename T>
T & LinkedList::front()
{
if(theSize == 0)
{
throw runtime_error("Index out of range");
}
else
return head->data;
}
template<typename T>
const T & LinkedList<T>::front() const
{
return front();
}
template<typename T>
T & LinkedList<T>::back()
{
if(theSize == 0)
{
throw runtime_error("Index out of range");
}
else
return tail->data;
}
template<typename T>
const T & LinkedList<T>::back() const
{
return back();
}
template<typename T>
T LinkedList<T>::pop_front()
{
if(theSize == 0)
throw runtime_error("No elements in the list");
else
{
Ptr temp = head;
head = head->next;
if(head = NULL)
tail = NULL;
theSize--;
T element = temp->data;
delete temp;
return element;
}
}
template<typename T>
T LinkedList<T>::pop_back()
{
if(size == 0)
throw runtime_error("No elements in the list");
else if(size == 1)
{
Ptr temp = head;
head = tail = NULL;
theSize = 0;
T element = temp->data;
delete temp;
return element;
}
else
{
Ptr current = head;
for(int i = 0;i < theSize - 2;i++)
current = current->next;
Ptr temp = tail;
tail = current;
tail->next = NULL;
theSize--;
T element = temp->data;
delete temp;
return element;
}
}
template<typename T>
void LinkedList<T>::clear()
{
while(head != NULL)
{
Ptr temp = head;
delete temp;
head = head->next;
}
tail = NULL;
}
template<typename T>
Ptr LinkedList<T>::insert(int position, const T &x)
{
if(position == 0)
{
push_front(x);
return head;
}
else if(position >= theSize)
{
push_back(x);
return tail;
}
else
{
Ptr current = head;
for(int i = 0;i < position;i++)
current = current->next;
Ptr temp = current->next;
current->next = new Node(x);
(current->next)->next =temp;
theSize++;
return current->next;
}
}
template<typename T>
Ptr LinkedList<T>::advance(int p)
{
if(p <0 || p > theSize -1)
runtime_error("Index out of range");
Ptr current = head;
for(int i = 0; i < p ;i++)
current = current->next;
return current;
}
template<typename T>
void LinkedList<T>::traverse(void (*visit)(T &))
{
Ptr current = head;
for(int i = 0;i < theSize;i++)
{
(*visit)(current->data);
current = current->next;
}
}
template<typename T>
Ptr LinkedList<T>::erase(int position)
{
if(positon < 0 || position >= theSize)
throw runtime_error("Index out of range");
else if(position == 0)
{
Ptr temp = new Node(0);
temp->data = head->data;
pop_front();
return temp;
}
else if(position == theSize - 1)
{
Ptr temp = new Node(0);
temp->data = tail->data;
pop_back();
return temp;
}
else
{
Ptr previous = head;
for(int i = 1;i < position;i++)
{
previous = previous->next;
}
Ptr current = previous->next;
previous ->next = current ->next;
theSize--;
delete current;
return previous->next;
}
}
#endif