/*  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= NULL;}
}; 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;}
}; LList::LList(){
Node* init= new Node();
this->head= new Node();
this->tail= new Node();
this->head= init;
this->tail= init;
this->length= ;
} LList::~LList(){
delete head;
delete tail;
} bool LList::insert(int pos, Node* node){
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;
while(p){
cout<<p->data<<"\t";
p= p->ptr;
}
cout<<endl;
delete p;
} 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); cout<<"\n\n*****************After Insert*******************\n";
list->print();
return ;
}

Console display:

xiaoq@xq-ubun:~/C/DataStructure$ g++ LList.cpp -o LList.o
xiaoq@xq-ubun:~/C/DataStructure$ ./LList.o ******************Begin Test********************** ******************Empty List********************** ******************After Append******************** *****************After Insert******************* xiaoq@xq-ubun:~/C/DataStructure$

写这个程序主要是练习一下链表的用法。代码中有许多需要改进的地方,敬请指正。

欢迎交流!

一个简单链表的C++实现的更多相关文章

  1. 一个简单链表的C++实现(二)

    /* LList.cpp * Author: Qiang Xiao * Time: 2015-07-12 */ #include<iostream> using namespace std ...

  2. 利用C#的指针编写都一个简单链表

    using System; namespace UnsafeTest { unsafe struct link { public int x; public link* next; } class P ...

  3. Go的List操作上的一个小“坑”

    转自http://sharecore.net/blog/2014/01/09/the-trap-in-golang-list/ 一直想不清楚一个问题,简单设计的东西到底是“坑多”还是“坑少”呢? 复杂 ...

  4. 【基础】链表的储存结构说明(python)

    [实现链表的添加] class aNode(): def __init__(self,data=None,nxt=None): self.data=data self.nxt=nxt class ru ...

  5. Linux内核同步

    Linux内核剖析 之 内核同步 主要内容 1.内核请求何时以交错(interleave)的方式执行以及交错程度如何. 2.内核所实现的基本同步机制. 3.通常情况下如何使用内核提供的同步机制. 内核 ...

  6. go语言从零学起(二)--list循环删除元素(转载)

    本篇系转载 在使用go的container/list的package时,你可能会无意间踩一个小坑,那就是list的循环删除元素. list删除元素,直观写下来的代码如下: package main i ...

  7. Pascal 基础教程

    Pascal现在还有人想学习吗?先给出一本不错的Pascal教程,Object Pascal的教程我日后给出. Pascal基础教程       第一课 初识PASCAL语言           …… ...

  8. linux下内存

    MMU由一个或一组芯片组成.其功能是把逻辑地址映射为物理地址,进行地址转换(MMU是CPU的一部分) 机器指令仍然用逻辑地址指定一个操作数的地址或一条指令的地址 每个逻辑地址都由一个段选择符(16位) ...

  9. 03C++基本数据类型

    基本数据类型 2.2.1整型数据 短整型(short int) 有符号短整型(signed short int) 无符号短整型(unsigned short int) 一般整型(int) 有符号一般整 ...

随机推荐

  1. 【LeetCode题意分析&解答】34. Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  2. 转:CI伪静态化

    去掉php框架CI默认url中的index.php 2010-03-17 17:33:07|  分类: php框架ci |字号 订阅   CI默认的rewrite url中是类似这样的,例如你的CI根 ...

  3. 如何使ListView具有像ios一样的弹性

    ListView 是我们在开发过程中经常使用的控件之一,通常情况下,当我们没有对它进行自定义或者给添加headerview 或者footerView的时候,他都没有一个很好的反馈效果,但是相比较而言, ...

  4. Jsoup代码解读之三-Document的输出

    Jsoup代码解读之三-Document的输出   Jsoup官方说明里,一个重要的功能就是output tidy HTML.这里我们看看Jsoup是如何输出HTML的. HTML相关知识 分析代码前 ...

  5. ListBox控件的操作与实现

    .NET FrameWork>參考>类库>System.Windows.Forms>ListBox类的属性 1. 属性列表:     SelectionMode     组件中 ...

  6. word排版论文小结

    毕业论文如何用WORD排版 本人折腾了一晚上看别人的百度经验,做个总结,方便后人看懂,其实特别简单 用WORD自动生成页码 第一种情况:从第一页或者从第二页开始设置页码 这种情况只要“插入-> ...

  7. 【踩坑】近来在Firefox上遇到的一些坑

    因为工作一年多以来,做的工作基本都是和webkit系列打交道. 先是做m站,后来做了两个app内嵌的hybrid项目,从来只考虑webkit前缀和相关的伪类. 最近四个多月开始做内部的管理系统,写写样 ...

  8. zoj 1184

    硬币称重,经典,1.若被判平,左右所有硬币必正常:2.若判轻或判重,对应硬币被判轻-1.重记数+1:3.只有球只被判轻或判重,且次数跟天平不平衡次数相等,该球才能是坏的,否则必然是好的.且>0的 ...

  9. Spring 中拦截器与过滤器的区别

    spring 中拦截器 与servlet 的filter 有相似之处.比如二者都是aop 编程思想的体现都能实现权限检查,日志记录等. 不同之处 使用范围不同 Filter 是Servlet 规定的. ...

  10. MYSQL—加写锁,加读锁,解锁

    链接地址:http://blog.sina.com.cn/s/blog_7fa2bcf50101j1lu.html 表级锁: 加写锁:          lock   tables   table_n ...