最近在学习数据结构,刚开始一直在看书,但是总是感觉似懂非懂,心想还不如自己操练一波,势必有所收获。现将实现代码发表此地,以备日后复习,若有错误或者建议,欢迎告知本人!

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++实现的更多相关文章

  1. 数据结构:DHUOJ 单链表ADT模板应用算法设计:长整数加法运算(使用单链表存储计算结果)

    单链表ADT模板应用算法设计:长整数加法运算(使用单链表存储计算结果) 时间限制: 1S类别: DS:线性表->线性表应用 题目描述: 输入范例: -5345646757684654765867 ...

  2. Python—数据结构——链表

    数据结构——链表 一.简介 链表是一种物理存储上非连续,数据元素的逻辑顺序通过链表中的指针链接次序,实现的一种线性存储结构.由一系列节点组成的元素集合.每个节点包含两部分,数据域item和指向下一个节 ...

  3. (js描述的)数据结构[链表](4)

    (js描述的)数据结构 [链表](4) 一.基本结构 二.想比于数组,链表的一些优点 1.内存空间不是必须连续的,可以充分利用计算机的内存,事项灵活的内存动态管理. 2.链表不必再创建时就确定大小,并 ...

  4. 数据结构和算法(Golang实现)(12)常见数据结构-链表

    链表 讲数据结构就离不开讲链表.因为数据结构是用来组织数据的,如何将一个数据关联到另外一个数据呢?链表可以将数据和数据之间关联起来,从一个数据指向另外一个数据. 一.链表 定义: 链表由一个个数据节点 ...

  5. Redis数据结构—链表与字典的结构

    目录 Redis数据结构-链表与字典的结构 链表 Redis链表节点的结构 Redis链表的表示 Redis链表用在哪 字典 Redis字典结构总览 Redis字典结构分解 Redis字典的使用 Re ...

  6. Redis数据结构—链表与字典

    目录 Redis数据结构-链表与字典 链表 Redis链表节点的结构 Redis链表的表示 Redis链表用在哪 字典 Redis字典结构总览 Redis字典结构分解 哈希算法 解决键冲突 rehas ...

  7. 《数据结构与算法分析》学习笔记(三)——链表ADT

    今天简单学习了下链表,待后续,会附上一些简单经典的题目的解析作为学习的巩固 首先要了解链表,链表其实就是由一个个结点构成的,然后每一个结点含有一个数据域和一个指针域,数据域用来存放数据,而指针域则用来 ...

  8. 单链表ADT

    本博客第一篇学术性博客,所以还是写点什么东西: 首先这篇博客以及以后的博客中的代码尽量百分之90是自己写过的: 可能有部分图片和代码是我认为别人更好的故摘抄下来, 本人三观正确,所以一定会表明来源: ...

  9. ①泡茶看数据结构-表ADT

    前言     小朽,晚上回到寝室.烧了开水,又泡了一杯下午喝了的小毛尖.耳机听着萨克斯,总结下今天学的数据结构和算法中的表ADT.       表ADT节点: #单链表   #双链表   #循环链表 ...

随机推荐

  1. Use of implicitly declared global variable

    https://stackoverflow.com/questions/7604419/resharper-javascript-use-of-implicitly-declared-global-v ...

  2. hive使用

    运行hadoop [root@hadoop0 ~]# start-all.sh 进入命令行[root@hadoop0 ~]# hive 查询昨天的表 hive> select * from st ...

  3. java运行代码连接mysql时提示:找不到类错误

    使用IntelliJ IDEA Community Edition进行代码编写.. 使用一下代码连接mysql时出现了:java.lang.ClassNotFoundException: com.my ...

  4. bzoj Strange Way to Express Integers【excrt】

    其实我没看懂题不如说根本没看--都说是excrt板子那就写个板子吧 注意开long long #include<iostream> #include<cstdio> using ...

  5. bzoj 1700: [Usaco2007 Jan]Problem Solving 解题【dp】

    很像贪心的dp啊 这个定金尾款的设定让我想起了lolita和jk制服的尾款地狱-- 设f[i][j]为从j到i的付定金的最早月份然后从f[k][j-1]转移来,两种转移f[i][j]=min(f[i] ...

  6. bzoj 1615: [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机【bfs】

    直接bfs即可,注意开double,还有驱动和终点的齿轮都在序列里,要把它们找出来= = #include<iostream> #include<cstdio> #includ ...

  7. thinkphp5 分页 paginate

    tp5分页带参数的时候,用到 paginate 后面的几个参数 paginate有三个参数, 第一个必须表是每页分多少个[如果配置文件中设置了,可以不用] 第二个参数表是的是简洁分页,如果为true, ...

  8. 递推 Codeforces Round #186 (Div. 2) B. Ilya and Queries

    题目传送门 /* 递推:用cnt记录前缀值,查询区间时,两个区间相减 */ #include <cstdio> #include <algorithm> #include &l ...

  9. Hibernate 一对多查询对set的排序

    Hibernate可以进行一对多的关联查询,例如:查询了试卷题目,可以自动获取试卷题目的选项对象. 但是关联出来的集合对象是无序的,那么在显示的时候就会有问题,经过百度发现可以对Set进行设置排序. ...

  10. Python---查看安装路径

    python是解释型脚本语言,在执行时,逐句解释执行,不需要进行预编译.但需要有自身的Python解释器. 所以在执行Python代码时,需要指定python解释器. 指定解释器方法: 在文件开头添加 ...