Untitled

 avatar
unknown
plain_text
3 years ago
2.0 kB
6
Indexable
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)
            {
                count += 1;
                temp = temp.Next;
                // Console.WriteLine("Number of items in list ", count);


            }
            return count;

        }

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

            if (temp == null)
                list = new LinkGen<T>(item);
            else
            {
                while (temp.Next != null)
                {
                    temp = temp.Next;
                }
                temp.Next = new LinkGen<T>(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;


        }
Editor is loading...