数据结构 单链表 头插 尾插 指定位置前插后插 以及 删除指定元素 C++ 面向对象方法实现
来源:互联网 发布:js 数组和对象 编辑:程序博客网 时间:2024/05/17 06:39
// 该内容来自http://stackoverflow.com/questions/22141477/simple-linked-list-c
// 经修改后得到(增加了尾插,指定位置前插、后插以及删除等功能)
List.h头文件:
<span style="font-size:12px;">/* * List.h * * Created on: 2015-11-23 * Author: John */#ifndef LIST_H_#define LIST_H_#include <iostream>#include <stdlib.h>// NULL ,也可以用0替代typedef int ElementType;using namespace std;class List {private:// Structure inside the class LinkedList// This is one node which is not needed by the caller. It is just// for internal work./*** 节点类 ***/class Node{public:ElementType data;Node* next;};// public memberpublic:/*** List 的函数成员 ***/// constructorList();void addValue(int val);void AddValueTail(int val);// 尾插入void AddValuePrepend(int specValue, int val); // 在指定值前面插入void AddValueAppend(int specValue, int val); // 在指定值后面插入int popValue();void deleteValue(int val);// ①传入引用 ostream &out// ② const 表示这个函数是一个“只读函数”,函数不能改变类对象的状态,不能改变对象的成员变量的值// 更多阅读: http://blog.csdn.net/changrui82/article/details/6184502void display(ostream & out)const;// private memberprivate:/*** List 的数据成员 ***/// 这个是私有的成员变量,他指向第一个节点。// this is the private member variable. It is just a pointer to the first NodeNode* head;};// 重载输出操作符<< 用来出书List列表的内容ostream & operator <<(ostream &out, const List & aList);#endif /* LIST_H_ */</span>List.cpp源文件:
/* * List.cpp * * Created on: 2015-11-23 * Author: John */// http://stackoverflow.com/questions/22141477/simple-linked-list-c#include "List.h"List::List() {// TODO Auto-generated constructor stubhead = NULL;}// 使用头插法在list的开始位置插入节点// This prepends a new value at the beginning of the listvoid List::addValue(int val){Node * n = new Node();// create new Node 创建一个新的节点n->data = val;// set value 设置值n->next = head;// make the node point to the next node. 将新节点指向另一个节点// If the list is empty, this is NULL, so the end of the list --> OK// 如果list为空,那么head的值为NULL,n->next的值也为NULL,该节点就是List的最后一个节点head = n;// last but not least, make the head point at the new node.// 最后,让head节点指向新创建的节点。}// 尾插 就应该叫 append吧。void List::AddValueTail(int val){Node * n = new Node();n->data = val;n->next = NULL;Node * temp = head;if(head==NULL){head = n;}else{while(temp->next){temp=temp->next;}temp->next = n;}}// 在指定值前面插入void List::AddValuePrepend(int specValue, int val){Node * temp = head;if(head->data == specValue){// 就是头插addValue(val);}else{while(temp->next){if(temp->next->data == specValue){break;}temp=temp->next;}if(temp->next->data == specValue){Node * n = new Node();n->data = val;n->next = temp->next;temp->next = n;}}}// 在指定值后面插入void List::AddValueAppend(int specValue, int val){Node * temp = head;while(temp){if(temp->data == specValue){break;}temp = temp->next;}if(temp->data == specValue){Node * n = new Node();n->data = val;n->next = temp->next;temp->next = n;}else{cout << "没有值" << specValue << endl;}}// 返回列表中第一个元素,并删除这个节点// returns the first element in the list and deletes the Node.// caution, no error-checking here!int List::popValue(){Node * n = head;if(head){int ret = n->data;head = head->next;delete n;return ret;}// C/C++中如何获得int的最大值unsigned int temp = 0;//http://blog.csdn.net/sanqi322/article/details/6321129return (~temp)/2+1;// 返回int最小值(使用最大有符号int,然后将其1溢出后的值)// 获取最大值最小值的另一个参考http://blog.csdn.net/xuexiacm/article/details/8122267}void List::deleteValue(int val){Node *temp = head;if(head){if(head->data == val){head = head->next;delete temp;}else{while(temp->next){if(temp->next->data==val){Node * delPtr = temp->next;temp->next = temp->next->next;delete delPtr;break;}temp = temp->next;}}}else{}}void List::display(ostream & out)const{Node * temp = head;while(temp){out << temp->data <<"" ;temp = temp->next;}}// 非成员函数,故没有List::限制符号ostream & operator <<(ostream &out, const List & aList){aList.display(out);return out;}
List_test.cpp测试:
/* * List_test.cpp * * Created on: 2015-11-23 * Author: John */#include "List.h"#include <iostream>using namespace std;int main(){// 头插List linkList;linkList.addValue(5);linkList.addValue(10);linkList.addValue(15);cout<<"头插法建立的linkList链表元素为:"<<linkList<<endl;cout<< linkList.popValue() <<endl;cout<< linkList.popValue() <<endl;cout<< linkList.popValue() <<endl;// 因为在popValue()函数里面没有错误检测,// 下面的语句将会导致不确定的行为// 程序有可能会崩溃,因为list没有用来弹出的值了。// because there is no error checking in popValue(), the following// is undefined behavior. Probably the program will crash, because// there are no more values in the list.// cout << list.popValue() << endl;cout<< linkList.popValue() <<endl;// 多弹出的一个元素// 尾插List linkListTail;linkListTail.AddValueTail(5);linkListTail.AddValueTail(10);linkListTail.AddValueTail(15);cout<<"尾插法建立的linkListTail链表元素为:"<<linkListTail<<endl;cout<< linkListTail.popValue() <<endl;cout<< linkListTail.popValue() <<endl;cout<< linkListTail.popValue() <<endl;cout<< linkListTail.popValue() <<endl;// 多弹出的一个元素// 指定位置插入List linkListOther;linkListOther.addValue(5);linkListOther.addValue(10);linkListOther.addValue(15);cout<<"当前linkListOther链表元素为:";cout<<linkListOther<<endl;cout << "在指定值之前插入,输入指定位置的值(-999停止录入):";int number;cin>> number;int val;while(number!=-999){cout<< "输入插入的值:" ;cin>>val;linkListOther.AddValuePrepend(number,val);cout<<"指定元素"<<number<<"之前插入了"<<val<<"之后,列表元素为:";cout <<linkListOther<<endl;cout<< "输入插入的位置值:" ;cin >> number;}List linkListAother;linkListAother.addValue(5);linkListAother.addValue(10);linkListAother.addValue(15);linkListAother.addValue(20);linkListAother.addValue(25);linkListAother.addValue(30);cout<<"当前linkListAother链表元素为:";cout<<linkListAother<<endl;cout<<"输入要删除的元素的值(-999结束删除)";int val1;while(val1!=-999){cin>>val1;linkListAother.deleteValue(val1);cout<<"删除元素"<<val1<<"之后,列表元素为:";cout <<linkListAother<<endl;cout<<"输入要删除的元素的值(-999结束删除)";}}
测试输出Output:
头插法建立的linkList链表元素为:1510515105-2147483648尾插法建立的linkListTail链表元素为:5101551015-2147483648当前linkListOther链表元素为:15105在指定值之前插入,输入指定位置的值(-999停止录入):10输入插入的值:9指定元素10之前插入了9之后,列表元素为:159105输入插入的位置值:15输入插入的值:13指定元素15之前插入了13之后,列表元素为:13159105输入插入的位置值:15输入插入的值:14指定元素15之前插入了14之后,列表元素为:1314159105输入插入的位置值:5输入插入的值:3指定元素5之前插入了3之后,列表元素为:13141591035输入插入的位置值:-999当前linkListAother链表元素为:30252015105输入要删除的元素的值(-999结束删除)20删除元素20之后,列表元素为:302515105输入要删除的元素的值(-999结束删除)30删除元素30之后,列表元素为:2515105输入要删除的元素的值(-999结束删除)25删除元素25之后,列表元素为:15105输入要删除的元素的值(-999结束删除)5删除元素5之后,列表元素为:1510输入要删除的元素的值(-999结束删除)10删除元素10之后,列表元素为:15输入要删除的元素的值(-999结束删除)15删除元素15之后,列表元素为:输入要删除的元素的值(-999结束删除)0删除元素0之后,列表元素为:输入要删除的元素的值(-999结束删除)-999删除元素-999之后,列表元素为:输入要删除的元素的值(-999结束删除)
0 0
- 数据结构 单链表 头插 尾插 指定位置前插后插 以及 删除指定元素 C++ 面向对象方法实现
- 【C语言】静态顺序表的实现(包括头插、头删、尾插、尾删、查找、删除指定位置)
- 单链表的基本操作(尾插,尾删,头插,头删,查找,指定位置插入、删除,指定元素删除、全部删除等)
- C语言:单链表的实现(包括:尾插、尾删、头插、头删、指定位置插入、长度、打印)
- C++类中单链表的实现(头插、尾插、头删、尾删、指定位置插入、指定位置删除、链表长度、清空链表、链表排序)
- 单链表的头插、中插、尾插、删除、逆序、显示(C语言实现)
- 数据结构中静态顺序表的初始化、尾插、尾删、头插、头删、任意位置的插入及删除
- C语言实现链表的头插,尾插,插入,修改,删除和遍历
- 单向循环链表的头插,尾插,中间插以及删除节点
- 单链表的头插 尾插 反序 遍历 插入 删除
- 单链表的头插、尾插、删除、合并等操作
- 单链表初始化,头插,尾插,查找,删除
- 建立单链表【头插,尾插】
- 删除线性表指定位置的元素(单链表实现)
- 双向循环链表的头插、中插、尾插、删除、逆序顺序显示(C++实现)
- 数组的链表实现:创建(头插/尾插)、打印、删除、插入
- C++语言实现单链表的尾插,尾删,头插,头删等
- 把一个字符串插到另一个字符串的指定位置
- CF - Profact(DFS + 剪枝)
- Android Resource Types之String简介
- Android的过滤机制
- 字符串和日期的相互转换
- C语言基础第一篇--Hello world !
- 数据结构 单链表 头插 尾插 指定位置前插后插 以及 删除指定元素 C++ 面向对象方法实现
- c和c++中的一维数组和二维数组的动态分配内存,以及参数传递
- [转载]iOS 获取WIFI,IP地址
- macOSX下,如何永久更改Mysql的字符编码格式
- 顺序栈的基本操作
- 高雅的数学表示
- scikit-learn初探:多种回归方法建立预测电力输出模型(一)
- path和classpath的区别
- GOF设计模式