Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Generic stack using array in c#

Tags:

stack

c#

generics

I am implementing a generic stack using array. But i get error as :

Cannot apply indexing with [] to an expression of type 'T'

on the line:

 data[SP] = data;

how to fix the issue? also i checked this link:

Cannot apply indexing to an expression of type 'T'

should i implement the same fix here in my situation too? or is there any other best option available?

Here is my code:

public class MyStack<T> 
{
    private T[] data { get; set; }
    private int SP { get; set; }
    private int Capacity { get; set; }
    public MyStack(int capacity)
    {
        this.Capacity = capacity;
        data = new T[Capacity];
        SP = -1;
        // it works here, dont know why??? ;)
        data[0] = default(T);
    }
    public void Push(T data)
    {
        ++SP;
        if(SP>=Capacity) growArray();
        // This is where i get error.
        data[SP] = data;
    }
    public T Pop()
    {
        if (SP < 0) throw new InvalidOperationException();
        T value = data[SP];
        data[SP] = default(T);
        SP--;
        return value;
    }
    public T Peak()
    {
        if (SP < 0) throw new InvalidOperationException();
        return data[SP];
    }
    private void growArray()
    {
        throw new NotImplementedException();
    }
}

Thanks in advance.

like image 383
Alagesan Palani Avatar asked Dec 27 '12 14:12

Alagesan Palani


People also ask

Can arrays be used to implement stacks?

A stack data structure can be implemented using a one-dimensional array. But stack implemented using array stores only a fixed number of data values. This implementation is very simple.

How do we implement two stacks in an array?

A simple way to implement two stacks is to divide the array in two halves and assign the half space to two stacks, i.e., use arr[0] to arr[n/2] for stack1, and arr[(n/2) + 1] to arr[n-1] for stack2 where arr[] is the array to be used to implement two stacks and size of array be n.


2 Answers

This is a scope issue, in the line

data[SP] = data;

data refers in both cases to the local parameter data which is of type T, not T[], hence the error. You can rename the local variable or explicitly reference the member variable using this:

this.data[SP] = data;
like image 149
Lee Avatar answered Oct 21 '22 12:10

Lee


I suspect that you expect data to mean the formal parameter when you are thinking about the formal parameter, and for data to mean this.data when you are thinking about the field. The C# compiler cannot read your mind; data in this case will always mean the formal parameter, which is not an array.

Instead of "data", name the array "values" and the value being pushed "value".

Also, it's legal but unusual to use private automatic properties instead of private fields. Is there some reason why you're doing that? Most people only use automatic properties for public, protected or internal properties.

like image 5
Eric Lippert Avatar answered Oct 21 '22 12:10

Eric Lippert