list.h
#pragma once
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode, *LinkList;
Status InitList(LinkList &L);
Status CreatList(LinkList &L);
Status ListInsert(LinkList &L, int i, ElemType e);
LNode* GetElem(LinkList L, int i);
Status InsertNextNode(LNode *p, ElemType e);
void ListTraverse(const LinkList &L);
Status LocateElem(const LinkList &L, int &i, ElemType e);
Status ListDelete(LinkList &L, int i, ElemType &e);
Status DeleteNextNode(LNode *p, ElemType &e);
int Getlength(const LinkList &L);
void Clearlist(LinkList &L);
oper_func.cpp
#include <iostream>
#include"list.h"
Status InitList(LinkList &L)
{
L = new LNode;
L->next = NULL;
if (L == NULL)
return ERROR;
else
return OK;
}
Status CreatList(LinkList &L)
{
using std::cin;
using std::cout;
using std::endl;
if (InitList(L))
{
LNode *p, *r = L;
int num;
cout << "输入你需要创建的单链表的个数:";
cin >> num;
for (int i = 0; i < num; i++)
{
p = new LNode;
cout << "输入第" << i + 1 << "个元素:";
cin >> p->data;
p->next = NULL;
r->next = p;
r = p;
}
return OK;
}
else
{
cout << "内存不够,单链表创建失败!" << endl;
return ERROR;
}
}
Status ListInsert(LinkList &L, int i, ElemType e)
{
if (i < 1)
return FALSE;
LNode *p = GetElem(L,i-1);
return InsertNextNode(p,e);
}
LNode* GetElem(LinkList L, int i)
{
LNode *p = L;
int j = 0;
while (p != NULL && j < i)
{
p = p->next;
j++;
}
return p;
}
Status InsertNextNode(LNode *p, ElemType e)
{
if (p == NULL)
return FALSE;
LNode *s = new LNode;
if(s == NULL)
return FALSE;
s->next = p->next;
s->data = e;
p->next = s;
return OK;
}
void ListTraverse(const LinkList &L)
{
using std::cout;
using std::endl;
int i = 1;
LNode *p = L->next;
if (p == NULL)
{
cout << "单链表为空,无法输出元素!" << endl;
return;
}
while (p)
{
cout << "第" << i++ << "个元素为:" << p->data << endl;
p = p->next;
}
}
Status LocateElem(const LinkList &L, int &i, ElemType e)
{
LNode *s = L->next;
int j = 1;
while (s && (s->data != e))
{
s = s->next;
j++;
}
if (s == NULL)
return FALSE;
else
{
i = j;
return OK;
}
}
Status ListDelete(LinkList &L, int i, ElemType &e)
{
if (i < 1)
return FALSE;
LNode *p = GetElem(L, i - 1);
return DeleteNextNode(p, e);
}
Status DeleteNextNode(LNode *p, ElemType &e)
{
if(p == NULL)
return FALSE;
if (p->next == NULL)
return FALSE;
LNode *s = p->next;
e = s->data;
p->next = s->next;
delete s;
return OK;
}
int Getlength(const LinkList &L)
{
int length = 0;
LNode*p = L;
while (p)
{
p = p->next;
length++;
}
return length-1;
}
void Clearlist(LinkList &L)
{
using std::cin;
using std::cout;
using std::endl;
int l = Getlength(L);
ElemType elem;
while (l--)
{
ListDelete(L,l+1, elem);
cout << "单链表中的" << l+1 << "位置元素:" << elem << "被删除" << endl;
}
}
main.cpp
#include <iostream>
#include"list.h"
void showMenu()
{
using std::cout;
using std::cin;
using std::endl;
cout << "*********************************************************" << endl;
cout << "*** 1.指定位置插入元素 2.查找指定位置元素 ***" << endl;
cout << "*** 3.查找指定元素,返回位置 4.删除指定位置元素 ***" << endl;
cout << "*** 5.遍历单链表 6.清空单链表 ***" << endl;
cout << "*** 7.单链表的长度 0.退出单链表 ***" << endl;
cout << "*********************************************************" << endl;
}
int main()
{
using std::cout;
using std::cin;
using std::endl;
int select = 0, flag = -1;
LinkList L;
if (CreatList(L))
{
cout << "单链表创建成功!" << endl;
}
else
cout << "单链表创建失败!" << endl;
while (true)
{
showMenu();
cout << "输入你的选择:";
cin >> select;
switch (select)
{
case 1: {
int position = 0, elem = 0;
cout << "输入插入的位置和元素:";
cin >> position >> elem;
if (ListInsert(L, position,elem))
cout << "指定位置插入元素成功!" << endl;
else
cout << "内存分配失败或者插入位置越界,插入失败!" << endl;
}
break;
case 2: {
int position = 0;
LNode *p;
cout << "输入指定位置:";
cin >> position;
if((p = GetElem(L, position)) == NULL)
cout << "查找位置越界,查找失败!" << endl;
else
cout << "查找指定位置元素成功!,元素为:" << p->data << endl;
}
break;
case 3: {
int position = 0, elem = 0;
cout << "输入指定元素:";
cin >> elem;
if (LocateElem(L, position, elem))
{
cout << "查找指定元素成功!位置为:" << position << endl;
}
else
{
cout << "单链表不存在此元素!" << endl;
}
}
break;
case 4: {
int position = 0, elem = 0;
cout << "输入指定位置:";
cin >> position;
if (ListDelete(L, position, elem))
{
cout << "删除指定位置元素成功!元素为:" << elem << endl;
}
else
{
cout << "单链表为空或者删除位置不合理!" << endl;
}
}
break;
case 5: {
ListTraverse(L);
}
break;
case 6: {
Clearlist(L);
flag++;
}
break;
case 7: {
cout << "单链表中元素个数为:" << Getlength(L) << "个" << endl;
}
break;
case 0: {
cout << "退出单链表!" << endl;
if (flag == -1)
{
Clearlist(L);
}
system("pause");
return 0;
}
break;
}
}
return 0;
}
|