文件组织
在实现链表的时候,我是利用C++声明了一个头文件,里面存放链表的基本操作(添加、删除、修改等),然后创建一个源文件用来实现这些操作,最后再创建一个源文件用于测试
声明头文件:
typedef char DataType;
typedef struct node
{
DataType data; //数据
struct node *next; //node类型的指针
}ListNode; //结点
typedef ListNode *LinkList;
LinkList CreatListF(LinkList head); //头插法
LinkList CreatListRH(LinkList head);//尾插法
ListNode *GetNode(LinkList head, int i);//按照序号查找
ListNode *LocateNode(LinkList head, DataType key);//按值查找
int InserList(LinkList head, DataType x, int i);//指定位置插入
int DeleteList(LinkList head, int i);//按照位置删除
int DeleteListType(LinkList head, DataType x);//按照值删除
int UpDataList(LinkList head, DataType x, int i);//按照位置修改
int UpDataListType(LinkList head, DataType x, DataType i);//按照值修改
void DestroyList(LinkList head);//销毁
void outNode(LinkList head);//输出链栈
测试文件
#include "pch.h"
#include <iostream>
#include"Node.h"
using namespace std;
int main()
{
int i;
cout << "\n\n\n\n\n";
cout << " * 1.创建链表************* 2.插入元素 *\n";
cout << " **************************************\n";
cout << " * 3.删除元素************* 4.修改元素 *\n";
cout << " **************************************\n";
cout << " * 5.查询元素************* 6.退出\\销毁*\n\n\n\n";
int choose1();
int choose2();
int choose3();
LinkList head = NULL;
while (true)
{
cout << "请选择操作序列号:\n";
cin >> i;
switch (i)
{
case 1:
{
int x = choose2();
if (x == 1)
{
head = CreatListF(head);
}
if (x == 2)
{
head = CreatListRH(head);
}
else
{
}
outNode(head);
break;
}
case 2:
{
char x;
int y;
cout << "请输入要插入的元素:\n";
cin >> x;
cout << "请输入要插入的位置:\n";
cin >> y;
if (InserList(head, x, y) == 1)
{
cout << "插入成功\n";
}
else
{
cout << " 插入失败\n";
}
outNode(head);
break;
}
case 3:
{
char x;
int a = choose1();
if (a == 1)
{
cout << "请输入要删除的元素:\n";
cin >> x;
if (DeleteListType(head, x) == 1)
{
cout << "删除成功\n";
}
else
{
cout << " 删除失败\n";
}
}
if (a == 2)
{
int y;
cout << "请输入要删除元素的位置:\n";
cin >> y;
if (DeleteList(head, y) == 1)
{
cout << "删除成功\n";
}
else
{
cout << "删除失败\n";
}
}
else
{
}
outNode(head);
break;
}
case 4:
{
outNode(head);
int z;
char x,y;
int b = choose1();
if (b == 1)
{
cout << "请输入要修改的元素:\n";
cin >> x;
cout << "请输入修改后的元素\n";
cin >> y;
if (UpDataListType(head, x, y) == 1)
{
cout << "修改成功\n";
}
else
{
cout << "修改失败\n";
}
}
if (b == 2)
{
cout << "请输入要修改元素的位置:\n";
cin >> z;
cout << "请输入修改后的元素\n";
cin >> y;
if (UpDataList(head, y, z) == 1)
{
cout << "修改成功\n";
}
else
{
cout << "修改失败\n";
}
}
else
{
}
break;
}
case 5:
{
char x;
ListNode *z;
int c = choose1();
if (c == 1)
{
cout << "请输入要查询的元素:\n";
cin >> x;
z = LocateNode(head, x);
if (z != NULL)
{
cout << "元素查询成功,值" << z->data << "与所查元素匹配\n";
}
else
{
cout << "查询失败\n";
}
}
if (c == 2)
{
int y;
cout << "请输入要查询元素的位置:\n";
cin >> y;
z = GetNode(head, y);
if (z != NULL)
{
cout << "元素查询成功,值" << z->data << "与所查位置匹配\n";
}
else
{
cout << "查询失败\n";
}
}
else
{
}
outNode(head);
break;
}
case 6:
{
int x = choose3();
if (x == 1)
{
DestroyList(head);
cout << "销毁完成\n";
}
if (x == 2)
{
exit(0);
}
else
{
}
outNode(head);
break;
}
default:
break;
}
if (i < 1 || i>6)
{
break;
}
}
}
int choose1()
{
cout << " 请选择操作:" << endl;
cout << " 1.按照值操作" << endl;
cout << " 2.按照位置操作:" << endl;
int x;
cin >> x;
switch (x)
{
case 1:return 1;
case 2:return 2;
default:
cout << "选择有误\n";
break;
}
return 0;
}
int choose2()
{
cout << " 请选择操作:" << endl;
cout << " 1.头查法" << endl;
cout << " 2.尾插法" << endl;
int x;
cin >> x;
switch (x)
{
case 1:return 1;
case 2:return 2;
default:
cout << "选择有误\n";
break;
}
return 0;
}
int choose3()
{
cout << " 请选择操作:" << endl;
cout << " 1.销毁" << endl;
cout << " 2.退出" << endl;
int x;
cin >> x;
switch (x)
{
case 1:return 1;
case 2:return 2;
default:
cout << "选择有误\n";
break;
}
return 0;
}
还有各个函数的具体实现
#include "pch.h"
#include "Node.h"
#include<iostream>
using namespace std;
LinkList CreatListF(LinkList head)//头插法
{
DataType ch;
ListNode *s;
head = new ListNode; //申请内存,大小为一个ListNode的大小
if (head == NULL)
{
cout << "申请内存空间失败\n";
return head;
}
head->next = NULL;
cout << "请输入插入链表节点的数据(字符型并以'0'作为结束标志):\n";
while (true)
{
cin >> ch;
if (ch=='0')
{
break;
}
s = new ListNode;
if (s == NULL)
{
cout << "申请内存空间失败"; break;
}
s->data = ch; //存放数据
s->next = head->next; //使指针打到头节点的下一个
head->next = s; //改变头指针
}
return head;
}
LinkList CreatListRH(LinkList head)
{
DataType ch;
ListNode *s, *r;
head = new ListNode;
if (head == NULL)
{
cout << "申请内存空间失败\n";
return head;
}
r = head;
cout << "请输入插入链表节点的数据(字符型并以'0'作为结束标志):\n";
while (true)
{
cin >> ch;
if (ch == '0')
{
break;
}
s = new ListNode;
if (s == NULL)
{
cout << "申请内存空间失败\n";
break;
}
s->data = ch;
r->next = s;
r = s;
}
r->next = NULL;
return head;
}
ListNode * GetNode(LinkList head, int i)
{
ListNode *p = head;
int j = 0;
while (p&&j<i)
{
p = p->next;
j++;
}
if (j == i)
{
return p;
}
else
return NULL;
}
ListNode * LocateNode(LinkList head, DataType key)
{
ListNode *p = head->next;
while (p&&p->data!=key)
{
p = p->next;
}
return p;
}
int InserList(LinkList head, DataType x, int i)
{
ListNode *p =GetNode(head,(i-1));
ListNode *s;
if (p == NULL)
{
cout << "没找到第" << (i - 1) << "个节点\n";
return 0;
}
s = new ListNode;
s->data = x;
s->next = p->next;
p->next = s;
return 1;
}
int DeleteList(LinkList head, int i)
{
ListNode *p = GetNode(head,(i-1));
ListNode *r;
if (p == NULL||p->next==NULL)
{
cout << "未找到第" << (i - 1) << "个结点或者第" << i << "个结点不存在\n";
return 0;
}
r = p->next;
p->next = r->next;
delete r;
return 1;
}
int DeleteListType(LinkList head, DataType x)
{
ListNode *p = head->next;
int i = 1;
while (p&&p->data != x)
{
p = p->next;
i++;
}
if(DeleteList(head, i)==1)
return 1;
else
{
return 0;
}
}
int UpDataList(LinkList head, DataType x, int i)
{
ListNode *p = GetNode(head, i);
if (p == NULL || p->next == NULL)
{
cout <<"第" << i << "个结点不存在\n";
return 0;
}
p->data = x;
return 1;
}
int UpDataListType(LinkList head, DataType x, DataType i)
{
ListNode *p = LocateNode(head, x);
if (p != NULL)
{
p->data = i;
return 1;
}
else
{
return 0;
}
}
void DestroyList(LinkList head)
{
ListNode *p, *s;
p = head->next;
while (p)
{
s = p;
p = p->next;
delete s;
}
}
void outNode(LinkList head)
{
ListNode *p = head->next;
cout << "链表元素为: ";
while (p)
{
cout << p->data<<" ";
p = p->next;
}
cout << endl;
}
也可参考上传的资源,里面是可运行的C++程序。