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

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. 【Poj1090】Chain

    Chain Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3414   Accepted: 1126 Description ...

  2. continue 的理解

    continue 一般出现循环体的开始部分,或中间部分,而不可能是结尾(没有必要,正常执行也会退出本次循环): 1. continue 的替代方案 while (true){ if (A || B){ ...

  3. VBNET AUTOCAD NETAPI 让插件随autocad启动

    定义一个函数,随AutoCAD 启动加载当前程序集到autocad,涉及到写入注册表,注意这是在autocad内部加载dll之后处理的方法.... 写入HKLM表示所有登录的用户都会受影响(autoc ...

  4. tomcat 参数调优

    JAVA_OPTS="-Xms2g -Xmx2g  -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath= ...

  5. 如何在Ubuntu上安装Wine 2.6

    Wine(Wine不是模拟器)是一种开源兼容层软件应用程序,可以让Linux和Unix用户通过Winelib软件库在他们的系统上运行Windows软件. sudo add-apt-repository ...

  6. JSON使用讲解

    前端操作json  一.JSON字符串与JSON对象的区别 1. 一个对象以“{”  开始,     “}”结束.   每个“名称”后跟一个“:”(冒号):“‘名称/值’ 对”之间运用 “,”(逗号) ...

  7. 04—AOP 实现项目中的切面编程

  8. ASP.NET 简介(转自Wiki)

    ASP.NET是由微软在.NET Framework框架中所提供,开发Web应用程序的类库,封装在System.Web.dll文件中,显露出System.Web名字空间,并提供ASP.NET网页处理. ...

  9. Winform学习知识汇总

    引用博客 http://www.cnblogs.com/peterzb/archive/2009/06/14/1502918.html

  10. LN : leetcode 191 Number of 1 Bits

    lc 191 Number of 1 Bits 191 Number of 1 Bits Write a function that takes an unsigned integer and ret ...