We looked at generic methods. In
this tutorial we are going to look at a more powerful application of generics:
generic classes. By making your classes generic you can define structures that
work on any type of data. To demonstrate how to make use of generic classes we
are going to develop a simple implementation of a stack.
Creating a Generic Class
First we need to look at how to create a generic class.
The basic skeleton is this:
Creating a Generic Class
First we need to look at how to create a generic class.
The basic skeleton is this:
Code:
class Stack<T> {
}
The line Stack<T> indicates this class is a generic
class with one generic type defined called T. This basically is all we need and
the rest is writing generic methods as described in the generic methods
tutorial.
Now we need to figure out how we are going to implement the stack. For this tutorial we are going to use an array where the first element in the array represents the first element added.
So update the class to look like this:
Now we need to figure out how we are going to implement the stack. For this tutorial we are going to use an array where the first element in the array represents the first element added.
So update the class to look like this:
Code:
class Stack<T> {
private T[] items;
private int count;
public Stack(int
length) {
items = new T[length];
count = 0;
}
}
Note that unlike generic methods we don’t have to declare
the methods as Stack<T>(int length). Since the class has the generic type
T then all of it’s members have access to T.
To create an instance of the stack we write:
To create an instance of the stack we write:
Code:
var stack = new Stack<int>(5);
This stack can have at most 5 elements in it.
Adding Item to Stack
Because we are using an array the basic idea is to increment a position counter and then update the next position in the array to be set to our new value. Since we are dealing with a limited space in the context of an array we have to check
Adding Item to Stack
Because we are using an array the basic idea is to increment a position counter and then update the next position in the array to be set to our new value. Since we are dealing with a limited space in the context of an array we have to check
Code:
public void Add(T item) {
if (count >= items.Length) return;
items[++count] = item; }
Removing Item from Stack
When removing an item from the stack we simply decrement the count. This doesn’t actually remove the item from the stack as it is still in the array but our interface only exposes the top element to the user. So to the end user the item appears to have been removed from the stack.
When removing an item from the stack we simply decrement the count. This doesn’t actually remove the item from the stack as it is still in the array but our interface only exposes the top element to the user. So to the end user the item appears to have been removed from the stack.
Code:
public T Remove()
{
var temp = items[count];
count--;
return temp;
}
This method returns the top item and removes it from the
stack.
Implementing Peek
Peek returns the top item without removing it fromt he stack. The peek method is also simple to implement. Simply return items[count] :
Implementing Peek
Peek returns the top item without removing it fromt he stack. The peek method is also simple to implement. Simply return items[count] :
Code:
public T Peek() {
return items[count];
}
Is the stack empty?
This is also an easy task to accomplish.We simply check if count is 0 (no items in array) and return true if count is 0 false otherwise.
Code:
public bool IsEmpty() {
return count == 0;
}
The final code looks like:
Code:
class Stack<T>
{
private T[] items;
private int count;
public Stack(int length)
{
items = new T[length];
count = 0;
}
public void Add(T item)
{
if (count >= items.Length)
return;
items[++count] = item;
}
public T Remove()
{
var temp = items[count];
count--;
return temp;
}
public T Peek()
{
return items[count];
}
public bool IsEmpty()
{
return count == 0;
}
}
No comments:
Post a Comment