O
O
OypiStudent2016-03-11 15:34:37
Java
OypiStudent, 2016-03-11 15:34:37

How to make your own Array class (generic) (for laba)?

How it's done?
In a couple of minutes, I whipped up a singly-linked list, in haste without strapping like square brackets, etc. it looks something like this (just for logic, the way I imagine it):

class Node
{

    public string value;
    public Node next;
}

class LinkedList
{
    public Node root;

    public LinkedList(string value)
    {
        var n = new Node();
        n.value = value;
        root = n;
    }

    public void Add(string value)
    {
        var n = new Node();
        n.value = value;
        getLast(root).next = n;
    }

    private Node getLast(Node n)
    {
        if (n.next != null)
            return getLast(n.next);
        else
            return n;
    }
}

...

var l = new LinkedList("0");
l.Add("1");
l.Add("2");
l.Add("3");
MessageBox.Show(l.root.next.next.next.value);

Everything is clear here.
And Array in general how to do? What will be the logic? Where is the data stored?
If I take a ready-made class, say List, or vector (C ++), and wrap it in a class, then this will not be the right solution, will it? The teacher won't count?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kovalsky, 2016-03-11
@dmitryKovalskiy

Well, there is the development of generics.
And forward to implementation.
UPD:
1) in my opinion, setting the task in the form of a "Linked List" is a little about nothing. Without a bypass method, it is not clear what you will implement (LIFO or FIFO or some other beast). There are also singly and doubly linked lists, XOR lists, cyclic lists, and so on. Which one are you implementing?
2) I see a method for adding new elements, but where is the get method? Although without a bypass method it is not clear how to implement it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question