Beispiel #1
0
        public int Remove()
        {
            int value;

            if (_head == null)
            {
                throw new IndexOutOfRangeException("List is empty");
            }
            else if (_head.Next == null)
            {
                value = _head.Value;
                _head = null;
            }
            else
            {
                SLNode runner = _head;
                while (runner.Next.Next != null)
                {
                    runner = runner.Next;
                }
                value       = runner.Next.Value;
                runner.Next = null;
            }
            _length--;
            return(value);
        }
Beispiel #2
0
        public void Delete(int target)
        {
            Console.WriteLine("Attempting to delete {0}...\n", target);
            while (Console.ReadKey().Key != ConsoleKey.Enter)
            {
            }
            SLNode targetNode = null;
            SLNode cur        = header;
            bool   deleted    = false;

            for (int i = maxLevel - 1; i >= 0; i--)
            {
                Console.WriteLine("On level {0}\nOn header", i);
                while (cur.forward[i].key < target)
                {
                    Console.WriteLine("On node  {0}", cur.forward[i].key);
                    cur = cur.forward[i];
                }

                if (cur.forward[i].key == target)
                {
                    Console.WriteLine("Target found at level {0}", i);
                    targetNode     = cur.forward[i];
                    cur.forward[i] = targetNode.forward[i];
                    Console.WriteLine("Spliced out target at level {0}\n", i);
                    while (Console.ReadKey().Key != ConsoleKey.Enter)
                    {
                    }
                    deleted = true;
                }

                if (!deleted)
                {
                    Console.WriteLine("Target not found on level {0}\n", i);
                    while (Console.ReadKey().Key != ConsoleKey.Enter)
                    {
                    }
                }
            }

            Console.WriteLine("\nLoop complete, all levels checked");

            if (deleted)
            {
                Console.WriteLine("target deleted, Delete() complete\n");
                while (Console.ReadKey().Key != ConsoleKey.Enter)
                {
                }
                count--;
            }
            else
            {
                Console.WriteLine("target not found, Delete() complete\n");
                while (Console.ReadKey().Key != ConsoleKey.Enter)
                {
                }
            }

            PrintGraphicalList();
        }
 public SLNode(int val)
 {
     Value  = val;
     Next   = null;
     Child  = null;
     isTail = false;
 }
Beispiel #4
0
        public int RemoveAt(int index)
        {
            int value;

            if (index < 0 || index >= _length)
            {
                throw new IndexOutOfRangeException("Index not in list.");
            }
            else if (index == _length - 1)
            {
                return(this.Remove());
            }
            else if (index == 0)
            {
                value = _head.Value;
                _head = _head.Next;
            }
            else
            {
                SLNode runner = _head;
                while (index != 1)
                {
                    runner = runner.Next;
                    index--;
                }
                value       = runner.Next.Value;
                runner.Next = runner.Next.Next;
            }
            _length--;
            return(value);
        }
Beispiel #5
0
        public void PrintListContents()
        {
            SLNode cur = header;

            while (cur.forward[0].forward[0] != null)
            {
                Console.WriteLine("key: {0} height: {1}", cur.forward[0].key, cur.forward[0].height);
                cur = cur.forward[0];
            }
        }
Beispiel #6
0
        public void PrintGraphicalList()
        {
            Console.WriteLine("SkipList visual:\n");
            SLNode cur = header;

            int[]         items         = new int[count];
            int[]         lengths       = new int[count];
            int[]         heights       = new int[count];
            int           counter       = 0;
            StringBuilder stringBuilder = new StringBuilder();

            while (cur.forward[0].forward[0] != null)
            {
                items[counter] = cur.forward[0].key;

                if (cur.forward[0].key < 10)
                {
                    lengths[counter] = 1;
                }
                else if (cur.forward[0].key < 100)
                {
                    lengths[counter] = 2;
                }
                else
                {
                    lengths[counter] = 3;
                }

                heights[counter] = cur.forward[0].height;

                counter++;
                cur = cur.forward[0];
            }

            for (int i = maxLevel; i > 0; i--)
            {
                stringBuilder.Append("[]");

                for (int j = 0; j < count; j++)
                {
                    if (heights[j] >= i)
                    {
                        stringBuilder.Append("-" + items[j] + "-");
                    }
                    else
                    {
                        stringBuilder.Append(new String('-', lengths[j] + 2));
                    }
                }
                stringBuilder.Append("[]\n");
            }

            stringBuilder.Append("\n");
            Console.Write(stringBuilder);
        }
Beispiel #7
0
        public override string ToString()
        {
            StringBuilder sb     = new StringBuilder();
            SLNode        runner = _head;

            while (runner != null)
            {
                sb.Append($"{runner} -> ");
                runner = runner.Next;
            }
            return(sb.ToString());
        }
Beispiel #8
0
        public SkipList(int maxLevel)
        {
            this.maxLevel = maxLevel;

            // SL will contain values between 0 and 999
            // Warning - nothing has been done to ensure this
            header   = new SLNode(-1, maxLevel);
            sentinel = new SLNode(1000, maxLevel);

            for (int i = 0; i < maxLevel; i++)
            {
                header.forward[i] = sentinel;
            }
        }
Beispiel #9
0
        static void Main(string[] args)
        {
            SLL list1 = new SLL();

            list1.Add(1);
            list1.Add(2);
            list1.Add(3);
            list1.Add(4);
            list1.Add(5);
            SLNode someNode = list1.Head.Next;
            SLNode tail     = list1.Head.Next.Next.Next.Next;

            tail.Next = someNode;
            // list1.Print();
            Console.WriteLine(list1.HasLoop());
        }
Beispiel #10
0
        public static void DisplayAllList()
        {
            Console.WriteLine("****** SkipList ******");

            for (int i = 0; i <= maxLevel - 1; i++)
            {
                SLNode node = header.forward[i];
                Console.WriteLine("Level  {0}", i);
                Console.WriteLine("ID   Names");
                while (node.forward[i] != null)
                {
                    Console.WriteLine("{0} {1}", node.key, node.name);
                    node = node.forward[i];
                }
                Console.WriteLine(" ");
            }
        }
Beispiel #11
0
        public static void PrintList()
        {
            SLNode node = header.forward[0];

            Console.WriteLine("****** EmployeeList ******");
            Console.WriteLine("ID   Names");

            for (int i = 0; i <= maxLevel - 1; i++)
            {
                while (node.forward[i] != null)
                {
                    Console.WriteLine("{0} {1}", node.key, node.name);
                    node = node.forward[i];
                }
            }
            Console.WriteLine(" ");
        }
Beispiel #12
0
 public void Add(int value)
 {
     if (_head == null)
     {
         _head = new SLNode(value);
     }
     else
     {
         SLNode runner = _head;
         while (runner.Next != null)
         {
             runner = runner.Next;
         }
         runner.Next = new SLNode(value);
     }
     _length++;
 }
Beispiel #13
0
        public bool Search(int searchKey)
        {
            Console.WriteLine("Searching for key {0}...\n", searchKey);
            while (Console.ReadKey().Key != ConsoleKey.Enter)
            {
            }
            SLNode cur = header;

            for (int i = maxLevel - 1; i >= 0; i--)
            {
                Console.WriteLine("On level {0}\nOn header", i);

                while (cur.forward[i].key < searchKey)
                {
                    Console.WriteLine("On node  {0}", cur.forward[i].key);
                    cur = cur.forward[i];
                }
            }

            Console.WriteLine("\nLoop complete, all levels checked");
            while (Console.ReadKey().Key != ConsoleKey.Enter)
            {
            }

            cur = cur.forward[0];

            Console.WriteLine("Seeing if next item {0} matches searchKey", cur.key);

            if (cur.key == searchKey)
            {
                Console.WriteLine("Found match, Search() complete\n");
                while (Console.ReadKey().Key != ConsoleKey.Enter)
                {
                }
                return(true);
            }
            else
            {
                Console.WriteLine("Match not found, Search() complete\n");
                while (Console.ReadKey().Key != ConsoleKey.Enter)
                {
                }
                return(false);
            }
        }
Beispiel #14
0
        public SkipListConsoleOutput(int maxLevel)
        {
            Console.WriteLine("SkipList with max height" +
                              " {0} created\n", maxLevel);

            this.maxLevel = maxLevel;

            // SL will contain positive values between 0 and 100
            header   = new SLNode(-1, maxLevel);
            sentinel = new SLNode(1000, maxLevel);

            for (int i = 0; i < maxLevel; i++)
            {
                header.forward[i] = sentinel;
            }

            PrintGraphicalList();
        }
Beispiel #15
0
        public int Find(int val)
        {
            int index = 0;

            if (_head != null)
            {
                SLNode runner = _head;
                while (runner != null)
                {
                    if (runner.Value == val)
                    {
                        return(index);
                    }
                    index++;
                    runner = runner.Next;
                }
            }
            return(-1);
        }
Beispiel #16
0
        public void Delete(int target)
        {
            SLNode targetNode;
            SLNode cur = header;

            for (int i = maxLevel - 1; i >= 0; i--)
            {
                while (cur.forward[i].key < target)
                {
                    cur = cur.forward[i];
                }

                // splice out
                if (cur.forward[i].key == target)
                {
                    targetNode     = cur.forward[i];
                    cur.forward[i] = targetNode.forward[i];
                }
            }
        }
Beispiel #17
0
        public bool Search(int searchKey)
        {
            SLNode cur = header;

            for (int i = maxLevel - 1; i >= 0; i--)
            {
                while (cur.forward[i].key < searchKey)
                {
                    cur = cur.forward[i];
                }
            }

            cur = cur.forward[0];

            if (cur.key == searchKey)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Beispiel #18
0
        public void Insert(int searchKey)
        {
            SLNode sLNode = new SLNode(searchKey, GenerateLevel());

            SLNode[] update = new SLNode[maxLevel];
            SLNode   cur    = header;

            for (int i = maxLevel - 1; i >= 0; i--)
            {
                while (cur.forward[i].key < searchKey)
                {
                    cur = cur.forward[i];
                }

                update[i] = cur;
            }

            // stitch in
            for (int i = 0; i < sLNode.height; i++)
            {
                sLNode.forward[i]    = update[i].forward[i];
                update[i].forward[i] = sLNode;
            }
        }
Beispiel #19
0
        public static void Main(string[] args)
        {
            maxLevel = 5; // this sets the max leave of the skip list

            // SL will contain values between 1000 and 9999
            // Warning - nothing has been done to ensure this
            header   = new SLNode(1000, "Start of list", maxLevel);
            sentinel = new SLNode(9999, "End of List", maxLevel);

            for (int i = 0; i < maxLevel; i++)
            {
                header.forward[i] = sentinel;
            }

            // Data //
            Insert(1015, "Office_Lady");
            Insert(1050, "Desk_Gentleman");
            Insert(1020, "New_Girl");
            Insert(1001, "Boss_Man");
            Insert(1070, "Boss_Woman");
            Insert(1060, "Office_Clown");
            Insert(1040, "Cat_Man");

            // UI //
            String userResponse = "";

            Console.WriteLine("Type COMMANDS to get a list of commands.");

            while (userResponse != "end")
            {
                Console.Write("//");
                userResponse = Console.ReadLine().ToLower();

                switch (userResponse)
                {
                case "?":
                case "help":
                case "commands":
                    Console.WriteLine("List of Commands:");
                    Console.WriteLine("");
                    Console.WriteLine("COMMANDS");
                    Console.WriteLine("     List commands availible.");
                    Console.WriteLine("LIST");
                    Console.WriteLine("     List all employees in the system.");
                    Console.WriteLine("LIST_ALL");
                    Console.WriteLine("     List all layers of the SkipList.");
                    Console.WriteLine("ADD");
                    Console.WriteLine("     Add a new employee and ID Number");
                    Console.WriteLine("REMOVE");
                    Console.WriteLine("     Delete an employee by their ID Number");
                    Console.WriteLine("FIND");
                    Console.WriteLine("     Looks through the list of employees to see if a certain ID Number is in uses.");
                    Console.WriteLine("END");
                    Console.WriteLine("     End program.");
                    break;

                case "list":
                    PrintList();
                    break;

                case "list_all":
                    DisplayAllList();
                    break;

                case "add":
                    Console.WriteLine("What is the new employee's ID? (must between 1000 and 9999 in addtion to not already being in uses.)");
                    Console.Write("//");
                    int newID = int.Parse(Console.ReadLine());

                    while (newID < 1000 || newID > 10000)
                    {
                        Console.WriteLine("Employee's ID {0} is out of range!!", newID);
                        Console.WriteLine("Try another ID number.");
                        Console.Write("//");
                        newID = int.Parse(Console.ReadLine());
                    }

                    while (Search(newID) == true)
                    {
                        Console.WriteLine("Employee with the ID {0} already exist!!!", newID);
                        Console.WriteLine("Try another ID number.");
                        Console.Write("//");
                        newID = int.Parse(Console.ReadLine());
                    }

                    Console.WriteLine("New Employee's name?");
                    Console.Write("//");
                    String newName = Console.ReadLine();
                    Insert(newID, newName);
                    Console.WriteLine("New Employee added.");
                    PrintList();
                    break;

                case "remove":
                    PrintList();
                    Console.WriteLine("What is the Employee's ID that is to be deleted?");
                    int deleteID = int.Parse(Console.ReadLine());
                    if (Search(deleteID))
                    {
                        Delete(deleteID);
                        Console.WriteLine("Employee with the ID of {0} removed.", deleteID);
                    }
                    else
                    {
                        Console.WriteLine("Employee with the ID of {0} could not be found.", deleteID);
                    }
                    break;

                case "find":
                    Console.WriteLine("Type in Employee's ID");
                    Console.Write("//");
                    int searchID = int.Parse(Console.ReadLine());
                    while (searchID < 1000 || searchID > 10000)
                    {
                        Console.WriteLine("Employee ID {0} is out of range!!", searchID);
                        Console.WriteLine("Try another ID number.");
                        Console.Write("//");
                        newID = int.Parse(Console.ReadLine());
                    }
                    if (Search(searchID))
                    {
                        Console.WriteLine("An Employee with the ID {0} exists", searchID);
                    }
                    else
                    {
                        Console.WriteLine("Did not find an Employee with the ID {0}", searchID);
                    }
                    break;

                case "end":
                    break;

                default:
                    Console.WriteLine("Command Unkown");
                    Console.WriteLine("");
                    break;
                }
            }

            Console.WriteLine("Program END");
        }
Beispiel #20
0
 ///<summary>Set <see cref="SLNode"/> in <see cref="Next"/> array.</summary>
 ///<param name="index">Index where new node will be inserted</param>
 ///<param name="next">New node to insert</param>
 ///<exception cref="IndexOutOfBoundsException"/>
 public void SetNext(int index, SLNode <T> next)
 {
     Next[index] = next;
 }
Beispiel #21
0
 public SLList()
 {
     _head   = null;
     _length = 0;
 }
Beispiel #22
0
 public SLNode(int value, SLNode next = null)
 {
     Value = value;
     Next  = next;
 }
Beispiel #23
0
        public void Insert(int searchKey)
        {
            Console.WriteLine("Inserting key {0}...\n", searchKey);
            while (Console.ReadKey().Key != ConsoleKey.Enter)
            {
            }
            SLNode[] update = new SLNode[maxLevel];
            SLNode   sLNode = new SLNode(searchKey, GenerateLevel());
            SLNode   cur    = header;

            for (int i = maxLevel - 1; i >= 0; i--)
            {
                Console.WriteLine("On level {0}", i);

                while (cur.forward[i].key < searchKey)
                {
                    cur = cur.forward[i];
                    Console.WriteLine("cur set to {0}", cur.key);
                }

                update[i] = cur;

                if (cur.key == -1)
                {
                    Console.WriteLine("Set update[{0}] to header\n", i);
                }
                else
                {
                    Console.WriteLine("Set update[{0}] to node {1}\n", i, cur.key);
                }

                while (Console.ReadKey().Key != ConsoleKey.Enter)
                {
                }
            }

            // stitch it in
            Console.WriteLine("Splicing in new node...\n");
            while (Console.ReadKey().Key != ConsoleKey.Enter)
            {
            }
            for (int i = 0; i < sLNode.height; i++)
            {
                if (update[i].forward[i].key == 1000)
                {
                    Console.WriteLine("Set new node's forward[{0}] to sentinel", i);
                }
                else
                {
                    Console.WriteLine("Set new node's forward[{0}] to node {1}", i, update[i].forward[i].key);
                }

                sLNode.forward[i] = update[i].forward[i];

                if (update[i].key == -1)
                {
                    Console.WriteLine("Set header's forward[{0}] to new node\n", i);
                }
                else
                {
                    Console.WriteLine("Set node {0}'s forward[{1}] to new node\n", update[i].key, i);
                }

                while (Console.ReadKey().Key != ConsoleKey.Enter)
                {
                }
                update[i].forward[i] = sLNode;
            }
            count++;
            Console.WriteLine("Insert() complete\n");
            while (Console.ReadKey().Key != ConsoleKey.Enter)
            {
            }
            PrintGraphicalList();
        }