我要慢慢研读~
#ifndef LISTDEFINE_H_INCLUDED
#define LISTDEFINE_H_INCLUDED
#include "pubuse.h"
template <typename T>
class LinkList
{
public:
struct node
{
T date;
node* next;
node* pre;
};
private:
node* head;
node* tail;
int dire;
int Size;
public:
LinkList()
{
head = CreatNode();
tail = CreatNode();
head->pre = NULL; head->next = tail;
tail->next = NULL; tail->pre = head;
dire = 1;
Size = 0;
}
~LinkList()
{
Clear();
free(head);
free(tail);
}
node* CreatNode()
{
node* p = (node*)malloc(sizeof(node));
p->next = p->pre = NULL;
return p;
}
int GetSize()
{
return Size;
}
T* GetFront()
{
if (dire)
return GetTheFront();
else
return GetTheBack();
}
T* GetBack()
{
if (dire)
return GetTheBack();
else
return GetTheFront();
}
private:
T* GetTheFront()
{
return &(head->next->date);
}
T* GetTheBack()
{
return &(tail->pre->date);
}
Status InsertBack(const T& date)
{
node* p = CreatNode();
if (p == NULL)
return ERROR;
p->date = date;
node* q = tail->pre;
q->next = p; p->pre = q;
p->next = tail; tail->pre = p;
Size++;
return OK;
}
Status InsertFront(const T& date)
{
node* p = CreatNode();
if (p == NULL)
return ERROR;
p->date = date;
node* q = head->next;
head->next = p; p->pre = head;
p->next = q; q->pre = p;
Size++;
return OK;
}
Status InsertByIndexFront(const T& date, int index)
{
if (index > Size)
return ERROR;
if (index == Size)
{
InsertBack(date);
return OK;
}
if (index == 0)
{
InsertFront(date);
return OK;
}
node* New = CreatNode();
if (New == NULL)
return ERROR;
New->date = date;
int cnt = 0;
node* p = head->next;
while (cnt < index)
{
p = p->next;
cnt++;
}
node* q = p->pre;
q->next = New; New->pre = q;
p->pre = New; New->next = p;
Size++;
return OK;
}
Status InsertByIndexBack(const T& date, int index)
{
if (index > Size)
return ERROR;
if (index == Size)
{
InsertFront(date);
return OK;
}
if (index == 0)
{
InsertBack(date);
return OK;
}
node* New = CreatNode();
if (New == NULL)
return ERROR;
New->date = date;
int cnt = 0;
node* p = tail->pre;
while (cnt < index)
{
p = p->pre;
cnt++;
}
node* q = p->pre;
p->next = New; New->pre = p;
q->pre = New; New->next = q;
Size++;
return OK;
}
int FindByIndexFront(const T& date)
{
int cnt = 0;
node* p = head->next;
while (p != tail && p->date != date)
{
p = p->next;
cnt++;
}
if (p == tail)
return ERROR;
return cnt;
}
int FindIndexBack(const T& date)
{
int cnt = 0;
node* p = tail->pre;
while (p != head && p->date != date)
{
p = p->pre;
cnt++;
}
if (p == head)
return ERROR;
return cnt;
}
T FindByIndexFront(int index)
{
int cnt = 0;
node* p = head->next;
while (cnt < index)
{
p = p->next;
cnt++;
}
return p->date;
}
T FindByIndexBack(int index)
{
int cnt = 0;
node* p = tail->pre;
while (cnt < index)
{
p = p->pre;
cnt++;
}
return p->date;
}
T* GetPByIndexFront(int index)
{
int cnt = 0;
node* p = head->next;
while (cnt < 0)
{
cnt++;
p = p->next;
}
return p;
}
T* GetPByIndexBack(int index)
{
int cnt = 0;
node* p = tail->pre;
while (cnt < 0)
{
cnt++;
p = p->pre;
}
return p;
}
Status DeleteByIndexFront(int index)
{
if (Size <= index)
return ERROR;
int cnt = 0;
node* p = head->next;
while (cnt < index)
{
p = p->next;
cnt++;
}
node* q = p->pre;
q->next = p->next; p->next->pre = q;
free(p);
Size--;
return OK;
}
Status DeleteByIndexBack(int index)
{
if (Size <= index)
return ERROR;
int cnt = 0;
node* p = tail->pre;
while (cnt < index)
{
p = p->pre;
cnt++;
}
node* q = p->pre;
q->next = p->next; p->next->pre = q;
free(p);
Size--;
return OK;
}
void ShowFront(char c)
{
node* p = head->next;
while (p != tail)
{
cout << p->date << c;
p = p->next;
}
cout << endl;
}
void ShowBack(char c)
{
node* p = tail->pre;
while (p != head)
{
cout << p->date << c;
p = p->pre;
}
cout << endl;
}
public:
Status InsertOnFront(const T& date)
{
if (dire)
return InsertFront(date);
else
return InsertBack(date);
}
Status InsertOnBack(const T& date)
{
if (dire)
return InsertBack(date);
else
return InsertFront(date);
}
Status InsertByIndex(const T& date, int index)
{
if (dire)
return InsertByIndexFront(date, index);
else
return InsertByIndexBack(date, index);
}
bool Empty()
{
return Size == 0;
}
int FindByValue(const T& date)
{
if (dire)
return FindByValueFront(date);
else
return FindByValueBack(date);
}
T FindByIndex(int index)
{
if (dire)
return FindByIndexFront(index);
else
return FindByIndexBack(index);
}
T* GetPByIndex(int index)
{
if (dire)
return GetPByIndexFront(index);
else
return GetPByIndexBack(index);
}
T* GetTail()
{
if (dire)
return tail;
else
return head;
}
T* GetHead()
{
if (dire)
return head;
else
return tail;
}
Status DeleteByValue(const T& date)
{
if (dire)
return DeleteByValueFront(date);
else
return DeleteByValueBack(date);
}
Status DeleteByIndex(int index)
{
if (dire)
return DeleteByIndexFront(index);
else
return DeleteByIndexBack(index);
}
Status Clear()
{
node* p = head->next;
while (p != tail)
{
node* q = p->next;
free(p);
p = q;
}
Size = 0;
return OK;
}
void Unique()
{
node* p = head->next;
node* q = p->next;
while (q != tail && q != NULL)
{
while (p->date == q->date)
{
q = q->next;
free(p->next);
p->next = q;
q->pre = p;
Size--;
}
p = q;
q = q->next;
}
}
void Reverse()
{
dire = !dire;
}
void Show(char c)
{
if (dire)
ShowFront(c);
else
ShowBack(c);
}
LinkList& operator = (const LinkList& a)
{
if (!this->Empty())
this->Clear();
int cnt = 0;
while (cnt < a.GetSize())
{
this->InsertOnBack(a.GetPByIndex(cnt++));
}
return *this;
}
};
#endif // LISTDEFINE_H_INCLUDED
神啊
最新推荐文章于 2023-08-05 17:47:34 发布