#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
//单链表节点定义
typedef struct LNode
{
int data;
struct LNode* next;
}LNode, * LinkList;
LinkList L;
//1.前插法初始化单链表
void CreatList_head(LinkList& L, int num1)
{
cout << "请依次换行输入" << num1 << "个数据!" << endl;
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL; //初始化单链表
LNode* s; //声明一个中间变量
for (int i = 0; i < num1; i++)
{
s = (LNode*)malloc(sizeof(LNode)); //生成一个新节点
cin >> s->data;
s->next = L->next; //新结点的next指针指向开始结点
L->next = s; //头结点的next指针指向新结点
}
cout << "创建成功!!!" << endl;
}
//2.尾插法初始化单链表
void CreatList_tail(LinkList& L, int num1)
{
cout << "请依次换行输入" << num1 << "个数据!" << endl;
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
LNode* s;
LNode* r = L;
for (int i = 0; i < num1; i++)
{
s = (LNode*)malloc(sizeof(LNode));
cin >> s->data;
r->next = s;
r = s;
}
r->next = NULL;
cout << "创建成功!!!" << endl;
}
void showfunction() //展示操作的功能
{
cout << "单链表功能展示" << endl;
cout << "1.前插法初始化单链表" << endl; // ok
cout << "2.尾插法初始化单链表" << endl; //ok
cout << "3.输出单链表" << endl; //ok
cout << "4.输出单链表长度" << endl; //ok
cout << "5.输出单链表第a个元素" << endl; //OK
cout << "6.输出元素b的位置" << endl; //ok
cout << "7.指定位置插入单个元素" << endl; // Ok
cout << "8.指定位置删除单个元素" << endl; //OK
cout << "9.判断单链表是否为空" << endl; //ok
cout << "0.释放单链表" << endl; //ok
}
void print(LinkList& L) //输出单链表
{
LNode* node = L->next;
cout << "表中元素为:";
if (node == NULL)
{
cout << " 该表为空!!!" << endl;
}
else
{
while (node != NULL)
{
cout << " " << node->data << " ";
node = node->next;
}
cout << endl;
}
}
void ListLength(LinkList& L) //输出单链表长度
{
int i = 0;
LNode* p = L;
while (p->next != NULL)
{
i++;
p = p->next;
}
cout << "单链表长度为"<<i << endl;
}
int Getbyzhi(LinkList& L, int elem) //给出元素查找位置
{
LNode* temp = L;
int i = 0;
while (temp->next)
{
i = i + 1;
temp = temp->next;
if (elem == temp->data)
{
cout << "该值在链表中的第" << i << "位" << endl;
return 0;
}
}
cout << "查找失败!!!" << endl;
return 0;
}
void GetElem(LinkList& L, int place) //输出单链表第a个元素
{
LNode* node = L;
int j = 0;
while (node && j < place)
{
node = node->next;
j = j + 1;
}
if (!node || j > place)
{
cout << "输入的值有问题!!!" << endl;
}
else
{
cout << "该位置上的值为" << node->data << endl;
}
}
void Insert(LinkList& L, int place1, int elem) //指定位置插入一个元素
{
LNode* p = L;
int j = 0;
while (p && (j < place1 - 1))
{
p = p->next;
j++;
}
if (p == NULL)
{
cout << "插入失败!!!" << endl;
}
else
{
LNode* s = (LNode*)malloc(sizeof(LNode));
s->data = elem;
s->next = p->next;
p->next = s;
cout << "插入成功!!!" << endl;
}
}
void DeleteElem(LinkList& L, int place3) //指定位置删除单个元素
{
LNode* p = L;
int j = 0;
while (p && j < place3 - 1)
{
p = p->next;
j++;
}
if (p == NULL)
{
cout << "删除失败!!!" << endl;
}
else
{
LNode* newone = p->next;
p->next = newone->next;
free(newone);
cout << "删除成功!!!" << endl;
}
}
void DestroyList(LinkList& L) //利用递归的方法进行释放每一个对象,,,迭代到最后一个对象,利用栈 思想进行释放内存
{
if (NULL ==L->next)
{
delete L;
}
else
{
DestroyList(L->next);
delete L;
}
}
void ListEmpty(LinkList& L) //判断单链表是否为空
{
if (L->next == NULL)
{
cout << "单链表为空" << endl;
}
else
{
cout << "单链表不为空" << endl;
}
}
int main()
{
while (true)
{
showfunction(); //功能展示表
int numx;
cout << "选择要使用的功能" << endl;
cin >> numx;
int num1; //初始化时插入几个元素
int place; //输出单链表第多少个元素
int place1; //尾插法插入多少元素
int elem1; //查找的元素名
int elem2; //插入的元素名
int place3; //指定位置删除单个元素
if (numx > 10)
{
cout << "无该功能,请重新选(╯‵□′)╯" << endl;
}
else
{
switch (numx)
{
case 1:
{
cout << "输入要插入的数据数" << endl;
cin >> num1;
CreatList_head(L, num1);
system("pause");
system("cls");
break;
}
case 2:
{
cout << "输入要插入的数据数" << endl;
cin >> num1;
CreatList_tail(L, num1);
system("pause");
system("cls");
break;
}
case 3:
{
print(L);
system("pause");
system("cls");
break;
}
case 4:
{
ListLength(L);
system("pause");
system("cls");
break;
}
case 5:
{
cout << "要输入元素的位置为" << endl;
cin >> place;
GetElem( L, place);
system("pause");
system("cls");
break;
}
case 6:
{
cout << "输入要查找的元素" << endl;
cin >> elem1;
Getbyzhi(L, elem1);
system("pause");
system("cls");
break;
}
case 7:
{
cout << "输入位置为" << endl;
cin >> place1;
cout << "插入元素为" << endl;
cin >> elem2;
Insert(L, place1, elem2);
system("pause");
system("cls");
break;
}
case 8:
{
cout << "输入要删除元素的位置" << endl;
cin >> place3;
DeleteElem(L, place3);
system("pause");
system("cls");
break;
}
case 9:
{
ListEmpty(L);
}
case 0:
{
DestroyList(L);
}
}
}
}
}
符华上仙早上好啊 ╮( ̄▽ ̄"")╭ 愿你今天快乐 ----一只符华单推人
非常瞌睡 早安上仙 今天不皮了 ~~