自己创建链表类,实现几个基本操作
2015-05-12 21:44
330 查看
/////List 创建 添加 删除 #include <iostream> #include <string> #include <stack> using namespace std; class Node { public: int Data; Node*Node_next; Node():Data(0),Node_next(NULL){} }; class list { Node*head; public: list(){head=NULL;} void insertlist(int iData);//链表尾部插入 void Deletelist(int aDate);//链表结点的删除 void Outputlist();//链表结点的输出 void ReverseOutput();////反向输出 Node*Gethead(){return head;} }; void list::ReverseOutput() { stack<int> stk; Node* phd = head; while (phd!= NULL) { stk.push(phd->Data); phd = phd->Node_next; } while (!stk.empty()) { cout<<stk.top()<<" "; stk.pop(); } cout<<endl; } void list::Deletelist(int iData) { if (head != NULL) { Node* ptemp = head; if (head->Data == iData) { ptemp = head; head = head->Node_next; delete ptemp; } else { Node* pn = ptemp; while ((ptemp!= NULL) && (ptemp->Data != iData)) { pn = ptemp; ptemp = ptemp->Node_next; } if (ptemp->Data == iData) { pn->Node_next = ptemp->Node_next; delete ptemp; } } } } void list::insertlist(int iData) { Node* pNewNode = new Node(); pNewNode->Data = iData; if (head == NULL) { head = pNewNode; } else { Node* phd = head; Node* ptmp = head; while(phd != NULL) { ptmp = phd; phd = phd->Node_next; } ptmp->Node_next = pNewNode; } } void list::Outputlist() { Node* phd = head; while (phd!= NULL) { cout<<phd->Data<<" "; phd = phd->Node_next; } cout<<endl; } ////代码测试 #define LEN 10 int main() { list A; int Data[10]={25,41,16,98,5,67,9,55,1,121}; for (int i=0; i<LEN; i++) { A.insertlist(Data[i]); //建立链表 } cout<<"\n链表A:"<<endl; A.Outputlist(); A.Deletelist(Data[5]); cout<<"删除元素Data[5]后"<<endl; A.Outputlist(); cout<<"删除元素Data[5]后反向输出"<<endl; A.ReverseOutput(); return 0; }
View Code
相关文章推荐
- 以邻接矩阵作为存储结构实现图的创建与基本操作
- [C#]实现对XML文件的基本操作(创建xml文件,增、删、改、查 xml节点信息)
- c语言:顺序表的实现(一) 创建,插入,删除,查找,输出等基本操作实现
- 编程实现单链表的创建等基本操作
- Zookeeper客户端基本操作java实现——创建连接、创建节点、添加修改节点内容、获取子节点、获取节点数据、删除节点
- C# 实现对XML文件的基本操作(创建xml文件,增、删、改、查 xml节点信息)
- Unreal 入门-创建人物,实现基本走、跑、跳操作。
- 单向循环链表的创建及基本操作的实现
- C# 用Linq的方式实现对Xml文件的基本操作(创建xml文件、增删改查xml文件节点信息)
- C语言实现单链表(带头结点)的基本操作(创建,头插法,尾插法,删除结点,打印链表)
- 数据结构 P64-65 算法实现 循环队列的创建和基本操作
- C语言实现二叉树的基本操作---创建、遍历、求深度、求叶子结点
- JAVA基础 day21 字节流的输入和输出 字节流的缓冲区 (自己实现BufferedInputStream) 键盘输入 转换流的用法 改变标准输入输出设备 IO流操作的基本规律
- 树的基本结构,以及利用链表实现树的各项操作(创建、添加/删除/打印树节点、销毁等等)
- C# 实现对XML文件的基本操作(创建xml文件,增、删、改、查 xml节点信息)
- 数据结构 P62-62 算法实现 单链队列的创建及基本操作
- C语言实现链表的创建、增加、删除、查询、逆序、清空等基本操作
- C# 实现对XML的基本操作(创建xml文件,增、删、改、查 xml节点信息)
- C# 用Linq的方式实现对Xml文件的基本操作(创建xml文件、增删改查xml文件节点信息)
- C语言实现二叉树的基本操作---创建、遍历、求深度、求叶子结点