一个简单链表的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) 有符号一般整 ...
随机推荐
- 四轴飞行器1.2.3 STM32F407时钟配置和升级标准库文件
原创文章,欢迎转载,转载请注明出处 这个星期进度比较慢哈,只有周末和晚上下班回来才能做,事件不连续,琐碎的事情又比较多,挺烦的,有多琐碎呢? 1.本人有点小强迫症哈,虽然RTT将文 ...
- JS表格排序
var employees = [] employees[0] = { name: "George", age: 32, retiredate: "March 12, 2 ...
- MockObject
http://www.mockobjects.com/ http://jmock.org/download.html https://jakarta.apache.org/cactus/mock_vs ...
- 在Linux下使用iconv转换字符串编码
在Linux下写C程序,尤其是网络通信程序时经常遇到编码转换的问题,这里要用到iconv函数库. iconv函数库有以下三个函数 123456 #include <iconv.h>icon ...
- HDU 5758 Explorer Bo(树形DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5758 [题目大意] 给出一棵树,每条路长度为1,允许从一个节点传送到任意一个节点,现在要求在传送次 ...
- oralce dubugs
1,The listener supports no services 2,invalid specification for system parameter LOCAL_LISTENER crea ...
- jQuery Lint: enables you to automatically inject jQuery Lint into the page as it is loaded (great for ad-hoc code validation)
FireQuery is a Firebug extension for jQuery development jQuery Lint: enables you to automatically in ...
- PHP中的一个很好用的文件上传类
<?php class FileUpload{ private $filepath; //设置上传文件的路径 private $allowtype=array('jpg',' ...
- sp_makewebtask
Transact-SQL 参考 sp_makewebtask 创建一项生成 HTML 文档的任务,该文档包含执行过的查询返回的数据. 说明 所有 Web 作业在企业管理器的"作业分类& ...
- Product(大数相乘)
Description The problem is to multiply two integers X, Y. (0<=X,Y<10250) Input The input will ...