一个简单链表的C++实现(二)
/* LList.cpp
* Author: Qiang Xiao
* Time: 2015-07-12
*/ #include<iostream>
using namespace std; class Node{
public:
int data;
Node* ptr;
Node(int elem= , Node* node= NULL){this->data= elem; this->ptr= node;}
}; class LList{
private:
Node* head;
Node* tail;
int length;
public:
LList();
~LList();
bool append(Node*);
bool insert(int, Node*);
void print();
int getLength(){return this->length;}
int getElementByPos(int);
void pop();
int getLast() const;
bool deleteElementByPos(int);
}; int LList::getLast() const{
return this->tail->data;
} bool LList::deleteElementByPos(int pos){
if(pos< || pos> this->getLength()- ){
cout<<"Out of range!"<<endl;
return false;
} Node* tmp= this->head;
int k= -;
while(k< pos- ){
tmp= tmp->ptr;
k++;
}
Node* del= tmp->ptr;
tmp->ptr= tmp->ptr->ptr;
delete del;
this->length--;
return true;
} void LList::pop(){ Node* tmp= this->head;
int i= ;
while(i< this->getLength()- ){
tmp= tmp->ptr;
i++;
}
Node* t= this->tail;
this->tail= tmp;
this->length--;
delete t, tmp;
} int LList::getElementByPos(int pos){
if(pos< || pos> this->getLength()- ){
cout<<"Out of range!"<<endl;
return -;
}
Node* p= this->head->ptr;
int k= ;
while(k<pos){
p= p->ptr;
k++;
}
int m= p->data;
return m;
} LList::LList(){
Node* init= new Node();
this->head= init;
this->tail= init;
this->length= ;
} LList::~LList(){
while(head){
Node* tmp= new Node(,head);
tmp= head;
head= head->ptr;
delete tmp;
}
delete head;
delete tail;
} bool LList::insert(int pos, Node* node){
if(pos>this->getLength()){
cout<<"Out of range!"<<endl;
return false;
}
int i= ;
Node* fence= new Node();
fence= this->head;
while(i< pos){
fence= fence->ptr;
i++;
}
node->ptr= fence->ptr;
fence->ptr= node;
this->length++;
return true;
} bool LList::append(Node* node){
this->tail->ptr= node;
this->tail= node;
this->length++;
return true;
} void LList::print(){
Node* p= this->head->ptr;
int k= ;
while(k< this->getLength()){
cout<<p->data<<"\t";
p= p->ptr;
k++;
}
cout<<endl;
} int main(){
cout<<"\n******************Begin Test**********************\n";
Node* node1= new Node();
Node* node2= new Node();
Node* node3= new Node();
Node* node4= new Node();
LList* list= new LList();
cout<<"\n******************Empty List**********************\n";
list->print();
list->append(node1);
list->append(node2);
list->append(node3);
list->append(node4);
cout<<"\n******************After Append********************\n";
list->print();
cout<<"\n\n";
Node* node5= new Node();
int pos= ;
list->insert(pos,node5);
Node* node6= new Node();
pos= ;
list->insert(pos,node6);
Node* nod1= new Node();
pos= ;
list->insert(pos,nod1); cout<<"\n\n*****************After Insert*******************\n";
list->print(); cout<<"\n\n****************Print one-by-one****************\n";
for(int i= ; i< list->getLength(); i++){
cout<<"The "<<i<<" element of list is: "<<list->getElementByPos(i)<<endl;
}
cout<<"\n\n*********************POP3***********************\n";
list->pop();
list->print(); cout<<"\n\n*******************Get Last*********************\n";
cout<<list->getLast()<<endl;
/*
Node* node7= new Node(7);
list->append(node7);
cout<<"\n******************After Append********************\n";
list->print();
*/
cout<<"\n******************After Delete********************\n";
int k2= ;
list->deleteElementByPos(k2);
list->print(); return ;
}
比上一版本(http://www.cnblogs.com/ruchicyan/p/4640665.html)仅仅是多了两个操作:pop() 和 deleteElementByPos(int pos)。
还是没有把指针给完全弄懂,这两天得花一些时间,好好看一下书中现成的代码。
敬请指正。
欢迎交流!
一个简单链表的C++实现(二)的更多相关文章
- IT公司100题-35- 求一个矩阵中最大的二维矩阵(元素和最大)
问题描述: 求一个矩阵中最大的二维矩阵(元素和最大).如: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 中最大的是: 4 5 9 10 分析: 2*2子数组的最大和.遍历求和,时 ...
- Qt信号槽机制的实现(面试的感悟,猜测每一个类保存的一个信号和槽的二维表,实际使用函数指针 元对象 还有类型安全的检查设定等等)
因为面试时问了我这道题,导致我想去了解信号槽到底是如何实现的,于是贴着顺序看了下源码,大致了解了整个框架.网上关于信号槽的文章也很多,但是大部分都是将如何应用的,这里我就写一下我所理解的如何实现吧, ...
- guozhongCrawler的是一个无须配置、便于二次开发
guozhongCrawler的是一个无须配置.便于二次开发的爬虫开源框架,它提供简单灵活的API,只需少量代码即可实现一个爬虫.模块化设计完全 面向业务提供接口,功能覆盖整个爬虫的生命周期(链接提取 ...
- 《剑指Offer》第1题(Java实现):在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
一.题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该 ...
- 功能要求:定义一个两行三列的二维数组 names 并赋值,使用二重循环输出二维数组中的元素。
功能要求:定义一个两行三列的二维数组 names 并赋值,使用二重循环输出二维数组中的元素 names={{"tom","jack","mike&qu ...
- 剑指offer-特定二维数组中查找一个元素是否存在-二分搜索-二维数组
int [][] array ={ {1,2,8,9}, {2,4,9,12}, {4,7,10,13}, {6,8,11,19} }; 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都 ...
- 如何一步一步用DDD设计一个电商网站(二)—— 项目架构
阅读目录 前言 六边形架构 终于开始建项目了 DDD中的3个臭皮匠 CQRS(Command Query Responsibility Segregation) 结语 一.前言 上一篇我们讲了DDD的 ...
- 今天犯了一个StringBuilder构造函数引起的二逼问题。
在.Net里,StringBuilder的构造函数有很多,最常用的是无参的构造函数,默认分配16个字符的空间.其次就是填写StringBuilder空间的带一个Int32的构造函数,这个在优化代码的时 ...
- 【重点】Jmeter----- 将 JDBC Request 查询结果作为下一个接口参数方法(二)
一.说明 jmeter与数据库mysql已连接成功 二.需求 1.前置条件: 1.已user数据库的前8位手机号码作为行动计划的名称 2.行动计划的日期是2018-10-17 2.操作步骤: 1)获取 ...
随机推荐
- MFC窗口实现最小化到托盘 右键菜单和还原
//.h文件 void toTray();//最小化到托盘 void DeleteTray();//删除托盘图标 afx_msg LRESULT OnShowTask(WPARAM wParam,LP ...
- 毕业bg(dfs)
毕业bg Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissi ...
- JavaScript中你可能不知道的九件事
今天凑巧去W3School扫了一遍JavaScript教程,发现从中看到了不少自己曾经没有注意过的细节. 我这些细节列在这里.分享给可能相同不知道的朋友: 1.使用 document.write() ...
- Nhibernate初入门基本配置(一)
文章出处:http://www.cnblogs.com/GoodHelper/archive/2011/02/14/nhiberante_01.html 一.NHibernate简介 什么是?NHib ...
- MOSS程序中如何发Mail?
我们使用.NET类库中的API发Mail的时候,我们要配置他的SMTP Server等,但是在Sharepoint里,已经提供了相关的封装的方法: SPUtility.SendEmail(SPWeb, ...
- sql update left join 更新,字段内容分隔符提取
UPDATE a SET [Province] = parsename(replace([FullName],'-','.'),2) from [dbo].[T_B_Emp] a left join ...
- Sliding Window Maximum
(http://leetcode.com/2011/01/sliding-window-maximum.html) A long array A[] is given to you. There is ...
- BZOJ 2561: 最小生成树(最小割)
U,V能在最小(大)生成树上,当且仅当权值比它小(大)的边无法连通U,V. 两次最小割就OK了. --------------------------------------------------- ...
- Android 开发笔记 “android调试遇到ADB server didn't ACK以及顽固的sjk_daemon进程 ”
资源来源:http://blog.csdn.net/wangdong20/article/details/20839533 做Android调试的时候经常会遇到,程序写好了,准备接上手机调试,可不一会 ...
- 【转】VPN服务器配置详解
参考博文: VPN服务器配置详解 等公司上服务器开始配置 vpn