Data Structure in C#

[ Data Structures in C# ]

By Pinky

  • In computer science, a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

 

  • Types of Data structures

Arrays

Matrix

Linked Lists

Doubly linked lists

B-Trees

Stacks

Queues

  • Array : An array is a systematic arrangement of objects, usually in rows and columns.

    0

    “Red”

    1

    “Green”

    2

    “Blue”

    3

    “Yellow”

    4

    “White”

    a[0] = "Red";
    
    a[1] = "Green";
    
    a[2] = "Blue";
    
    a[3] = "Yellow";
    
  • Linked lists : A number of connected items or names written or printed consecutively. In computer science a list is an ordered collection of values (items, entries, elements) where a value may occur more than once.

  • List behaviour

A list …

Adds an element

Removes an elements

Searches for an element

And does other operations like concatenation etc

  • Code of Singly Linked List in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// Linked List

namespace LinkList
{
    class Node
    {
        public Node next = null;
        public int item;
    }

    class Program
    {
        private Node node = null;
        private Node currentNode = null;

        static void Main(string[] args)
        {
            int choice;
            Program program = new Program();

            do
            {
                Console.WriteLine("\n1 for Adding A New Node");
                Console.WriteLine("2 for Printing Nodes");
                Console.WriteLine("3 for Removing a Node");
                Console.WriteLine("4 for searching a Node");
                Console.WriteLine("0 for Exit \n");
                Console.Write("Enter Your Selection :: ");
                choice = int.Parse(Console.ReadLine());

                switch (choice)
                {
                    case 1:
                        Console.Write("Enter Node Item to Add: ");
  int x = int.Parse(Console.ReadLine());                                             program.AddNode(x);
                        break;

                    case 2:
                        program.PrintNode();
break;

                    case 3:
                        Console.Write("Enter Node Item to Remove: ");
  int y = int.Parse(Console.ReadLine());                   program.RemoveNode(y);
                        break;

                    case 4:
                        Console.Write("Enter Node Item to Search : ");
                        int z = int.Parse(Console.ReadLine());
                        program.SearchNode(z);
                        break;
                }
            }
            while (choice != 0);

        }

        public void AddNode(int item)
        {
            if (node == null)
            {
                node = new Node();
                currentNode = node;
                currentNode.item = item;
            }
            else
            {
                currentNode.next = new Node();
                currentNode.next.item = item;
                currentNode = currentNode.next;
            }
        }

        public void RemoveNode(int item)
        {
            Node reqNode = node;
            Node prevNode = null;

            while (reqNode != null && reqNode.item != item)
            {
                prevNode = reqNode;
                reqNode = reqNode.next;
            }

            if (reqNode != null && reqNode.item == item)
            {
                if (prevNode == null)
                {
                    prevNode.next = reqNode.next;
                }

                else
                {
                    prevNode.next = reqNode.next;
                }

            }

        }

        public void PrintNode()
        {
            Node tempNode = node;

            while (tempNode != null)
            {
                Console.WriteLine(tempNode.item);

                tempNode = tempNode.next;
            }

        }

        public void SearchNode(int item)
        {
            Node reqNode = node;

            while (reqNode != null && reqNode.item != item)
            {
                reqNode = reqNode.next;
            }

            if (reqNode != null && reqNode.item == item)
            {
                Console.WriteLine(reqNode.item);
            }
        }
    }
}

 

Results

Adding a new Node
If we want to add the nodes such as 111,222,333,444,
We should press number 2. It will be presented below that.

Screenshot 2016-06-10 17.24.56

Printing nodes
If we want to print the nodes, we should press number 2.
And then it will be displayed below that.

Screenshot 2016-06-10 17.28.13

Removing a node
If we want to delete one node, we should press 3.
And then it will be presented below that.

Screenshot 2016-06-10 17.30.13

 

  • Doubly linked lists : In computer science, a doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes. The beginning and ending nodes’ previous and next links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list. If there is only one sentinel node, then the list is circularly linked via the sentinel node. It can be done.

 

  • Linked Lists

    Features of Doubly-linked List as compared to single linked list :

    Double-linked lists require more space per node .

    Their elementary operations are more expensive.

    They are often easier to manipulate (insert , delete) because they allow sequential access to the list in both directions.

 

  • Code of Doubly Linked List in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// Doubly Linked List 

namespace DoublyLinkedList
{

    class Node
    {
        public int item;
        public Node previous;
        public Node next;

        public Node(int item, Node previous, Node next)
        {
            this.item = item;
            this.previous = previous;
            this.next = next;
        }
    }
    class Program
    {
        Node head;
        static void Main(string[] args)
        {
            int choice;
            Program program = new Program();

            do
            {
                Console.WriteLine("\n1 for Adding A New Node");
                Console.WriteLine("2 for Printing Nodes");
                Console.Write("Enter Your Selection :: ");
                choice = int.Parse(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        Console.Write("Enter Node Item to Add: ");
                        int x = int.Parse(Console.ReadLine());
                        program.AddNode(x);
                        break;
                    case 2:
                        program.PrintList();
                        break;
                }
            }
            while (choice != 0);
        }

        public void AddNode(int item)
        {
            if (head == null)
                head = new Node(item, null, null);
            else
            {
                Node currentNode = head;

                while (currentNode.next != null)
                    currentNode = currentNode.next;

                currentNode.next = new Node(item, currentNode, null);
            }
        }
        public void PrintList()
        {
            Node currentNode = head;
            Node prevNode = null;

            Console.WriteLine("In forward direction");

            while (currentNode != null)
            {
                Console.WriteLine(currentNode.item);
                prevNode = currentNode;
                currentNode = currentNode.next;
            }

            Console.WriteLine("In previous direction");

            while (prevNode != null)
            {
                Console.WriteLine(prevNode.item);
                prevNode = prevNode.previous;
            }

        }
    }
}

Adding a new Node
If we want to add the nodes such as 111,222,333,444,
We should press number 2. It will be presented doubly linked data below that. We can have a look the doubled data.

Screenshot 2016-06-10 17.54.13

Advertisements

Author: iotmaker

I am interested in IoT, robot, figures & leadership. Also, I have spent almost every day of the past 15 years making robots or electronic inventions or computer programs.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s