常用算法手册学习笔记1

顺序表

 

源码:

 

主函数

#include"head.h"

int main()
{
    OrederTable();//顺序表算法
    system("Pause");
    return 0;
}

 

头文件head.h

#include<iostream>
#include<string>


using namespace std;


const int MAXLEN = 10;

typedef struct
{
    string name;
    string num;
    string sex;
    int phone;
    string place;
}DATA,*pDATA;

typedef struct
{
    DATA data[MAXLEN];
    int count;
}STR,*pSTR;

void face();//登录界面
void Interface();//界面
void OrederTable();//顺序表主程序
pSTR OrederTable_create();//创建表
bool OrederTable_init(pSTR,int);//初始化表
void OrederTable_out(pSTR);//输出表中信息
int OrederTable_len(pSTR);//表长
bool OrederTable_empty(pSTR);//判断表是否为空
bool OrederTable_add(pSTR,int);//添加信息
bool OrederTable_del(pSTR,int);//删除信息
bool OrederTable_Find(pSTR,string);//查找按名字查找信息

 

操作界面

 

#include<string>
#include<stdlib.h>
#include"head.h"

void Interface()
{
    pSTR phead = OrederTable_create();
    int len,flag = 1,pos=0,choose;
    string name;
    face();
    cout << "\t\t请输入你的选择序号:";
    cin >> choose;
    while (choose < 1 || choose > 7 )
    {
        system("CLS");
        face();
        cout << "\n你的选择有误,请重新选择:";
        cin >> choose;
    }
    while(choose!=7)
    {
        switch (choose)
        {
        case 1:
            system("CLS");
            int amount;
            cout << "请输入初始化的学生人数:";
            cin >> amount;
            OrederTable_init(phead,amount);
            break;
        case 2:
            system("CLS");
            len = OrederTable_len(phead);
            cout << "\n\n表中有" << len << "名学生\n\n";
            break;
        case 3:
            system("CLS");
            cout << "请输入插入位置:";
            cin >> pos;
            if (OrederTable_add(phead, pos))
            {
                cout << "\n插入成功!\n";
            }
            else
            {
                cout << "\n插入失败!\n";
            }
            break;
        case 4:
            cout << "请输入删除位置:";
            cin >> pos;
            if (OrederTable_del(phead, pos))
            {
                cout << "\n删除成功!\n";
            }
            else
            {
                cout << "\n删除失败!\n";
            }
            break;
        case 5:
            if (OrederTable_empty(phead))
                OrederTable_out(phead);
            break;
        case 6:
            cout << "请输入要学生名字:";
            cin >> name;
            if (OrederTable_Find(phead, name))
            {
                
            }
            else
            {
                cout << "无此学生信息!!\n";
            }
            break;
        case 7:
            cout << "欢迎下次光临!!!\n";
            flag = 0;
            break;
        }
        face();
        cout << "请输入选择序号:";
        cin >> choose;
        while (choose < 1 || choose > 7)
        {
            system("CLS");
            face();
            cout << "\n你的选择有误,请重新选择:";
            cin >> choose;
        }
    }
    if(flag)
        cout << "欢迎下次光临!!!\n";
}

 

登录界面

#include"head.h"

void face()
{
    cout << "\n\n\n\n\t\t*******************" << endl;
    cout << "\t\t**** 欢迎 光临 ****\n";
    cout << "\t\t*******************" << endl;
    cout << "\t\t1.初始化学生信息表\n";
    cout << "\t\t2.查看学生人数\n";
    cout << "\t\t3.添加学生信息\n";
    cout << "\t\t4.删除学生信息\n";
    cout << "\t\t5.显示学生信息\n";
    cout << "\t\t6.按学生姓名查找\n";
    cout << "\t\t7.退出\n";
}

 

顺序表操作算法

#include"head.h"

void OrederTable()
{
    Interface();//登录界面
}

pSTR OrederTable_create()//创建表
{
    pSTR phead = new STR;
    phead->count = 0;
    return phead;
}

bool OrederTable_init(pSTR phead,int amount)//初始化
{
    int i;
    if (amount > MAXLEN)
    {
        cout << "初始人数过多,内存不足!\n无法存储!\n";
        return false;
    }
    for (i = 0; i < amount; i++)
    {
        cout << "请输入第" << i + 1 << "个学生信息:\n";
        cout << "姓名:";
        cin >> phead->data[i].name;
        cout << "学号:";
        cin >> phead->data[i].num;
        cout << "性别:";
        cin >> phead->data[i].sex;
        cout << "电话:";
        cin >> phead->data[i].phone;
        cout << "地址:";
        cin >> phead->data[i].place;
    }
    phead->count = amount;
    system("CLS");
    return true;
}

void OrederTable_out(pSTR phead)//输出表中信息
{
    int i = 0;
    system("CLS");
    cout << "\n\n学生信息\n\n";
    while (i < phead->count)
    {
        cout << "姓名:" << phead->data[i].name << endl;
        cout << "学号:" << phead->data[i].num << endl;
        cout << "性别:" << phead->data[i].sex << endl;
        cout << "电话:" << phead->data[i].phone << endl;
        cout << "地址:"<<phead->data[i].place<<endl;
        i++;
    }
}

int OrederTable_len(pSTR phead)//表实长
{
    system("CLS");
    return phead->count;
}

bool OrederTable_empty(pSTR phead)//判断表中是否有信息
{
    system("CLS");
    if (phead->count == 0)
    {
        cout << "表中无信息!\n";
        return false;
    }
    else
    {
        return true;
    }
}

bool OrederTable_add(pSTR phead, int pos)//插入信息
{
    while (phead->count == MAXLEN)
    {
        cout << "表已满,无空间插入信息!\n";
        return false;
    }
    while (pos<1 || pos>phead->count+1)
    {
        cout << "插入位置有误!\n";
        return false;
    }
    int i = phead->count;
    while (i != pos-1)
    {
        phead->data[i] = phead->data[i - 1];
        i--;
    }
    DATA student;
    cout << "请输入插入学生信息:\n";
    cout << "姓名:";
    cin >> student.name;
    cout << "学号:";
    cin >> student.num;
    cout << "性别:";
    cin >> student.sex;
    cout << "电话:";
    cin >> student.phone;
    cout << "地址:";
    cin >> student.place;
    phead->data[i] = student;
    (phead->count)++;
    return true;
}

bool OrederTable_del(pSTR phead, int pos)//删除信息
{
    system("CLS");
    int i = pos;
    while (phead->count == 0)
    {
        cout << "表中无信息,无法执行删除指令!\n";
        return false;
    }
    while (pos <= 0 || pos > phead->count)
    {
        cout << "删除位置有误!\n";
        return false;
    }
    cout << "删除信息为:\n";
    cout << "姓名:" << phead->data[pos - 1].name << endl;
    cout << "学号:" << phead->data[pos - 1].num << endl;
    cout << "性别:" << phead->data[pos - 1].sex << endl;
    cout << "电话:" << phead->data[pos - 1].phone << endl;
    cout << "地址:" << phead->data[pos - 1].place << endl;
    while (i != phead->count)
    {
        phead->data[i - 1] = phead->data[i];
        i++;
    }
    (phead->count)--;
    return true;
}

bool OrederTable_Find(pSTR phead, string name)//按姓名查找
{
    system("CLS");
    int i ;
    for (i = 0; i != phead->count ;i++)
    {
        if (name == phead->data[i].name)
        {
            cout << "查找到此学生信息:\n";
            cout << "姓名:";
            cout << phead->data[i].name<<endl;
            cout << "学号:";
            cout << phead->data[i].num<<endl;
            cout << "性别:";
            cout << phead->data[i].sex<<endl;
            cout << "电话:";
            cout << phead->data[i].phone<<endl;
            cout << "地址:";
            cout << phead->data[i].place<<endl;
            return true;
        }
    }
    return false;
}

 

笔记:

在初始化表时,只需将其存放实长的对象置0即可,返回其首地址。

初始化表时,需先判断其初始化长度是否大于存储长度,然后输入插入信息,最后实长等于初始化长度。

输出表信息无注意事项。

返回表实长时,只需return其实长对象即可。

判断是否为空,只需用实长与0比较即可。

添加信息时,应先判断其存储空间是否已满,,在判断添加位置是否正确,然后用临时变量存储实长,并用其将数据移动至留出插入的位置。
然后声明一个其插入此类型的数据变量(//注意此为顺序表不是链表,不用动态分配内存,将其变量声明为与数据域类型相同的变量就行,别声明指针类型)。
然后整体赋值到表中,实长加1。

插入信息时,先判断表中是否有信息,即用实长与0比较,然后判断删除位置是否有误,
(//注意其插入判断条件是或关系,即小于等于0或大于实长【切记是大于而不是大于等于,大于等于时总会表中最后一个删除失败】)
然后输出其删除信息(//待解决问题:不会讲信息用地址传出去输出)
最后将删除位置后的所有信息向前移动(从删除位置开始移动),其删除原理,只是将其删除位置信息覆盖,其实长减1。

操作界面存在bug,一旦输入字符将无限循环,一开始想的用字符型定义选择,但其影响是一旦输入双数如:45,会被认为两个字符,导致其操作选择错误。此问题暂未解决。其他的操作都行。

 

下面是操作的界面:

posted @ 2016-03-13 15:33  Sayer  阅读(234)  评论(0编辑  收藏  举报