码迷,mamicode.com
首页 > 编程语言 > 详细

用C++结构体实现顺序表的诸多功能

时间:2019-03-21 01:16:58      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:元素   构造   out   std   sea   数据   fine   i++   查找   

编译运行环境为visual studio 2017 for community

#include <iostream>

#include<stdlib.h>

#define maxsize 100

#define ok 0

typedef int elemtype;

typedef int status;

using namespace std;

struct slist

{

    elemtype *elem = NULL;

    int length;

};

void create(slist&q)//顺序表的初始化,构造一个空的顺序表L

{

    q.elem = new int[maxsize];//为顺序表分配一个大小为maxsize的数组空间

    q.length = 0;

    if (q.elem != NULL) //存储分配成功

         cout << "success" << endl;

    else//存储分配失败

         cout << "faild" << endl;

}

status deleteall(slist &q)

{

    delete[]q.elem;//释放分配空间

    return ok;

}

void input(slist &q)//输入数据

{

    int a, b;

    cout << "需要输入的数据个数" << endl;

    cin >> a;

    cout << "输入数据"<<endl;

    for (int i = 0; i < a; i++)

    {

         cin >> b;

         q.elem[i] = b;

         q.length++;

    }

    cout << "数据已输入" << endl;

 

}

status searchnumber(slist q, int v)//查找数据

{

    cout << "所查元素的值为" << q.elem[v - 1] << endl;

    return ok;

}

status searchdata(slist q, int v)//查找序号

{

    int i = 0;

    for (; i < q.length; i++)

    {

         if (q.elem[i] == v)

         {

             cout << "查找数据序号:" << i + 1 << endl;

             break;

         }

    }

    return ok;

}

status insert(slist &q, int n, int data)//插入数据

{

    if (n < q.length)

    {

         for (int i = q.length; i > n-1; i--)

         {

             q.elem[i] = q.elem[i-1];

         }

         q.elem[n-1] = data;

        

    }

    else

    {

         q.elem[q.length] = data;

        

    }

    q.length++;

    cout << "插入数据成功" << endl;

    return ok;

}

status deleteone(slist& q, int numb2)

{

    if (numb2 > q.length)

    {

         cout << "删除值不合法" << endl;

    }

    else

    {

         int t, i;

         for (i = numb2-1; i < q.length-1; i++)

         {

             q.elem[i] = q.elem[i + 1];

         }

         q.length--;

         cout << "删除成功" << endl;

    }

    return ok;

 

}

void coutfunction(slist q)

{

    cout << "顺序表数据如下" << endl;

    for (int i = 0; i < q.length; i++)

    {

         cout << q.elem[i] << endl;

    }

}

int main()

{

    slist q;

    cout << "1. 建立顺序表\n";

    cout << "2. 输入数据\n";

    cout << "3. 查找\n";

    cout << "4. 插入\n";

    cout << "5. 删除\n";

    cout << "6. 输出数据\n";

    cout << "0. 退出\n\n";

    int choose = 9, numb, data, need1, need2, numb1, numb2,numb0, data1;

    while (choose != 0)

    {

         cin >> choose;

         switch (choose)

         {

         case 0:

             break;

         case 1:

             create(q);

             break;

         case 2:

             input(q);

             break;

         case 3:

             cout << "查找序号按 1,查找数值按 2" << endl;

             cin >> numb;

             if (numb == 1)

             {

                  cin >> numb0;

                  need1 = searchnumber(q, numb0);

             }

                 

             else

             {

                  cin >> numb0;

                  need2 = searchdata(q, numb0);

             }

             break;

         case 4:

             cout << "输入插入的序号和数值" << endl;

             cin >> numb1 >> data1;

             insert(q, numb1, data1);

             break;

         case 5:

             cout << "输入删除的序号" << endl;

             cin >> numb2;

             deleteone(q, numb2);

             break;

         case 6:

             cout << "当前顺序表为:\n";

             coutfunction(q);

             break;

         }

    }

    return 0;

    system("pause");

}

用C++结构体实现顺序表的诸多功能

标签:元素   构造   out   std   sea   数据   fine   i++   查找   

原文地址:https://www.cnblogs.com/wwywy/p/10568931.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!