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

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

posted @ 2022-12-20 20:03  师大无语  阅读(43)  评论(0编辑  收藏  举报