线性表实现插入、删除、查找
线性表的顺序存储:线性表的顺序存储是指用一组地址连续的存储单元依次存储线性表中的各个元素,使得线性表在逻辑结构上相邻的元素存储在连续的物理存储单元中,即:通过数据元素物理存储的连续性来反应元素之间逻辑上的相邻关系。采用顺序存储结构存储的线性表通常简称为顺序表。
示意图
顺序表的基础操作代码
#include "iostream"
using namespace std;
#define MAXSIZE 1000 //设置顺序表最大长度
typedef struct
{
int *data; //存储线性表中的元素
int length; //顺序表中元素的个数
}SqList;
void InitList(SqList &L) //顺序表初始化
{
L.data = new int[MAXSIZE]; //开辟空间
if (L.data == NULL){
cout << "存储空间分配失败" << endl;
exit(0);
}
L.length = 0;
cout << "顺序表初始化完成" << endl;
}
void Create(SqList &L,int n) //创建顺序表
{
int e;
for (int i = 0;i < n;i++){
cin >> e;
L.data[i] = e;
L.length++;
}
}
void Insert(SqList &L,int i,int e) //插入元素
{
if ((i < 1) || (i > L.length+1)){ //判断插入地址是否合法
cout<<"插入地址不合法"<<endl;
exit(0); //结束程序
}
if (L.length == MAXSIZE){ //判断存储空间是否满
cout << "存储空间已满" << endl;
exit(0); //结束程序
}
for (int j = L.length - 1; j >= i-1; j--){ //腾出插入空间
L.data[j+1] = L.data[j];
}
L.data[i-1] = e; //进行插入操作
++L.length;
}
void Delete(SqList &L,int i) //删除操作
{
if ((i < 1) || (i > L.length)){
cout<<"删除地址不合法"<<endl; //判断删除地址是否合法
exit(0);
}
for (int j = i; j < L.length; j++){ //需要删除的位置后面的元素往前覆盖,以实现删除操作
L.data[j-1] = L.data[j];
}
--L.length;
}
int Get(SqList L,int i) //按位查找,返回元素
{
if ((i < 1) || (i > L.length)){
cout<<"查找地址不合法"<<endl; //判断查找地址是否合法
exit(0);
}
else return L.data[i - 1];
}
int Locate(SqList L,int data) //按值查找,返回元素序号
{
for (int i = 0; i < L.length; i++)
if (L.data[i] == data) return i + 1;
cout << "查找失败 " <<endl;
return 0;
}
void Print(SqList L) //遍历顺序表
{
if (L.length == 0){
cout << "线性表长度为0";
exit(0);
}
for (int k = 0;k < L.length;k++){
if (k == L.length-1){
cout << L.data[k];
}else {
cout << L.data[k] << " ";
}
}
cout << endl;
}
void menu() //操作菜单
{
cout << "1.插入" << endl;
cout << "2.删除" << endl;
cout << "3.查找" << endl;
cout << "4.遍历" << endl;
cout << "5.退出" << endl;
}
void Operation(SqList &L,int num)
{
int element,i;
switch (num){
case 1:
cout << "请输入要插入的元素及其插的位置:" << endl;
cin >> element >> i;
Insert(L , i , element);
cout << "插入之后的数组:" << endl;
Print(L);
break;
case 2:
cout << "请输入要删除的元素的位置:" << endl;
cin >> i;
Delete(L , i);
cout << "删除之后的数组:" << endl;
Print(L);
break;
case 3:
cout << "1.按位查找 2.按值查找:";
cin >> i;
if (i == 1){
cout << "请输入查找的位置: ";
cin >> i;
cout << Get(L , i) << endl;
break;
}else {
cout << "请输入查找的值: ";
cin >> i;
cout << Locate(L , i) << endl;
break;
}
case 4:
Print(L);
break;
default:
exit(0);
break;
}
}
int main()
{
int n,i,e,num;
SqList L;
cout << "请输入线性表的个数" << endl;
cin >> n;
InitList(L);
cout << "请输入线性表的各个元素:" << endl;
Create(L , n);
cout << "原线性表如下:" << endl;
Print(L);
while (1){
menu();
cout << "输入需要操作的功能:" ;
cin >> num;
Operation(L , num);
}
return 0;
}
单链表实现插入、删除、查找
示意图
链表的基础操作代码
#include<iostream>
using namespace std;
typedef int ElemType;
typedef struct node //链式存储结构
{
ElemType data;//结点的数据域
struct node *next;//结点的指针域
}SLink,*LinkList;
LinkList CreateStudent() //采用尾插法创建单链表,最后返回头指针
{
ElemType score;
LinkList h,s,p;
h = new SLink;
h->next = NULL;
p = h;
cin >> score;
while (score != -1){
s = new SLink;
s->data = score;
p->next = s;
p = s;
cin >> score;
}
p->next = NULL;
return h;
}
int ListLength(LinkList h) //求单链表长度
{
LinkList p;
p = h;
ElemType j = 0;
while (p->next != NULL){
p = p->next;
j++;
}
return j;
}
int Get(LinkList h,int i) //查找单链表第i个位置的结点,并返回其结点的data
{
int m;
m = ListLength(h);
LinkList p;
p = h;
ElemType count = 0;
if (i < 1 || i > m){
cout << "您查找的元素不存在" << endl;
return 0;
}
while (count < i && p != NULL){
p = p->next;
count++;
}
return p->data;
}
int Locate(LinkList h,int data)
{
int m;
m = ListLength(h);
LinkList p;
p = h;
ElemType count = 0;
while (p != NULL){
if (p->data == data) return count;
p = p->next;
count++;
}
return 0;
}
int DelList(LinkList h,int i) //删除单链表第i个结点,即删除第i个元素
{
LinkList pre,r;
pre = h;
int j = 0;
int m = ListLength(h);
if(i < 1 || i > m){
cout << "删除位置不合法!" << endl;
return 0;
}
while(j < i-1){
pre = pre->next;
j = j+1;
}
r = pre->next;
pre->next = r->next;
return r->data;
free(r);
}
void Insert(LinkList h, int i, int data) //插入结点
{
LinkList p = h , s = NULL;
int count = 0;
while (p != NULL && count < i-1){
p = p->next;
count++;
}
if (p == NULL){
cout << "插入位置错误" << endl;
}else {
s = new node;
s->data = data;
s->next = p->next;
p->next = s;
}
}
void Print(LinkList h)
{
LinkList p;
p = h->next; //头指针指向第一个结点
while (p != NULL){
cout << p->data <<" ";
p = p->next;
}
cout << endl;
}
void menu() //操作菜单
{
cout << "1.插入" << endl;
cout << "2.删除" << endl;
cout << "3.查找" << endl;
cout << "4.遍历" << endl;
cout << "5.退出" << endl;
}
void operation(LinkList h,int num)
{
int element,i;
switch (num){
case 1:
cout << "请输入要插入的元素及其插的位置:" << endl;
cin >> element >> i;
Insert(h , i , element);
cout << "插入之后元素有:" << endl;
Print(h);
break;
case 2:
cout << "请输入要删除的元素的位置:" << endl;
cin >> i;
cout << "您删除的第" << i << "元素是:" << DelList(h , i) << endl;
cout << "删除之后剩余的元素为:" << endl;
Print(h);
break;
case 3:
cout << "1.按位查找 2.按值查找:";
cin >> i;
if (i == 1){
cout << "请输入查找的位置: ";
cin >> i;
cout << "第" << i << "位置的值为:" << Get(h , i) << endl;
break;
}else {
cout << "请输入查找的值: ";
cin >> i;
cout << i << "在第" << Locate(h , i) << "位置" << endl;
break;
}
case 4:
Print(h);
break;
default:
exit(0);
break;
}
cout << endl;
}
int main()
{
LinkList head;
int i,num;
cout << "请输入元素,以-1结束" << endl;
head = CreateStudent();
cout << "您输入的元素个数为:" << ListLength(head) << endl;
while (1){
menu();
cout << "输入需要操作的功能:" ;
cin >> num;
operation(head , num);
}
}