示例#1
0
        public void DeleteFirst()
        {
            intNode temp = head;

            head = head.Next;
            temp = null;
        }
示例#2
0
        public void DeleteLast()
        {
            IntNode temp = head;

            while (temp.Next != null)
            {
                temp = temp.Next;
            }
            temp = null;
        }
示例#3
0
        public int FindLast()
        {
            IntNode temp = head;

            while (temp.Next != null)
            {
                temp = temp.Next;
            }
            return(temp.Data);
        }
示例#4
0
        public int Length()
        {
            int     length = 0;
            IntNode temp   = head;

            while (temp != null)
            {
                lenght++;
                temp = temp.Next;
            }
            return(length);
        }
示例#5
0
        public void Clear()
        {
            IntNode temp   = head;
            IntNode obrisi = temp;

            while (temp != null)
            {
                temp   = temp.Next;
                obrisi = null;
                obrisi = temp;
            }
        }
示例#6
0
        public int Sum()
        {
            IntNode temp = head;
            int     suma = 0;

            while (temp != null)
            {
                suma               = suma + temp.Data
                              temp = temp.Next;
            }
            return(suma);
        }
示例#7
0
        public void AddFirst(int data)
        {
            IntNode newNode = new IntNode(data);

            //ne mora
            //if (head == null)
            //{
            //    head = newNode;
            //    return;
            //}

            newNode.Next = head;
            head         = newNode;
        }
示例#8
0
        public int[] ToArray()
        {
            IntNode temp = head;

            int[] niz    = new int[Length()];
            int   brojac = 0;

            while (temp != null)
            {
                niz[brojac] = temp.Data;
                temp        = temp.Next;
                brojac++;
            }
            return(niz[]);
        }
示例#9
0
        public void Merge(IntNode head2)
        {
            IntNode temp  = head;
            IntNode temp2 = head2;

            while (temp.Next != null)
            {
                temp = temp.Next;
            }
            while (temp2 != null)
            {
                temp.Next = temp2;
                temp2     = temp2.Next;
                temp      = temp.Next;
            }
        }
示例#10
0
        public override string ToString()
        {
            string str = "";

            if (IsEmpty())
            {
                return("prazna"); //ili "" i tom slucaju ceo if ne mora da postoji
            }

            IntNode temp = head;

            while (temp != null)
            {
                str += (temp.Data + " ");
                temp = temp.Next;
            }
            return(str);
        }
示例#11
0
        public bool Add(int data, int index)
        {
            //TODO implement
            //true ako uspe da doda, false ako ne, npr ako neko zada neispravan index
            //index ide od 0
            IntNode newNode = new IntNode(data);
            bool    provera = true;

            if (index == 0)
            {
                AddFirst(data);
            }
            else if (IsEmpty())
            {
                provera = false;
            }
            else if (Length() < index + 1)
            {
                provera = false;
            }
            else if (Length() == index)
            {
                AddLast(data);
            }
            else
            {
                IntNode temp      = head;
                IntNode tempstari = head;
                int     brojac    = 0;
                while (brojac < index)
                {
                    tempstari = temp;
                    temp      = temp.Next;
                    brojac++;
                }
                newNode.Next   = temp;
                tempstari.Next = newNode;
            }
            return(provera);
        }
示例#12
0
 public void Reverse()
 {
     if (!IsEmpty())
     {
         IntNode temp = head.Next
                        IntNode tempstari = temp;
         IntNode tempstariji = head;
         IntNode templast    = head
                               while (templast.Next != null)
         {
             templast = templast.Next;
         }
         head.Next = templast;
         while (temp != null)
         {
             tempstari.Next = tempstariji;
             tempstariji    = tempstari;
             tempstari      = temp;
             temp           = temp.Next;
         }
     }
 }
示例#13
0
        public bool Edit(int data, int index)
        {
            bool    provera = true;
            IntNode temp    = head;

            if (index == 0)
            {
                temp.Data = data;
            }
            else if (IsEmpty())
            {
                provera = false;
            }
            else if (Length() < index + 1)
            {
                provera = false;
            }
            else if (Length() == index)
            {
                while (temp.Next != null)
                {
                    temp = temp.Next;
                }
                temp.Data = data;
            }
            else
            {
                int brojac = 0;
                while (brojac < index)
                {
                    temp = temp.Next;
                    brojac++;
                }
                temp.Data = data;
            }
            return(provera);
        }
示例#14
0
        public bool Delete(int index)
        {
            bool provera = true;

            if (index == 0)
            {
                DeleteFirst();
            }
            else if (IsEmpty())
            {
                provera = false;
            }
            else if (Length() < index + 1)
            {
                provera = false;
            }
            else if (Length() == index)
            {
                DeleteLast();
            }
            else
            {
                IntNode temp      = head;
                IntNode tempstari = head;
                int     brojac    = 0;
                while (brojac < index)
                {
                    tempstari = temp;
                    temp      = temp.Next;
                    brojac++;
                }
                tempstari.Next = temp.Next;
                temp           = null;
            }
            return(provera);
        }
示例#15
0
        public void AddLast(int data)
        {
            IntNode newNode = new IntNode(data);

            if (IsEmpty())
            {
                head = newNode;
                return;
            }

            IntNode temp = head;

            while (temp.Next != null)  //ne mozemo tmp != null jer cemo preskociti poslednji element na koji treba da se zakacimo. Zato moramo i ostaviti onu gore proveru za head == null
            {
                temp = temp.Next;
            }
            //sa petljom iznad samo prolazimo kroz listu dok ne stignemo do kraja
            //na kraju petlje se nalazimo na poslednjem elementu
            //ostalo je jos samo da se zakacimo za njega
            temp.Next = newNode;


            //ovde je, recimo, specijalan slucaj i kad lista ima jedan elemenat, proveris na brzaka, u glavi, jel i to ok
        }
示例#16
0
 public IntNode(int data, IntNode next)
 {
     Data = data;
     Next = next;
 }
示例#17
0
        public int FindFirst()
        {
            IntNode temp = head;

            return(temp.Data);
        }
示例#18
0
        static void Main(string[] args)
        {
            IntNode node1 = new IntNode();
            IntNode node2 = new IntNode(5);
            IntNode node3 = new IntNode(3, node2);

            IntLinkedList list = new IntLinkedList();


            Console.WriteLine("-------------------");
            list.AddFirst(12);
            Console.WriteLine(list.ToString());

            list.AddFirst(5);
            Console.WriteLine(list.ToString());

            Console.WriteLine("-------------------");
            list.AddLast(2);
            Console.WriteLine(list.ToString());


            //testiranje dodavanja na proizvoljnu poziciju
            Console.WriteLine("-------------------");
            //ocekujemo da ne radi
            if (!list.Add(3, -12))
            {
                Console.WriteLine("Greska");
            }
            else
            {
                Console.WriteLine(list);
            }

            //ocekujemo da ne radi
            if (!list.Add(3, 12))
            {
                Console.WriteLine("Greska");
            }
            else
            {
                Console.WriteLine(list);
            }


            //ocekujemo da radi
            if (!list.Add(3, 1))
            {
                Console.WriteLine("Greska");
            }
            else
            {
                Console.WriteLine(list);
            }

            //isto kao AddFirst
            if (!list.Add(6, 0))
            {
                Console.WriteLine("Greska");
            }
            else
            {
                Console.WriteLine(list);
            }

            //isto kao AddLast
            if (!list.Add(4, list.Length()))
            {
                Console.WriteLine("Greska");
            }
            else
            {
                Console.WriteLine(list);
            }
        }