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

2024-03-31 18:48

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

内容和提示:

类似1.1


#include <iostream>
using namespace std;
const int SIZE=20 ;template <class T>
class LinearList
{
protected:
int n; 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)=0;
virtual bool Delete(int i)=0;
virtual bool Update(int i,T x)=0;
virtual void Output(ostream &out) const=0;
};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>
{
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 Reserve();                 //链接表逆置
bool DeleteX(const T &x);       //删除所有元素private:
Node<T> *first;
};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 bounds"; 
return false;
}
Node<T> *p = first;
for (int j = 0; j < i - 1; 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 bounds" << 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 bounds" << 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>::Reserve()          //链接表逆置
{ 
for (int i = 0; i <n -1;i++)
{
Insert(n - 1-i, first->element);
Delete(0);
}
}template<class T>
bool SingleList<T>::DeleteX(const T &x)     
{
if (Search(x) < 0)
return false;
else
{
while (Search(x)>=0)
Delete(Search(x));
}
return true;
}void main()
{
SingleList <int> LB;
int x,n,a;
cout<<"Please input the length:"<<endl;
cin>>n;
cout<<"Please input the SingleList:"<<endl;
for(int  i=0;i<n;i++)
{ 
cin>>x;
LB.Insert(i-1,x);
}
LB.Output(cout);cout<<"Please input x to be deleted:"<<endl;
cin>>a;
LB.DeleteX(a);
cout<<"After delete:"<<endl;
LB.Output(cout);LB.Reserve();
cout<<"After reserved:"<<endl;
LB.Output(cout);
}


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



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

相关文章

Git可视化管理工具(SourceTree)使用操作大全经典

《Git可视化管理工具(SourceTree)使用操作大全经典》本文详细介绍了SourceTree作为Git可视化管理工具的常用操作,包括连接远程仓库、添加SSH密钥、克隆仓库、设置默认项目目录、代码... 目录前言:连接Gitee or github,获取代码:在SourceTree中添加SSH密钥:Cl

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

Python ZIP文件操作技巧详解

《PythonZIP文件操作技巧详解》在数据处理和系统开发中,ZIP文件操作是开发者必须掌握的核心技能,Python标准库提供的zipfile模块以简洁的API和跨平台特性,成为处理ZIP文件的首选... 目录一、ZIP文件操作基础三板斧1.1 创建压缩包1.2 解压操作1.3 文件遍历与信息获取二、进阶技

Java中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

Java字符串操作技巧之语法、示例与应用场景分析

《Java字符串操作技巧之语法、示例与应用场景分析》在Java算法题和日常开发中,字符串处理是必备的核心技能,本文全面梳理Java中字符串的常用操作语法,结合代码示例、应用场景和避坑指南,可快速掌握字... 目录引言1. 基础操作1.1 创建字符串1.2 获取长度1.3 访问字符2. 字符串处理2.1 子字

Python 中的 with open文件操作的最佳实践

《Python中的withopen文件操作的最佳实践》在Python中,withopen()提供了一个简洁而安全的方式来处理文件操作,它不仅能确保文件在操作完成后自动关闭,还能处理文件操作中的异... 目录什么是 with open()?为什么使用 with open()?使用 with open() 进行

Linux ls命令操作详解

《Linuxls命令操作详解》通过ls命令,我们可以查看指定目录下的文件和子目录,并结合不同的选项获取详细的文件信息,如权限、大小、修改时间等,:本文主要介绍Linuxls命令详解,需要的朋友可... 目录1. 命令简介2. 命令的基本语法和用法2.1 语法格式2.2 使用示例2.2.1 列出当前目录下的文

C#数据结构之字符串(string)详解

《C#数据结构之字符串(string)详解》:本文主要介绍C#数据结构之字符串(string),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录转义字符序列字符串的创建字符串的声明null字符串与空字符串重复单字符字符串的构造字符串的属性和常用方法属性常用方法总结摘