示例#1
0
        public void RemoveAt(int position)
        {
            IntNode head = First;

            if (head == null)
            {
                return;
            }

            IntNode temp = head;

            if (position == 0)
            {
                head = temp.Next;
                return;
            }

            for (int i = 0; temp != null && i < position - 1; i++)
            {
                temp = temp.Next;
            }

            if (temp == null || temp.Next == null)
            {
                return;
            }
            IntNode next = temp.Next.Next;

            temp.Next = next;
        }
示例#2
0
        public void InsertBeforeMax(int x)
        {
            IntNode temp = new IntNode(x);
            IntNode node = GetMax();

            InsertBeforeP(node, temp);
        }
示例#3
0
        public void InsertAfterMin(int z)
        {
            IntNode temp = new IntNode(z);
            IntNode node = GetMin();

            InsertAfterP(node, temp);
        }
示例#4
0
        public void InsertBeforeP(IntNode p, IntNode newNode)
        {
            InsertAfterP(p, newNode);
            //Hoan Vi
            int temp = p.Data;

            p.Data       = newNode.Data;
            newNode.Data = temp;
        }
示例#5
0
 public void GetOddList()
 {
     for (IntNode pointer = First; pointer != null; pointer = pointer.Next)
     {
         if (pointer.Data % 2 != 0)
         {
             Console.Write(pointer.Data + " -> ");
         }
     }
     Console.WriteLine("NULL");
 }
示例#6
0
        public void ShowList()
        {
            IntNode p = First;

            while (p != null)
            {
                Console.Write(p.Data + " -> ");
                p = p.Next;
            }
            Console.WriteLine("NULL");
        }
示例#7
0
 public IntNode FindX(IntNode x)
 {
     for (IntNode temp = First; temp != null; temp = temp.Next)
     {
         if (temp.Data == x.Data)
         {
             return(x);
         }
     }
     return(null);
 }
示例#8
0
 public void AddLast(IntNode newNode)
 {
     if (IsEmpty())
     {
         First = Last = newNode;
     }
     else
     {
         Last.Next = newNode;
         Last      = newNode;
     }
 }
示例#9
0
 public void AddFirst(IntNode newNode)
 {
     if (IsEmpty())
     {
         First = Last = newNode;
     }
     else
     {
         newNode.Next = First;
         First        = newNode;
     }
 }
示例#10
0
 public void InsertAfterP(IntNode p, IntNode newNode)
 {
     if (p == Last)
     {
         AddLast(newNode);
     }
     else
     {
         IntNode pSau = p.Next;
         newNode.Next = pSau;
         p.Next       = newNode;
     }
 }
示例#11
0
        public void InsextXBeforeY(int x, int y)
        {
            int pos = 0;

            for (IntNode temp = First; temp != null; temp = temp.Next)
            {
                pos++;
                if (temp.Data == y)
                {
                    break;
                }
            }
            InsertAt(x, pos - 1);
        }
示例#12
0
        public IntNode GetMin()
        {
            IntNode minNode = new IntNode();

            minNode = First;

            for (IntNode pointer = First; pointer != null; pointer = pointer.Next)
            {
                if (pointer.Data <= minNode.Data)
                {
                    minNode = pointer;
                }
            }
            return(minNode);
        }
示例#13
0
 public void Sort()
 {
     for (IntNode i = First; i != null; i = i.Next)
     {
         for (IntNode j = i.Next; j != null; j = j.Next)
         {
             if (i.Data > j.Data)
             {
                 int temp = j.Data;
                 j.Data = i.Data;
                 i.Data = temp;
             }
         }
     }
 }
示例#14
0
        public IntNode GetMax()
        {
            IntNode maxNode = new IntNode();

            maxNode = First;

            for (IntNode pointer = First; pointer != null; pointer = pointer.Next)
            {
                if (pointer.Data >= maxNode.Data)
                {
                    maxNode = pointer;
                }
            }
            return(maxNode);
        }
示例#15
0
        public void InsertAt(int x, int pos)
        {
            IntNode node = new IntNode();

            node.Data = x;
            node.Next = null;


            if (First == null)
            {
                if (pos != 0)
                {
                    return;
                }
                else
                {
                    First = node;
                }
            }

            if (First != null && pos == 0)
            {
                node.Next = First;
                First     = node;
                return;
            }

            IntNode current  = First;
            IntNode previous = null;

            int i = 0;

            while (i < pos)
            {
                previous = current;
                current  = current.Next;
                if (current == null)
                {
                    break;
                }
                i++;
            }
            node.Next     = current;
            previous.Next = node;
        }
示例#16
0
        public void Input()
        {
            int x;

            do
            {
                Console.Write("Enter new Node: (-1 exit): ");
                int.TryParse(Console.ReadLine(), out x);
                if (x < 0)
                {
                    break;
                }
                else
                {
                    IntNode newNode = new IntNode(x);
                    Length++;
                    //AddFirst(newNode); Delete comment if use want to list reverse
                    AddLast(newNode);
                }
            } while (true);
        }
示例#17
0
        public void RemoveX(int x)
        {
            IntNode temp = First, prev = null;

            if (temp != null && temp.Data == x)
            {
                First = temp.Next;
                return;
            }

            while (temp != null && temp.Data != x)
            {
                prev = temp;
                temp = temp.Next;
            }

            if (temp == null)
            {
                return;
            }
            prev.Next = temp.Next;
        }