#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct tagNote
{
int nNumber;
struct tagNote* pNext;
}Note;
bool CreateList(Note*&pListHead);
bool DisposeList(Note*&pListHead);
bool ListInsertItem(Note*&pListHead, int nValue, int nIndex = -1);
bool ListItemPushBack(Note*&pListHead,int nValue);
bool ListItemPushFront(Note*&pListHead,int nValue);
bool ListDeleteItem(Note*&pListItem,int nIndex=-1);
bool ListDeleteFirstItem(Note*&pListHead);
bool ListDeleteLastItem(Note*&pListHead);
bool ListGetFirstItem(Note*&pListHead,int &nValue);
bool ListGetLastItem(Note*&pListHead,int &nValue);
bool ListGetItemNumber(Note*&pListHead,int &nNumber);
bool ListGetItem(Note*&pListHead,int&nValue,int nIndex=-1);
bool ListPrint(Note*&pListHead);
void ShowMenu();
int main()
{
Note*pListHead = NULL;
int nSelect = 0;
bool isLoop = true;
int nTmpInput1, nTmpInput2, nTmpOut;
while (isLoop)
{
ShowMenu();
cin >> nSelect;
switch (nSelect)
{
case 0:
isLoop = false;
break;
case 1:
if (!CreateList(pListHead))
{
cout << "创建链表失败" << endl;
}
else
{
cout << "创建链表成功" << endl;
}
break;
case 2:
cout << "请输入插入项的值:" << endl;
cin >> nTmpInput1;
if (!ListItemPushFront(pListHead, nTmpInput1))
{
cout << "输入插入值(" << nTmpInput1 << ")失败" << endl;
}
else
{
cout << "插入项成功" << endl;
}
break;
case 3:
cout << "请输入插入项的值:" << endl;
cin >> nTmpInput1;
if (!ListItemPushBack(pListHead, nTmpInput1))
{
cout << "输入插入值(" << nTmpInput1 << ")失败" << endl;
}
else
{
cout << "插入值成功" << endl;
}
break;
case 4:
cout << "请输入插入项的值:" << endl;
cin >> nTmpInput1;
cout << "请输入插入项的位置:" << endl;
cin >> nTmpInput2;
if (!ListInsertItem(pListHead, nTmpInput1, nTmpInput2))
{
cout << "输入插入值(" << nTmpInput1 << ")在位置" << nTmpInput2 << ")失败" << endl;
}
else
{
cout << "插入项成功" << endl;
}
break;
case 5:
if (!ListGetFirstItem(pListHead, nTmpOut))
{
cout << "取第一个值失败" << endl;
}
else
{
cout << "取第一个值失败" << nTmpOut << endl;
}
break;
case 6:
if (!ListGetLastItem(pListHead, nTmpOut))
{
cout << "取最后一个值失败" << endl;
}
else
{
cout << "取最后一个值为" << nTmpOut << endl;
}
break;
case 7:
cout << "请输入要取值的位置:" << endl;
cin >> nTmpInput1;
if (!ListGetItem(pListHead, nTmpOut, nTmpInput1))
{
cout << "取值在位置(" << nTmpInput1 << ")失败" << endl;
}
else
{
cout << "取值在位置(" << nTmpInput1 << ")值为" << nTmpOut << endl;
}
break;
case 8:
if (!ListDeleteFirstItem(pListHead))
{
cout << "删除第一个项目失败" << endl;
}
else
{
cout << "删除第一个项目成功" << endl;
}
break;
case 9:
if (!ListDeleteLastItem(pListHead))
{
cout << "删除最后一个项目失败" << endl;
}
else
{
cout << "删除最后一个项目失败" << endl;
}
break;
case 10:
cout << "请输入要删除项的位置:" << endl;
cin >> nTmpInput1;
if (!ListDeleteItem(pListHead, nTmpInput1))
{
cout << "删除第(" << nTmpInput1 << ")项目失败" << endl;
}
else
{
cout << "删除第(" << nTmpInput1 << ")项目成功" << endl;
}
case 11:
if (!ListGetItemNumber(pListHead, nTmpOut))
{
cout << "取项目个数失败" << endl;
}
else
{
cout << "取项目个数为" << nTmpOut << endl;
}
break;
case 12:
if (!DisposeList(pListHead))
{
cout << "销毁链失败" << endl;
}
else
{
cout << "销毁链成功" << endl;
}
case 13:
ListPrint(pListHead);
break;
default:
break;
}
if (isLoop)
{
system("pause");
system("cls");
}
}
DisposeList(pListHead);
return 0;
}
void ShowMenu()
{
cout << "--------------------------------" << endl;
cout << "1.\t创建一个单向链表" << endl;
cout << "2.\t从头部插入一个项" << endl;
cout << "3.\t从尾部插入一个项" << endl;
cout << "4.\t从指定位置插入一个项" << endl;
cout << "5.\t取出第一个项的值" << endl;
cout << "6.\t取出最后一个项的值" << endl;
cout << "7.\t取出指定项的值" << endl;
cout << "8.\t删除第一个项的值" << endl;
cout << "9.\t删除最后一个项的值" << endl;
cout << "10.\t删除指定项的值" << endl;
cout << "11.\t得到表中的项目个数" << endl;
cout << "12.\t销毁当前链表" << endl;
cout << "13.\t打印整个链表" << endl;
cout << "0.\t推出程序" << endl;
cout << "--------------------------------" << endl;
cout << "请输入你的选择:\t";
}
bool CreateList(Note*&pListHead)
{
if (pListHead != NULL)
{
return false;
}
else
{
pListHead = new Note;
pListHead->nNumber = 0;
pListHead->pNext = NULL;
return true;
}
}
bool DisposeList(Note*&pListHead)
{
if (pListHead == NULL)
{
return false;
}
else
{
while (ListDeleteLastItem(pListHead))
{
delete pListHead;
pListHead = NULL;
}
return true;
}
}
bool ListInsertItem(Note*&pListHead, int nValue, int nIndex)
{
if (pListHead == NULL)
{
return false;
}
int nNum = 0;
ListGetItemNumber(pListHead, nNum);
if (nIndex > nNum)
{
return false;
}
else if (nIndex == -1)
{
nIndex = nNum;
}
Note*pTmp = pListHead;
for (int i = 0; i < nIndex; i++)
{
pTmp = pTmp->pNext;
}
Note*pNext = pTmp->pNext;
pTmp->pNext = new Note;
pTmp->pNext->nNumber = nValue;
pTmp->pNext->pNext = pNext;
return true;
}
bool ListItemPushFront(Note*&pListHead, int nValue)
{
return ListInsertItem(pListHead, nValue, 0);
}
bool ListItemPushBack(Note*&pListHead, int nValue)
{
return ListInsertItem(pListHead, nValue, -1);
}
bool ListDeleteItem(Note*&pListHead, int nIndex)
{
if (pListHead == NULL)
{
return false;
}
int nNum = 0;
ListGetItemNumber(pListHead, nNum);
if (nNum == 0)
{
return false;
}
if (nIndex > nNum)
{
return false;
}
else if (nIndex == -1)
{
nIndex = nNum - 1;
}
Note*pTmp = pListHead;
for (int i = 0; i < nIndex; i++ )
{
pTmp = pTmp->pNext;
}
Note*pNext = pTmp->pNext->pNext;
delete pTmp->pNext;
return true;
}
bool ListDeleteFirstItem(Note*&pListHead)
{
return ListDeleteItem(pListHead, 0);
}
bool ListDeleteLastItem(Note*&pListHead)
{
return ListDeleteItem(pListHead, -1);
}
bool ListGetItem(Note*&pListHead, int&nValue, int nIndex)
{
if (pListHead == NULL)
{
return NULL;
}
int nNum = 0;
ListGetItemNumber(pListHead, nNum);
if (nIndex >= nNum)
{
return false;
}
else if (nIndex == -1)
{
nIndex = nNum;
}
Note*pTmp = pListHead;
for (int i = 0; i <= nIndex; i++)
{
pTmp = pTmp->pNext;
}
nValue = pTmp->nNumber;
return true;
}
bool ListGetFirstItem(Note*&pListHead, int&nValue)
{
return ListGetItem(pListHead, nValue, 0);
}
bool ListGetLastItem(Note*&pListHead, int&nValue)
{
return ListGetItem(pListHead, nValue, -1);
}
bool ListGetItemNumber(Note*&pListHead, int&nNumber)
{
if (pListHead == NULL)
{
return false;
}
Note*pTmp = pListHead->pNext;
nNumber = 0;
while (pTmp != NULL)
{
nNumber++;
pTmp = pTmp->pNext;
}
return true;
}
bool ListPrint(Note*&pListHead)
{
if (pListHead == NULL)
{
return false;
}
Note*pTmp = pListHead->pNext;
int nIndex = 0;
while (pTmp != NULL)
{
cout << nIndex++ << ":\t" << pTmp->nNumber << endl;
pTmp = pTmp->pNext;
}
return true;
}
转载于:https://blog.51cto.com/zhangyongjian/1398842