1.顺序表的基本操作

知识点:list
顺序表的基本操作
作业要求:
1、实现顺序表的基本操作,包括建立、按值查找、按位置查找、按位置插入、按值删除、按位置删除
2、要求1的程序必须完成,除此,建议有时间情况下尽可能扩展功能,如取表长等,扩展功能自行决定;
3.程序的所有功能请写在一个.c/.cpp的文件中,可通过“菜单”调用各个功能;
4.程序中的出现的自定义变量、函数名等、函数的功能、重要的结构处,请必须填加注释,中英文均可。

/************************
Date:2017-2-23
Author:Sedate
Description:线性表的基本操作
************************/

#include<iostream>
using namespace std;

#define INT_MIN 0x80000000
#define MAXSIZE 50  
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

typedef int ElementType;//数据元素的类型为整型
typedef int Status;     //表示函数返回的状态值
typedef struct SqlList
{
    ElementType data[MAXSIZE];  //数组长度maxsize,即线性表最大长度为maxsize
    int size;           //记录线性表的当前长度
};

/*
* Summary: initialize a sqlist
* Parameters:
    list:   the sqlist
* Return: the status
*/
Status initeSqList(SqlList &list)
{
    for (int i = 0; i < MAXSIZE; i++)
    {
        list.data[i] = INT_MIN;
    }
    list.size = 0;
    return OK;
}

/*
* Summary: find by elem's value, get the elem's position
* Parameters: 
    list:   the sqlist
    e:      the elem
    pos:    the position where the elem is
* Return: the status
*/
Status find_by_elem(SqlList &list,ElementType e,int pos)
{
    if (list.size <= 0)
        return ERROR;
    for (int i = 0; i < list.size; i++)
    {
        if (e == list.data[i])
        {
            pos = i;
            return OK;  //find
        }
    }
    return ERROR;       //not find
}

/*
* Summary: find by elem's poistion, get the elem's value
* Parameters:
    list:   the sqlist
    e:      the elem
    pos:    the position where the elem is
* Return: the status
*/
Status find_by_position(SqlList list,ElementType e,int pos)
{
    if (list.size <= 0)
        return ERROR;
    if (pos >= list.size|| pos < 0)
    {
        return ERROR;
    }
    e = list.data[pos];
    return OK;
}

/*
* Summary: insert an elem into the list in a specific position
* Parameters:
    list:   the sqlist
    e:      the elem
    pos:    the position the elem to be inserted in
* Return: the status
*/
Status insert(SqlList list, ElementType e, int pos)
{
    if (pos < 0 || pos >= list.size || list.size < 0 || list.size >= MAXSIZE)
        return ERROR;
    for (int i = list.size; i >= pos; i--)
    {
        list.data[i] = list.data[i - 1];
    }
    list.data[pos] = e;
    ++list.size;
    return OK;
}

/*
* Summary: delete elem by poistion
* Parameters:
list:   the sqlist
pos:    the position where the elem is
* Return: the status
*/
Status delete_by_position(SqlList list, int pos)
{
    if (pos < 0 || pos >= list.size || list.size<1 || list.size>MAXSIZE)
        return ERROR;
    for (int i = pos; i < list.size-1; i++)
    {
        list.data[i] = list.data[i + 1];
    }
    list.data[list.size - 1] = INT_MIN;
    --list.size;
    return OK;
}

/*
* Summary: delete the first elem whose value equals e
* Parameters:
    list:   the sqlist
    e:      the elem to be deleted
* Return: the status
*/
Status delete_by_value(SqlList list, ElementType e)
{
    if (list.size<1 || list.size>MAXSIZE)
        return ERROR;
    int i, j;
    for (i = 0; i < list.size; i++)
        if (list.data[i] == e)  break;
    if (i == list.size)
        return ERROR;
    for (j = i; j < list.size - 1; j++)
    {
        list.data[j] = list.data[j + 1];
    }
    list.data[list.size - 1] = INT_MIN;
    --list.size;
    return OK;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值