• 数据结构实验1:C++实现静态顺序表类


           写了3个多小时,还是太慢了、太菜了!

           图1 程序运行演示截图1      

    图1 程序运行演示截图1

     

    实验1

     

    1.1 实验目的

    熟练掌握线性表的顺序存储结构。

    熟练掌握顺序表的有关算法设计。

    根据具体问题的需要,设计出合理的表示数据的顺序结构,并设计相关算法。

    1.2 实验要求

    顺序表结构和运算定义,算法的实现以库文件方式实现,不得在测试主程序中直接实现;

    比如存储、算法实现放入文件:seqList.h

    实验程序有较好可读性,各运算和变量的命名直观易懂,符合软件工程要求;

    程序有适当的注释。

    1.3 实验任务

    编写算法实现下列问题的求解。

    <1>求顺序表中第i个元素(函数),若不存在,报错。

    实验测试数据基本要求:

    第一组数据:顺序表长度n≥10,i分别为5,n,0,n+1,n+2

    第二组数据:顺序表长度n=0,i分别为0,2

    <2>在第i个结点前插入值为x的结点。

    实验测试数据基本要求:

    第一组数据:顺序表长度n≥10,x=100,  i分别为5,n,n+1,0,1,n+2

    第二组数据:顺序表长度n=0,x=100,i=5

    <3>删除顺序表中第i个元素结点。

    实验测试数据基本要求:

    第一组数据:顺序表长度n≥10,i分别为5,n,1,n+1,0

    第二组数据:顺序表长度n=0, i=5

    <4>在一个递增有序的顺序表L中插入一个值为x的元素,并保持其递增有序特性。

    实验测试数据基本要求:

    顺序表元素为(10,20,30,40,50,60,70,80,90,100),

    x分别为25,85,110和8

    <5>将顺序表L中的奇数项和偶数项结点分解开(元素值为奇数、偶数),分别放入新的顺序表中,然后原表和新表元素同时输出到屏幕上,以便对照求解结果。

    实验测试数据基本要求:

    第一组数据:顺序表元素为(1,2,3,4,5,6,7,8,9,10,20,30,40,50,60)

    第二组数据:顺序表元素为(10,20,30,40,50,60,70,80,90,100)

    <6>求两个递增有序顺序表L1和L2中的公共元素,放入新的顺序表L3中。

    实验测试数据基本要求:

    第一组

    第一个顺序表元素为(1,3,6,10,15,16,17,18,19,20)

    第二个顺序表元素为(1,2,3,4,5,6,7,8,9,10,18,20,30)

    第二组

    第一个顺序表元素为(1,3,6,10,15,16,17,18,19,20)

    第二个顺序表元素为(2,4,5,7,8,9,12,22)

    第三组

    第一个顺序表元素为()

    第二个顺序表元素为(1,2,3,4,5,6,7,8,9,10)

    <7>删除递增有序顺序表中的重复元素,并统计移动元素次数,要求时间性能最好。

    实验测试数据基本要求:

    第一组数据:顺序表元素为(1,2,3,4,5,6,7,8,9)

    第二组数据:顺序表元素为(1,1,2,2,2,3,4,5,5,5,6,6,7,7,8,8,9)

    第三组数据:顺序表元素为(1,2,3,4,5,5,6,7,8,8,9,9,9,9,9)

    1.4* 顺序表扩展实验

    非必做内容,有兴趣的同学选做,

    <1>递增有序顺序表L1、L2,对2表进行合并,并使得合并后成为一个集合,集合的元素放回L1表中保存,要求时间性能最好。

    <2>(递增有序)顺序表表示集合A、B,实现:

    C=A∩B,C=A∪B,C=A-B

    A=A∩B,A=A∪B,A=A-B

    <3>(递增有序)顺序表表示集合A、B,判定A是否B的子集。

    <4>(2011)(15 分)一个长度为L(L≥1)的升序序列S,处在第L/2个位置的数称为S 的中位数。例如,若序列S1=(11, 13, 15, 17, 19),则S1 的中位数是15。两个序列的中位数是含它们所有元素的升序序列的中位数。例如,若S2=(2, 4, 6, 8, 20),则S1 和S2 的中位数是11。

    现有两个等长升序序列A 和B,试设计一个在时间和空间两方面都尽可能高效的算法,找出两个序列A 和B 的中位数。要求:

    (1)给出算法的基本设计思想。

    (2)根据设计思想,采用C 或C++语言描述算法,关键之处给出注释。

    (3)说明你所设计算法的时间复杂度和空间复杂度。

    1.5 运行结果截图

    图2 测试(1)①

     

     

     图3 测试(1)②

     

    图4 测试(2)① 

     

     

     图5 测试(2)②

     

     

    图6 测试(3)① 

     

     图7 测试(3)②

     

     

     图8 测试(4)

     图9 测试(5)①

     

     图10 测试(5)②

     

    图11 测试(6)① 

     

     

     图12 测试(6)②

     

     图13 测试(6)③

     

     图14 测试(7)①

     

     图15 测试(7)②

     

    图16 测试(7)③

     

     

     

           StdAfx.h文件:

     1 // stdafx.h : include file for standard system include files,
     2 //  or project specific include files that are used frequently, but
     3 //      are changed infrequently
     4 //
     5 
     6 #if !defined(AFX_STDAFX_H__D36E9D40_3BCB_4A85_9D48_AC876E7A2942__INCLUDED_)
     7 #define AFX_STDAFX_H__D36E9D40_3BCB_4A85_9D48_AC876E7A2942__INCLUDED_
     8 
     9 #if _MSC_VER > 1000
    10 #pragma once
    11 #endif // _MSC_VER > 1000
    12 
    13 #include <stdc++.h>//万能头文件,非常好用!我用的是VC6.0,里面没有;
    14 //因此手动将codeblocks的 stdc++.h 文件导入到 D:softwareMicrosoft Visual StudioVC98Include 中
    15 
    16 typedef int elementType;
    17 const int maxn = 10000+13;
    18 using namespace std;
    19 
    20 // TODO: reference additional headers your program requires here
    21 
    22 //{{AFX_INSERT_LOCATION}}
    23 // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    24 
    25 #endif // !defined(AFX_STDAFX_H__D36E9D40_3BCB_4A85_9D48_AC876E7A2942__INCLUDED_Code
     1 // stdafx.h : include file for standard system include files,
     2 //  or project specific include files that are used frequently, but
     3 //      are changed infrequently
     4 //
     5 
     6 #if !defined(AFX_STDAFX_H__D36E9D40_3BCB_4A85_9D48_AC876E7A2942__INCLUDED_)
     7 #define AFX_STDAFX_H__D36E9D40_3BCB_4A85_9D48_AC876E7A2942__INCLUDED_
     8 
     9 #if _MSC_VER > 1000
    10 #pragma once
    11 #endif // _MSC_VER > 1000
    12 
    13 #include <stdc++.h>//万能头文件,非常好用!我用的是VC6.0,里面没有;
    14 //因此手动将codeblocks的 stdc++.h 文件导入到 D:softwareMicrosoft Visual StudioVC98Include 中
    15 
    16 typedef int elementType;
    17 const int maxn = 10000+13;
    18 using namespace std;
    19 
    20 // TODO: reference additional headers your program requires here
    21 
    22 //{{AFX_INSERT_LOCATION}}
    23 // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    24 
    25 #endif // !defined(AFX_STDAFX_H__D36E9D40_3BCB_4A85_9D48_AC876E7A2942__INCLUDED_)

           SeqList1.h文件:

    // SeqList1.h: interface for the SeqList class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #if !defined(AFX_SEQLIST1_H__5F69CE41_7D8B_4396_BAAE_F849B1FD54D1__INCLUDED_)
    #define AFX_SEQLIST1_H__5F69CE41_7D8B_4396_BAAE_F849B1FD54D1__INCLUDED_
    
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    
    class SeqList  
    {
    public:
        SeqList();
        virtual ~SeqList();
        void printList();
        int Length();
        int locate( elementType value );//返回第一个值对value的位置,没有则返回-1
        bool isEmpty();//判空
        bool isFull();//判满
        bool getElement( int pos, elementType& value );//获取pos位置的值
        bool insertList( int pos, elementType value );//在pos位置前插入value值
        bool insertList_1( elementType value );//在尾部插入value值
        bool deleteListNode( int pos, elementType& value );//按位置删除元素
        bool deleteListNode_1( int value );//按值删除元素
        bool deleteListNode_2( int value );//按值删除所有对应元素
    private:
        elementType Arr[maxn];//存放表元素的数组
        size_t listSize;//记录当前顺序表的大小
    };
    
    #endif // !defined(AFX_SEQLIST1_H__5F69CE41_7D8B_4396_BAAE_F849B1FD54D1__INCLUDED_)

           SeqList1.cpp文件:

      1 // SeqList1.cpp: implementation of the SeqList class.
      2 //
      3 //////////////////////////////////////////////////////////////////////
      4 
      5 #include "stdafx.h"
      6 #include "SeqList1.h"
      7 
      8 //////////////////////////////////////////////////////////////////////
      9 // Construction/Destruction
     10 //////////////////////////////////////////////////////////////////////
     11 
     12 SeqList::SeqList()
     13 {
     14     listSize = 0;
     15 }
     16 
     17 SeqList::~SeqList()
     18 {
     19     cout << this << " 顺序表已销毁!" << endl;
     20 }
     21 
     22 void SeqList::printList()
     23 {
     24     int column = 0;
     25     for( int i = 0; i < listSize; i ++ )
     26     {
     27         cout<< setiosflags(ios::left) << setw(5) << Arr[i] << " ";
     28         if( ++ column % 10 == 0 )
     29             cout << endl;
     30     }
     31     cout << endl;
     32 }
     33 
     34 int SeqList::Length()
     35 {
     36     return listSize;
     37 }
     38 
     39 int SeqList::locate( elementType value )
     40 {
     41     for( int i = 0; i < listSize; i ++ )
     42         if( Arr[i] == value )
     43             return i + 1;
     44     return -1;
     45 }
     46 
     47 bool SeqList::isEmpty()
     48 {
     49     return listSize == 0;
     50 }
     51 
     52 bool SeqList::isFull()
     53 {
     54     return listSize == maxn;
     55 }
     56 
     57 bool SeqList::insertList( int pos, elementType value )
     58 {
     59     if( isFull() )
     60     {
     61         cout << "顺序表已满!插入失败!" << endl;
     62         return false;
     63     }
     64     if( pos > listSize )
     65     {
     66         cout << "插入位置超过当前顺序表容量!插入失败!" << endl;
     67         return false;
     68     }
     69     if( pos <= 0 )
     70     {
     71         cout << "插入位置必须大于0!插入失败!" << endl;
     72         return false;
     73     }
     74     for( int i = listSize - 1; i >= pos - 1; i -- )
     75         Arr[ i + 1 ] = Arr[i];
     76     Arr[ pos - 1 ] = value;
     77     listSize ++;//一定不能少!
     78     return true;//一定不能少!
     79 }
     80 
     81 bool SeqList::insertList_1( elementType value )
     82 {
     83     if( isFull() )
     84     {
     85         cout << "顺序表已满!插入失败!" << endl;
     86         return false;
     87     }
     88     Arr[ listSize ++ ] = value;
     89     return true;//一定不能少!
     90 }
     91 
     92 bool SeqList::deleteListNode( int pos, elementType& value )
     93 {
     94     if( isEmpty() )
     95     {
     96         cout << "顺序表为空!删除失败!" << endl;
     97         return false;
     98     }
     99     if( pos > listSize )
    100     {
    101         cout << "删除位置大于表长!删除失败!" << endl;
    102         return false;
    103     }
    104     value = Arr[ pos - 1 ];
    105     for( int i = pos; i < listSize - 1; i ++ )
    106         Arr[ i - 1 ] = Arr[i];
    107     listSize --;//一定不能少!
    108     return true;//一定不能少!
    109 }
    110 
    111 bool SeqList::deleteListNode_1( int value )
    112 {
    113     if( isEmpty() )
    114     {
    115         cout << "顺序表为空!删除失败!" << endl;
    116         return false;
    117     }
    118     if( locate(value) == -1 )
    119     {
    120         cout << "表中无此元素!删除失败!" << endl;
    121         return false;
    122     }
    123     int index = locate(value);
    124     for( int i = index - 1; i < listSize; i ++ )
    125         Arr[i] = Arr[ i + 1 ];
    126     listSize --;//一定不能少!否则会出现已失效的位置仍占有先前元素的错误!
    127     return true;//一定不能少!
    128     /*精简版如下!
    129     void delete(int A[],int key,int& n) 
    130     { 
    131         int i,j; 
    132         for(i=0;i<n&&A[i]-key;i++); //查找key值元素 
    133         if(i>=n) 
    134             cout<<"not found"<<endl; 
    135         else
    136         { 
    137             for(j=i;j<n-1;A[j]=A[j+1],j++);//若找到,将该元素后边的值向前覆盖 
    138             --n;//数组长度减1 
    139         } 
    140     }
    141     --------------------- 
    142     作者:castle_kao 
    143     来源:CSDN 
    144     原文:https://blog.csdn.net/castle_kao/article/details/53487610?utm_source=copy 
    145     版权声明:本文为博主原创文章,转载请附上博文链接!
    146     */
    147 }
    148 
    149 bool SeqList::deleteListNode_2( int value )
    150 {
    151     if( isEmpty() )
    152     {
    153         cout << "顺序表为空!删除失败!" << endl;
    154         return false;
    155     }
    156     if( locate(value) == -1 )
    157     {
    158         cout << "表中无此元素!删除失败!" << endl;
    159         return false;
    160     }
    161     int cnt = 0;
    162     for( int i = 0; i < listSize; i ++ )
    163         if( Arr[i] == value )
    164             cnt ++;
    165     while( cnt -- )
    166     {
    167         int pos = locate(value), data;
    168         deleteListNode( pos, data );
    169     }
    170     return true;
    171 }

           SeqList.cpp(测试函数)文件:

     1 // SeqList.cpp : Defines the entry point for the console application.
     2 //
     3 
     4 #include "stdafx.h"
     5 #include "SeqList1.h"
     6 
     7 int main(int argc, char* argv[])
     8 {
     9     ios::sync_with_stdio(false);
    10     freopen( "x1.in", "r", stdin );
    11     //freopen( "x1.out", "w", stdout );
    12     //printf( "%d
    ", (bool)-1 );
    13     SeqList L1;
    14     if( L1.isEmpty() )
    15     {
    16         cout << "空表!" << endl;
    17     }
    18     int n;
    19     cin >> n;
    20     for( int i = 0; i < n; i ++ )
    21     {
    22         int num;
    23         cin >> num;
    24         L1.insertList_1(num);
    25     }    
    26     cout << "当前表长为:" << L1.Length() << endl;
    27     L1.printList();
    28     L1.insertList( 5, -1 );
    29     cout << "当前表长为:" << L1.Length() << endl;
    30     L1.printList();
    31     int data;
    32     L1.deleteListNode( 4, data );
    33     cout << "值为 " << data << " 的元素已删除!" << endl;
    34     
    35 
    36     L1.deleteListNode_1(6);
    37     cout << L1.Length() << endl;
    38     L1.printList();
    39 
    40     L1.deleteListNode_1(7);
    41     cout << "当前表长为:" << L1.Length() << endl;
    42     L1.printList();
    43 
    44     int delKey = 2;
    45     L1.deleteListNode_2(delKey);
    46     cout << "所有值为" << delKey << "元素删除后," << "当前表长为:" << L1.Length() << endl;
    47     L1.printList();
    48 
    49     SeqList L2;
    50     if( L2.isEmpty() )
    51     {
    52         cout << "空表!" << endl;
    53     }
    54     for( int j = 0; j < maxn; j ++ )
    55     {
    56         L2.insertList_1( j + 1 );
    57     }
    58     if( L2.isFull() )
    59     {
    60         cout << "表满!" << endl;
    61     }
    62     cout << "当前表长为:" << L2.Length() << endl;
    63     L2.printList();//改为    L1.printList();  会有意想不到的效果!
    64     
    65     return 0;
    66 }

    图2 程序运行演示截图2

    图17 程序运行演示截图2

  • 相关阅读:
    Android开发之SQLite的使用方法
    【转】如何分析解决Android ANR
    error log
    33层高楼为什么27楼和28楼最贵 次顶层房价高原因揭秘
    Could not allocate CursorWindow size due to error -12 错误解决方法
    过来人讲述买房血泪史:什么样的房子不能碰
    cocos2d-x删除vs2010项目模板
    Lua学习笔记5:类及继承的实现
    Linux vsftpd服务配置具体解释
    Android_Dialog_设置Dialog窗体的大小
  • 原文地址:https://www.cnblogs.com/25th-engineer/p/9781148.html
Copyright © 2020-2023  润新知