• 建立顺序表


    顺序表的概念:顺序表就是用连续的存储空间来存储数据。

    顺序表的优点:方便查询,当查询时,直接用下标就可以,时间复杂度O(1);

    遇到的问题:srand函数中time()方法要加上头文件#include <time.h>  ,使用srand()时,rand()函数生成的随机数会不同,srand()在for循环的外面。

    代码:

    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    using namespace std;
    
    struct  Snode
    {
        int i;
        int data;
    }Slist[50];
    int MaxLength=0;
    void initSlist(int num)
    {
        if(MaxLength>=50)
        {
            cout<<"顺序表长度已经超过最大长度"<<endl;
        }
        else if(MaxLength==0)
        {
             Slist[MaxLength].i=0;
             Slist[MaxLength].data=num;
             MaxLength++;
        }
        else 
        {
            Slist[MaxLength].i=MaxLength;
            Slist[MaxLength].data=num;
            MaxLength++;
        }
    }
    void display()
    {
        for(int i=0;i<50;i++)
        {
             cout<<Slist[i].i<<":  "<<Slist[i].data<<endl;
        }
    }
    
    int main()
    {
         srand((unsigned)time(0));
         for(int j=100;j>50;j--)
         {  
             int num=rand()%100;
             initSlist(num);
         }
         cout<<"输出顺序表:"<<endl;
         display();
         return 0;
    }

    运行结果:

  • 相关阅读:
    近两年目标
    Spring使用ajax异步上传文件
    java注解
    js 点击文本框,预览选择图片
    修改服务器系统时间(包括hive)
    队列原理
    EMR目录
    2个CDH的hive数据同步
    CDH建表字符集问题
    EMR的fair-scheduler.xml
  • 原文地址:https://www.cnblogs.com/xshang/p/3016070.html
Copyright © 2020-2023  润新知