locked
Doubly Linked List in Windows Forms RRS feed

  • Question

  • I have a linked list called music with all my songs. How do I use a button next and a button previous to move to the next and previous song?

    P.s. the songs are played on windows media player using win form

    Thursday, July 23, 2020 12:44 PM

Answers

  • Hi Dolphine82340, 
    You can use the index of the doubly linked list to achieve it.
    Here is a code example you can refer to.

    DbLinkedList<String> dblink = new DbLinkedList<string>();
    private void Form1_Load(object sender, EventArgs e)
    {
              
        DbNode<String> head = new DbNode<string>("x");
        dblink.Head = head;
        dblink.AddBefore("w", 0);
        dblink.AddBefore("v", 0);
        dblink.AddBefore("y",0);
    
    }
        int i=0;
    private void buttonNext _Click(object sender, EventArgs e)
    {
        if (i< dblink.Count())
        {
            Console.WriteLine(dblink.GetItemAt(i));
            i = ++i;
        }           
    }
    
    private void buttonPrevious _Click(object sender, EventArgs e)
    {
        if (i>-1)
        {
            Console.WriteLine(dblink.GetItemAt(i));
            i =--i;
        }
    } 

    List.cs

     public class DbNode<T>
        {
            private T _data; 
            private DbNode<T> _prev; 
            private DbNode<T> _next; 
            public T Data
            {
                get { return this._data; }
                set { this._data = value; }
            }
    
            public DbNode<T> Prev
            {
                get { return this._prev; }
                set { this._prev = value; }
            }
    
            public DbNode<T> Next
            {
                get { return this._next; }
                set { this._next = value; }
            }
            public DbNode(T data, DbNode<T> prev, DbNode<T> next)
            {
                this._data = data;
                this._prev = prev;
                this._next = next;
            }
    
            public DbNode(T data, DbNode<T> prev)
            {
                this._data = data;
                this._prev = prev;
                this._next = null; //结尾处的node
            }
    
            public DbNode(DbNode<T> next)
            {
                this._data = default(T);
                this._next = next;
                this._prev = null;
            }
    
            public DbNode(T data)
            {
                this._data = data;
                this._prev = null;
                this._next = null;
            }
    
            public DbNode()
            {
                this._data = default(T);
                this._prev = null;
                this._next = null;
            }
        }
        class DbLinkedList<T>
        {
            private DbNode<string> _head;
    
            public DbNode<string> Head
            {
                get { return this._head; }
                set { this._head = value; }
            }
    
            public DbLinkedList()
            {
                Head = null;
            }
            public string this[int index]
            {
                get
                {
                    return this.GetItemAt(index);
                }
            }
            public bool IsEmpty()
            {
                return Head == null;
            }
    
           
            public string GetItemAt(int i)
            {
                if (IsEmpty())
                {
                    Console.WriteLine("The double linked list is empty.");
                    return default(string);
                }
    
                DbNode<string> p = new DbNode<string>();
                p = Head;
                if (0 == i)
                {
                    return p.Data;
                }
    
                int j = 0;
                while (p.Next != null && j < i)
                {
                    j++;
                    p = p.Next;
                }
    
                if (j == i)
                {
                    return p.Data;
                }
                else
                {
                    Console.WriteLine("The node dose not exist.");
                    return default(string);
                }
            }
            public int Count()
            {
                DbNode<string> p = Head;
                int length = 0;
                while (p != null)
                {
                    length++;
                    p = p.Next;
                }
                return length;
            }
    
            public void AddBefore(string item, int i)
            {
                if (IsEmpty() || i < 0)
                {
                    Console.WriteLine("The double linked list is empty or the position is uncorrect.");
                    return;
                }
    
                if (0 == i) 
                {
                    DbNode<string> newNode = new DbNode<string>(item);
                    newNode.Next = Head;
                    Head.Prev = newNode;
                    Head = newNode; 
                    return;
                }
    
                DbNode<string> n = Head;
                DbNode<string> d = new DbNode<string>();
                int j = 0;
    
                while (n.Next != null && j < i)
                {
                    d = n; 
                    n = n.Next;
                    j++;
                }
    
                if (n.Next == null) 
                {
                    DbNode<string> newNode = new DbNode<string>(item);
                    n.Next = newNode;
                    newNode.Prev = n;
                    newNode.Next = null;
                }
                else 
                {
                    if (j == i)
                    {
                        DbNode<string> newNode = new DbNode<string>(item);
                        d.Next = newNode;
                        newNode.Prev = d;
                        newNode.Next = n;
                        n.Prev = newNode;
                    }
                }
            }
    
       
            public int IndexOf(string value)
            {
                if (IsEmpty())
                {
                    Console.WriteLine("The list is empty.");
                    return -1;
                }
    
                DbNode<string> p = new DbNode<string>();
                p = Head;
                int i = 0;
                while (p.Next != null && !p.Data.Equals(value))
                {
                    p = p.Next;
                    i++;
                }
                return i;
            }
        }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Dolphine82340 Saturday, July 25, 2020 7:27 AM
    Friday, July 24, 2020 10:14 AM