Sunday, October 2, 2016

SET ADT - Linked List Implementation

using System;

namespace Set_Linked_List_Implementation
{
     class Set
     {
          public class Node
          {
               public object item;
               public Node next;

               public Node()
               {
               }

               public Node(object item)
               {
                    this.item = item;
               }
          }

          private int length;
          private Node head;

          public Set()
          {
               length = 0;
               head = null;
          }

          public int Length
          {
               get { return length; }
          }

          public bool isEmpty()
          {
               return length == 0;   // or head == null
          }

          public Node search(Object item)
          {
               Node nodeptr = head;
               while (nodeptr != null)
               {
                    if (nodeptr.item.ToString().Equals(item.ToString()))
                    {
                         break;
                    }
                    nodeptr = nodeptr.next;
               }
               return nodeptr;
          }

          public void delete(Object item)
          {
               Node nodeptr = head;
               Node prevptr = null;

               while (nodeptr != null)
               {
                    if (nodeptr.item.ToString().Equals(item.ToString()))
                    {
                         break;
                    }
                    prevptr = nodeptr;
                    nodeptr = nodeptr.next;
               }
               if (nodeptr != null)
               {
                    prevptr.next = nodeptr.next;
                    length--;
               }
               else
               {
                    Console.WriteLine("Can't Delete! Item not found.");
               }
          }

          public void insert(object item)
          {
               length++;

               head = new Node()
               {
                    next = head,
                    item = item
               };
          }

          public override string ToString()
          {
               Node nodeptr = head;
               string nodes = "";

               while (nodeptr != null)
               {
                    nodes += nodeptr.item + " ";
                    nodeptr = nodeptr.next;
               }
               return nodes;
          }

          public Set Union(Set B)
          {
               Set C = new Set();
               Node nodeptr = head;
               while (nodeptr != null)
               {
                    C.insert(nodeptr.item);
                    nodeptr = nodeptr.next;
               }
               nodeptr = B.head;
               while (nodeptr != null)
               {
                    C.insert(nodeptr.item);
                    nodeptr = nodeptr.next;
               }
               return C;
          }

          public Set Intersection(Set B)
          {
               Set C = new Set();
               Node nodeAptr = head;
               Node nodeBptr = B.head;
               while (nodeAptr != null)
               {
                    if (B.search(nodeAptr.item) != null)
                    {
                         C.insert(nodeAptr.item);
                    }
                    nodeAptr = nodeAptr.next;
               }

               return C;
          }

          public Set Difference(Set B)
          {
               Set C = new Set();
               Node nodeAptr = head;
               Node nodeBptr = B.head;
               while (nodeAptr != null)
               {
                    if (B.search(nodeAptr.item) == null)
                    {
                         C.insert(nodeAptr.item);
                    }
                    nodeAptr = nodeAptr.next;
               }

               return C;
          }

          public void print()
          {
               Console.WriteLine("{" + string.Join(", ", ToString().TrimEnd().Split(' ')) + "}");
          }
     }
}
---------------------------------------------------------------------------------------------------------------

Driver program using Set ADT

using System;

namespace Set_Linked_List_Implementation
{
     class Program
     {
          static void Main(string[] args)
          {
               Set A = new Set();
               Set B = new Set();
               Set C = new Set();

               A.insert(1);
               A.insert(3);
               A.insert(4);

               Console.Write("Set A = ");
               A.print();

               B.insert(2);
               B.insert(3);
               B.insert(5);
               Console.Write("Set B = ");
               B.print();

               C = A.Union(B);
               Console.Write("Union of A and B = ");
               C.print();

               C = A.Intersection(B);
               Console.Write("Intersection of A and B = ");
               C.print();

               C = A.Difference(B);
               Console.Write("Difference of A and B = ");
               C.print();

               Console.WriteLine("Press any key to continue.");
               Console.ReadKey();
          }
     }
}

No comments:

Post a Comment