zoukankan      html  css  js  c++  java
  • 数据结构学习日记1:实现一个简单的线性表功能(顺序存储方式)

    数据结构:

    一个简单的线性表的实现

      学习了数据结构有一段时间了,那是半年前老师课堂上讲的,最后由于一些原因,没能听到最后,前几天在写一些算法的时候,发现自己的数据结构还是太渣了,因此便又拿起了那本很有价值的数据结构的书,重新来啃这本厚厚的书,数据结构在我们编程中是非常的重要的,希望这次的学习能有一个好的开头,并且能在这个过程中有所得吧!

    下面是我写的一个简单的线性表的实现:

      

    #include"stdafx.h"
    #include<iostream>
    using namespace std;
    
    // MyList.cpp : Defines the entry point for the console application.
    //
    
    #define    MAXCOUNT 50
    #define ERROR 0
    #define OK 1
    #define YES 2
    #define NO 3
    typedef int ElemType;
    typedef int Status;
    
    
    typedef struct
    {
        int length;
        ElemType data[MAXCOUNT];
    }MyList;
    
    
    //初始化一个线性表
    void InitList(MyList *list,int length)
    {
    
        for (int i = 0; i<length; i++)
        {
            list->data[i]= i;
        }
        list->length = length;
    }
    
    
    //判断一个线性表是否为空
    Status ListEmety(MyList list)
    {
        if (list.length == 0)
        {
            return YES;
        }
    
        else
        {
            return NO;
        }
    }
    
    //获取一个线性表中的指定的元素
    Status GetElem(MyList list, int i, ElemType *e)
    {
        if (i>list.length||i<1)
        {
            return ERROR;
        }
    
        *e = list.data[i];
        return OK;
    }
    
    //清空一个线性表
    void ClearList(MyList *list)
    {
        for (int i = 0; i < list->length; i++)
        {
            list->data[i] = NULL;
        }
        list->length = 0;
    }
    
    //查找一个值是否与线性表中的数据相等
    Status ExistElem(MyList list,ElemType e)
    {
        for (int i = 0; i < list.length; i++)
        {
            if (e == list.data[i])
            {
                return YES;
            }
        }
    
        return NO;
    }
    
    //放回表中数据的个数
    int ListLength(MyList list)
    {
        return list.length;
    }
    
    //插入一个数据
    Status InsertList(MyList *list,int x,ElemType e)
    {
        if (list->length >= MAXCOUNT||x>=MAXCOUNT||x<=0)
        {
            return ERROR;
        }
    
        //移动元素
        for (int i = list->length; i >= x - 1; i--)
        {
            list->data[i] = list->data[i - 1];
        }
        list->data[x - 1] = e;
        list->length++;
        return OK;
    }
    
    //删除一个数据
    Status DeleteElem(MyList *list, int x, ElemType *e)
    {
        if (list->length == 0 || x <= 0 || x > MAXCOUNT)
        {
            return ERROR;
        }
        
        *e = list->data[x - 1];
    
        for (int i = x - 1; i < list->length - 1; i++)
        {
            list->data[i] = list->data[i + 1];
        }
    
    
        list->length--;
        return OK;
    }
    
    int main()
    {
        //获取状态的信息
        Status status;
    
        //新建一个线性表
        MyList list;
        //MyList *p;
        //p= &list;
        //InitList(p);//指针的方式
        InitList(&list,20);
    
    
        ElemType value;
        //ElemType *e;
        //e = &value;
        //status = GetElem(list,19,e);   //指针的方式
        status= GetElem(list,19,&value);
        cout << "status:" << status << "
    " << "values:" << value << endl;
    
        //判断是否存在3
        ElemType e = 3;
        Status stats;
        stats = ExistElem(list,e);
        if (stats == 2)
        {
            cout << "存在" << endl;
        }
        else if (stats == 3)
        {
            cout << "不存在" << endl;
        }
        else
        {
            cout << "程序有错误" << endl;
        }
    
    
        //插入一个数据
        Status st;
        st = InsertList(&list,3,99);
        cout << st << endl;
        cout << list.data[2] << endl;
        cout << ListLength(list) << endl;
    
    
        //删除一个数据
        Status ss;
        ElemType ee;
        ss = DeleteElem(&list, 3, &ee);
        cout << ss << endl;
        cout << ee << endl;
        cout << ListLength(list) << endl;
    
        cin.get();
    
        return 0;
    }

      代码中存在一些没有完善的地方,大致的功能已经实现,在后续的学习中会逐步完善。

  • 相关阅读:
    泛型类型转为DataTable类型
    FineUploader 学习笔记
    如何理解Python中的if __name__ == '__main__'
    什么是python 中的顶层代码?
    任务十三:零基础JavaScript编码(一)
    目前比较全的CSS重设(reset)方法总结(转)
    任务十二:学习CSS 3的新特性
    JS高级程序设计第三版——变量、作用域和内存问题
    任务十:Flexbox 布局练习
    任务九:使用HTML/CSS实现一个复杂页面
  • 原文地址:https://www.cnblogs.com/springword/p/6072196.html
Copyright © 2011-2022 走看看