链表实现代码
来源:互联网 发布:dr.wu维他命c美白 知乎 编辑:程序博客网 时间:2024/05/22 12:03
每天积一小步,虽然之前学过数据结构,此次试着把其实现代码都写出来,这是第一天。
List类如下图所示:
代码如下:
#include <iostream>#include <assert.h>using namespace std;struct Node{ int info; Node *link;};class List{public: List(); List(const List &other); bool create(); void print() const; void destory(); Node* getHead()const; ~List(); //-------------------------------------------------- int length()const; Node* find(const int x)const; bool insert(const int x, const int local)const; bool delValue(const int x)const; //--------------------------------------------------private: Node *head;};List::List(){ head = NULL;}List::List(const List &other){//要理解复制构造函数的运行时间,复制构造函数与复制功能的函数的由来目的是不一样的。 head = NULL;//构造函数只是运行一个就ok的 Node *temp = other.getHead(); Node *last = NULL; while(NULL != temp){Node *temp2 = new Node;temp2->info = temp->info;temp2->link = NULL;if(NULL == head){ head = last =temp2;}else{ last->link = temp2; last = temp2;}temp = temp->link; } }bool List::create(){ Node *last = NULL; Node *newNode = NULL; int input=0; cout << "input your number (enter -1 to over your input):" << endl; cin >> input; while(-1 != input){ newNode = new Node; assert(newNode); newNode->info = input; newNode->link = NULL; if(NULL == head){head = newNode;last = newNode; }else{last->link = newNode;last = newNode; } cout << "input your number (enter -1 to over your input):" << endl; cin >> input; } cout << "create function run over" << endl; return true;}Node* List::getHead()const{ return head;}void List::destory(){ while(NULL != head){ Node *temp = head->link; delete head; head = temp; }}List::~List(){ cout << "deconstruction has been run." << endl; destory();}void List::print() const{ Node *temp = head; cout << "here is list contain:" << endl; while(NULL != temp){cout << temp->info;cout << endl; temp = temp->link;} cout << "print list over!" << endl;}int List::length()const{ Node *temp = head; int i = 0; while(NULL != temp){ i++; temp = temp->link; } return i;}Node* List::find(const int x)const{ Node *temp = head; int i = 0; while(NULL != temp){++i;if(x == temp->info){ cout << x << " has been found. it's localtion is " << i << endl; return temp; break; }temp = temp->link; } return NULL;}bool List::insert(const int x, const int local)const{ if(local > length()){cout << "out of range." <<endl;return false; } Node *temp = head; int i = 0; while(NULL != temp){i++;temp = temp->link;if((local-1) == i)break; } Node *newNode = new Node; newNode->info = x; newNode->link = temp->link; temp->link =newNode; cout << x << " has been inserted into list." << endl; return true;}bool List::delValue(const int x)const{ if(NULL == head)return false; Node *pretemp = head; Node *temp = head->link; while(NULL != temp){ if(x == temp->info){pretemp->link = temp->link; delete temp;return true;} pretemp = pretemp->link; temp = temp->link; } cout << "there is not x in this list." << endl; return false; //不要限定自己使用变量的数量}int main(){ List list; list.create(); list.print(); list.find(3); list.insert(10,2); list.print(); list.delValue(10); list.print(); return 0;}
一个体会:我们可以定义变量为我们所用,所以不要困在自己没有资源可用的地步。定义变量其实就是建造资源的过程。
- 链表实现代码
- 链表代码实现
- 链表操作代码实现
- java代码实现链表
- 数据结构-链表代码实现
- 【旧代码】fortran中的指针实现链表的代码
- 链表相关操作代码实现
- 双向链表代码实现-C语言
- 【整理】链表特征以及代码实现
- 代码,用c++实现线性链表
- 【第五节】Java代码实现链表
- C++ 数据结构链表的实现代码
- c实现双向链表代码
- 用C++实现双向链表代码
- 链表的代码实现及其讲解
- 线性表实现代码
- 线索链表的实现 C++代码实现
- 链队列的实现,C++代码实现
- myEclipse 8.5 无法新建web project
- 使用泛型
- 继承、重载、重写、static、super、this
- 洗头发时突有所悟写下的JSP+STRUTS 分页朴素的代码和算法……2012-2-27
- 因素分解方法数
- 链表实现代码
- 计算句子中的字符数
- Linux0.12中断处理
- DirectX修复工具通用数据包
- C++类和数据在内存中的存储
- C++程序设计实验报告(三十)---第二周任务三(3-2)
- asp.net网站的性能优化
- C++第二周任务一*(1‐2)编写自定义函数将十进制数转换成二进制数输出。
- dom常见的属性