/*  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. java中的四则运算

    代码的思路是通过正则判断计算每个最小的计算单元.以下是代码: package cn.com.lawchat.forpublicmvc.util; import java.math.BigDecimal ...

  2. jquery.validate 一些技巧

    1.Validator.element() Validates a single element, returns true if it is valid, false otherwise. http ...

  3. Linux 用户信息,组信息,密码信息!

    1: 用户信息保存在  /etc/passwd 文件下 2: 密码信息保存在 /etc/shadow 3: 组相关的信息 /etc/group

  4. Less基础教程

    Less基础教程 less是较早出现的css预处理器. LESS API 参考 安装和使用 安装比较简单,通过nmp或bower安装即可. npm install less -g bower inst ...

  5. delphi datasnap 心跳包

    为了能让我们的服务程序更加稳定,有些细节问题必须解决.就如上一讲中提到的客户端拔掉网线,造成服务器上TCP变成死连接,如果死连接数量过多,对服务器能长期稳定运行是一个巨大的威胁.另外,经过测试,如果服 ...

  6. java selenium webdriver实战 seleniumIDE

    Selenium是ThoughtWorks公司,一个名为Jason Huggins的测试为了减少手工测试的工作量,自己实现的一套基于Javascript语言的代码库 使用这套库可以进行页面的交互操作, ...

  7. linux下卸载安装程序及其配置的方法

    首先要知道软件包的名字 dpkg --list 如果知道关键字的话可以用下面这种方法 dpkg --list|grep -i ‘packagename’ 找到所要删除的软件包之后 sudo apt-g ...

  8. HTML本地存储,localstorg的应用实例

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. [置顶] 内存映射失败MapViewOfFile 失败 返回 8

    问题描述1 在使用内存映射方式读写数据时,将文件A的内容拷贝至文件B中,偶尔会出来文件拷贝后的文件,内容为空,或部分为空 问题分析1 怀疑是内存映射方式读写数据的稳定性(可笑的怀疑,内存映射可以Win ...

  10. linux内核之网络协议栈

    https://www.ibm.com/developerworks/cn/linux/l-ntflt/