Exemplo n.º 1
0
        /// <summary>
        /// Returns item by number (index)
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public SimpleListItem <T> GetItem(int num)
        {
            if ((num < 0 || num >= this.Count)) //if idex is incorrect
            {
                throw new Exception("Out of range!");
            }

            SimpleListItem <T> cur = this.first;
            int i = 0;

            while (i < num)
            {
                cur = cur.next;
                i++;
            }
            return(cur);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds "item" to the list
        /// </summary>
        /// <param name="item"></param>
        public void Add(T item)
        {
            SimpleListItem <T> NewItem = new SimpleListItem <T>(item);

            this.Count++;

            if (last == null) //if list is empty
            {
                this.first = NewItem;
                this.last  = NewItem;
            }
            else //else
            {
                this.last.next = NewItem;
                this.last      = NewItem;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Removes item from the stack and returns it
        /// </summary>
        /// <returns></returns>
        public T Pop()
        {
            T res = default(T);  //default value for the following type

            if (this.Count == 0) //if the stack is empty
            {
                return(res);     //returns default value for the following type
            }
            if (this.Count == 1)
            {
                res        = this.first.data;
                this.first = null;
                this.last  = null;
            }
            else
            {
                SimpleListItem <T> NewLast = this.GetItem(this.Count - 2);
                res          = NewLast.next.data;
                this.last    = NewLast;
                NewLast.next = null;
            }
            this.Count--;
            return(res);
        }