zoukankan      html  css  js  c++  java
  • (c++)数据结构顺序表编码练习

    //list.h
    
    #ifndef LIST_H
    #define LIST_H
    
    class List
    {
    public:
        List(int size);
        ~List();
        void ClearList();
        bool ListEmpty();
        int ListLength();
        bool GetElem(int i,int *e);
        int LocateElem(int *e);
        bool PriorElem(int *currentElem,int *preElem);
        bool NextElem(int *currentElem,int *nextElem);
        void ListTraverse();
        bool listInsert(int i,int *e);
        bool ListDelete(int i,int *e);
    private:
        int *m_pList;
        int m_iSize;
        int m_iLength;
    
    };
    #endif
    

      

    //lsit.cpp
    
    #include<list.h>
    #include<iostream>
    using namespace std;
    
    List::List(int size){
        m_iSize = size;
        m_pList = new int[m_iSize];
        m_iLength = 0;
    }
    
    List::~List(){
        delete []m_pList;
        m_pList = NULL;
    }
    
    void List::ClearList(){
        m_iLength = 0;
    }
    
    bool List::ListEmpty(){
        return m_iLength == 0?true:false;
    }
    
    int List::List(){
        return m_iLength;
    }
    
    bool List::GetElem(int i,int *e){
        if(i < 0 || i >=m_iSize)
            return false;
        *e = m_pList[i];
        return true;
    }
    
    int List::LocateElem(int *e){
    
        for(int i = 0;i < m_iLength;++i)
        {
            if(*e == m_pList[i])return i;
        }
        return -1;
    }
    
    bool List::PriorElem(int *currentElem,int *preElem){
    
        int  temp = LocateElem(*currentElem);
        if(temp == -1)
            return false;
        else{
            if(temp == 0)
                return false;
            else
                *preElem = m_pList[temp - 1];
                return true;
        }
    }
    
    bool List::NextElem(int *currentElem,int *nextElem){
    
        int  temp = LocateElem(*currentElem);
        if(temp == -1)
            return false;
        else{
            if(temp == m_iLength - 1)
                return false;
            else
            *preElem = m_pList[temp + 1];
            return true;
        }
    }
    
    void Lisit::ListTraverse(){
    
        for(int i = 0;i < m_iLength;++i){
            cout<<m_pList[i];
        }
    }
    
    bool List::ListDelete(int i,int *e){
        if(i < 0 || i >= m_iLength)
            return false;
        *e = m_pList[i];
    
        for(k = i + 1;k < m_iLength;;++k){
            m_pList[k - 1] = m_pList[k];
        }
        m_iLength--;
        return true;
    }
    

      

  • 相关阅读:
    DotText源码阅读
    看来这世界看得太清楚了也未必是种好事呢~~~~~~~
    在Init之前究竟执行了什么?
    孙子兵法
    Excel区域重命名
    Getbuffer ReleaseBuffer Cstring
    批量删除svn标志
    VB制作网页自动填表(强烈推荐)
    GetModuleFileName
    ansi编码
  • 原文地址:https://www.cnblogs.com/cwenliu/p/5989757.html
Copyright © 2011-2022 走看看