C++ 数据结构学习二(单链表)
模板类
//LinkList.h 单链表
#ifndef LINK_LIST_HXX
#define LINK_LIST_HXX
#include <iostream>
using namespace std;
template<class T>
struct Node
{
T data;
Node * next;
};
template<class T>
class LinkList
{
public:
LinkList(); //无参构造函数,建立只有头结点的空链表
LinkList(T a[],int n); //有参构造函数,建立有n个元素的单链表
~LinkList(); //析构函数
int Length(); //求单链表长度
T Get(int i); //按位查找,在单链表中查找第i个结点的元素值
int Locate(T x); //按值查找,在单链表中查找值为x的元素序号
void Insert(int i,T x); //插入操作,在第i个位置插入元素值为x的结点
T Delete(int i); //删除操作,在单链表中删除第i个结点
void PrintList(); //遍历操作,按顺序依次输出各元素
private:
Node<T> * first; //单链表的头指针
};
template<class T>
LinkList<T>::LinkList()
{
first=new Node<T>; //生成头结点
first->next=NULL; //头结点指针置空
}
/*
template<class T>
LinkList<T>::LinkList(T a[],int n) //头插法建立单链表LinkList
{
first=new Node<T>;
first->next=NULL; //初始化一个空链表
for(int i=0;i<n;i++)
{
Node<T> *s=new Node<T>;
s->data=a[i]; //为每个元素建立一个结点
s->next=first->next;
first->next=s; //将结点s插入到头结点之后
}
}
*/
template<class T>
LinkList<T>::LinkList(T a[],int n) //尾插法建立单链表LinkList
{
first=new Node<T>;
Node<T> *r=new Node<T>;
r=first;
for(int i=0;i<n;i++)
{
Node<T> *s=new Node<T>;
s->data=a[i]; //为每个元素建立一个结点
r->next=s; //将结点s插入到终结点之后
r=s;
}
r->next=NULL; //单链表建立完毕,将终结点的指针置空
}
template<class T>
LinkList<T>::~LinkList()
{
while(first!=NULL) //释放单链表的每一个结点的存储空间
{
Node<T> *q=new Node<T>;
q=first; //暂存被释放的结点
first=first->next;//first指向被释放结点的下一个结点
delete q;
}
}
template<class T>
int LinkList<T>::Length()
{
Node<T> *p=first->next;
int count=0; //工作指针p和累加器count初始化
while(p!=NULL)
{
p=p->next;
count++;
}
return count; //注意count的初始化和返回值的关系
}
template<class T>
T LinkList<T>::Get(int i)
{
Node<T> *p=first->next;
int count=1;
while(p!=NULL&&count<i)
{
p=p->next;
count++;
}
if(p==NULL) throw "位置错误";
else return p->data;
}
template<class T>
int LinkList<T>::Locate(T x)
{
Node<T> *p=first->next;
int count=1;
while(p!=NULL)
{
if(p->data==x) return count; //查找成功,结束返回位号
p=p->next;
count++;
}
return 0; //返回0表明查找失败
}
template<class T>
void LinkList<T>::Insert(int i,T x)
{
Node<T> *p=first->next; //工作指针指向头结点
int count=1;
while(p!=NULL&&count<i-1) //查找第i-1个结点
{
p=p->next; //工作指针后移
count++;
}
if(p==NULL) throw "位置"; //没有找到第i-1个结点
else{
Node<T> *s=new Node<T>;
s->data=x;
s->next=p->next; //将结点s插到结点p之后
p->next=s;
}
}
template<class T>
T LinkList<T>::Delete(int i)
{
Node<T> *p=new Node<T>;
p=first->next;
int count=1;
while(p!=NULL&&count<i-1)
{
p=p->next;
count++;
}
if(p==NULL||p->next==NULL) throw "位置"; //结点p不存在 或者 p指向的下一个结点不存在
else{
Node<T> *q=new Node<T>;
q=p->next;
T x=p->data;
p->next=q->next; //摘链
delete q;
return x;
}
}
template<class T>
void LinkList<T>::PrintList()
{
Node<T> *p=first->next;
while(p!=NULL)
{
cout<<p->data<<endl;
p=p->next;
}
}
#endif
测试
//LinkListDemo.cpp
#include <iostream>
#include "LinkList.h"
using namespace std;
int main()
{
int a[10]={1,2,3,4,5,6,7,8,9,10};
LinkList<int> linkList(a,10);
cout<<"原始单链表数据"<<endl;
linkList.PrintList();
cout<<"数据总量"<<linkList.Length()<<endl;
linkList.Delete(5);
cout<<"删除后数据"<<endl;
linkList.PrintList();
cout<<"数据总量"<<linkList.Length()<<endl;
linkList.Insert(5,108);
cout<<"插入后数据"<<endl;
linkList.PrintList();
cout<<"数据总量"<<linkList.Length()<<endl;
cout<<"查询数据"<<linkList.Get(10)<<endl;
cout<<"查询位置"<<linkList.Locate(108);
return 0;
}
C++ 数据结构学习二(单链表)的更多相关文章
- 数据结构——Java实现单链表
一.分析 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素.链表中的数据是以结点来表示的,每个结点由元素和指针构成.在Java中,我们可以将单链表定义成一个类,单链表的基 ...
- PHP数据结构之实现单链表
学习PHP中,学习完语法,开始尝试实现数据结构,今天实现单链表 <?php class node //节点的数据结构 { public $id; public $name; public $ne ...
- js数据结构与算法--单链表的实现与应用思考
链表是动态的数据结构,它的每个元素由一个存储元素本身的节点和一个指向下一个元素的引用(也称指针或链接)组成. 现实中,有一些链表的例子. 第一个就是寻宝的游戏.你有一条线索,这条线索是指向寻找下一条线 ...
- 自己动手实现java数据结构(二) 链表
1.链表介绍 前面我们已经介绍了向量,向量是基于数组进行数据存储的线性表.今天,要介绍的是线性表的另一种实现方式---链表. 链表和向量都是线性表,从使用者的角度上依然被视为一个线性的列表结构.但是, ...
- c++学习笔记—单链表基本操作的实现
用c++语言实现的单链表基本操作,包括单链表的创建(包括头插法和尾插法建表).结点的查找.删除.排序.打印输出.逆置.链表销毁等基本操作. IDE:vs2013 具体实现代码如下: #include ...
- 链表学习二:链表反转与查找倒数第K个
//单链表反转 ListNode* RevertList(ListNode* m_pHead){ ListNode* pCurrent = m_pHead; ListNode* pPrev=NULL; ...
- 数据结构:DHUOJ 单链表ADT模板应用算法设计:长整数加法运算(使用单链表存储计算结果)
单链表ADT模板应用算法设计:长整数加法运算(使用单链表存储计算结果) 时间限制: 1S类别: DS:线性表->线性表应用 题目描述: 输入范例: -5345646757684654765867 ...
- c++学习之单链表以及常用的操作
新学数据结构,上我写的代码. #include <iostream> #include <cstdlib> using namespace std; typedef int E ...
- C语言学习016:单链表
#include <stdio.h> //定义一个链表,链表是一种递归结构,在定义的时候必须要给结构起一个名字 typedef struct folder{ int level; char ...
随机推荐
- CMS(Concurrent Mark-Sweep)
CMS(Concurrent Mark-Sweep)是以牺牲吞吐量为代价来获得最短回收停顿时间的垃圾回收器.对于要求服务器响应速度的应用上,这种垃圾回收器非常适合.在启动JVM参数加上-XX:+Use ...
- SVG彩虹字
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Android学习之Notification
Notification可以在手机的状态栏发出一则通知,它需要用NotificationManager来管理,实现Notification其实很简单. 1.通过getsystemservice方法获得 ...
- Win手机安卓程序初体验
老大说快看博客园,Windows手机可以装安卓程序了. 啊,真的么?可以在我的撸妹1520上愉快的玩COC了么?我还可以愉快的看小说,不对,是听小说,哈哈,安卓君的三千万程序兵,等着老夫来一一临幸你们 ...
- UVA 529 - Addition Chains,迭代加深搜索+剪枝
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
- Binder的非正常消亡时的重置方法
一.原理 当Binder非正常消亡的时候,会导致远程调用失败,这样客户端功能就会受到影响. 解决:给Binder设置一个死亡代理,当Binder死亡时,我们就会收到通知,这个时候可以重新发起连接. 二 ...
- Bitmap、BitmapDrawable、BitmapFactory、Matrix类之间的关系
1.BitmapFactory是一个工具类 Bitmap实现在android.graphics包中.但是Bitmap类的构造函数是私有的,外面并不能实例化,只能是通过JNI实例化.这必然是 某个辅助类 ...
- phpcms在自定义模块中的自定义标签分页
如果你是一个经验丰富的phpcms二次开发人员,本篇文章可以忽略不计,因为这里的写法自己都觉得很恶心 今天在开发一个网站自建了一个模块叫做论坛模块,目录名称:luntan ...
- RoHS认证简介
RoHS认证是<电气.电子设备中限制使用某些有害物质指令>(The restriction of the use of certain hazardous substances in el ...
- Android Studio下载安装及配置图文教程
原文 http://jingyan.baidu.com/article/9c69d48f56835e13c9024e95.html AndroidStudio下载地址:https://develope ...