首页 > 编程语言 >C++实现单链表相关操作

C++实现单链表相关操作

时间:2022-12-20 20:11:12浏览次数:45  
标签:Status 单链 cout int C++ next Linklist 操作 LNode

#include<iostream>
#include<cstdlib>//C++动态分配存储空间
using namespace std;
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef int Elemtype;
typedef int Status;
typedef struct LNode//定义结点
{
Elemtype date;//数据域
struct LNode* next;//地址域
}LNode,*Linklist;
Status InitLinklist(Linklist& L);//初始化链表
Status CreateLinklist(Linklist& L, int i);//创建单链表
Status GetLinklist(Linklist L, int i, Elemtype& e);//获取单链表
Status LocateLinklist(Linklist L, Elemtype e);//单链表中查找元素
Status InsertLinklist(Linklist& L, int i, Elemtype& e);//在单链表中插入元素
Status DeleteLinklist(Linklist& L, int i, Elemtype& e);//在单链表中删除元素
Status PrintLinklist(Linklist L);//在单链表中打印元素
int main(void)
{
int k = 0;
int len = 0;
int e = 0;
int i = 0;
Linklist L;
L = NULL;
do {
cout << "*****单链表的相关操作*****";
cout << "\n1.单链表的初始化";
cout << "\n2.创建含有len个元素的单链表(前插)";
cout << "\n3.在单链表中第i个位置前插入元素e";
cout << "\n4.在单链表中删除第i个元素";
cout << "\n5.在单链表中查找指定元素e的位置";
cout << "\n6.在单链表中获取第i个位置元素";
cout << "\n7.输出所有元素";
cout << "\n0.结束程序运行";
cout << "\n输入想要执行的操作序号:";
cin >> k;
switch (k)
{
case 1:
if (InitLinklist(L))
cout << "初始化成功。" << endl;
else
cout << "初始化失败。" << endl;
break;
case 2:
cout << "输入想要创建单链表元素的len的值为:";
cin >> len;
if (CreateLinklist(L,len))
cout << "创建成功。" << endl;
else
cout << "创建失败。" << endl;
break;
case 3:
cout << "\n输入想要插入的元素e和想要插入的位置i分别为:";
cin >> e >> i;
if (InsertLinklist(L, i, e))
cout << "插入成功。" << endl;
else
cout << "插入失败。" << endl;
break;
case 4:
cout << "\n想要删去元素的位置i";
cin >> i;
if (DeleteLinklist(L, i, e))
cout << "删去成功。" << endl;
else
cout << "删除失败。" << endl;
break;
case 5:
cout << "\n想要查找元素e为:";
cin >> e;
if (LocateLinklist(L,e))
cout << "查找成功。" << endl;
else
cout << "查找失败。" << endl;
break;
case 6:
cout << "\n想要获取的元素:";
cin >> e;
if (GetLinklist(L, i, e))
cout << "获取成功。" << endl;
else
cout << "获取失败。" << endl;
break;
case 7:
if(PrintLinklist(L))
cout << "打印成功。" << endl;
else
cout << "打印失败。" << endl;
break;
}
} while (k != 0);
return 0;
}
Status InitLinklist(Linklist& L)
{
L = new LNode;
L->next = NULL;
return OK;
}
Status CreateLinklist(Linklist& L, int i)
{
LNode* p;
int m = 0;
cout << "\n输入单链表的数据为:";
for (int t = 0; t < i; t++)
{
p = new LNode;
cin >> m;
p->date = m;
p->next = L->next;//前插
L->next = p;
}
return OK;
}
Status InsertLinklist(Linklist& L, int i, Elemtype& e)
{
LNode* p;
int j = 0;
p = L;
while (p && j < i - 1)
{
p = p->next;
++j;
}
if (!p || j > i - 1) return ERROR;
LNode* s;
s = new LNode;
s->date = e;
s->next = p->next;
p->next = s;
return OK;
}
Status DeleteLinklist(Linklist& L, int i, Elemtype& e)
{
LNode* p;
int j = 0;
p = L;//p指向头结点
while ((p->next) && j < i - 1)
{
p = p->next;
++j;
}
if (!(p->next) || j > i - 1) return ERROR;
LNode* q;
q = p->next;
e = q->date;
p->next = q->next;
delete q;
cout << "删除元素为:" << e << endl;
return OK;
}
Status LocateLinklist(Linklist L, Elemtype e)
{
LNode* p;
int j = 1;
p = L->next;//p指向单链表的首元节点
if (p == NULL) return ERROR;
while (p != NULL && p->date != e)
{
p = p->next;
j++;
}
cout << "元素" << e << "所在的位置是第" << j << "位";
return OK;
}
Status GetLinklist(Linklist L, int i,Elemtype& e)
{
LNode* p;
p = L->next;
int j = 1;
while (p && j != i)
{
p = p->next;
j++;
}
if (!p ||j>i ) return ERROR;
e = p->date;
cout << "第" << i << "位置元素为" << e << endl;
return OK;
}
Status PrintLinklist(Linklist L)
{
LNode* p;
p = L->next;
if (L == NULL)
{
cout << "\n表不存在。";
return ERROR;
}
cout << "\n元素为:";
while (p != NULL)
{
cout << p->date << " ";
p = p->next;
}
return OK;
}

标签:Status,单链,cout,int,C++,next,Linklist,操作,LNode
From: https://www.cnblogs.com/shidawuyu/p/16994999.html

相关文章

  • Java实现基本MySQL连接 - 数据的基本操作
    importjava.sql.*;publicclassMain{//MySQL8.0以下版本-JDBC驱动名及数据库URL//staticfinalStringJDBC_DRIVER="com.mysql.jdbc.Driver";......
  • linux操作系统的kill -9 和 kill -15 的区别
    在linux操作系统中,要杀死一个进程需要使用的命令是kill。1kill-9PID强制停掉进程,不给进程使用回调函数的机会,也不会等进程处理完手上的工作,对于已经进入生产环境的系......
  • c中关于文件操作
    读写文件与printf、scanf关联   printf--屏幕--标准输出   scanf--键盘--标准输入   perror--屏幕--标准错误系统文件:   标准输入--stdin-......
  • 多对多三种关系的三种方式 django内置序列组件 分页操作 form组件
    今日内容总结ajax补充主要针对回调函数args接收到的响应数据#1.后端request.is_ajax() 判断当前的请求是否由Ajax发出返回布尔值#2.后端返回的三板斧都会被args......
  • C++| 1-RAII
     RAII,完整的英文是ResourceAcquisitionIsInitialization,是C++所特有的资源管理方式。RAII依托栈和析构函数,来对所有的资源——包括堆内存在内——进行管理。对......
  • Python操作Excel(openpyxl)
    1.引入openpyxl库安装openpyxl库:pipinstallopenpyxl引入openpyxl库:fromopenpyxlimportload_worbook2.代码实现fromopenpyxlimportload_workbook#打开Excelw......
  • ajax补充,多对多三种创建方式,Django内置序列化,orm批量操作数据,自定义分页,form组件
    目录今日内容概要今日内容详细ajax补充说明多对多三种创建方式django内置序列化组件(drf前身)批量操作数据分页器思路自定义分页器的使用form组件今日内容概要ajax补充......
  • VScode配置支持c++11和配置自动编译调试功能
    VScode配置支持c++11和配置自动编译调试功能​​1在工程目录下新建.vscode目录​​​​2在.vscode目录下创建c_cpp_properties.json文件内容如下​​​​2.1添加c_cpp_pr......
  • c++ 去除字符串首尾的空白字符
    c++去除字符串首尾的空白字符​​方法一使用find_first_not_of和find_last_not_of​​​​方法二使用正则表达式(c++11)​​​​测试​​​​测试结果​​方法一使用find_f......
  • C++中的class
    用classqxz{};定义一个类,其中private:以下的每一行都是私有成员,而public:以下的每一行都是公有成员(两者可以反复交叉出现多次)。私有成员只能被自己类中的成员函数访问,而......