📄 list_.cpp
字号:
#include "List.h"
template <class T>
List<T>::List()
{
head = NULL;
pre = NULL;
cur = NULL;
total = 0; //暂时的语句
}
template <class T>
List<T>::~List()
{
fstream file;
file.open("bookDatas.txt",ios::out|ios::binary);
if(!file)
{
cout<<"bookDatas文件打开失败!";
exit(0);
}
if(listIsEmpty())
return;
else
{
toHead();
while(!atEnd())
{
file<<displayCurData()<<endl;
advance();
}
file<<displayCurData()<<endl;
file.close();
toHead();
advance();
while(!atEnd())
{
delete pre;
advance();
}
delete cur;
}
}
template <class T>
long List<T>::getTotal()
{
return total;
}
template <class T>
bool List<T>::atHead()
{
return cur == head;
}
template <class T>
bool List<T>::atEnd()
{
return cur->next == NULL;
}
template <class T>
bool List<T>::listIsEmpty()
{
return head == NULL;
}
template <class T>
bool List<T>::curIsEmpty()
{
return cur == NULL;
}
template <class T>
void List<T>::toHead()
{
pre = NULL;
cur = head;
}
template <class T>
void List<T>::advance()
{
if(!atEnd())
{
pre = cur;
cur = cur->next;
}
}
template <class T>
dataNode<T> * List<T>::newNode(const T &obj)
{
dataNode<T> * ptr = new dataNode<T>; // T yao ma?
ptr->data = obj;
ptr->next = NULL;
return ptr;
}
template <class T>
void List<T>::destroyNode(const dataNode<T> * ptr)
{
delete ptr;
}
template <class T>
void List<T>::nodeReferenceError()
{
cout<<"当前没有节点或是不合理的节点,即将结束程序!"<<endl;
exit(0);
}
template <class T>
dataNode<T> * List<T>::creatList(fstream &file)
{
T temp;
head = NULL;
pre = NULL;
cur = NULL;
file>>temp;
while(!file.eof())
{
insertAfter(temp);
file>>temp;
// cout<<file.tellg()<<endl; //调试用,以后要删掉
}
// insertAfter(temp); //不用!因为文件将读最后一个空隔
toHead();
return cur;
}
template <class T>
void List<T>::storeInfo(const T &obj )
{
cur->data = obj;
}
template <class T>
void List<T>::insert(const T &obj)
{
dataNode<T> * temp = newNode(obj);
if(listIsEmpty())
{
head = temp;
cur = temp;
pre = NULL;
}
else if(curIsEmpty())
{
nodeReferenceError();
}
else if(atHead())
{
temp->next = cur;
head = temp;
cur = temp;
}
else
{
temp->next = cur;
pre->next = temp;
cur = temp;
}
total++;
}
template <class T>
void List<T>::insertAfter(const T &obj)
{
dataNode<T> * temp = newNode(obj);
if(listIsEmpty())
{
head = temp;
cur = temp;
pre = NULL;
}
else if(curIsEmpty())
{
nodeReferenceError();
}
else
{
pre = cur;
temp->next = cur->next;
cur->next = temp;
cur = temp;
}
total++;
}
template <class T>
bool List<T>::deleteNode(char goal[])
{
bool sign = false;
dataNode<T> * temp = NULL; //为了释放被删除的指针
if(!search(goal))
{
cout<<"没有查到数据。"<<endl; //如果没有要删除的数据会重复输出提示
return sign;
}
else if(atHead())
{
if(cur->next == NULL ) //链表中只有一个数据,且为所查找的数据
{
delete cur;
head = NULL;
cur = NULL;
pre = NULL;
}
else
{
head = cur->next;
delete cur;
cur = head;
}
}
else if(atEnd())
{
pre->next = NULL;
delete cur;
toHead(); //如果要删除的数据在末尾,CUR将指向HEAD;
}
else
{
temp = NULL;
delete cur;
cur = temp;
pre->next = cur;
}
total--;
sign = true;
return sign;
}
template <class T>
bool List<T>::search(char goal[])
{
bool sign = false;
toHead();
if(listIsEmpty())
{
cout<<"链表为空,没有要搜索的数据。"<<endl;
return false;
}
while(!atEnd())
{
if(cur->data == goal)
return true;
advance();
}
if(cur->data == goal)
return true;
return false;
}
template <class T>
T List<T>::displayCurData()
{
return cur->data;
}
template <class T>
void List<T>::compositor() //功能未完成
{
dataNode<T> * temp = NULL;
long count;
toHead();
if(listIsEmpty())
{
cout<<"目前没有数据!"<<endl;
return;
}
else if(cur->next == NULL)
{
return;
}
else if((cur->next)->next ==NULL)
{
if(cur->data > (cur->next)->data)
{
head = head->next;
head->next = cur;
cur->next = NULL;
cur = head;
}
}
else
for(count = 0;count <= total;count++) //冒泡排序
{
toHead();
if(cur->data > (cur->next)->data)
{
head = head->next;
temp = head->next;
head->next = cur;
cur->next = temp;
cur = head;
}
advance();
while(!atEnd())
{
if(cur->data > (cur->next)->data)
{
temp = cur;
pre->next = cur->next;
cur->next = (cur->next)->next;
(pre->next)->next = temp;
cur = pre->next;
}
advance();
}
}
}
template <class T>
void List<T>::displayAllData()
{
int count = 1;
if(listIsEmpty())
{
cout<<"目前没有数据!"<<endl;
return;
}
else
{
toHead();
for(;!atEnd();count++)
{
cout<<displayCurData()<<endl;
advance();
if(!(count %= 20))
{
cout<<"按任意键继续:"<<endl;
getch();
}
}
cout<<displayCurData()<<endl;
}
}
/*template <class T>
List<T>::List()
{
head = NULL;
pre = NULL;
cur = NULL;
total = 0; //暂时的语句
}
template <class T>
List<T>::~List()
{
fstream file;
file.open("bookDatas.txt",ios::out|ios::binary);
if(!file)
{
cout<<"bookDatas文件打开失败!";
exit(0);
}
if(listIsEmpty())
return;
else
{
toHead();
while(!atEnd())
{
file<<displayCurData()<<endl;
advance();
}
file<<displayCurData()<<endl;
file.close();
toHead();
advance();
while(!atEnd())
{
delete pre;
advance();
}
delete cur;
}
}
template <class T>
long List<T>::getTotal()
{
return total;
}
template <class T>
bool List<T>::atHead()
{
return cur == head;
}
template <class T>
bool List<T>::atEnd()
{
return cur->next == NULL;
}
template <class T>
bool List<T>::listIsEmpty()
{
return head == NULL;
}
template <class T>
bool List<T>::curIsEmpty()
{
return cur == NULL;
}
template <class T>
void List<T>::toHead()
{
pre = NULL;
cur = head;
}
template <class T>
void List<T>::advance()
{
if(!atEnd())
{
pre = cur;
cur = cur->next;
}
}
template <class T>
dataNode<T> * List<T>::newNode(const T &obj)
{
dataNode * ptr = new dataNode<T>; // T yao ma?
ptr->data = obj;
ptr->next = NULL;
return ptr;
}
template <class T>
void List<T>::destroyNode(const dataNode<T> * ptr)
{
delete ptr;
}
template <class T>
void List<T>::nodeReferenceError()
{
cout<<"当前没有节点或是不合理的节点,即将结束程序!"<<endl;
exit(0);
}
template <class T>
dataNode<T> * List<T>::creatList(fstream &file)
{
T temp;
head = NULL;
pre = NULL;
cur = NULL;
file>>temp;
while(!file.eof())
{
insertAfter(temp);
file>>temp;
// cout<<file.tellg()<<endl; //调试用,以后要删掉
}
// insertAfter(temp); //不用!因为文件将读最后一个空隔
toHead();
return cur;
}
template <class T>
void List<T>::storeInfo(const T &obj )
{
cur->data = obj;
}
template <class T>
void List<T>::insert(const T &obj)
{
dataNode * temp = newNode(obj);
if(listIsEmpty())
{
head = temp;
cur = temp;
pre = NULL;
}
else if(curIsEmpty())
{
nodeReferenceError();
}
else if(atHead())
{
temp->next = cur;
head = temp;
cur = temp;
}
else
{
temp->next = cur;
pre->next = temp;
cur = temp;
}
total++;
}
template <class T>
void List<T>::insertAfter(const T &obj)
{
dataNode * temp = newNode(obj);
if(listIsEmpty())
{
head = temp;
cur = temp;
pre = NULL;
}
else if(curIsEmpty())
{
nodeReferenceError();
}
else
{
pre = cur;
temp->next = cur->next;
cur->next = temp;
cur = temp;
}
total++;
}
template <class T>
bool List<T>::deleteNode(char goal[])
{
bool sign = false;
dataNode * temp = NULL; //为了释放被删除的指针
if(!search(goal))
{
cout<<"没有查到数据。"<<endl; //如果没有要删除的数据会重复输出提示
return sign;
}
else if(atHead())
{
if(cur->next == NULL ) //链表中只有一个数据,且为所查找的数据
{
delete cur;
head = NULL;
cur = NULL;
pre = NULL;
}
else
{
head = cur->next;
delete cur;
cur = head;
}
}
else if(atEnd())
{
pre->next = NULL;
delete cur;
toHead(); //如果要删除的数据在末尾,CUR将指向HEAD;
}
else
{
temp = NULL;
delete cur;
cur = temp;
pre->next = cur;
}
total--;
sign = true;
return sign;
}
template <class T>
bool List<T>::search(char goal[])
{
bool sign = false;
toHead();
if(listIsEmpty())
{
cout<<"链表为空,没有要搜索的数据。"<<endl;
return false;
}
while(!atEnd())
{
if(strcmp(cur->data.getISBN(),goal) == 0)
return true;
advance();
}
if(strcmp(cur->data.getISBN(),goal) == 0)
return true;
return false;
}
template <class T>
T List<T>::displayCurData()
{
return cur->data;
}
template <class T>
void List<T>::compositor() //功能未完成
{
dataNode * temp = NULL;
long count;
toHead();
if(listIsEmpty())
{
cout<<"目前没有数据!"<<endl;
return;
}
else if(cur->next == NULL)
{
return;
}
else if((cur->next)->next ==NULL)
{
if(cur->data > (cur->next)->data)
{
head = head->next;
head->next = cur;
cur->next = NULL;
cur = head;
}
}
else
for(count = 0;count <= total;count++) //冒泡排序
{
toHead();
if(cur->data > (cur->next)->data)
{
head = head->next;
temp = head->next;
head->next = cur;
cur->next = temp;
cur = head;
}
advance();
while(!atEnd())
{
if(cur->data > (cur->next)->data)
{
temp = cur;
pre->next = cur->next;
cur->next = (cur->next)->next;
(pre->next)->next = temp;
cur = pre->next;
}
advance();
}
}
}
template <class T>
void List<T>::displayAllData()
{
int count = 1;
if(listIsEmpty())
{
cout<<"目前没有数据!"<<endl;
return;
}
else
{
toHead();
for(;!atEnd();count++)
{
cout<<displayCurData()<<endl;
advance();
if(!(count %= 20))
{
cout<<"按任意键继续:"<<endl;
getch();
}
}
cout<<displayCurData()<<endl;
}
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -