Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.7 kB
3
Indexable
Never
class LinkListGen<T> where T : IComparable
    {
        private LinkGen<T> list = null; //default value – empty list


        public void AddItem(T item) //add item to front of list
        {
            list = new LinkGen<T>(item, this.list);

        }

        public string DisplayItems() //write items to string and return
        {
            LinkGen<T> temp = list;
            string buffer = "";
            while (temp != null) // move one link and add head to the buffer
            {
                buffer = buffer + temp.Data + ",";// use this instead of console.writeline as this can be viewed in gui not only console apps
                temp = temp.Next;



            }
            return buffer;
        }

        public int Count() // returns number of items in list
        {
            int count = 0;
            LinkGen<T> temp = list;

            while (temp != null)    //stopping condition while temp is not null..
            {
                count += 1;
                temp = temp.Next;
                // Console.WriteLine("Number of items in list ", count);


            }
            return count;   //when temp = null return the count

        }

        public void AppendItem(T item)
        {
            LinkGen<T> temp = list;

            if (temp == null)   //if temp has nothing in it add a new item
                list = new LinkGen<T>(item);
            else
            {
                while (temp.Next != null)   //while temp.next is not empty
                {
                    temp = temp.Next;   //go to the next temp
                }
                temp.Next = new LinkGen<T>(item);   //add new link with item
            }

        }

      


        public void RemoveItem(T item)  
        {

            LinkGen<T> temp = list;
            LinkListGen<T> newList = new LinkListGen<T>();
            while (temp != null)
            {
                if (item.CompareTo(temp.Data) != 0)
                    newList.AppendItem(temp.Data);

                temp = temp.Next;
            }

            list = newList.list;


        }
                    

          public void InsertInOrder(T item) //adds each item and put its in order 
          {
              LinkGen<T> temp = list;
              LinkListGen<T> newList = new LinkListGen<T>();
              if (list == null)
                  AddItem(item);
              else
              {
                  while (temp != null) ;

              }

          }  

}
}


 private void button4_Click(object sender, EventArgs e)
        {
            myList.InsertInOrder(textBox2.Text);
        }