/*  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++实现(二)的更多相关文章

  1. IT公司100题-35- 求一个矩阵中最大的二维矩阵(元素和最大)

    问题描述: 求一个矩阵中最大的二维矩阵(元素和最大).如: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 中最大的是: 4 5 9 10   分析: 2*2子数组的最大和.遍历求和,时 ...

  2. Qt信号槽机制的实现(面试的感悟,猜测每一个类保存的一个信号和槽的二维表,实际使用函数指针 元对象 还有类型安全的检查设定等等)

    因为面试时问了我这道题,导致我想去了解信号槽到底是如何实现的,于是贴着顺序看了下源码,大致了解了整个框架.网上关于信号槽的文章也很多,但是大部分都是将如何应用的,这里我就写一下我所理解的如何实现吧, ...

  3. guozhongCrawler的是一个无须配置、便于二次开发

    guozhongCrawler的是一个无须配置.便于二次开发的爬虫开源框架,它提供简单灵活的API,只需少量代码即可实现一个爬虫.模块化设计完全 面向业务提供接口,功能覆盖整个爬虫的生命周期(链接提取 ...

  4. 《剑指Offer》第1题(Java实现):在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

    一.题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该 ...

  5. 功能要求:定义一个两行三列的二维数组 names 并赋值,使用二重循环输出二维数组中的元素。

    功能要求:定义一个两行三列的二维数组 names 并赋值,使用二重循环输出二维数组中的元素 names={{"tom","jack","mike&qu ...

  6. 剑指offer-特定二维数组中查找一个元素是否存在-二分搜索-二维数组

    int [][] array ={ {1,2,8,9}, {2,4,9,12}, {4,7,10,13}, {6,8,11,19} }; 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都 ...

  7. 如何一步一步用DDD设计一个电商网站(二)—— 项目架构

    阅读目录 前言 六边形架构 终于开始建项目了 DDD中的3个臭皮匠 CQRS(Command Query Responsibility Segregation) 结语 一.前言 上一篇我们讲了DDD的 ...

  8. 今天犯了一个StringBuilder构造函数引起的二逼问题。

    在.Net里,StringBuilder的构造函数有很多,最常用的是无参的构造函数,默认分配16个字符的空间.其次就是填写StringBuilder空间的带一个Int32的构造函数,这个在优化代码的时 ...

  9. 【重点】Jmeter----- 将 JDBC Request 查询结果作为下一个接口参数方法(二)

    一.说明 jmeter与数据库mysql已连接成功 二.需求 1.前置条件: 1.已user数据库的前8位手机号码作为行动计划的名称 2.行动计划的日期是2018-10-17 2.操作步骤: 1)获取 ...

随机推荐

  1. 无法打开 configsource 文件

    右键点击*.config文件,属性里的“复制到输出目录”选项,选择“始终复制”或“如果较新则复制”,这样生成或运行时,该文件就会出现在bin目录或obj目录中.

  2. Windows Server 2012 R2 Standard序列号

    备用一个吧,免得用起来的时候找不到. NB4WH-BBBYV-3MPPC-9RCMV-46XCB

  3. 编程器NAND Flash 技术入门

    NAND Flash分类 SLC(Single-Level Cell)架构:单一储存单元(Cell)可储存1bit data MLC(Multi-Level Cell)架构:单一储存单元(Cell)可 ...

  4. shell编程之文本与日志过滤

    1:grep命令: grep -v  "char"  file_name 匹配不包括"char"的文本 grep -n -w "char" ...

  5. WebRTC学习笔记_Demo收集

    1.     WebRTC学习 1.1   WebRTC现状 本人最早接触WebRTC是在2011年底,那时Google已经在Android源代码中增加了webrtc源代码,放在/external/w ...

  6. cc150:实现一个算法来删除单链表中间的一个结点,仅仅给出指向那个结点的指针

    实现一个算法来删除单链表中间的一个结点,仅仅给出指向那个结点的指针. 样例: 输入:指向链表a->b->c->d->e中结点c的指针 结果:不须要返回什么,得到一个新链表:a- ...

  7. Android 标签控件

    版本号:1.0 日期:2014.7.24 版权:© 2014 kince 转载注明出处      在有的应用中可能须要设置一些标签来方便用去去查询某些信息,比方手机助手或者购物软件之类都会有一些标签. ...

  8. [学习笔记]js动画实现方法,作用域,闭包

    一,js动画基本都是依靠setInterval和setTimeout来实现 1,setInterval是间隔执行,过一段时间执行一次代码 setInterval(function(){},500);即 ...

  9. HDU - 1116 Play on Words(欧拉图)

    有向图是否具有欧拉通路或回路的判定: 欧拉通路:图连通:除2个端点外其余节点入度=出度:1个端点入度比出度大1:一个端点入度比出度小1 或 所有节点入度等于出度 欧拉回路:图连通:所有节点入度等于出度 ...

  10. ActiveMQ下载及安装

    1.下载ActiveMQ 官方网站:http://activemq.apache.org/ 根据需要下载不同的版本.我下载的是5.13.3-win64的版本 2.运行ActiveMQ服务 2.1解压缩 ...