才开始学数据结构,第一次当然是线性表。

线性表有顺序存储和链式存储。

前天发了一个顺序存储,今天想重新改一下。

下面是以一个Booklist为例;

包含了六个函数:初始化(InitList)、取值(GetElem)、查找(LocateElem)、插入(ListInsert)、删除(ListDelete)和显示(ListDisplay)。

首先,定义常量和结构体

#include <iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status; 
#define MAXSIZE 100

 

typedef struct Book
{
    char no[20];
    char name[50];
    float price;
    bool operator==(const Book& anotherbook)//运算符重载
    {
        return (!strcmp(no, anotherbook.no));
    }
}Book;

typedef struct
{
    Book *elem;    //elem包含‘书号,书名,价格’
    int length;
}SqList;

 

然后是函数

一、初始化

Status InitList(SqList &L)
{
    L.elem = new Book[MAXSIZE];//为顺序表分配MAXSIZE的数组空间
    if (!L.elem) exit(OVERFLOW);
    L.length = 0;
    return OK;
}

 

二、取值函数

Status GetElem(SqList L, int i, Book &e)//取值函数 
{
    if (i<1 || i>L.length) return ERROR;
    e = L.elem[i - 1];
    return OK;
}

 

三、查找函数

int LocateElem(SqList L, Book e)//查找函数
{
    for (int i = 0; i < L.length; i++)
        if (L.elem[i] == e) return i + 1;
    return 0;
}

 

四、插入函数

Status ListInsert(SqList &L, int i, Book e)//插入函数
{
//在第i个位置插入元素,从length到i的所有元素依次后移
//注意是从后面开始移,避免元素覆盖就是元素
if (i<1 || i>L.length + 1) return ERROR; if (L.length == MAXSIZE) return ERROR; for (int j = L.length - 1; j >= i - 1; j--)//i是位置序号,j是数组下标 L.elem[j + 1] = L.elem[j]; L.elem[i - 1] = e; ++L.length; return OK; }

 

五、删除函数

Status ListDelete(SqList &L, int i)//元素删除
{      //删除第i个元素,i后面的元素依次前移一个位置
    if (i<1 || i>L.length) return ERROR;
    for (int j = i; j <= L.length; j++)//i是位置序号,j是数组下标
        L.elem[j - 1] = L.elem[j];
    --L.length;
    return OK;
}

 

六、显示函数

void ListDisplay(SqList L)//显示顺序表
{
    cout << "书号\t书名\t价格\n";
    for (int i = 1; i <= L.length; i++)
    {
        cout << L.elem[i - 1].no << "\t" << L.elem[i - 1].name << "\t" << L.elem[i - 1].price << "\n";
        cout << "------------------------\n";
    }
}

最后是main

int main()
{
    SqList bookList;
    InitList(bookList);
    char ch;
    while (1)
    {
        cout << "\n\n---------------------------------------------------------------------------------------\n";
        cout << "+请选择要进行的操作:<a>显示    <b>插入元素    <c>删除指定位置元素    <d>查找    <x>退出+\n";
        cout << "---------------------------------------------------------------------------------------\n";
        ch = _getwch(); //接收回车用\r,键盘上回车键先产生\r再产生\n
        if (ch == 'x')
            break;
        else if (ch == 'a')
            ListDisplay(bookList);
        else if (ch == 'b')
        {
            int i;
            cout << "请输入插入的位置(输入0表示在顺序表的末尾之后插入):";
            cin >> i;
            if (i == 0)
                i = bookList.length + 1;
            Book b;
            cout << "请输入书号:"; cin >> b.no;
            cout << "请输入书名:"; cin >> b.name;
            cout << "请输入价格:"; cin >> b.price;
            if (ListInsert(bookList, i, b))
                cout << "元素插入第" << i << "个位置成功。\n";
            else
                cout << "元素插入第" << i << "个位置失败。\n";
        }
        else if (ch == 'c')
        {
            cout << "请输入要删除的元素的位置序号:";
            int i;
            cin >> i;
            if (ListDelete(bookList, i) == OK)
                cout << "删除成功\n";
            else
                cout << "删除失败\n";
        }
        else if (ch == 'd')
        {
            cout << "请输入要查找的书籍的书号:";
            Book t;
            cin >> t.no; 
            int i = LocateElem(bookList, t);
            if (i == 0)
                cout << "查找失败!\n";
            else
                cout << "查找成功,在第" << i << "个位置\n";

        }
    }
}

这个Booklist是Liu老师写的,感谢感谢!

理解的差不多了就放上来了。坚持学习加油!

最后今天提到了符号重载,有点似懂非懂。老师给了我一张图

然后给我的讲解是:istream是输入流。i 是缓冲区(就是控制台窗口输入时,是先到缓冲区再传入变量中)。

应该是这样吧,后面再慢慢学习。

顺便老师提醒了我每周总结。第一次用博客,觉得就在这里总结也挺好的,可以随时查看复习,也可以和大家分享!

加油,坚持下去!