#include <iostream>
using namespace std;
struct Infor
{
int nLocal;
char szWord;
Infor *next;
};
class Test_2
{
public:
Test_2();
~Test_2();
void InterFace();
void BuildList();
void SearchOne();
void DeleteOne();
void GetListLength();
void GetListLocal();
void ShowList();
void Exit();
protected:
private:
struct Infor *m_pHead;
struct Infor *m_pTail;
int m_nSum;
};
Test_2::Test_2()
{
m_pTail = m_pHead = new struct Infor;
m_pHead->next = NULL;
m_nSum = 0;
}
Test_2::~Test_2()
{
if (m_pHead)
{
delete[]m_pHead;
m_pHead = NULL;
}
}
void Test_2::BuildList()
{
// cout << "输入你要插入的组数" << endl;
int i, j, k, l = 1;
static int s = 0;
cin >> i;
/* if (0 >= i || m_nSum < i)
{
cout << "位置不正确" << endl;
}
*/
char szTemp;
struct Infor *p = NULL;
for( k = 0; k < i; k++) //使用尾插法
{
p = new struct Infor;
p->next = NULL;
if (0 == s)
{
// cout << "输入插入的位置和元素" << endl;
cin >> j >> szTemp;
p->szWord = szTemp;
p->nLocal = j;
m_pTail->next = p;
m_pTail = p;
}
else
{
// cout << "输入插入的位置和元素" << endl;
cin >> j >> szTemp;
m_pTail = m_pHead;
p->szWord = szTemp;
p->nLocal = j;
while ((NULL !=m_pTail) && (l < j))
{
m_pTail = m_pTail->next;
l++;
}
p->next = m_pTail;
m_pTail->next = p;
}
m_nSum++; //记录全部的节点数
}
s++;
InterFace();
}
void Test_2::SearchOne()
{
// cout << "输入要查找的元素" <<endl;
char szTemp;
cin >> szTemp;
struct Infor *p = NULL;
p = m_pHead->next;
while (NULL != p)
{
if (szTemp == p->szWord)
{
cout << p->nLocal << endl;
goto Exit0;
}
p = p->next;
}
cout << "None" << endl;
Exit0:
InterFace();
}
void Test_2::DeleteOne()
{
// cout << "输入你要删除的元素" << endl;
int nNum, i = 0;
cin >> nNum;
struct Infor *p = m_pHead, *p2 = m_pHead;
while (nNum > i)
{
p2 = p;
p = p->next;
i++;
}
if (NULL != p)
{
if (NULL != p->next) //不是尾节点
{
cout << p->szWord << endl;
p2->next = p->next;
}
m_nSum--;
delete p;
}
else
{
cout << "位置不正确" << endl;
}
InterFace();
}
void Test_2::GetListLength()
{
cout << m_nSum << endl;
InterFace();
}
void Test_2::Exit()
{
exit(0);
}
void Test_2::GetListLocal()
{
// cout << "输入要查找的位置" << endl;
int i, j = 0;
cin >> i;
struct Infor *p = m_pHead;
if (NULL == p)
{
cout << "输入错误" << endl;
goto Exit;
}
else if (m_nSum < i)
{
cout << "输入错误" << endl;
goto Exit;
}
else if (i < 0)
{
cout << "输入错误" << endl;
goto Exit;
}
while (i > j)
{
p = p->next;
j++;
}
if (NULL != p)
{
cout << p->szWord << endl;
}
else
{
cout << "None" << endl;
}
Exit:
InterFace();
}
void Test_2::ShowList()
{
struct Infor *p = m_pHead->next;
if (NULL == p)
{
goto Exit;
}
while(NULL != p)
{
cout << p->szWord << endl;
p = p->next;
}
Exit:
InterFace();
}
void Test_2::InterFace()
{
char szTemp;
/* cout << "输入选项" << endl;
cout << "输入插入的: :I" << endl;
cout << "查找某个元素 :S" << endl;
cout << "获取某个位置的元素 :G" << endl;
cout << "删除某个位置的元素 :D" << endl;
cout << "遍历输出所有元素 :V" << endl;
cout << "表长 :L" << endl;
cout << "退出 :E" << endl;
*/
cin >> szTemp;
switch (szTemp)
{
case 'I' | 'i':
BuildList();
break;
case 'V' | 'v':
ShowList();
break;
case 'E' | 'e':
Exit();
break;
case 'S' | 's':
SearchOne();
break;
case 'D' | 'd':
DeleteOne();
break;
case 'L' | 'l':
GetListLength();
break;
case 'G' | 'g':
GetListLocal();
break;
default:
break;
}
}
int main()
{
Test_2 t2;
t2.InterFace();
return 0;
}