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

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. Spring 的Bean管理的常用注解

    属性注入的注解(使用注解注入的方式,可以不用提供set方法) @Value 用于注入普通类型 @Autowired 自动装配 :默认按类型进行装配  按名称注入 @Qualifier 强制使用名称注入 ...

  2. 关于Domain Sepcific Lang

    今天在看一些关于CO的东东 里面提到,用从语言派生出来的领域语言再去编写代码会大大加速开发进程 PHP应该是个典型的领域语言(Perl之于文本处理也是这样),虽然不是从什么其他领域派生出来的,但是使用 ...

  3. CSU 1807: 最长上升子序列~ 分类讨论

    1807: 最长上升子序列~ Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 138  Solved: 17[Submit][Status][Web Bo ...

  4. 【Codeforces】716D Complete The Graph

    D. Complete The Graph time limit per test: 4 seconds memory limit per test: 256 megabytes input: sta ...

  5. 国产手机没有google services 和google play崩溃,判断google services是否存在

    public static boolean isGooglePlayServiceAvailable (Context context) { int status = GooglePlayServic ...

  6. flask-migrate 数据迁移

    作用:做数据库迁移依赖:flask-script flask-sqlalchemy 使用 项目结构 manage.py(其它文件内容与flask-sqlalchemy中一样) from s8day13 ...

  7. UVa10288 Coupons 分数模版

    UVa10288 题目非常简单, 答案就是 n/n+n/(n-1)+...+n/1; 要求分数输出.套用分数模板.. #include <cstdio> #include <cstr ...

  8. bzoj 2015: [Usaco2010 Feb]Chocolate Giving【spfa】

    因为是双向边,所以相当于两条到1的最短路和,先跑spfa然后直接处理询问即可 #include<iostream> #include<cstdio> #include<q ...

  9. [C++ STL] 常用算法总结

    1 概述 STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<alg ...

  10. Android内存管理(15)SparseArray系列代替HashMap系列

    参考: https://liuzhichao.com/p/832.html http://www.2cto.com/kf/201311/255640.html 1,简介: SparseArray是an ...