Advertisment

DataStructures In C#: Part I

author-image
CIOL Bureau
Updated On
New Update

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.

Advertisment


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

Advertisment



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



    Advertisment

  1. Plain Arrays





  2. Jagged Arrays



    Stack



    The next and more serious data structure



    System.Collections.Stack

    class -- .NET Framework Base Class Library

      • It is a FILO (First In, Last Out)
      • 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()

      • Popping an item removes the item from the top of the stack. 
      Advertisment

      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;

      }

      Advertisment

       

      /**



      * 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;

      }

      }

      }

      Advertisment

      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

      (int i=0;i<10;i++){

      Advertisment

      // make a string "objN" where N ranges 0 to 9



      string

      objToPush = "obj" + i;

      // push the object on the stack



      stack.push(objToPush); }

      // pop all elements from the stack



      while

      (!stack.isEmpty()) {

      // pop an object from the stack



      string

      objPoped = (string)stack.pop();

      // 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.









      tech-news