DS order table - continuous operation

Title Description

Establishing an order table class attributes comprising: an array, the actual length, the maximum length (set to 1000)

This class has the following member functions:

Constructor: implement initialization sequence table.

Inserting a plurality of data multiinsert (int i, int n, int item []) function, implemented in the i-th position, insert consecutive n data item from the array, i.e., a plurality of data i inserted starting from the position.

Remove the plurality of data multidel (int i, int n) function, implemented starting from the i-th position, n consecutive delete data, i.e., data from a plurality of start deleting i positions.

The main function test write sequence table class.

 

Entry

Line 1 represents the first n have n input data, i.e. the actual length n; n data followed by the input

2 first input line i represents the start position of the insert, and then there are k represents k input data is inserted, then k data input

Line 3 indicates the position of the first input i delete start, and then to be deleted k represents k input data

Export

Order table includes the actual length of the sequence table and the data, the data separated by spaces between

Order after the table of contents of the first line of output created

The second output line is continuously performed after table of contents insertion order

Output line 3 is continuously performed after table of contents deletion order

 

Sample input

6 11 22 33 44 55 66 2 3 99 88 77 4 5

Sample Output

6 11 22 33 44 55 66 9 11 99 88 77 22 33 44 55 66 4 11 99 88 66

prompt

 

#include <the iostream>
 the using  namespace STD;
 #define OK 0
 #define error -1 class SeqList 
{ Private :
     int * List; // element array int MAXSIZE; // order table maximum length int size; // order table actual length of public : 
    SeqList () 
    { 
        MAXSIZE = 1000 ; 
        size = 0 ; 
        List = new new int [MAXSIZE]; 
    } 
    SeqList ( int
 


    
    
  n,int *p)
    {
        maxsize=1000;
        list=new int[maxsize];
        size=n;
        for(int i=0;i<n;i++)
            list[i]=p[i];
    }
    ~SeqList()
    {
        delete []list;
    }
    int list_size()
    {
        return size;
    }
    int list_insert(int i,int item)
    {
        if(i<1||i>size+1)
        {
            cout<<"error"<<endl;
            return error;
        }
        int t=i-1;
        for(int j=size-1;j>=t;j--)
        {
            list[j+1]=list[j];
        }
        list[t]=item;
        size++;
        return ok;
    }
    int list_del(int i)
    {
        if(i<1||i>size)
        {
            cout<<"error"<<endl;
            return error;
        }
        int t=i-1;
        for(int j=t;j<size-1;j++)
        {
            list[j]=list[j+1];
        }
        size--;
        return ok;
    }
    int list_get(int i)
    {
        if(i<1||i>size)
        {
            cout<<"error"<<endl;
            return error;
        }
        int t=i-1;
        return list[t];
    }
    void list_display()
    {
        cout<<size<<" ";
        for(int i=0;i<size;i++)
            cout<<list[i]<<" ";
        cout<<endl;
    }
    int multiinsert(int i,int n,int item[])
    {
        if(i<1||i>size+1)
            return error;
        for(int count=0;count<n;count++)
        {
            list_insert(i,item[n-1-count]);
        }
        return ok;
    }
    int multidel(int i,int n)
    {
        if(i<1||i>size||i+n-1>size)
            return error;
        for(int count=0;count<n;count++)
        {
            list_del(i);
        }
        return ok;
    }
};
 
 
int main()
{
    int n;
    cin>>n;
    int *p=new int[n];
    for(int i=0;i<n;i++)
    {
        cin>>p[i];
    }
    SeqList L(n,p);
    L.list_display();
    int index;
    cin>>index>>n;
    int *q=new int[n];
    for(int i=0;i<n;i++)
    {
        cin>>q[i];
    }
    if(L.multiinsert(index,n,q)!=error)
        L.list_display();
    cin>>index>>n;
    if(L.multidel(index,n)!=error)
        L.list_display();
    delete []p;
    delete []q;
    return 0;
}

Guess you like

Origin www.cnblogs.com/SZU-DS-wys/p/12177790.html