/*  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. latex 批量注释

    在LaTex中的注释有3种方法: 1. 右键单击选中要注释的文本,选择 Insert Comment ,WinEdt 就会自动给选定的行添加 % ,完成段落文本的注释: 右键单击选中已经注释的文本,选 ...

  2. [转]printf 函数实现的深入剖析

    研究printf的实现,首先来看看printf函数的函数体 int printf(const char *fmt, ...) { int i; char buf[256];          va_l ...

  3. easy ui 学习笔记,不断整理中............

    $.message.show({//浏览器右下角弹框,我列出了几个属性,具体请看API                   title: '提示',                   msg: '恭 ...

  4. 转: git常用命令

    # git配置 #---------------------------------------------- #配置用户名和邮箱: $ git config --global user.name & ...

  5. [转贴]JavaScript中Array(数组)的属性和方法

    数组有四种定义的方式 使用构造函数:var a = new Array();var b = new Array(8); var c = new Array("first", &qu ...

  6. URAL 1297 Palindrome(后缀数组+ST表)

    [题目链接] http://acm.timus.ru/problem.aspx?num=1297 [题目大意] 求最长回文子串,并输出这个串. [题解] 我们将原串倒置得到一个新的串,加一个拼接符将新 ...

  7. 迭代器(iterators)

    1.迭代器的概念 迭代器是一种抽象的设计概念.在设计模式中,迭代器模式定义为:提供一种方法,使之能够依序访问某个容器中所含的各个元素,而又无需暴露该容器的内部组织结构. 迭代器可以看做一种行为类似指针 ...

  8. 深度优先搜索算法(DFS)以及leetCode的subsets II

    深度优先搜索算法(depth first search),是一个典型的图论算法.所遵循的搜索策略是尽可能“深”地去搜索一个图. 算法思想是: 对于新发现的顶点v,如果它有以点v为起点的未探测的边,则沿 ...

  9. Session会话跟踪

    用encodeURL重写URL public class SessionA extends HttpServlet { @Override protected void doGet(HttpServl ...

  10. hadoop 主节点存储告警

    之前只他调整过dfs 的存储目录到最大配额的目录,其它没有处理(就是在默认的/ 目录下,而这个目录的存储配额只有50G) 运行一周的时间不到,集群开始告警,查看是目录/ 的存储占用超过了60% 再查看 ...