简体   繁体   中英

Displaying items of a generic linked list

I am creating a Windows Form Application where a user enters the ISBN of a book and upon pressing on the "Add" button, the ISBN will be added to the Generic LinkedList in addition to that the label "No ISBNs entered" will be replaced by the ISBN entered. When a user presses on the "Remove Button" the ISBN inside the textbox will be removed from the LinkedList, unfortunately, the below error shows.

This is the error I'm getting:

"Error CS0311 The type 'ISBNProject.Book' cannot be used as type parameter 'T' in the generic type or method 'LinkListGen'. There is no implicit reference conversion from 'ISBNProject.Book' to 'System.IComparable'. ISBNProject"

在此处输入图片说明

I've created 3 classes in my project:

  1. Book Class
  2. LinkListGen Class
  3. LinkGen Class

Book Class Code

class Book
    {
        private double isbn;
        public Book(double isbn)
        {
            this.isbn = isbn;
        }

        public double ISBN
        {
            set { this.isbn = value; }
            get { return this.isbn; }
        }
        public override string ToString()
        {
            return isbn + "";
        }


     
    }

LinkList Class

class LinkListGen<T>
    {
        private LinkGen<T> list;

        public LinkListGen()
        {
            list = null;
        }

        public void AddItem(T item)
        {
            list = new LinkGen<T>(item, list); //create a new link on the front of the list
        }

        public string DisplayList()
        {
            string buffer = "";
            LinkGen<T> temp = list; //temp starts beginning of list
            while (temp != null) //not at end of list
            {
                buffer = buffer + "," + temp.Data;
                temp = temp.Next; //move along a link

            }

            return buffer;

        }
        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;
            }
            newList.list = list;
        }

    }

LinkGen Class

class LinkGen<T>
    {
        private T data;
        private LinkGen<T> next;

        public LinkGen(T item, LinkGen<T> list)
        {
            this.data = item;
            this.next = list;
        }

        public LinkGen<T> Next
        {
            set { this.next = value; }
            get { return this.next; }
        }

        public T Data
        {
            set { this.data = value; }
            get { return this.data; }
        }
    }

Form class

public partial class Form1 : Form
    {
        LinkListGen<Book> ISBNList = new LinkListGen<Book>();
        public Form1()
        {
            InitializeComponent();
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            double insertedISBN = Convert.ToDouble(ISBNtextBox.Text);
            Book newBook = new Book(insertedISBN);
            ISBNList.AddItem(newBook);
            DisplayLabel.Text = ISBNList.DisplayList();
        }
        private void RemoveButton_Click(object sender, EventArgs e)
        {
            double insertedISBN = Convert.ToDouble(ISBNtextBox.Text);
            Book removeBook = new Book(insertedISBN);
            ISBNList.RemoveItem(removeBook);
        }
    }

In the Book class, add the following method:

    public override string ToString()
    {
        return isbn.ToString();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM