• 顺序表


    顺序表
    概念
    线性表的顺序表示指的是用一组地址连续的储存单元依次存储线性表的数据元素,这种表示也称作线性表的顺序存储结构顺序映像。通常,称这种存储结构的线性表为顺序表(SequentialList)
    特点
    1.存储地址连续。
    2.随机存取
    3.存储密度大
    4.(初始化)浪费空间
    5.不可自由扩充
    6.删除插入操作移动繁琐
    实例 : 简单图书管理系统
    存储结构

    //数据元素
    typedef struct
    {
        char no[20];
        char name[20];
        float price;
    }Book;
    //顺序表
    typedef struct{
        Book *book;
        int length;
    }SqList;
    

    顺序表初始化

    int InitList(SqList &L)
    {
        L.book=new Book[MAXSIZE];
        if(!L.book){return OVERFLOW;}
        L.length=0;
        return OK;
    }
    

    插入

    int ListInsert(SqList &L,int i,Book b)
    {
        if((i<1)||(i>L.length+1)){return ERROR;}
        if(L.length==MAXSIZE){return ERROR;}
        for(int j=L.length;j>=i-1;j--)
        {
            L.book[j+1]=L.book[j];
        }
        L.book[i-1]=b;
        ++L.length;
        return OK;
    }
    

    删除

    int ListDelete(SqList &L,int i)
    {
        if((i<1)||(i>L.length)){return ERROR;}
        for(int j=i-1;j<L.length;j++)
        {
           L.book[j]=L.book[j+1];
        }
        --L.length;
        return OK;
    }
    

    原码

    #include <iostream>
    #include <string.h>
    #define MAXSIZE 100
    #define OK 1
    #define ERROR 0
    #define OVERFLOW -2
    using namespace std;
    //书元素
    typedef struct
    {
        char no[20];
        char name[20];
        float price;
    }Book;
    //书表
    typedef struct{
        Book *book;
        int length;
    }SqList;
    //顺序表初始化
    int InitList(SqList &L)
    {
        L.book=new Book[MAXSIZE];
        if(!L.book){return OVERFLOW;}
        L.length=0;
        return OK;
    }
    //取值(第i本书)
    int GetBook(SqList L,int i,Book &b)
    {
      if(i<1||i>L.length) {return ERROR;}
      b=L.book[i-1];
      return OK;
    }
    //查找
    int LocateBook(SqList L,Book b)
    {
        for(int i=0;i<L.length;i++)
        {
            if(strcmp(L.book[i].no,b.no)==0) {return i+1;}
        }
        return ERROR;
    }
    //插入
    int ListInsert(SqList &L,int i,Book b)
    {
        if((i<1)||(i>L.length+1)){return ERROR;}
        if(L.length==MAXSIZE){return ERROR;}
        for(int j=L.length;j>=i-1;j--)
        {
            L.book[j+1]=L.book[j];
        }
        L.book[i-1]=b;
        ++L.length;
        return OK;
    }
    //删除
    int ListDelete(SqList &L,int i)
    {
        if((i<1)||(i>L.length)){return ERROR;}
        for(int j=i-1;j<L.length;j++)
        {
           L.book[j]=L.book[j+1];
        }
        --L.length;
        return OK;
    }
    void in_book(Book &t)
    {
      cout<<"
    ISBN:";cin>>t.no;cout<<"书名:";cin>>t.name;cout<<"价格:";cin>>t.price;
    }
    void out_book(Book b)
    {
        cout<<"ISBN "<<b.no<<" |书名 "<<b.name<<" |价格 "
        <<b.price<<endl;
        return;
    }
    void display(SqList &L)
    {
        int i=0;
        while(i<L.length)
        {
            out_book(L.book[i]);
            i++;
        }
        return;
    }
    void add_book(SqList &L)
    {
        int i=L.length+1;
        int n=1;
        while(n)
        {
        Book t;
        cout<<"	[数据录入]"<<endl;
        in_book(t);
        ListInsert(L,i,t);
        ++i;
        cout<<"	是[1]否[0]继续?"<<endl;
        cin>>n;
        }
    }
    void del_book(SqList &L)
    {
        Book b;
        cout<<"	[删除数据]
    ISBN:"<<endl;
        cin>>b.no;
        ListDelete(L,LocateBook(L,b));
        return;
    }
    void search_book(SqList &L)
    {
        Book b,m;
        cout<<"	[查找数据]
    ISBN:"<<endl;
        cin>>b.no;
        GetBook(L,LocateBook(L,b),m);
        out_book(m);
    
        return;
    }
    void edi_book(SqList &L)
    {
        Book b,m,t;
        cout<<"	[修改数据]
    ISBN:"<<endl;
        cin>>b.no;
        GetBook(L,LocateBook(L,b),m);
        out_book(m);
        in_book(t);
        L.book[LocateBook(L,b)-1]=t;
        return;
    }
    int main()
    {
        SqList List;
        InitList(List);
        //增
        add_book(List);
        display(List);
        //删
        del_book(List);
        display(List);
        //查
        search_book(List);
        //改
        edi_book(List);
        display(List);
    }
    
    

    运行结果

  • 相关阅读:
    继承与钻石继承
    面向对象----对象的组合和
    认知类和对象的关系
    初识面向对象----类和对象的关系
    其他题目
    三级菜单
    用户登陆
    购物车题目
    函数练习题目
    类加载的过程
  • 原文地址:https://www.cnblogs.com/hugboy/p/SequentialList.html
Copyright © 2020-2023  润新知