数据结构---链表ADT C++实现
最近在学习数据结构,刚开始一直在看书,但是总是感觉似懂非懂,心想还不如自己操练一波,势必有所收获。现将实现代码发表此地,以备日后复习,若有错误或者建议,欢迎告知本人!
1. 节点类
class Node {
public:
int data;
Node *next;
Node(int da):
data(da), next(NULL){}
};
这里节点类的定义采用多数OJ的模版(当然,也可以使用 struct )
2. 链表类ADT
class List{
private:
Node *head;
public:
List(): head(NULL){}
~List(){
delete head;
cout<<"The list is deleted."<<endl;
};
int size(); // 链表长度
bool isEmpty(); // 是否为空
void printList(); // 打印链表
void insert(int position, int value); // 指定位置插入
void insertHead(int value); // 插入到最前
void insertTail(int value); // 插入到最后
int getValue(int position); // 查找指定位置的值
int search(int value); // 查找指定元素的位置
void update(int position, int value); // 更新指定位置的值
int erase(int position); // 删除指定位置的节点
void reverse(); // 反转链表
//void clearList() // 清空链表
};
3. 完整代码
#include<iostream>
using namespace std; class Node {
public:
int data;
Node *next;
Node(int da):
data(da), next(NULL){}
}; class List{
private:
Node *head;
public:
List(): head(NULL){}
~List(){
delete head;
cout<<"The list is deleted."<<endl;
};
int size(); // 链表长度
bool isEmpty(); // 是否为空
void printList(); // 打印链表
void insert(int position, int value); // 指定位置插入
void insertHead(int value); // 插入到最前
void insertTail(int value); // 插入到最后
int getValue(int position); // 查找指定位置的值
int search(int value); // 查找指定元素的位置
void update(int position, int value); // 更新指定位置的值
int erase(int position); // 删除指定位置的节点
void reverse(); // 反转链表
//void clearList() // 清空链表
}; // 返回链表大小
int List::size(){
Node *p = head;
int index = ;
while(p != NULL){
index++;
p = p->next;
}
return index;
} // 判断链表是否为空
bool List::isEmpty(){
if(List::size() == )
return true;
else
return false;
} // 打印链表
void List::printList(){
Node *p = head;
while(p != NULL){
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
cout<<endl;
} // 在position位置插入value
void List::insert(int position, int value){
if(position< || position>List::size()){
cout<<"position error, please check."<<endl;
return ;
}
Node *s = new Node(value); // new node
Node *p = head;
if(head == NULL){ // isEmpty = true
head = s;
}
else{ // isEmpty = false
if(position == ){
s->next = p;
head = s;
}
else{
int index = ;
while(index != position-){
p = p->next;
index++;
}
s->next = p->next;
p->next = s;
}
}
if (position == )
cout<<"insert "<<value<<" at the first."<<endl;
else if (position == List::size())
cout<<"insert "<<value<<" at the tail."<<endl;
else
cout<<"insert "<<value<<" at position "<<position<<endl;
} // 头部插入
void List::insertHead(int value){
List::insert(, value);
} // 尾部插入
void List::insertTail(int value){
List::insert(List::size(), value);
} // 搜索数据value,并返回位置
int List::search(int value){
Node *p = head;
int index = ;
while(p != NULL && p->data != value){
index++;
p = p->next;
}
if(p == NULL){
cout<<"the value is not in the list, please check."<<endl;
return -;
}
else{
cout<<value<<" at position "<<index<<endl;
return index;
}
} // 将position位置的数据更新为value
void List::update(int position, int value){
if(position< || position>(List::size()-)){
cout<<"position error, please check."<<endl;
return ;
}
Node *p = head;
int index = ;
while(index != position){
p = p->next;
index++;
}
p->data = value;
cout<<"update "<<value<<" at position "<<position<<endl;
} // 删除position位置的数据,并返回
int List::erase(int position){
if(position< || position>(List::size()-)){
cout<<"position error, please check."<<endl;
return -;
}
int res = List::getValue(position);
Node *p = head;
int index = ;
cout<<"erase data at position "<<position<<endl;
if(position == ){
head = p->next;
return res;
}
else{
while(index != position-){
p = p->next;
index++;
}
Node *temp = p->next;
p->next = temp->next;
return res;
}
} // 反转链表
void List::reverse(){
if (head == NULL || head->next == NULL)
return ;
Node *prev = head;
Node *cur = head->next;
Node *temp = head->next->next;
while(cur){
temp = cur->next;
cur->next = prev;
prev = cur;
cur = temp;
}
head->next = NULL; // 更新末尾元素的指针
head = prev; // 更新头结点
cout<<"reverse the list."<<endl;
} // 返回position位置的数据
int List::getValue(int position){
if(position< || position>(List::size()-)){
cout<<"position error, please check."<<endl;
return -;
}
Node *p = head;
int index = ;
while(index != position){
p = p->next;
index++;
}
cout<<"position "<<position<<" is "<<p->data<<endl;
return p->data;
}
/*
void List::clearList(){
Node *p = head;
while(p){
Node *temp = p->next;
delete p;
p = temp;
}
}
*/
int main() {
List l1;
l1.insertTail();l1.printList();
l1.insertHead();l1.printList();
l1.insert(, );l1.printList();
l1.insert(, );l1.printList();
l1.insert(, );l1.printList();
l1.insert(, );l1.printList();
l1.insert(, );l1.printList();
l1.search();
l1.getValue();
l1.update(, );l1.printList();
l1.erase();l1.printList();
l1.reverse(); l1.printList();
cout<<"The size of the list: "<<l1.size()<<endl;
return ;
}
4. 运行结果
insert at the first. insert at the first. insert at position insert at the first. insert at position insert at the first. insert at position at position
position is
update at position position is
erase data at position reverse the list. The size of the list:
The list is deleted.
[Finished in .0s]
关于每一部分的详解会继续补充。
数据结构---链表ADT C++实现的更多相关文章
- 数据结构:DHUOJ 单链表ADT模板应用算法设计:长整数加法运算(使用单链表存储计算结果)
单链表ADT模板应用算法设计:长整数加法运算(使用单链表存储计算结果) 时间限制: 1S类别: DS:线性表->线性表应用 题目描述: 输入范例: -5345646757684654765867 ...
- Python—数据结构——链表
数据结构——链表 一.简介 链表是一种物理存储上非连续,数据元素的逻辑顺序通过链表中的指针链接次序,实现的一种线性存储结构.由一系列节点组成的元素集合.每个节点包含两部分,数据域item和指向下一个节 ...
- (js描述的)数据结构[链表](4)
(js描述的)数据结构 [链表](4) 一.基本结构 二.想比于数组,链表的一些优点 1.内存空间不是必须连续的,可以充分利用计算机的内存,事项灵活的内存动态管理. 2.链表不必再创建时就确定大小,并 ...
- 数据结构和算法(Golang实现)(12)常见数据结构-链表
链表 讲数据结构就离不开讲链表.因为数据结构是用来组织数据的,如何将一个数据关联到另外一个数据呢?链表可以将数据和数据之间关联起来,从一个数据指向另外一个数据. 一.链表 定义: 链表由一个个数据节点 ...
- Redis数据结构—链表与字典的结构
目录 Redis数据结构-链表与字典的结构 链表 Redis链表节点的结构 Redis链表的表示 Redis链表用在哪 字典 Redis字典结构总览 Redis字典结构分解 Redis字典的使用 Re ...
- Redis数据结构—链表与字典
目录 Redis数据结构-链表与字典 链表 Redis链表节点的结构 Redis链表的表示 Redis链表用在哪 字典 Redis字典结构总览 Redis字典结构分解 哈希算法 解决键冲突 rehas ...
- 《数据结构与算法分析》学习笔记(三)——链表ADT
今天简单学习了下链表,待后续,会附上一些简单经典的题目的解析作为学习的巩固 首先要了解链表,链表其实就是由一个个结点构成的,然后每一个结点含有一个数据域和一个指针域,数据域用来存放数据,而指针域则用来 ...
- 单链表ADT
本博客第一篇学术性博客,所以还是写点什么东西: 首先这篇博客以及以后的博客中的代码尽量百分之90是自己写过的: 可能有部分图片和代码是我认为别人更好的故摘抄下来, 本人三观正确,所以一定会表明来源: ...
- ①泡茶看数据结构-表ADT
前言 小朽,晚上回到寝室.烧了开水,又泡了一杯下午喝了的小毛尖.耳机听着萨克斯,总结下今天学的数据结构和算法中的表ADT. 表ADT节点: #单链表 #双链表 #循环链表 ...
随机推荐
- CANopen——笔记
1. c语言的typedef高级用法 typedef void (*post_sync_t)(CO_Data*); http://zhidao.baidu.com/link?url=_lDBGq_uk ...
- python-----写入txt用法
代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/1/14 11:23 # @Author : zxb file_p ...
- 使用nginx搭建媒体点播服务器
使用nginx搭建媒体点播服务器 最新由于兴趣,对ubuntu和安卓上的视频点播直播等应用比较感兴趣,所以在vmware的虚拟机里面搭建了一个视频点播网站,参考了fengzhanhai的文章Nginx ...
- JeePlus:代码生成器-生成示例(操作)
ylbtech-JeePlus:代码生成器-生成示例(操作) 1.返回顶部 1. 生成示例由以下部分组成 单表 主附表 树表 富文本 图片管理 自定义树组件 自定义Grid 多对多 左树右表 2. 2 ...
- MyBatis高级查询 存储过程
1.第一个存储过程 根据用户id查询用户其他信息 #第一个存储过程 #根据用户id查询用户其他信息 DROP PROCEDURE IF EXISTS `select_user_by_id`; DEL ...
- goalng——time包学习
1.星期:type Weekday int const ( Sunday Weekday = iota Monday Tuesday Wednesday Thursday Friday Saturda ...
- Cmake编译protobuf
编译指令,在powershell中执行 : .\protoc.exe .\ive.proto --cpp_out . .\protoc.exe .\ive.proto --csha ...
- [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列
Description N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色,每头牛有多种特色,用二进制01表示它的特色ID.比如特色ID为13(1101),则 ...
- ASP.NET 简介(转自Wiki)
ASP.NET是由微软在.NET Framework框架中所提供,开发Web应用程序的类库,封装在System.Web.dll文件中,显露出System.Web名字空间,并提供ASP.NET网页处理. ...
- cocos2d-x lua中实现异步加载纹理
原文地址: http://www.cnblogs.com/linchaolong/p/4033118.html 前言 问题:最近项目中需要做一个loading个界面,界面中间有一个角色人物走动的 ...