算法导论第十章数据结构--双向链表
看的概念挺朦胧的,没有明白好双链表究竟须要哪些方法,事实上针对这样的结构应该能够写非常多方法,并没有什么特定标准。
只是真是仅仅看不练不行啊,一下手各种错误。各种溢出
#include<iostream>
using namespace std;
template<class T> struct Node
{
T value;
Node<T>* pre;
Node<T>* next;
};
template<class T> class Flist
{
private:
Node<T>* front;
Node<T>* end; //每写一个函数,遍历类中变量哪个要维护
int count;
public:
Flist(); //defaut constructor
~Flist();
Flist(const Flist& C_list);//支持了这个,没有写支持 = 能够写个空的内联函数来放空这样的行为。否则有相关操作时。编译器会自己主动产生
inline void Deeply_Copy(const Flist& Copylist);
bool Isempty() const;
int Listsize() const;
T& Listfront()const;
T& Listend() const; //返回引用是不是能够改首尾node的key值了
void push_front(T N);
void push_back(T N);
void del_front();
void del_back();
//Node<T>* Listfind(T x); //搜索有key值x的节点
T ShowKey(int n); //输出第n个值
};
template<class T> Flist<T>::Flist():count(0),front(0),end(0) //初始化顺序与类中声明有关与这无关
{
}
template<class T> Flist<T>::~Flist()
{
Node<T>* temp;
while (front != 0)
{
temp = front;
delete temp;
front = front->next;
}
temp = NULL;
}
template<class T> bool Flist<T>::Isempty() const
{
return 0 == count;
}
template<class T> int Flist<T>::Listsize() const{
return count;
}
template<class T> Flist<T>::Flist(const Flist& C_list)
{
Deeply_Copy(C_list);
}
template<class T> T& Flist<T>::Listfront() const
{
return front->value;
}
template<class T> T& Flist<T>::Listend() const
{
return end->value;
}
template<class T> void Flist<T>::push_front(T N)
{
Node<T>* p = new Node<T>;
p->value = N;
p->next = front;
if (front != 0)
front->pre = p;
front = p;
if(end == 0)
end = p ;
p->pre = 0;
count++;
}
template<class T> void Flist<T>::push_back(T N)
{
Node<T>* p = new Node<T>;
p->value = N;
p->pre = end;
if (end != 0) //一開始这里崩溃是由于end没有初始化0就用
end->next = p;
end = p;
end->next = 0;
count++;
}
template<class T> void Flist<T>::del_front()
{
Node<T>* temp = front;
front = front->next;
count--;
front->pre = 0;
delete temp;
}
template<class T> void Flist<T>::del_back()
{
Node<T>* temp = end;
end = end->pre;
end->next = 0;
count--;
delete temp;
}
template<class T> T Flist<T>::ShowKey(int n)
{
/*
if (front == 0)
{
cout << "there is no element is the list.." << endl;
return ; //未解决,size为0时,怎样提示
}*/
if ((front !=0) && (n <=count) && (n >= 1))
{
Node<T>* temp = front;
while(--n) //这里假设while(n--)就溢出崩溃。n用后再减。这里的用是推断,不要以为仅仅是括号就能够前后随便用
{
temp = temp->next; //n大于size也会溢出
}
return temp->value;
//这里temp指向了一个也有其它指针指向节点,可不要delete
}
}
template<class T> void Flist<T>::Deeply_Copy(const Flist& Copylist)
{
front = end = 0;
if (Copylist.front == 0)
return ; //这里起到了结束函数的作用
front = new Node<T>;
Node<T>* cp = Copylist.front;
Node<T>* np = front;
np->value = cp->value;
count = 1;
np->pre = 0;
cp = cp->next;
while (cp != 0)
{
np->next = new Node<T>;
count++;
(np->next)->pre = np;
np = np->next;
np->value = cp->value;
cp = cp->next;
}
end = np;
}
int main()
{
Flist<int> listf;
int a[5] = {1,2,3,4,5};
for (int i = 0 ; i < 5 ; i++)
listf.push_front(a[i]);
cout << "lisrfsize: " << listf.Listsize() << endl;
for (int i = 0 ; i < 5 ; i++)
listf.push_back(a[i]);
cout << "lisrfsize: " << listf.Listsize() << endl;
cout << "listf is empty?
: " << listf.Isempty() << endl;
Flist<int> listf2(listf);
cout << "Listf2size : " << listf2.Listsize() << endl;
listf2.del_front();
listf2.del_back();
cout << "Listf2 front :" << listf2.Listfront() << endl;
cout << "Listf2 end :" << listf2.Listend() << endl;
cout << "Listf2 size :" << listf2.Listsize() << endl;
for (int i = 1; i <= listf2.Listsize(); i++)
cout << listf2.ShowKey(i) << endl;
return 0;
}