顺序表的操作实验

实验一 顺序表的操作实验

一、实验名称和性质
所属课程 数据结构与算法
实验名称 顺序表的操作
实验学时 2
实验性质 √验证 □综合 □设计
必做/选做 √必做 □选做

二、实验目的
1.掌握线性表的顺序存储结构的表示和实现方法。
2.掌握顺序表基本操作的算法实现。
3.了解顺序表的应用。

三、实验内容
1.建立含n个数据元素的顺序表并输出该表中各元素的值。
2.在有序顺序表L中插入元素x使线性表仍然有序。
3.写一个函数删除有序顺序表中的重复元素。

四、知识准备
前期要求熟练掌握了C语言的编程规则、方法和顺序表的基本操作算法。
五、实验要求
编程实现如下功能:
(1)根据输入顺序表的长度n和各个数据元素值建立一个顺序表,输出顺序表中各元素值,对比是否操作成功。
(2)根据输入的n个非递减的有序数据建立一个有序顺序表,并输出有序顺序表中各元素值。在有序顺序表L中一个值为x的元素,并输出插入后顺序表仍然有序,输出插入后的顺序表中各元素值。
(3)删除有序顺序表中重复的元素,并输出删除后的顺序表中各元素值。

六、扩展实验题
编程实现一个简单学生成绩表的操作。
此系统的功能包括:
① 查询:按特定的条件查找学生
② 修改:按学号对某个学生的某门课程成绩进行修改
③ 插入:增加新学生的信息
④ 删除:按学号删除已退学的学生的信息。
学生成绩表的数据如下:
学号 姓名 性别 大学英语 高等数学
2008001 Alan F 93 88
2008002 Danie M 75 69
2008003 Helen M 56 77
2008004 Bill F 87 90
2008006 Peter M 79 86
2008006 Amy F 68 75
要求采用顺序存储结构来实现对上述成绩表的相关操作。

**

题解:

今天下午数据结构与算法开始做顺序表的实验了,本以为掌握的不错,但是一动手开始写的时候还是有些忐忑不安。(结果记不起来学的知识!)还是太菜了,所以晚自习看了很多的资料,决定要把知识点梳理一遍,把题目完完全全做出来。

先看第一个:建立含n个数据元素的顺序表并输出该表中各元素的值。
题目单纯的是顺序表,没有其他的记录,所以只要构建一个顺序表,顺序表里包含表中所要储存的数据段,即data[i]数组,下标尽量取大一点。同时结构体中还包含这个顺序表的实际长度len。是否宏定义都没关系,有个概念就行。接着大致上是在顺序表中读入数据,再原封不动地输出即可。在这里形参我使用的是指针,而不是引用,因为我更喜欢指针,用不用的好那就是另一回事了~请大家多多指教!下面是我第一次写的错误代码:

#include <stdlib.h>//不要忘记头文件<stdlib.h>!
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#define maxlen 10000
using namespace std;
typedef struct Sqlist
{
    int data[maxlen];
    int len;
};
void creatlist(Sqlist *L)
{
    int n;
    if (n < 1 || n > L->len)
    {
        cout << "输入错误!" << endl;
    }
    else if (L->len > maxlen)
    {
        cout << "数据溢出!" << endl;
    }
    for (int i = 0; i < n; i++)
    {
        cin >> L->data[i];
    }
    for (int i = 0; i < n; i++)
    {
        cout << L->data[i];
    }
}
int main()
{
    int i, n;
    cin >> n;
    Sqlist *L;
    creatlist(L);
    return 0;
}

在课上越看越急,以至于根本没发现自己没有申请内存!说都是顺序表了,不申请内存系统才不会帮你留位置嘞。所以要申请内存,后一章的链表也是,不过当时实验室先讲的是链表~继续,

不要怕写不出,一定要自己去尝试!多试错!面子没有知识那么重要!

同时自己写的时候没有规定顺序表的实际长度,本来应该是L->len=n,但是没有写,应该加在输入数据之后的。改了之后:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#define maxlen 10000
using namespace std;
typedef struct
{
    int data[maxlen];
    int len;
}Sqlist;
void creatlist(Sqlist *L,int n)
{
    if (n < 1 || n > L->len)
    {
        cout << "输入错误!" << endl;
    }
    else if (L->len > maxlen)
    {
        cout << "数据溢出!" << endl;
    }
    for (int i = 0; i < n; i++)
    {
        cin >> L->data[i];
    }
    L->len = n;//好家伙我直接傻眼!
    for (int i = 0; i < n; i++)
    {
        cout << L->data[i];
    }
}
int main()
{
    Sqlist *L = (Sqlist *)malloc(sizeof(Sqlist));
    int i, n;
    cin >> n;
    creatlist(L,n);
    return 0;
}

结果还是错的!(说实话输入和输出都写在一起了???)
改!

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define maxlen 10000
using namespace std;
typedef struct
{
    int data[maxlen];
    int len;
} Seqlist;
void creatlist(Seqlist *L, int n) //创建顺序表
{
    int i;
    for (i = 0; i < n; i++)
    {
        cin >> L->data[i];
    }
    L->len = n;
}
void putlist(Seqlist *L) //输出顺序表
{
    int i;
    for (i = 0; i < L->len; i++)
    {
        cout << L->data[i]<<" ";
    }
    cout << endl;
}
int main()
{
    Seqlist *L = (Seqlist *)malloc(sizeof(Seqlist));
    int n;
    cout << "请输入你要创建的顺序表的长度;\n"<< endl;
    cin >> n;
    cout << "请输入你要创建的顺序表;\n"<< endl;
    creatlist(L, n);
    cout << "输出顺序表;\n"<< endl;
    putlist(L);
}

在这里插入图片描述
终于对了!(谢天谢地了)

第二个:在有序顺序表L中插入元素x使线性表仍然有序
当然首先还是要有顺序表。

#include <cstdio>
#include <iostream>
#include <stdlib.h>
#include <cstring>
#define maxlen 10000
using namespace std;
typedef struct
{
    int data[maxlen];
    int len;
} Seqlist;
void creatlist(Seqlist *L)
{
    for (int i = 0; i < n; i++)
    {
        cin >> L->data[i];
    }
    L->len = n;
}
void insertlist(Seqlist *L, int x)
{
    for (int i = 0; i < L->len; i++)
    {
        if (L->data[i] < x && L->data[i + 1] >= x)
        {
            L->data[i + 2] = L->data[i + 1];
            L->len++;
        }
        else if (L->data[i] >= x && L->data[i + 1] < x)
        {
            L->data[i + 2] = L->data[i + 1];
            L->len++;
        }
    }
}
void putlist(Seqlist *L)
{
    for (int i = 0; i < L->len + 1; i++)
    {
        cout << L->data[i] << " ";
    }
    cout << endl;
}
int main()
{
    Seqlist *L = (Seqlist *)malloc(sizeof(Seqlist));
    int n;
    cout << "请输入你要创建的顺序表的长度;\n"<< endl;
    cin >> n;
    cout << "请输入你要创建的顺序表;\n"<< endl;
    creatlist(L, n);
    insertlist(L, x);
    cout << "输出顺序表;\n"<< endl;
    putlist(L);
}

今天时间不太够了,剩下的明天再写。先回寝室了!加油!

  • 2
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值