数据结构顺序表类删除一定范围内的数的总结

首先输入顺序表大小,再依次依次输入顺序表的各个结点的元素,再输入两个数x,y,代表删除大于等于下,小于等于y的结点,


   template<class T>
int List<T>::Search(T x)
{
    int i = 0;
    LinkNode<T>*current = first->link;
    while (current != NULL)
    {
        if (current->data == x)
        {
            i++;
            break;
        }
        i++;
        current = current->link; 
    }
    return i;
}
template<class T>
LinkNode<T>* List<T>::Locate(int i)                         //定位函数,返回第i个结点的地址,i从一开始,因为涉及到remove
{
    int n = Length();
    if (i < 0|| i>n)
    {
        return NULL;
    }
    LinkNode<T>*current = first;
    int k = 0;
    while (current != NULL && k < i)
    {
        current = current->link;           
        k++;
    }
    return current;
}
template<class T>
bool List<T>::getData(int i, T&x)
{
    if (i < 0||i==0||i>Length())
        return false;
    LinkNode<T>*current = Locate(i);
    if (current == NULL)
        return false;
    else
    {
        x = current->data;
        return  true;
    }
}
template<class T>
bool List<T>::Remove(int i, T&x)
{
    LinkNode<T>*current = Locate(i - 1);                               //  i - 1 限制了i从1开始
    LinkNode<T>*del = current->link;
    if (current == NULL || current->link == NULL)
    {
        return false;
    }
    current->link = del->link;
    x = del->data;
    delete del;
    return true;
}

emplate<class T>
void List<T>::output()
{
    LinkNode<T>*current = first->link;
    if (current == NULL)
    {
        cout << "{}" << endl;                                            记得考虑 空顺序表时的输出
    }
    else
    {
        cout << "{ " << current->data;
        current = current->link;
        while (current != NULL)
        {
            cout << ' ' << current->data;
            current = current->link;
        }
        cout << " }" << endl;
    }
}
template<class T>
void List<T>::inputFront(T endTag)
{
    LinkNode<T>*newNode;
    T val;
    makeEmpty();
    cin >> val;
    while (val != endTag)
    {
        newNode = new LinkNode<T>(val);
        newNode->link = first->link;
        first->link = newNode;
        cin >> val;
    }
}
template<class T>
void List<T>::inputRear(int num)
{
    int i = 0;
    LinkNode<T> *newNode, *last;
    T val;
    makeEmpty();
    last = first;
    while (i<num)
    {
        cin >> val;
        newNode = new LinkNode<T>(val);
        last->link = newNode;
        last = newNode;
        i++;
    }
    last->link = NULL;
}
template<class T>
bool Delete(List<T> L, int num, int x, int y)
{
    bool A, B;
    int i, l, mid;
    for (i = 1; i <num+1; i++)
    {
        A = L.getData(i, mid);
        if (A = false)
        {
            return false;
        }
        if ((mid > x || mid == x) && (mid < y || mid == y))
        {
            B = L.Remove(i, x);
            if (B = false)
            {
                return false;
            }
            i--;                                                                删除第i个以后,第i+1个变成第i个,所以i--;
            num--;                                                           顺序表长度-1,所以num--,控制循环次数;
        }
    }
    L.output();
    return true;
}
int main()
{
    bool A;
    int num, s, t;
    List<int> L;
    cin >> num;
    L.inputRear(num);
    cin >> s >> t;
    if (s > t)
    {
        cout << "s or t error!";
        return 0;
    }
    A = Delete(L, num, s, t);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值