1.顺序表的基本操作

来源:互联网 发布:高级编程语言 编辑:程序博客网 时间:2024/04/29 21:02

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

/************************Date:2017-2-23Author:SedateDescription:线性表的基本操作************************/#include<iostream>using namespace std;#define INT_MIN 0x80000000#define MAXSIZE 50  #define OK 1#define ERROR 0#define TRUE 1#define FALSE 0typedef 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 sqlistpos:    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;}
0 0