C++完整代码 实现线性表和单链表的插入、删除、查找等功能

线性表实现插入、删除、查找

线性表的顺序存储:线性表的顺序存储是指用一组地址连续的存储单元依次存储线性表中的各个元素,使得线性表在逻辑结构上相邻的元素存储在连续的物理存储单元中,即:通过数据元素物理存储的连续性来反应元素之间逻辑上的相邻关系。采用顺序存储结构存储的线性表通常简称为顺序表。
示意图
在这里插入图片描述

顺序表的基础操作代码

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

在这里插入图片描述

  • 13
    点赞
  • 112
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
#include <iostream> using namespace std; const int MaxSize=100; template <class T> //模板类 class SeqList { public: SeqList() {length=0;} //无参构造函数 SeqList(T a[],int n); //有参构造函数 ~SeqList(){} //析构函数 int Length() {return length;} //求线性表长度 T Get(int i); //按位查找 int Locate(T x); //按值查找 void Insert (int i, T x); // 插入函数 T Delete(int i); //删除函数 void PrintList(); //遍历线性表,按序号依次输出各个元素。 private: T data[MaxSize]; int length; }; template<class T> SeqList<T>::SeqList(T a[],int n) //有参构造函数 { if(n>MaxSize)throw"参数非法"; for(int i=0;i<n;i++) data[i]=a[i]; length=n; } template <class T> void SeqList<T>::Insert(int i,T x) //插入函数 { if (length>=MaxSize) throw "上溢"; if(i<1||i>length+1) throw "位置异常"; for (int j=length;j>=i;j--) data[j]=data[j-1]; data[i-1]=x; length++; } template <class T> T SeqList<T>::Get(int i) //按位查找函数 { if(i<1||i>length) throw "查找位置非法"; else return data[i-1]; } template <class T> int SeqList<T>::Locate(T x) //按值查找函数 { for (int i=0;i<length;i++) if (data[i]==x)return i+1; return 0; } template <class T> T SeqList<T>::Delete(int i) //删除函数 { if (int length=0)throw "下溢"; if(i<1||i>length)throw "位置异常"; T x=data[i-1]; for(int j=i;j<length;j++) data[j-1]=data[j]; length--; return x; } template<class T> void SeqList<T>::PrintList() // 遍历线性表 { for (int i=0;i<length;i++) cout<<data[i]<<" "; cout<<endl; } void menu() { cout<<" 欢迎,欢迎!"<<endl; cout<<" 1.插入查询"<<endl; cout<<" 2.删除函数"<<endl; cout<<" 3.求表长"<<endl; cout<<" 4.按值查找"<<endl; cout<<" 5.按位查找"<<endl; cout<<" 6.遍历线性表"<<endl; cout<<" 7.退出"<<endl; cout<<" "<<endl; cout<<" 请选择编号:"<<endl; } int main() { int i,j,x; int a[7]={12,15,24,56,67,68,86}; SeqList<int> s1(a,7); int flag=1; while(flag) { menu(); cin>>j; switch(j) { case 1: { try { cout<<"显示要插入的位序及数值:"<<endl; cin>>i>>x; cout<<endl; s1.Insert(i,x); s1.PrintList(); } catch (char *s){cout<<s<<endl;} }; break; case 2: { try { cout<<"输入元素所在位置"; cin>>i; x=s1.Delete(i); cout<<"已删除:"<<x<<endl; cout<<"删除数据后表变为:"<<endl; s1.PrintList(); } catch (char *s){cout<<s<<endl;} }; break; case 3: { try { s1.Length(); cout<<"线性表长度为:"<<s1.Length()<<endl; } catch(char *s){cout<<s<<endl;} }; break; case 4: { try { cout<<"输入查找数据x:"<<endl; cin>>x; s1.Locate(x); cout<<"所查数据在第"<<s1.Locate(x)<<"位"<<endl; } catch(char *s){cout<<s<<endl;} }; break; case 5: { try { cout<<"查找位置i="; cin>>i; s1.Get(i); cout<<"该位置元素为:"<<s1.Get(i)<<endl; } catch(char *s){cout<<s<<endl;} }; break; case 6: { try { s1.PrintList(); } catch(char *s){cout<<s<<endl;} }; break; case 7: flag=0; break; default:cout<<"错误!!!"<<endl; break; } } return 0; }
好的,我可以为您提供一个基于C++线性表实现示例,其中包括插入删除查找功能。 ```c++ #include <iostream> using namespace std; const int MAX_SIZE = 100; // 线性表的最大长度 template <typename T> class LinearList { private: T data[MAX_SIZE]; // 存储数据元素的数组 int length; // 线性表的长度 public: LinearList() { length = 0; } // 构造函数,初始化长度为0 // 插入操作,将元素插入到指定位置 bool insert(int index, T value) { if (index < 0 || index > length || length >= MAX_SIZE) { return false; // 插入位置不合法或线性表已满,返回false } for (int i = length - 1; i >= index; i--) { data[i + 1] = data[i]; // 将指定位置及其后面的元素后移一位 } data[index] = value; // 将元素插入到指定位置 length++; // 线性表长度加1 return true; // 插入成功,返回true } // 删除操作,删除指定位置的元素 bool remove(int index) { if (index < 0 || index >= length) { return false; // 删除位置不合法,返回false } for (int i = index; i < length - 1; i++) { data[i] = data[i + 1]; // 将指定位置后面的元素前移一位 } length--; // 线性表长度减1 return true; // 删除成功,返回true } // 查找操作,查找指定元素在线性表中的位置 int search(T value) { for (int i = 0; i < length; i++) { if (data[i] == value) { return i; // 找到指定元素,返回其位置 } } return -1; // 未找到指定元素,返回-1 } // 输出线性表中的所有元素 void print() { for (int i = 0; i < length; i++) { cout << data[i] << " "; } cout << endl; } }; int main() { LinearList<int> list; list.insert(0, 1); list.insert(1, 3); list.insert(1, 2); list.print(); // 输出1 2 3 list.remove(2); list.print(); // 输出1 2 cout << list.search(2) << endl; // 输出1 cout << list.search(3) << endl; // 输出-1 return 0; } ``` 在上面的代码中,我们使用模板类`LinearList`来定义线性表,其中`T`表示数据元素的类型。线性表的最大长度为`MAX_SIZE`,存储数据元素的数组为`data`,线性表的长度为`length`。 具体实现中,我们定义了插入删除查找和输出线性表中所有元素的函数。插入函数`insert`将元素插入到指定位置,删除函数`remove`删除指定位置的元素,查找函数`search`查找指定元素在线性表中的位置,输出函数`print`输出线性表中的所有元素。 在`main`函数中,我们通过调用上述函数来对线性表进行操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值