一个简单链表的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= 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++实现的更多相关文章
- 一个简单链表的C++实现(二)
/* LList.cpp * Author: Qiang Xiao * Time: 2015-07-12 */ #include<iostream> using namespace std ...
- 利用C#的指针编写都一个简单链表
using System; namespace UnsafeTest { unsafe struct link { public int x; public link* next; } class P ...
- Go的List操作上的一个小“坑”
转自http://sharecore.net/blog/2014/01/09/the-trap-in-golang-list/ 一直想不清楚一个问题,简单设计的东西到底是“坑多”还是“坑少”呢? 复杂 ...
- 【基础】链表的储存结构说明(python)
[实现链表的添加] class aNode(): def __init__(self,data=None,nxt=None): self.data=data self.nxt=nxt class ru ...
- Linux内核同步
Linux内核剖析 之 内核同步 主要内容 1.内核请求何时以交错(interleave)的方式执行以及交错程度如何. 2.内核所实现的基本同步机制. 3.通常情况下如何使用内核提供的同步机制. 内核 ...
- go语言从零学起(二)--list循环删除元素(转载)
本篇系转载 在使用go的container/list的package时,你可能会无意间踩一个小坑,那就是list的循环删除元素. list删除元素,直观写下来的代码如下: package main i ...
- Pascal 基础教程
Pascal现在还有人想学习吗?先给出一本不错的Pascal教程,Object Pascal的教程我日后给出. Pascal基础教程 第一课 初识PASCAL语言 …… ...
- linux下内存
MMU由一个或一组芯片组成.其功能是把逻辑地址映射为物理地址,进行地址转换(MMU是CPU的一部分) 机器指令仍然用逻辑地址指定一个操作数的地址或一条指令的地址 每个逻辑地址都由一个段选择符(16位) ...
- 03C++基本数据类型
基本数据类型 2.2.1整型数据 短整型(short int) 有符号短整型(signed short int) 无符号短整型(unsigned short int) 一般整型(int) 有符号一般整 ...
随机推荐
- svn添加强制注释,pre-commit结合python
鉴于组内有些人在提交代码的时候并不写注释,而且没有固定格式,所以准备给svn提交时增加强制注释. 首先找到代码库里的hooks目录,正常建svn库的时候都有这个目录.进入hooks目录,找到pre-c ...
- pyVmomi入门
简要说明 pyVmomi is the Python SDK for the VMware vSphere API that allows you to manage ESX, ESXi, and v ...
- Gulp 从0开始
http://www.w3ctech.com/topic/134 (该文章有很多错误) http://markpop.github.io/2014/09/17/Gulp%E5%85%A5%E9%97 ...
- Storm博客收集
http://wbj0110.iteye.com/category/292875 http://blog.csdn.net/hguisu/article/details/8454368?reload ...
- Jsoup代码解读之一-概述
Jsoup代码解读之一-概述 今天看到一个用python写的抽取正文的东东,美滋滋的用Java实现了一番,放到了webmagic里,然后发现Jsoup里已经有了…觉得自己各种不靠谱啊!算了,静下心来学 ...
- HDU 5724 Chess(博弈论)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5724 [题目大意] 给出一个n行,每行有20格的棋盘,棋盘上有一些棋子,每次操作可以选择其中一个棋 ...
- Python3.2官方文档翻译--作用域和命名空间
6.2 Python作用域和命名空间 在介绍类之前.首先我想告诉你一些关于python作用域的规则. 类的定义很巧妙地运用了命名空间,你须要知道范围和命名空间的工作原理以能全面了解接下来发生的. 顺便 ...
- 【C/C++多线程编程之九】pthread读写锁
多线程编程之读写锁 Pthread是 POSIX threads 的简称,是POSIX的线程标准. pthread读写锁把对共享资源的訪问者分为读者和写者,读者仅仅对共享资源 ...
- ostringstream的使用方法
ostringstream的使用方法 [本文来自]http://www.builder.com.cn/2003/0304/83250.shtml http://www.cppblog.com/alan ...
- WCF 双工通信
注释:本学习是参考Artech大神的资料: 在WCF 实现双工通信 在这里我就不介绍双工通信的概念了,我写博客的目的是检测自己掌握情况,看我wcf通信后,觉得纸上得来终觉浅,绝知此事要躬行. 我使用的 ...