public static DLList initialArr(String filePath)
        {
            FileStream f    = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            DLList     pDLL = new DLList();

            string line;

            try
            {
                StreamReader m_streamReader = new StreamReader(f);
                //StreamReader read txt
                m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                //read from head to tail by lines
                while ((line = m_streamReader.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                    // process every line
                    DLLNode p = new DLLNode(line);
                    pDLL.addToTail(p);
                }
                // Console.WriteLine("===============");
                m_streamReader.Close();
            }

            catch (FileNotFoundException e)
            {
                Console.WriteLine("errors" + e.ToString());
            }
            return(pDLL);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Display the DDList:");
            DLList p = initialArr("C:/Users/edgu1/Desktop/222525.txt");

            Console.WriteLine("///////////////////////////////////");

            Console.WriteLine("Enter the search number:");
            string  sn   = Console.ReadLine();
            DLLNode node = search(p, sn);

            Console.WriteLine((node == null) ? "This number is not exist!" : ("The position of the  " + node.str + " " + "is  " + node.number));
            Console.WriteLine("///////////////////////////////////");

            Console.WriteLine("Removed number:");
            DLLNode nodehead = removeHead(p);

            Console.WriteLine(nodehead.str);
            Console.WriteLine("Display the current DDList:");
            printsAppearNumbers(p);
            Console.WriteLine("///////////////////////////////////");

            /*DLLNode nodetail = removeTail(p);
             * Console.WriteLine(nodetail);*/
            Console.WriteLine("Enter the node the you want to remove,please:");
            string  r       = Console.ReadLine();
            DLLNode delnode = new DLLNode(r);

            removeNode(p, delnode);
            Console.WriteLine("Display the current DDList:");
            printsAppearNumbers(p);
            Console.WriteLine("///////////////////////////////////");
        }
示例#3
0
        } // removeHead

        public void removeTail()
        {
            /* Changed this code as this would not remove the tail
             * Original code:
             * if (this.head == this.tail)
             * {
             * this.head = null;
             * this.tail = null;
             * return;
             * }
             * To this below: */

            if (head == null)
            {
                return;
            }
            else if (head != tail)
            {
                tail      = tail.previous;
                tail.next = null;
            }
            else
            {
                head = tail = null;
            }
        } // remove tail
        }                                             // constructor

        /*-------------------------------------------------------------------
         * The methods below includes several errors. Your  task is to write
         * unit test to discover these errors. During delivery the tutor may
         * add or remove errors to adjust the scale of the effort required by
         */
        public void addToTail(DLLNode p)
        {
            //changed code
            if (head == null)
            {
                head = p;
                tail = p;
            }
            else
            {
                tail.next  = p;
                p.previous = tail;
                p.next     = null;
                tail       = p;
            }

            /*if (head == null)
             * {
             *  head = p;
             *  tail = p;
             * }
             * else
             * {
             *  tail.next = p;
             *  tail = p;
             *  p.previous = tail;
             * }*/
        } // end of addToTail
        } // end of search (return pionter to the node);

        public DLLNode removeNode(DLLNode p)
        { // removing the node p.
            DLLNode rnode = null;

            if (p != null)
            {
                p = search(p.str);
                if (p != null)
                {
                    rnode = p;
                    if (p.next == null)
                    {
                        p              = this.tail;
                        this.tail      = this.tail.previous;
                        this.tail.next = null;
                        p.previous     = null;
                        return(rnode);
                    }
                    if (p.previous == null)
                    {
                        p                  = this.head;
                        this.head          = this.head.next;
                        p.next             = null;
                        this.head.previous = null;
                        return(rnode);
                    }
                    p.next.previous = p.previous;
                    p.previous.next = p.next;
                    p.next          = null;
                    p.previous      = null;
                }
            }
            ordered();
            return(rnode);
        } // end of remove a node
        } // removeHead

        public void removeTail()
        {
            //change code
            if (this.tail == null)
            {
                return;
            }
            if (this.head == this.tail)
            {
                this.head = null;
                this.tail = null;
                return;
            }
            this.tail      = this.tail.previous;
            this.tail.next = null;
            return;

            /*if (this.tail == null) return;
             * if (this.head == this.tail)
             * {
             *  this.head = null;
             *  this.tail = null;
             *  return;
             * }*/
        } // remove tail
        } // end of search (return pionter to the node);

        public void removeNode(DLLNode p)
        {                                       // removing the node p.
            //changed code
            if (p == null || this.head == null) //checking whether p is null or empty list
            {
                return;
            }

            if (this.head == p && this.tail == p) //when there is only one node in a list
            {
                this.head = null;
                this.tail = null;
                return;
            }

            if (p.next == null) //when p is last node
            {
                this.tail      = this.tail.previous;
                this.tail.next = null;
                p.previous     = null;
                return;
            }

            if (p.previous == null) //when p is the first node
            {
                this.head          = this.head.next;
                p.next             = null;
                this.head.previous = null;
                return;
            }

            //when p is in middle position
            p.previous.next = p.next;
            p.next.previous = p.previous;
            p.next          = null;
            p.previous      = null;
            return;

            /*if (p.next == null)
             * {
             *  this.tail = this.tail.previous;
             *  this.tail.next = null;
             *  p.previous = null;
             *  return;
             * }
             *
             * if (p.previous == null)
             * {
             *  this.head = this.head.next;
             *  p.next = null;
             *  this.head.previous = null;
             *  return;
             * }
             * p.next.previous = p.previous;
             * p.previous.next = p.next;
             * p.next = null;
             * p.previous = null;
             * return;*/
        } // end of remove a node
        } // remove tail

        /*-------------------------------------------------
         * Return null if the string does not exist.
         * ----------------------------------------------*/
        public void ordered()
        {
            int     count = 0;
            DLLNode p     = head;

            while (p != null)
            {
                count++;
                p.number = count;
                p        = p.next;
            }
        }
        } // end of removeNode()

        public int total()
        {
            DLLNode p     = head;
            int     total = 0;

            while (p != null)
            {
                total++;
                p = p.next;
            }
            return(total);
        } // end of total
        public static void printsAppearNumbers(DLList dll1)
        {
            DLLNode head = dll1.head;

            //  ArrayList  list1 = new ArrayList ();
            while (head.next != null)
            {
                Console.WriteLine(head.str);
                head = head.next;
            }
            Console.WriteLine(head.str);
        }
示例#11
0
        } // end of search (return pionter to the node);

        public void removeNode(DLLNode p)
        { // removing the node p.
          /* Cleaned this section, as I felt this code was too complicated for what it was trying to do.
           * Replaced with the following code: (Previous code used is commented out)*/

            DLLNode node = head;

            while (node.num != p.num)
            {
                //Tested using testRemoveNode1
                if (node.next == null)
                {
                    /* Original code:
                     * this.tail = this.tail.previous;
                     * this.tail.next = null;
                     * p.previous = null;
                     * return;*/
                    return;
                }
                else
                {
                    /* Original code:
                     * this.head = this.head.next;
                     * p.next = null;
                     * this.head.previous = null;
                     * return;*/

                    //Tested using testRemoveNode2
                    node = node.next;
                }
            }

            //Tested using testRemoveNode3
            if (node == head)
            {
                removeHead();
            }

            //Tested using testRemoveNode4
            else if (node == tail)
            {
                removeTail();
            }

            else
            {
                //Tested using testRemoveNode2
                node.previous.next = node.next;
                node.next.previous = node.previous;
            }
        } // end of remove a node
        } // end of search

        public void removeNode(DLLNode p)
        {
            DLLNode node = head;

            //base case (empty list)
            if (head == null)
            {
                return;
            }

            //find the node
            while (node.num != p.num)
            {
                if (node.next == null)
                {
                    return; //node doesn't exist
                }
                else
                {
                    node = node.next;
                }
            }

            //1 node list
            if (node == head && node == tail)
            {
                head = null;
                tail = null;
            }
            else if (node == head)

            //node is head
            {
                head          = head.next;
                head.previous = null;
            }
            else if (node == tail)

            //node is tail
            {
                tail      = tail.previous;
                tail.next = null;
            }
            else

            //node is neither head nor tail
            {
                node.next.previous = node.previous;
                node.previous.next = node.next;
            }
        } // end of removeNode()
        }                                              // constructor

        /*-------------------------------------------------------------------
         * The methods below includes several errors. Your  task is to write
         * unit test to discover these errors. During delivery the tutor may
         * add or remove errors to adjust the scale of the effort required by
         */
        public void addToTail(DLLNode p)
        {
            if (head == null)
            {
                head = p;
                tail = p;
            }
            else
            {
                tail.next  = p;
                tail       = p;
                p.previous = tail;
            }
        } // end of addToTail
        } // end of addToHead

        public DLLNode removeHead()
        {
            if (head == null)
            {
                return(null);
            }
            DLLNode p = head;

            head          = head.next;
            head.previous = null;
            p.next        = null;
            ordered();
            return(p);
        } // removeHead
        } // end of addToTail

        public void addToHead(DLLNode p)
        {
            if (head == null)
            {
                head = p;
                tail = p;
            }
            else
            {
                p.next             = this.head;
                this.head.previous = p;
                head = p;
            }
        } // end of addToHead
        } // removeHead

        public DLLNode removeTail()
        {
            if (this.tail == null)
            {
                return(null);
            }
            DLLNode p = tail;

            tail       = tail.previous;
            p.previous = null;
            tail.next  = null;
            ordered();
            return(p);
        } // remove tail
示例#17
0
        } // end of addToTail

        public void addToHead(DLLNode p)
        {
            //Simplified code by removing "this."
            if (head == null)
            {
                head = p;
                tail = p;
            }
            else
            {
                p.next        = head;
                head.previous = p;
                head          = p;
            }
        } // end of addToHead
        } // end of addToTail

        public void addToHead(DLLNode p)
        {
            if (head == null)
            {
                head = p;
                tail = p;
            }
            else
            {
                p.next        = head;
                head.previous = p;
                head          = p;
            }
            return;
        } // end of addToHead
示例#19
0
        } // end of remove a node

        public int total()
        {
            DLLNode p   = head;
            int     tot = 0;

            while (p != null)
            {
                /* Original code:
                 * tot += p.num;
                 * p = p.next.next;
                 *
                 * Changed to the code below :*/
                p = p.next;
                tot++;
            }
            return(tot);
        } // end of total
        public DLLNode search(String str)
        {
            int     count = 0;
            DLLNode p     = head;

            while (p != null)
            {
                count++;
                p.number = count;
                if (p.str == str)
                {
                    break;
                }
                p = p.next;
            }
            return(p);
        } // end of search (return pionter to the node);
        } // removeHead

        public void removeTail()
        {
            // corrected this code so that when tail is removed,
            // the previous node becomes the tail
            if (head == null)
            {
                return;
            }
            else if (head != tail)
            {
                tail      = tail.previous;
                tail.next = null;
            }
            else
            {
                head = null;
                tail = null;
            }
        } // remove tail
示例#22
0
        } // remove tail

        public DLLNode search(int num)
        {
            DLLNode p = head;

            if (head == null)
            {
                return(null);
            }

            while (p != null)
            {
                if (p.num == num)
                {
                    return(p);
                }

                p = p.next;
            }
            return(null);
        } // end of search (return pionter to the node);
        } // end of addToHead

        public void removeHead()
        {
            if (head == null)
            {
                return;
            }
            // corrected this code so that when head is removed,
            // next node becomes head & next node.previous is null
            else if (head != tail)
            {
                head          = head.next;
                head.previous = null;
            }

            else //only 1 node in the list
            {
                head = null;
                tail = null;
            }
        } // removeHead
        } // remove tail

        /*-------------------------------------------------
         * Return null if the string does not exist.
         * ----------------------------------------------*/
        public DLLNode search(int num)
        {
            DLLNode p = head;

            // if list is null, return null
            if (p == null)
            {
                return(null);
            }

            while (p != null)
            {
                if (p.num.Equals(num))
                {
                    return(p); // Returns pointer to node
                }
                p = p.next;    // iterate through list AFTER checking num
            }
            return(null);      // If p is null after while loop, return null (no match)
        } // end of search
        } // end of addToHead

        public void removHead()
        {
            //changed code
            if (this.head == null)
            {
                return;
            }
            if (this.head == this.tail)
            {
                this.head = null;
                this.tail = null;
                return;
            }
            this.head          = this.head.next;
            this.head.previous = null;
            return;

            /* if (this.head == null) return;
             * this.head = this.head.next;
             * this.head.previous = null;*/
        } // removeHead
示例#26
0
        } // end of addToHead

        public void removeHead()
        {
            //Edited this section of code by simplified it by removing "this."

            //Tested using testRemoveHead2
            if (head == null)
            {
                return;
            }

            else if (head != tail)
            {
                head          = head.next;
                head.previous = null;
            }

            else
            {
                head = tail = null;
            }
        } // removeHead
        } // end of remove a node

        public int total()
        {
            //changed code
            DLLNode p   = head;
            int     tot = 0;

            while (p != null)
            {
                tot += p.num;
                p    = p.next;
            }
            return(tot);

            /*DLLNode p = head;
             * int tot = 0;
             * while (p != null)
             * {
             *  tot += p.num;
             *  p = p.next.next;
             * }
             * return (tot);*/
        } // end of total
        } // remove tail

        /*-------------------------------------------------
         * Return null if the string does not exist.
         * ----------------------------------------------*/
        public DLLNode search(int num)
        {
            //change code
            DLLNode p = head;

            while (p != null)
            {
                if (p.num == num)
                {
                    break;
                }
                p = p.next;
            }
            return(p);

            /*DLLNode p = head;
             * while (p != null)
             * {
             *  p = p.next;
             *  if (p.num == num) break;
             * }
             * return (p);*/
        } // end of search (return pionter to the node);
 public DLLNode tail; // pointer to the tail of the list
 public DLList()
 {
     head = null;  tail = null;
 }                                             // constructor
示例#30
0
 public DLLNode previous; // pointer to the previous node
 public DLLNode(int num)
 {
     this.num = num;
     next     = null;
     previous = null;
 } // end of constructor