What are DataStructures?
Data structures
are abstract structures, or classes, that are used to organize data and provide a set of operations that can be performed upon this data.Everyone's Favorite Linear, Direct Access, Homogeneous Data Structure
Simplest and most widely used data structures in computer programs
Contents are stored in contiguous memory
Homogeneous data structures
Creating an Array
arrayName = new arrayType<allocationSize>;
booleanArray = new bool<10>;
This allocates a contiguous block of memory in the CLR-managed heap, large enough to hold the allocationSize number of
arrayTypes.
Types Of Arrays
Plain Arrays
Jagged Arrays
Stack
The next and more serious data structure
System.Collections.Stack
class -- .NET Framework Base Class Library
It has only one point where data enters or leaves Default capacity is 10 elements Can't insert or remove elements into or from the middle of the stack Methods available are push(...), pop(), and isEmpty() |
|
|
|
A Stack can be visualized graphically as a vertical collection of items. When an item is pushed onto the stack, it is placed on top of all other items.
Example
public
class pArrayStack
{
// declare some properties
protected object<> data;
protected int pointer;
/**
* construct a new stack given the capacity
*/
public pArrayStack(int capacity)
{
// initialize data array with given capacity
data = new object
; // start with an empty stack
pointer = 0;
}
/**
* push an object onto the stack
*/
public void push(object o)
{
// check if there is enough space
if(pointer < data.Length)
data
= o;
}
/**
* pop an object from the stack
*/
public object pop()
{
// check if stack is empty
if(isEmpty())
return null;
// get object to be returned
object o = data<--pointer>;
// null out memory location
data
= null; // return object we got
return o;
}
}
}
Program calling above class file is
//Stack
DataStructures.pArrayStackTest st = new pArrayStackTest();
// create a stack with maximum capacity 10
pArrayStack stack = new pArrayStack(10);
// push 10 elements onto the stack for
// make a string "objN" where N ranges 0 to 9 string
// push the object on the stack
stack.push(objToPush); }
// pop all elements from the stack while
// pop an object from the stack string
// display what we're doing
Console.WriteLine("poping: {0}",objPoped); }
**Watch out for the next article in the series where the author shall discuss the Queue type of Data Structure.