zoukankan      html  css  js  c++  java
  • 链表的数组实现

    #include <iostream>
    using namespace std;
    
    #define MAXSIZE 100
    
    /*
    链表的数组实现
    */
    typedef struct
    {
        int listData[MAXSIZE];
        int last;
    }List, *pList;
    
    pList CreateEmptyList()
    {
        pList pL;
        pL = new List;
        pL->last = -1;
        return pL;
    }
    
    int FindListElement(int X, pList pL)
    {
        int i = 0;
        while ( i <= pL->last && pL->listData[i] != X )
        {
            i++;
        }
        if (i > pL->last)
        {
            return -1;    //链表中找不到对应元素
        }
        else
        {
            return i;
        }
    }
    
    void InsertListElement(int X, int position, pList pL)
    {
        int i;
        if ( pL->last == MAXSIZE - 1 )
        {
            cout << "List Full!" << endl;
            return;
        }
        if ( position < 1 || position > pL->last + 2 )    //链表起始位置数组从1开始,last为当前数组下标从0开始(last = -1代表链表为空)
                                                        //last + 2为在链表尾部添加一个元素(不影响链表中已经存在的元素)
        {
            cout << "Error Position!" << endl;
            return;
        }
        for ( i = pL->last; i >= position - 1; i++)    //将元素插入位置position之前
        {
            pL->listData[i + 1] = pL->listData[i];
        }
        pL->listData[position - 1] = X;
        pL->last++;
        return;
    }
    
    void DeleteListElement(int position, pList pL)    //position为第K个元素
    {
        int i;
        if ( position < 1 || position > pL->last + 1 )
        {
            cout << "Error Position!" << endl;
            return;
        }
        for ( i = position - 1; i < pL->last ; i++)
        {
            pL->listData[i] = pL->listData[i + 1];
        }
        pL->last--;
        return;
    }
  • 相关阅读:
    loj #6201. 「YNOI2016」掉进兔子洞
    poj 3683 Priest John's Busiest Day
    hdu 1814 Peaceful Commission
    poj 3207 Ikki's Story IV
    loj #2305. 「NOI2017」游戏
    uoj #111. 【APIO2015】Jakarta Skyscrapers
    洛谷P1550 [USACO08OCT]打井Watering Hole
    uoj #110. 【APIO2015】Bali Sculptures
    loj #2116. 「HNOI2015」开店
    codevs 3044 矩形面积求并
  • 原文地址:https://www.cnblogs.com/liangchao/p/4274969.html
Copyright © 2011-2022 走看看