Example #1
0
        public void InsertAfter(SNode prev_node, object data)
        {
            if (prev_node == null)
            {
                Console.WriteLine("The given previous node Cannot be null");
                return;
            }
            SNode newNode = new SNode(data);

            newNode.Next   = prev_node.Next;
            prev_node.Next = newNode;
        }
Example #2
0
        public StackList ConvertCommaSeparatedToStack(string[] array)
        {
            StackList list = new StackList();

            for (int index = 0; index < array.Length; index++)
            {
                SNode snode = new SNode();
                snode.Value = Convert.ToInt32(array[index]);
                list.Push(snode);
            }
            return(list);
        }
Example #3
0
        public void InsertLast(SingleLinkedList linkedList, object data)
        {
            SNode newNode = new SNode(data);

            if (linkedList.Head == null)                // listede node yoksa ??
            {
                linkedList.Head = newNode;              // listenin başını yeni node yap.
                return;
            }
            SNode lastNode = GetLastNode(linkedList);   //listede node varsa ??

            lastNode.Next = newNode;                    // listedeki son nodenin nextine --->> yeni nodeyi ekle.
        }
Example #4
0
        public SNode Pop()
        {
            if (Length <= 0)
            {
                return(null);
            }
            SNode node = new SNode();

            node = root;
            root = root.Next;
            Length--;
            return(node);
        }
Example #5
0
        public StackList ConvertToStack(char[] array)
        {
            SNode     node;
            StackList stack = new StackList();

            for (int index = 0; index < array.Length; index++)
            {
                node     = new SNode();
                node.Val = array[index];
                stack.Push(node);
            }
            return(stack);
        }
Example #6
0
        private void btnInfixToPostfix_Click(object sender, EventArgs e)
        {
            string expression = txtArray1.Text;

            char[]    array     = expression.ToCharArray();
            StackList stackList = new StackList();
            SNode     node;

            char[] result = new char[array.Length];
            int    rIndex = 0, index = 0;

            while (index < array.Length)
            {
                if (array[index] >= 'a' && array[index] <= 'z')
                {
                    result[rIndex] = array[index];
                    rIndex++;
                }
                else if (array[index] == '/' || array[index] == '*' || array[index] == '^' ||
                         array[index] == '+' || array[index] == '-')
                {
                    node     = new SNode();
                    node.Val = array[index];
                    if (stackList.Length == 0)
                    {
                        stackList.Push(node);
                    }
                    else
                    {
                        int diff = GetOperatorValue(node.Val) - GetOperatorValue(stackList.Peek().Val);
                        if (diff > 0)
                        {
                            stackList.Push(node);
                        }
                        else
                        {
                            result[rIndex] = stackList.Pop().Val;
                            rIndex++;
                            continue;
                        }
                    }
                }
                index++;
            }
            while (stackList.Length != 0)
            {
                result[rIndex] = stackList.Pop().Val;
                rIndex++;
            }
            txtResult.Text = string.Join("", result);
        }
Example #7
0
        private void btnEvalPostfix_Click(object sender, EventArgs e)
        {
            char[] array = txtArray1.Text.ToCharArray();
            //abc*+b-
            int       index = 0, num, op1, op2;
            StackList list = new StackList();
            SNode     node;
            bool      isDigit;

            while (index < array.Length)
            {
                isDigit = int.TryParse(array[index].ToString(), out num);
                if (isDigit)
                {
                    node       = new SNode();
                    node.Value = num;
                    list.Push(node);
                }
                else
                {
                    op1  = list.Pop().Value;
                    op2  = list.Pop().Value;
                    node = new SNode();
                    switch (array[index])
                    {
                    case '/':
                        node.Value = op2 / op1;
                        break;

                    case '*':
                        node.Value = op2 * op1;
                        break;

                    case '+':
                        node.Value = op2 + op1;
                        break;

                    case '-':
                        node.Value = op2 - op1;
                        break;

                    case '^':
                        node.Value = op2 ^ op1;
                        break;
                    }
                    list.Push(node);
                }
                index++;
            }
            txtResult.Text = Convert.ToString(list.Peek().Value);
        }
Example #8
0
        public static SNode Reverse(SNode head)
        {
            SNode curr = head;
            SNode next = head;
            SNode prev = null;

            while (curr != null)
            {
                next      = next.next;
                curr.next = prev;
                prev      = curr;
                curr      = next;
            }

            return(prev);
        }
Example #9
0
 public string[] ConvertStackToArray(StackList list)
 {
     string[] array = new string[list.Length];
     for (int index = array.Length - 1; index >= 0; index--)
     {
         SNode node = list.Pop();
         if (String.IsNullOrWhiteSpace(Convert.ToString(node.Val)))
         {
             array[index] = node.Val.ToString();
         }
         else
         {
             array[index] = node.Value.ToString();
         }
     }
     return(array);
 }
Example #10
0
        public void DeletedNodeByKey(SingleLinkedList linkedList, object key)
        {
            SNode temp = linkedList.Head;
            SNode prev = null;

            if (temp != null && temp.Data == key)
            {
                linkedList.Head = temp.Next;
                return;
            }
            while (temp != null && temp.Data != key)
            {
                prev = temp;
                temp = temp.Next;
            }
            if (temp == null)
            {
                return;
            }
            prev.Next = temp.Next;
        }
Example #11
0
        public static SNode DeleteAll(SNode head, int data)
        {
            SNode curr = head;
            SNode prev = null;

            // Delete all nodes with the given data.
            while (curr != null)
            {
                // If the current node matches the data:
                if (curr.data == data)
                {
                    // If head node:
                    if (prev == null)
                    {
                        // Skip over the node.
                        head = curr.next;
                    }
                    // If any other node:
                    else
                    {
                        // Update the incoming reference to the current node
                        // to skip over the node.
                        prev.next = curr.next;
                    }
                }
                // Current node does not match data:
                else
                {
                    // Update the previous node to be current node.
                    prev = curr;
                }

                // Move to the next node.
                curr = curr.next;
            }

            return(head);
        }
Example #12
0
        public void DeleteLast(SingleLinkedList linkedList)
        {
            SNode SecondLastNode = GetSecondLastNode(linkedList);

            SecondLastNode.Next = null;
        }
Example #13
0
 public SNode(int SNodeData)
 {
     data = SNodeData;
     next = null;
 }
Example #14
0
 public MyLinkedList()
 {
     headNode = null;
 }
Example #15
0
 public static void InsertAfter(SNode node, SNode a)
 {
     a.next    = node.next;
     node.next = a;
 }
Example #16
0
 public SNode(object Data)
 {
     this.Data = Data;
     this.Next = null;
 }