public virtual Node Pop()
 {
     if (top == null) {
         return null;
     } else {
         Node d = new Node(top.Data);
         top = top.Next;
         return d;
     }
 }
 public override void Push(Node n)
 {
     base.Push (n);
     if (minimum == null) {
         minimum = n;
     }
     else if (n.Data < minimum.Data) {
         minimum = n;
     }
 }
Example #3
0
 /// <summary>
 /// Deletes the self from linked list.
 /// 2.3 Implement an algorithm to delete a node in the middle of a single linked list, 
 /// given only access to that node. 
 /// EXAMPLE
 ///	Input: the node ā€˜cā€™ from the linked list a->b->c->d->e
 ///	Result: nothing is returned, but the new linked list looks like a->b->d->e
 /// </summary>
 /// <returns>
 /// The self. If self is the last one in the list, return null
 /// </returns>
 public Node DeleteSelfFromLinkedList()
 {
     if (Next == null) {
         return null;
     } else {
         this.Data = Next.Data;
         this.Next = Next.Next;
     }
     return this;
 }
 public void AppendNewNode(Node newNode)
 {
     if (Head == null) {
         Head = newNode;
     } else {
         Node n = Head;
         while(n.Next!=null){
             n=n.Next;
         }
         n.Next = newNode;
     }
 }
 //For Pop to be operate in O(1) time, need to use an extra sorted List.
 //And when pop, check if the pop one is the minimum, if so, assign new minimum from the list.
 public override Node Pop()
 {
     Node n = base.Pop ();
     if (minimum  == n && minimum != null && top != null) {
         minimum = top;
         Node temp = top;
         while(temp.Next!=null){
             if(temp.Next.Data<minimum.Data){
                 minimum=temp.Next;
             }
             temp = temp.Next;
         }
     }
     return n;
 }
 public void DeleteNodeByValue(int data)
 {
     if (Head != null && Head.Data == data) {
         Head = Head.Next;
     } else if (Head != null) {
         Node n = Head;
         while(n.Next!=null){
             if(n.Next.Data==data){
                 n.Next = n.Next.Next;
             }
             else{
                 n=n.Next;
             }
         }
     }
 }
 public virtual void Push(Node n)
 {
     n.Next = top;
     top = n;
 }
 public NodeStack()
 {
     top = null;
 }
Example #9
0
        static void TestRemoveNodeFromLinkedList()
        {
            Node head = new Node (1);
            LinkedList list = new LinkedList (head);
            list.AppendNewNode (new Node (2));
            list.AppendNewNode (new Node (3));
            list.AppendNewNode (new Node (4));
            Node five = new Node(5);
            list.AppendNewNode (five);
            list.AppendNewNode (new Node (2));
            list.AppendNewNode (new Node (7));
            list.AppendNewNode (new Node (8));
            list.ToString ();
            //Node newHead = head.DeleteSelfFromLinkedList ();
            //while (newHead!=null) {
            //	Console.Write(newHead.Data);
            //	Console.Write(" ");
            //	newHead = newHead.Next;
            //}
            //Console.WriteLine(" ");

            five.DeleteSelfFromLinkedList();
            list.ToString();

            //Test single node list
            Node one = new Node(2);
            LinkedList list2 = new LinkedList(one);
            list2.Head = one.DeleteSelfFromLinkedList();
            list2.ToString();
        }
Example #10
0
        //Chapter 2
        static void TestLinkedList()
        {
            Node head= new Node(1);
            LinkedList list = new LinkedList(head);
            list.AppendNewNode(new Node(2));
            list.AppendNewNode(new Node(3));
            list.AppendNewNode(new Node(4));
            list.AppendNewNode(new Node(5));
            list.AppendNewNode(new Node(2));
            list.AppendNewNode(new Node(7));
            list.AppendNewNode(new Node(8));
            list.ToString();

            list.RemoveDuplicateNode();
            list.ToString();

            list.AppendNewNode(new Node(2));
            list.AppendNewNode(new Node(7));
            list.ToString();

            list.RemoveDuplicateNodeWithoutHashTable();
            list.ToString();

            //Find the Node which is Nth to the last node
            Node result = list.FindTheNthToLastNode(4);
            Console.WriteLine(result.Data);
            result = list.FindTheNthToLastNode(14);
            Console.WriteLine(result == null);

            result = list.FindTheNthToLastNode(0);
            Console.WriteLine(result.Data);

            result = list.FindTheNthToLastNode(1);
            Console.WriteLine(result.Data);

            Node s = null;
            LinkedList list2 = new LinkedList(s);
            result = list2.FindTheNthToLastNode(2);
            Console.WriteLine(result == null);
        }
Example #11
0
 static void TestCircularList()
 {
     Node start = new Node (99);
     LinkedList list1 = new LinkedList (new List<Node>{
         new Node(3),
         new Node(0),
         new Node(5),
         new Node(6),
         new Node(7),
         new Node(9),
         new Node(12),
         new Node(33),
         new Node(11),
         new Node(13),
         new Node(54)
     });
     var list = new List<Node>{
         new Node(3),
         new Node(0),
         new Node(5),
         new Node(6),
         new Node(7),
         new Node(9),
         new Node(12),
         new Node(33),
         new Node(11),
         new Node(13),
         new Node(54),
     };
     Node h = list1.Head;
     while (h.Next!=null) {
         h = h.Next;
     }
     h.Next = start;
     h = h.Next;
     foreach (var node in list) {
         h.Next = node;
         h=h.Next;
     }
     h.Next = start;
     var test1 = new GetCircularStartNodeInList(list1);
     test1.Run();
 }
Example #12
0
 public Node(int data, Node next = null)
 {
     Data = data;
     Next = null;
 }
 public LinkedList(Node node)
 {
     Head = node;
 }
 public bool Run()
 {
     Node head = new Node (-1);
     Node h = head;
     int addition = 0;
     if (listOne.Head == null || listTwo.Head == null) {
         if(listOne.Head == null){
             head = listTwo.Head;
         }
         else {
             head = listOne.Head;
         }
     } else {
         Node head1 = listOne.Head;
         Node head2 = listTwo.Head;
         head.Data = listOne.Head.Data + listTwo.Head.Data;
         if(head.Data >= 10){
             // listOne and listTwo's nodes' data should all be less than 10
             head.Data = head.Data-10;
             addition = 1;
         }
         else{
             addition = 0;
         }
         while(head1.Next!=null && head2.Next!=null){
             int newData = head1.Next.Data + head2.Next.Data + addition;
             if(newData>=10){
                 newData -= 10;
                 addition = 1;
             }
             else{
                 addition = 0;
             }
             Node newNode = new Node(newData);
             h.Next = newNode;
             h = h.Next;
             head1 = head1.Next;
             head2 = head2.Next;
         }
         if(head1.Next==null && head2.Next == null){
             if(addition == 1){
                 Node newNode = new Node(1);
                 h.Next = newNode;
             }
         }
         else if(head1.Next==null){
             while(head2.Next!=null){
                 int newData = head2.Next.Data + addition;
                 if(newData>=10){
                     newData -= 10;
                     addition = 1;
                 }
                 else{
                     addition = 0;
                 }
                 Node newNode = new Node(newData);
                 h.Next = newNode;
                 h = h.Next;
                 head2 = head2.Next;
             }
         } else if(head2.Next == null) {
             while(head1.Next!=null){
                 int newData = head1.Next.Data + addition;
                 if(newData>=10){
                     newData -= 10;
                     addition = 1;
                 }
                 else{
                     addition = 0;
                 }
                 Node newNode = new Node(newData);
                 h.Next = newNode;
                 h = h.Next;
                 head1 = head1.Next;
             }
         }
     }
     Console.WriteLine("List Addition Result: ");
     Node n = head;
     while (n!=null) {
         Console.Write(n.Data);
         Console.Write(" ");
         n=n.Next;
     }
     Console.WriteLine(" ");
     return true;
 }
 public void Enqueue(Node newNode)
 {
     tail.Push(newNode);
 }