Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by crossfireHD ( 14 years ago )
#include <iostream>

using namespace std;

class CSparseVector
    {
    struct list{
        int num;
        int key;
        list* next;
    };
    list* vector;
    int count;
public:
    CSparseVector(unsigned int size)
    {
        if (size==0)
        {
            vector=NULL;
            count=0;
        }
        else
        {
            vector=new list[1];
            size--;
            count=1;
            vector->num=count;
            vector->key=0;
            list*tmp=vector->next;
            while(size>0)
            {
                tmp=new list[1];
                count++;
                tmp->num=count;
                tmp->key=0;
                size--;
                tmp=tmp->next;
            }
        }
    }
    CSparseVector(const CSparseVector&s)
    {
        list*dest=vector;
        count=s.count;
        do
        {
            dest=new list[1];
            dest->num=s.vector->num;
            dest->key=s.vector->key;
            dest=dest->next;
            s.vector=s.vector->next;
        }while(s.vector!=NULL);
    }
    CSparseVector& CSparseVector::operator equ(const CSparseVector&s1;)
    {
        list*tmp=vector;
        list*tmp2;
        while(tmp!=NULL)
        {
            tmp2=tmp->next;
            free(tmp);
            tmp=tmp2;
        }
        count=s1.count;
        tmp=vector;
        while(s1.vector!=NULL)
        {
            tmp=new list[1];
            tmp->num=s1.vector->num;
            tmp->key=s1.vector->key;
            tmp=tmp->next;
            s1.vector=s1.vector->next;
        }
    }
    int operator[](unsigned int offset)
    {
        while(vector!=NULL)
            if (vector->num==offset)
                return (vector->key);
            else
                vector=vector->next;
        return -1;
    }
    unsigned int size()
    {
        return count;
    }
    friend CSparseVector operator summ(const CSparseVector &v1;, const CSparseVector &v2;)
    {
        list*tmp=vector;
        list*tmp2;
        while(tmp!=NULL)
        {
            tmp2=tmp->next;
            free(tmp);
            tmp=tmp2;
        }
        count=v1.count+v2.count;
        tmp=vector;
        while(v1.vector!=NULL)
        {
            tmp=new list[1];
            tmp->num=v1.vector->num;
            tmp->key=v1.vector->key;
            tmp=tmp->next;
            v1.vector=v1.vector->next;
        }
        while(v2.vector!=NULL)
        {
            tmp=new list[1];
            tmp->num=v1.vector->num;
            tmp->key=v1.vector->key;
            tmp=tmp->next;
            v1.vector=v1.vector->next;
        }
    }
    int write(unsigned int offset, int value)
};

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}

 

Revise this Paste

Your Name: Code Language: