博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
单链表范例
阅读量:4495 次
发布时间:2019-06-08

本文共 5011 字,大约阅读时间需要 16 分钟。

1 #include 
2 #include
3 using namespace std; 4 typedef struct tagNote 5 { 6 int nNumber; 7 struct tagNote* pNext; 8 }Note; 9 bool CreateList(Note*& pListHead); 10 bool DisposeList(Note*& pListHead); 11 bool ListInsertItem(Note*& pListHead,int nValue,int nIndex=-1); 12 bool ListItemPushBack(Note*& pListHead,int nValue); 13 bool ListItemPushFront(Note*& pListHead,int nValue); 14 bool ListDeleteItem(Note*& pListHead,int nIndex=-1); 15 bool ListDeleteFirstItem(Note*& pListHead); 16 bool ListDeleteLastItem(Note*& pListHead); 17 bool ListGetFirstItem(Note*& pListHead,int& nValue); 18 bool ListGetLastItem(Note*& pListHead,int& nValue); 19 bool ListGetItemNumber(Note*& pListHead,int& nNumber); 20 bool ListGetItem(Note*& pListHead,int& nValue,int nIndex=-1); 21 bool ListPrint(Note*& pListHead); 22 void ShowMenu(); 23 24 int main() 25 { 26 Note* pListHead = NULL; 27 int nSelect = 0; 28 bool isLoop = true; 29 int nTmpInput1,nTmpInput2,nTmpOut; 30 while (isLoop) 31 { 32 ShowMenu(); 33 cin>>nSelect; 34 switch (nSelect) 35 { 36 case 0: 37 isLoop = false; 38 break; 39 case 1: 40 if (!CreateList(pListHead)) 41 { 42 cout<<"创建链表失败"<
>nTmpInput1; 52 if (!ListItemPushFront(pListHead,nTmpInput1)) 53 { 54 cout<<"输入插入值("<
<<")失败"<
>nTmpInput1; 64 if (!ListItemPushBack(pListHead,nTmpInput1)) 65 { 66 cout<<"输入插入值("<
<<")失败"<
>nTmpInput1; 76 cout<<"请输入插入项的位置:"<
>nTmpInput2; 78 if (!ListInsertItem(pListHead,nTmpInput1,nTmpInput2)) 79 { 80 cout<<"输入插入值("<
<<")在位置("<
<<")失败"<
>nTmpInput1;110 if (!ListGetItem(pListHead,nTmpOut,nTmpInput1))111 {112 cout<<"取值在位置("<
<<")失败"<
>nTmpInput1;142 if (!ListDeleteItem(pListHead,nTmpInput1))143 {144 cout<<"删除第("<
<<")项目失败"<
nNumber = 0;215 pListHead->pNext = NULL;216 return true;217 }218 }219 bool DisposeList(Note*& pListHead)220 {221 if (pListHead == NULL)222 {223 return false;224 }225 else226 {227 while (ListDeleteLastItem(pListHead))228 {229 ;230 }231 delete pListHead;232 pListHead = NULL;233 return true;234 }235 }236 bool ListInsertItem(Note*& pListHead,int nValue,int nIndex)237 {238 if (pListHead == NULL)239 {240 return false;241 }242 int nNum = 0;243 ListGetItemNumber(pListHead,nNum);244 if (nIndex > nNum)245 {246 return false;247 }248 else if (nIndex == -1)249 {250 nIndex = nNum;251 }252 Note* pTmp = pListHead;253 for (int i=0;i
pNext;256 }257 Note* pNext = pTmp->pNext;258 pTmp->pNext = new Note;259 pTmp->pNext->nNumber = nValue;260 pTmp->pNext->pNext = pNext;261 return true;262 }263 bool ListItemPushFront(Note*& pListHead,int nValue)264 {265 return ListInsertItem(pListHead,nValue,0);266 }267 bool ListItemPushBack(Note*& pListHead,int nValue)268 {269 return ListInsertItem(pListHead,nValue,-1);270 }271 bool ListDeleteItem(Note*& pListHead,int nIndex)272 {273 if (pListHead == NULL)274 {275 return false;276 }277 int nNum = 0;278 ListGetItemNumber(pListHead,nNum);279 if (nNum == 0)280 {281 return false;282 }283 if (nIndex > nNum)284 {285 return false;286 }287 else if (nIndex == -1)288 {289 nIndex = nNum-1;290 }291 Note* pTmp = pListHead;292 for (int i=0;i
pNext;295 }296 Note* pNext = pTmp->pNext->pNext;297 delete pTmp->pNext;298 pTmp->pNext = pNext;299 return true;300 }301 bool ListDeleteFirstItem(Note*& pListHead)302 {303 return ListDeleteItem(pListHead,0);304 }305 bool ListDeleteLastItem(Note*& pListHead)306 {307 return ListDeleteItem(pListHead,-1);308 }309 bool ListGetItem(Note*& pListHead,int& nValue,int nIndex)310 {311 if (pListHead == NULL)312 {313 return false;314 }315 int nNum = 0;316 ListGetItemNumber(pListHead,nNum);317 if (nIndex >= nNum)318 {319 return false;320 }321 else if (nIndex == -1)322 {323 nIndex = nNum;324 }325 Note* pTmp = pListHead;326 for (int i=0;i<=nIndex;i++)327 {328 pTmp = pTmp->pNext;329 }330 nValue = pTmp->nNumber;331 return true;332 }333 bool ListGetFirstItem(Note*& pListHead,int& nValue)334 {335 return ListGetItem(pListHead,nValue,0);336 }337 bool ListGetLastItem(Note*& pListHead,int& nValue)338 {339 return ListGetItem(pListHead,nValue,-1);340 }341 bool ListGetItemNumber(Note*& pListHead,int& nNumber)342 {343 if (pListHead == NULL)344 {345 return false;346 }347 Note* pTmp = pListHead->pNext;348 nNumber = 0;349 while (pTmp != NULL)350 {351 nNumber++;352 pTmp = pTmp->pNext;353 }354 return true;355 }356 bool ListPrint(Note*& pListHead)357 {358 if (pListHead == NULL)359 {360 return false;361 }362 Note* pTmp = pListHead->pNext;363 int nIndex = 0;364 while (pTmp != NULL)365 {366 cout<
<<":\t"<
nNumber<
pNext;368 }369 return true;370 }

 

转载于:https://www.cnblogs.com/herizai/p/3157638.html

你可能感兴趣的文章
小程序开发快速入门教程(附源码)
查看>>
基于cropper.js的图片上传和裁剪
查看>>
车联网SaaS平台多租户平台技术选型参考
查看>>
我是如何快速积累工作经验
查看>>
用信号量进程同步与互斥
查看>>
随笔1
查看>>
Codeforces Round #469 (Div. 2)
查看>>
JavaScript:Number 对象
查看>>
事务同步多线程
查看>>
怎么去掉联系人、通话记录、拨号列表界面中的电话号码中间的空格?
查看>>
node.js常见的一些错误信息
查看>>
MySQL启动出现The server quit without updating PID file错误解决办法
查看>>
什么是多租户
查看>>
jQuery的效果
查看>>
express node 框架介绍
查看>>
adt-bundle-windows-x86-20131030
查看>>
Socket
查看>>
正则表达式之 数据验证 与 文本替换
查看>>
RPC是什么?
查看>>
CLR via C#:CLR的执行模型
查看>>