顺序链表(C++)

顺序表结构

struct Sq_list 
{
    int elem[Max_size+1];
    int length;
};

 创建并初始化顺序表

int Init_list(Sq_list *L)
{
    L->length = 0;
    return 0;
}

 按序插入元素

int insert (Sq_list *L)
{
    int n;
    cout << "请输入顺序表的长度:" << endl;
    cin >> n;
    cout << "请输入元素:" << endl;
    for (int i = 1; i <= n; i++)
    {
        int data;
        cin >> data;
        L->elem[i] = data;
        L->length++;
    }
    return 0;
}

在指定位置插入元素

int Insert_elem(Sq_list *L)
{
    int cor,elem;
    cout << "请输入指定位置的元素:" << endl;
    cin >> cor;
    cin >> elem;
    if (cor<1 || cor>L->length+1)
    {
        cout << "输入的位置不在顺序链表内!" << endl;
    }
    else
    {
        for (int i = L->length; i >= cor; i--)
        {
            L->elem[i+1] = L->elem[i];
        }
        L->elem[cor] = elem;
        L->length++;
    }
    return 0;
}

删除元素

删除指定值元素

int Delete(Sq_list *L)
{
    int data;
    cout << "请输入要删除的元素值:" << endl;
    cin >> data;
    int length = L->length;//存放表的原长
    for (int i = 1; i <= L->length; i++)
    {
        if (L->elem[i] == data)
        {
                for (int j = i; j < L->length; j++)
                {
                    L->elem[j] = L->elem[j + 1];
                }
                i--;
                L->length--;
        }
    }
    if (L->length == length)//判断表长是否发生改变
    {
        cout << "您想要删除的元素不在本链表中" << endl;
    }
    return 0;
}

删除指定位置的元素

int Delete2(Sq_list *L)
{
    int length;
    cout << "请输入要删除元素的位置:" << endl;
    cin >> length;
    if (length<1 || length>L->length)
    {
        cout << "您输入的位置不在本表中" << endl;
    }
    else
    {
        for (int i = length; i <= L->length; i++)
        {
            L->elem[i] = L->elem[i + 1];
        }
        L->length--;
    }
    return 0;
}

查找

按值查找

int seek_elem(Sq_list *L)
{
    int elem2;
    bool check = false;//用于判断表中是否有该元素    
    cout << "请输入您要查找的值:" << endl;
    cin >> elem2;
    cout << "您所查元素的位置为" << ' ';
    for (int i = 1; i <= L->length; i++)
    {
        if (L->elem[i] == elem2)
        {
            cout << i << ' ';
            check = true;
        }
    }
    cout << '
';
    if (check == false)
    {
        cout << "表中没有您需要查找的元素" << endl;
    }
    return 0;
}

按位置查找

int seek_length(Sq_list *L)
{
    int length2;
    cout << "请输入您要查找的位置:" << endl;
    cin >> length2;
    if (length2 < 1 || length2 > L->length)
    {
        cout << "您输入的位置不在本表中" << endl;
    }
    else
    {
        cout << "您所查找位置的元素为 " << L->elem[length2] << endl;
    }
    return 0;
}

遍历一遍顺序表

int show(Sq_list *L)
{
    cout << "遍历一遍当前数据表" << endl;
    for (int i = 1; i < L->length; i++)
    {
        cout << L->elem[i] <<' ';
    }
    cout << L->elem[L->length] << ' ' << endl;
    return 0;
}

完整代码

//注:主函数没写,自行调用功能函数即可测试,"stdafx.h"为vs编译器必须,其他可忽视
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#define Max_size 1000
using namespace std;
//顺序表结构
struct Sq_list 
{
    int elem[Max_size+1];
    int length;
};
//创建并初始化顺序表
int Init_list(Sq_list *L)
{
    L->length = 0;
    return 0;
}
//按序插入元素
int insert (Sq_list *L)
{
    int n;
    cout << "请输入顺序表的长度:" << endl;
    cin >> n;
    cout << "请输入元素:" << endl;
    for (int i = 1; i <= n; i++)
    {
        int data;
        cin >> data;
        L->elem[i] = data;
        L->length++;
    }
    return 0;
}
//在指定位置插入元素
int Insert_elem(Sq_list *L)
{
    int cor,elem;
    cout << "请输入指定位置的元素:" << endl;
    cin >> cor;
    cin >> elem;
    if (cor<1 || cor>L->length+1)
    {
        cout << "输入的位置不在顺序链表内!" << endl;
    }
    else
    {
        for (int i = L->length; i >= cor; i--)
        {
            L->elem[i+1] = L->elem[i];
        }
        L->elem[cor] = elem;
        L->length++;
    }
    return 0;
}
//删除指定值元素
int Delete(Sq_list *L)
{
    int data;
    cout << "请输入要删除的元素值:" << endl;
    cin >> data;
    int length = L->length;//存放表的原长
    for (int i = 1; i <= L->length; i++)
    {
        if (L->elem[i] == data)
        {
                for (int j = i; j < L->length; j++)
                {
                    L->elem[j] = L->elem[j + 1];
                }
                i--;
                L->length--;
        }
    }
    if (L->length == length)//判断表长是否发生改变
    {
        cout << "您想要删除的元素不在本链表中" << endl;
    }
    return 0;
}
//删除指定位置的元素
int Delete2(Sq_list *L)
{
    int length;
    cout << "请输入要删除元素的位置:" << endl;
    cin >> length;
    if (length<1 || length>L->length)
    {
        cout << "您输入的位置不在本表中" << endl;
    }
    else
    {
        for (int i = length; i <= L->length; i++)
        {
            L->elem[i] = L->elem[i + 1];
        }
        L->length--;
    }
    return 0;
}
//按值查找
int seek_elem(Sq_list *L)
{
    int elem2;
    bool check = false;//用于判断表中是否有该元素    
    cout << "请输入您要查找的值:" << endl;
    cin >> elem2;
    cout << "您所查元素的位置为" << ' ';
    for (int i = 1; i <= L->length; i++)
    {
        if (L->elem[i] == elem2)
        {
            cout << i << ' ';
            check = true;
        }
    }
    cout << '
';
    if (check == false)
    {
        cout << "表中没有您需要查找的元素" << endl;
    }
    return 0;
}
//按位置查找
int seek_length(Sq_list *L)
{
    int length2;
    cout << "请输入您要查找的位置:" << endl;
    cin >> length2;
    if (length2 < 1 || length2 > L->length)
    {
        cout << "您输入的位置不在本表中" << endl;
    }
    else
    {
        cout << "您所查找位置的元素为 " << L->elem[length2] << endl;
    }
    return 0;
}
//遍历顺序表
int show(Sq_list *L)
{
    cout << "遍历一遍当前数据表" << endl;
    for (int i = 1; i < L->length; i++)
    {
        cout << L->elem[i] <<' ';
    }
    cout << L->elem[L->length] << ' ' << endl;
    return 0;
}

觉得文章不错,可以点个赞和关注哟.

原文地址:https://www.cnblogs.com/Lazy-Cat/p/9827350.html