Пример #1
0
        //Utility functions for managing a Linked List
        //Not what MS thinks is a "List".  A real list, dammit.

        //Add a new node as the pos'th item.  0 means add to front,
        // -1 or a pos greater than the last item means add to end.
        public static Node Add(Node head, string value, int pos)
        {
            //Create my new Node.
            Node newNode = new Node();
            newNode.Value = value;

            //Am I adding to head?
            if (head == null || pos == 0)
            {
                newNode.Next = head;
                if (head != null) head.Previous = newNode;
                newNode.Previous = null;
                return newNode;
            }

            //Find the position I care about.
            int position = 0;
            Node current = head;
            while (current.Next != null && (position < pos - 1 || pos == -1))
            {
                position++;
                current = current.Next;
            }
            //At this point, I should be looking at either the last node,
            //Or the node before position pos.  Insert here.
            newNode.Next = current.Next;
            newNode.Previous = current;
            current.Next = newNode;
            return head;
        }
Пример #2
0
        static public Node Pop(Node stack, out string value)
        {
            if (stack == null)
            {
                value = "";
                return null;
            }

            value = stack.Value;
            Node newTop = stack.Next;
            if (newTop != null) newTop.Previous = null;
            return newTop;
        }
Пример #3
0
        public static Node InsertAlphabetical(Node head, string value)
        {
            //Insert at head?
            if (head == null || string.Compare(head.Value, value) == 1)
            {
                return Add(head, value, 0);
            }
            //Otherwise, find insertion point

            Node current = head;
            int pos = 1;
            while (current.Next != null && (string.Compare(current.Next.Value, value) < 1))
            {
                pos++;
                current = current.Next;
            }
            return Add(head, value, pos);

        }
Пример #4
0
 static public string Peek(Node stack)
 {
     if (stack == null) throw new NullReferenceException("Stack is empty");
     return stack.Value;
 }
Пример #5
0
 static public Node Push(Node stack,string value)
 {
     return List.Add(stack, value, 0);
 }
Пример #6
0
 static public string Peek(Node queue)
 {
     return Stack.Peek(queue);
 }
Пример #7
0
 static public Node Dequeue(Node queue, out string value)
 {
     return Stack.Pop(queue, out value);
 }
Пример #8
0
 static public Node Enqueue(Node queue, string value)
 {
     //Add to tail
     return List.Add(queue, value, -1);
 }
Пример #9
0
 public static Node GetTail(Node head)
 {
     //Recursive, cause it's fun
     if (head == null || head.Next == null) return head;
     return GetTail(head.Next);
 }
Пример #10
0
 public static void PrintBackward(Node tail)
 {
     if (tail == null) { Console.WriteLine(); return; }
     Console.Write(tail.Value + " ");
     PrintBackward(tail.Previous);
 }
Пример #11
0
 //Print forward.  Recursive
 public static void PrintForward(Node head)
 {
     if (head == null) { Console.WriteLine(); return; }
     Console.Write(head.Value + " ");
     PrintForward(head.Next);
 }