单链表
2016-03-01 15:07
225 查看
// List.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <vld.h> using namespace std; typedef struct _List { int nData; struct _List*next; }; void insertList(_List* pList, int nData) { while (pList->next){ pList = pList->next; } _List *p = new _List; p->nData = nData; p->next = NULL; pList->next = p; } void output(_List *pList) { while (pList){ cout<<pList->nData<<endl; pList = pList->next; } cout<<"//////////////////////////"<<endl; } void DeleteList(_List *pList) { while (pList){ _List *p = pList->next; delete pList; pList = p; } } void DeleteNode(_List *pList, int nPos) { int i = 0; while (pList && i++ < nPos){ if (i == nPos){ _List *p = pList->next->next; delete pList->next; pList->next = p; break; } pList = pList->next; } } void SortList(_List *pList) { while (pList){ _List *p = pList->next; while (p){ if (pList->nData < p->nData){ int nTemp = pList->nData; pList->nData = p->nData; p->nData = nTemp; } p = p->next; } pList = pList->next; } } int _tmain(int argc, _TCHAR* argv[]) { _List *pList = new _List; pList->nData = 0; pList->next = NULL; insertList(pList, 1); insertList(pList, 2); insertList(pList, 3); insertList(pList, 4); insertList(pList, 5); insertList(pList, 6); insertList(pList, 7); insertList(pList, 8); insertList(pList, 9); insertList(pList, 10); output(pList); SortList(pList); output(pList); DeleteNode(pList, 3); output(pList); DeleteList(pList); return 0; }
相关文章推荐
- 设计模式 ( 一 )
- C++自制Redis 数据库 (十五)【重构】数据库学习(一)
- HDU 2553 N皇后问题
- AngularJS 对象
- spring整合quartz实现定时任务
- 跨域
- scala符号
- 在vs2010或者vs2008中配置PC-lint9.0版http://blog.csdn.net/whatday/article/details/7890092
- 安卓7.0首批截图公布:惊现汉堡菜单
- int 0x13中断的參数传递
- 堆和栈的区别
- AngularJS 对象
- struts2的工作原理
- RegionServer Splitting Implementation:regionServer 分裂过程分析
- 深入分析js中的constructor 和prototype
- Android开发之创建桌面快捷方式
- scala def隐式函数
- 内存位置访问无效
- AngularJS 字符串
- Spring AOP 需要包含的各种包