南邮数据结构实验1 单链表操作

2024-03-20 14:58

本文主要是介绍南邮数据结构实验1 单链表操作,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

内容和提示:


1.在顺序表类SingleList中增加成员函数void Reverse(),实现顺序表的逆置。

2.在顺序表类SingleList中增加成员函数bool DeleteX(const T &x),删除表中所有元素值等于x的元素。若表中存在这样的元素,则删除之,且函数返回true;否则函数返回false。

3.编写main函数,调用上述新增函数。

4.提示:创建LinearList.h SingleList.h文件包含程序2.1和程序2.3的代码。在其中新增上述两个函数。


#include <iostream>
using namespace std;
template <class T>
class LinearList
{
public:virtual bool IsEmpty() const = 0;virtual int Length() const = 0;virtual bool Find(int i,T& x) const = 0;virtual int Search(T x) const = 0;virtual bool Insert(int i,T x) = 0;virtual bool Delete(int i) = 0;virtual bool Update(int i,T x) = 0;virtual void Output(ostream& out) const = 0;
protected:int n;             //线性表的长度
};template <class T> class SingleList;
template <class T>
class Node
{
private:T element;Node<T> *link;friend class SingleList<T>;
};template <class T>
class SingleList : public LinearList<T>
{
private:Node<T>* first;//int n;
public:SingleList(){first = NULL; n = 0;}~SingleList();bool IsEmpty() const;int Length() const;bool Find(int i,T& x) const;int Search(T x) const;bool Insert(int i,T x);bool Delete(int i);bool Update(int i,T x);void Clear();void Output(ostream& out) const;void Reverse();bool DeleteX(const T& x);
};template <class T>
SingleList<T> :: ~SingleList()
{Node<T> *p;while(first){p = first -> link;delete first;first = p;}
}template <class T>
int SingleList<T> :: Length()const
{return n;
}template <class T>
bool SingleList<T> :: IsEmpty()const
{return n == 0;
}template <class T>
bool SingleList<T> :: Find(int i, T &x)const
{if(i < 0 || i > n - 1){cout << "Out Of Bound" << endl;return false;}Node<T> *p = first;for(int j = 0; j < i; j++)p = p -> link;x = p -> element;return true;
}template <class T>
int SingleList<T> :: Search(T x)const
{int j;Node<T> *p = first;for(j = 0; p && p -> element != x; j++)p = p -> link;if(p)return j;return -1;
}template <class T>
bool SingleList<T> :: Insert(int i, T x)
{if(i < -1 || i > n - 1){cout << "Out Of Bounds";return false;}Node<T> *q = new Node<T>;q -> element = x;Node<T> *p = first;for(int j = 0; j < i; j++)p = p -> link;if(i > -1){q -> link = p -> link;p -> link = q;}else{q -> link = first;first = q;}n++;return true;
}template <class T>
bool SingleList<T> :: Delete(int i)
{if(!n){cout << "UnderFlow" << endl;return false;}if(i < 0 || i > n - 1){cout << "Out Of Bound" << endl;return false;}Node<T> *p = first, *q = first;for(int j = 0; j < i - 1; j++)q = q -> link;if(i == 0)first = first -> link;else{p = q -> link;q -> link = p -> link;}delete p;n--;return true;
}template <class T>
bool SingleList<T> :: Update(int i, T x)
{if(i < 0 || i > n - 1){cout << "Out Of Bound" << endl;return false;}Node<T> *p = first;for(int j = 0; j < i; j++)p = p -> link;p -> element = x;return true;
}template <class T>
void SingleList<T> :: Output(ostream &out) const
{Node<T> *p = first;while(p){out << p -> element << " ";p = p -> link;}out << endl;
}template <class T>
void SingleList<T> :: Reverse()
{Node<T> *pre = first, *cur = first -> link, *next;while(cur){next = cur -> link;cur -> link = pre;pre = cur;cur = next;}first -> link = NULL;first = pre;
}template <class T>
bool SingleList<T> :: DeleteX(const T &x)
{int re = 0;bool ok = false;Node<T> *p = first;while(p){if(p -> element == x){re++;Delete(Search(x));ok = true;p  = first;}p = p -> link;}p = first;if(p -> element == x)Delete(Search(x));return ok;
}int main()
{int del_data, len ,num;SingleList<int> A;cout << "Input the length of the list: ";cin >> len;cout << "\nInput each element: ";for(int i = 0; i < len; i++){cin >> num;A.Insert(i - 1, num);}cout << "\nInitial list: ";  A.Output(cout);A.Reverse();cout << "\nResevered list: ";  A.Output(cout);cout << "\nInput the element to be deleted: ";  cin >> del_data;if(A.DeleteX(del_data) == true){cout << "\nList after being deleted: ";  A.Output(cout);}elsecout << "\nNot found" << endl;  return 0;
}


这篇关于南邮数据结构实验1 单链表操作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/829833

相关文章

python panda库从基础到高级操作分析

《pythonpanda库从基础到高级操作分析》本文介绍了Pandas库的核心功能,包括处理结构化数据的Series和DataFrame数据结构,数据读取、清洗、分组聚合、合并、时间序列分析及大数据... 目录1. Pandas 概述2. 基本操作:数据读取与查看3. 索引操作:精准定位数据4. Group

Python操作PDF文档的主流库使用指南

《Python操作PDF文档的主流库使用指南》PDF因其跨平台、格式固定的特性成为文档交换的标准,然而,由于其复杂的内部结构,程序化操作PDF一直是个挑战,本文主要为大家整理了Python操作PD... 目录一、 基础操作1.PyPDF2 (及其继任者 pypdf)2.PyMuPDF / fitz3.Fre

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

MySQL 强制使用特定索引的操作

《MySQL强制使用特定索引的操作》MySQL可通过FORCEINDEX、USEINDEX等语法强制查询使用特定索引,但优化器可能不采纳,需结合EXPLAIN分析执行计划,避免性能下降,注意版本差异... 目录1. 使用FORCE INDEX语法2. 使用USE INDEX语法3. 使用IGNORE IND

Python使用openpyxl读取Excel的操作详解

《Python使用openpyxl读取Excel的操作详解》本文介绍了使用Python的openpyxl库进行Excel文件的创建、读写、数据操作、工作簿与工作表管理,包括创建工作簿、加载工作簿、操作... 目录1 概述1.1 图示1.2 安装第三方库2 工作簿 workbook2.1 创建:Workboo

Ubuntu 24.04启用root图形登录的操作流程

《Ubuntu24.04启用root图形登录的操作流程》Ubuntu默认禁用root账户的图形与SSH登录,这是为了安全,但在某些场景你可能需要直接用root登录GNOME桌面,本文以Ubuntu2... 目录一、前言二、准备工作三、设置 root 密码四、启用图形界面 root 登录1. 修改 GDM 配

JSONArray在Java中的应用操作实例

《JSONArray在Java中的应用操作实例》JSONArray是org.json库用于处理JSON数组的类,可将Java对象(Map/List)转换为JSON格式,提供增删改查等操作,适用于前后端... 目录1. jsONArray定义与功能1.1 JSONArray概念阐释1.1.1 什么是JSONA

Java操作Word文档的全面指南

《Java操作Word文档的全面指南》在Java开发中,操作Word文档是常见的业务需求,广泛应用于合同生成、报表输出、通知发布、法律文书生成、病历模板填写等场景,本文将全面介绍Java操作Word文... 目录简介段落页头与页脚页码表格图片批注文本框目录图表简介Word编程最重要的类是org.apach

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE