#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;
}

最新文章

  1. UML图中经常用到几种的关系图例
  2. 舍弃Nunit拥抱Xunit
  3. hibernate笔记--组件映射方法
  4. JS Scoping and Hoisting
  5. ubuntn14.04 32位安装hadoop2.7.2
  6. Android的横竖屏切换
  7. iOS使用阿里云OSS对象存储 (SDK 2.1.1)
  8. 在Activity之间传递数据—传递值对象
  9. linux的学习系列 1---简介
  10. noip普及组2005 陶陶摘苹果
  11. Java中enum的学习总结
  12. Spider_Man_3 の selenium
  13. 【Luogu1393】动态逆序对(CDQ分治)
  14. 爬坑二 activiti流数据库版本错误引发的问题
  15. 二、Java神经网络框架Neuroph的使用和架构分析
  16. PHP进阶-网络编程基础概念
  17. js总结001
  18. 更新npm至最新版本
  19. spring cloud ribbon源码解析(二)
  20. 第二阶段Sprint冲刺会议10

热门文章

  1. 肖sir_多线程Thread(threading)__知识点
  2. Easyui 表格列数据合并!
  3. Swift async await 使用介绍
  4. 应用kafka的经验
  5. cypress初探
  6. 备份docker mysql数据库
  7. 常见的Native Crash类型,bug解决记录
  8. Nginx的重写功能——Rewrite
  9. Self-Attention学习
  10. verilog 和system verilog 文件操作