#pragma once
#include<iostream>
using namespace std;
#define MAXSIZE 201
enum NODEUSE
{
e_NOUSE = -1,
e_LAST=-2,
};
template<class T>
struct STNode
{
T data;
int cur;
};
template<class T>
class StaticList
{
public:
StaticList();
~StaticList();
public:
int findAnIdlePos();
bool ListInsert(int pos,const T&e);
bool ListDelete(int pos);
bool GetElem(int pos, T& e);;
int LocateElem(const T& e);
void DispList();
int LengthList();
bool EmptyList();
private:
STNode<T> m_data[MAXSIZE];
int m_length;
};
template<class T>
StaticList<T>::StaticList()
{
for (int i = 1; i < MAXSIZE; i++)
{
m_data[i].cur = e_NOUSE;
}
m_length = 0;
}
template<class T>
int StaticList<T>::findAnIdlePos()
{
for (int i = 1; i < MAXSIZE; i++)
{
if (m_data[i].cur == e_NOUSE)
{
return i;
}
}
return -1;
}
template<class T>
bool StaticList<T>::ListInsert(int pos, const T& e)
{
if (pos<1 || pos>(m_length + 1))
{
cout << "不合法" << endl;
return false;
}
int index;
if ((index=findAnIdlePos()) == -1)
{
cout << "满了" << endl;
return false;
}
int dataCount = 1;
int indexPrev;
if (pos == 1)
{
m_data[index].data = e;
if (m_length == 0)
{
m_data[index].cur = e_LAST;
}
else
{
m_data[index].cur = m_data[0].cur;
}
m_data[0].cur = index;
}
else
{
int posCount = 0;
int tmpcur = m_data[0].cur;
while (true)
{
posCount++;
if (posCount >= (pos - 1))
{
indexPrev = tmpcur;
break;
}
}
int iTmpcurr = m_data[indexPrev].cur;
m_data[indexPrev].cur = index;
m_data[index].data = e;
m_data[index].cur = iTmpcurr;
}
m_length++;
cout << "插入成功" << endl;
return true;
}
template<class T>
bool StaticList<T>::ListDelete(int pos)
{
if (m_length < 1 || pos<1 || pos>m_length)
{
cout << "不合法" << endl;
return false;
}
int tmpcur = m_data[0].cur;
if (pos == 1)
{
if (m_length != 1)
{
m_data[0].cur = m_data[tmpcur].cur;
}
m_data[tmpcur].cur = e_NOUSE;
cout << "删除成功" << endl;
}
else
{
while (true)
{
posCount++;
if (posCount >= (pos - 1))
{
indexPrev = tmpcur;
break;
}
tmpcur= m_data[tmpcur].cur;
}
int iTmpcurr = m_data[indexPrev].cur;
m_data[indexPrev].cur = m_data[iTmpcurr].cur;
m_data[iTmpcurr].cur = e_NOUSE;
cout << "删除成功" << endl;
}
m_length--;
return true;
}
template<class T>
bool StaticList<T>::GetElem(int pos, T& e)
{
if (m_length < 1 || pos<1 || pos>m_length)
{
cout << "不合法" << endl;
return false;
}
int tmpcur = m_data[0].cur;
int ppos = 0;
while (true)
{
ppos++;
if (ppos == pos)
{
e = m_data[tmpcur].data;
cout << "找到了" << e << endl;
return true;
}
tmpcur = m_data[tmpcur].cur;
}
return false;
}
template<class T>
int StaticList<T>::LocateElem(const T&e)
{
if (m_length < 1 || pos<1 || pos>m_length)
{
cout << "不合法" << endl;
return -1;
}
int tmpcur = m_data[0].cur;
int ppos = 0;
while (true)
{
ppos++;
if (m_data[tmpcur].data == e && m_data[tmpcur].cur != e_NOUSE)
{
cout << "找到了" << ppos << endl;
return tmpcur;
}
if (m_data[tmpcur].cur == e_LAST)
{
break;
}
tmpcur = m_data[tmpcur].cur;
}
cout << "没找到" << endl;
return -1;
}
template<class T>
void StaticList<T>::DispList()
{
if (m_length < 1)
{
cout << "链表为空" << endl;
}
int tmpcur = m_data[0].cur;
while (true)
{
cout << m_data[tmpcur].data << " ";
if ((tmpcur = m_data[tmpcur].cur) == e_LAST)
{
break;
}
cout << endl;
}
}
template<class T>
int StaticList<T>::LengthList()
{
return m_length;
}
template<class T>
bool StaticList<T>::EmptyList()
{
if (m_length < 1)
return true;
return false;
}
template<class T>
StaticList<T>::~StaticList()
{
}
#pragma once
#include<iostream>
using namespace std;
template<class T>
struct DNode
{
T data;
DNode<T>* prior;
DNode<T>* next;
};
template<class T>
class DList
{
public:
DList();
~DList();
public:
bool DListInsert(int i, const T& e);
bool DListDelete(int i);
bool GetElem(int i,T&e);
int LocateElem(const T& e);
int GetLength();
bool Empty();
void DispDList();
private:
DNode<T>* m_head;
int m_length;
};
template<class T>
DList<T>::DList()
{
m_head = new DNode<T>;
m_head->next = nullptr;
m_head->prior = nullptr;
m_length = 0;
}
template<class T>
bool DList<T>::DListInsert(int i, const T& e)
{
if (i<1 || i>(m_length + 1))
{
cout << "不合法" << endl;
return false;
}
DNode<T>* p_curr = m_head;
for (int j = 0; j < (i - 1); j++)
{
p_curr = p_curr->next;
}
DNode<T>* node = new DNode<T>;
node->data = e;
node->next = p_curr->next;
node->prior = p_curr;
if (p_curr->next!= nullptr)
{
p_curr->next->prior = node;
}
p_curr->next = node;
m_length++;
cout << "插入成功" << endl;
return true;
}
template<class T>
bool DList<T>::DListDelete(int i)
{
if (i<1 || i>(m_length + 1)||m_length<1)
{
cout << "删除不合法" << endl;
return false;
}
DNode<T>* p_curr = m_head;
for (int j = 0; j < (i - 1); j++)
{
p_curr = p_curr->next;
}
DNode<T>* p_willptr = p_curr->next;
p_curr->next = p_willptr->next;
if (p_willptr->next != nullptr)
{
p_willptr->next->prior = p_curr;
}
delete p_willptr;
m_length--;
cout << "删除成功" << endl;
return true;
}
template<class T>
bool DList<T>::GetElem(int i, T& e)
{
if (i<1 || i>m_length || m_length < 1)
{
cout << "链表为空" << endl;
return false;
}
DNode<T>* p_curr = m_head;
for (int j = 0; j < (i - 1); j++)
{
p_curr = p_curr->next;
}
e = p_curr->next->data;
cout << "查找值:" << p_curr->next->data << endl;
return true;
}
template<class T>
int DList<T>::LocateElem(const T& e)
{
DNode<T>* p_curr = m_head;
for (int j = 0; j <= m_length; j++)
{
if (p_curr->next->data == e)
{
cout << "找到:" << p_curr->next->data << " ";
return j;
}
p_curr = p_curr->next;
}
cout << "找不到" << endl;
return -1;
}
template<class T>
int DList<T>::GetLength()
{
return m_length;
}
template<class T>
void DList<T>::DispDList()
{
DNode<T>* p = m_head->next;
while (p != nullptr)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
template<class T>
bool DList<T>::Empty()
{
if (m_length == 0)
{
return true;
}
return false;
}
template<class T>
DList<T>::~DList()
{
DNode<T>* pnode = m_head->next;
DNode<T>* ptmp;
while (pnode != nullptr)
{
ptmp = pnode;
pnode = pnode->next;
delete ptmp;
}
delete m_head;
m_head = nullptr;
m_length = 0;
}
#pragma once
#include<iostream>
using namespace std;
template<class T>
struct SNode
{
T data;
SNode<T>* prior;
SNode<T>* next;
};
template<class T>
class SList
{
public:
SList();
~SList();
public:
bool SListInsert(int i,const T& e);
bool DListDelete(int i);
bool GetElem(int i, T& e);
int LocateElem(const T& e);
int GetLength();
bool Empty();
void DispDList();
private:
int m_length;
SNode<T>* m_head;
};
template<class T>
SList<T>::SList()
{
m_head = new SNode<T>;
m_head->next = m_head;
m_head->prior = m_head;
m_length = 0;
}
template<class T>
bool SList<T>::SListInsert(int i, const T& e)
{
if (i<1 || i>(m_length + 1))
{
cout << "不合法" << endl;
return false;
}
SNode<T>* p_curr = m_head;
for (int j = 0; j < (i - 1); j++)
{
p_curr = p_curr->next;
}
SNode<T>* node = new SNode<T>;
node->data = e;
node->next = p_curr->next;
node->prior = p_curr;
if (p_curr->next != m_head)
{
p_curr->next->prior = node;
}
p_curr->next = node;
m_length++;
cout << "插入成功" << endl;
return true;
}
template<class T>
bool SList<T>::DListDelete(int i)
{
if (i<1 || i>(m_length + 1))
{
cout << "不合法" << endl;
return false;
}
SNode<T>* p_curr = m_head;
for (int j = 0; j < (i - 1); j++)
{
p_curr = p_curr->next;
}
SNode<T>* p_willptr = p_curr->next;
p_curr->next = p_willptr->next;
if (p_willptr->next != m_head)
{
p_willptr->next->prior = p_curr;
}
delete p_willptr;
m_length--;
cout << "删除成功" << endl;
return true;
}
template<class T>
bool SList<T>::GetElem(int i, T& e)
{
if (i<1 || i>m_length || m_length < 1)
{
cout << "链表为空" << endl;
return false;
}
SNode<T>* p_curr = m_head;
for (int j = 0; j < (i - 1); j++)
{
p_curr = p_curr->next;
}
e = p_curr->next->data;
cout << "查找值:" << p_curr->next->data << endl;
return true;
}
template<class T>
int SList<T>::LocateElem(const T& e)
{
SNode<T>* p_curr = m_head;
for (int j = 0; j <= m_length; j++)
{
if (p_curr->next->data == e)
{
cout << "找到:" << p_curr->next->data << " ";
return j;
}
p_curr = p_curr->next;
}
cout << "找不到" << endl;
return -1;
}
template<class T>
int SList<T>::GetLength()
{
return m_length;
}
template<class T>
bool SList<T>::Empty()
{
if(m_head->next==m_head)
{
return true;
}
return false;
}
template<class T>
void SList<T>::DispDList()
{
SNode<T>* p = m_head;
while (p->next != m_head )
{
cout << p->next->data << " ";
p = p->next;
}
cout << endl;
}
template<class T>
SList<T>::~SList()
{
SNode<T>* pnode = m_head->next;
SNode<T>* ptmp;
while (pnode != m_head)
{
ptmp = pnode;
pnode=pnode->next;
delete ptmp;
}
delete m_head;
m_head = nullptr;
m_length = 0;
}
void test_List_D()
{
DList<int> dl;
dl.DListInsert(1, 10);
dl.DListInsert(1, 20);
dl.DListInsert(1, 30);
dl.DispDList();
dl.DListDelete(1);
dl.DispDList();
int val = 0;
dl.GetElem(1, val);
int pos = dl.LocateElem(10);
if (pos != -1)
{
cout << "该值下标为:" << pos << endl;
}
}
void test_List_S()
{
SList<int> sl;
sl.SListInsert(1, 10);
sl.SListInsert(1, 20);
sl.DispDList();
sl.DListDelete(1);
sl.DispDList();
}
int main()
{
test_List_D();
test_List_S();
system("pause");
return 0;
}