Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9200599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:48:14+00:00 2026-06-17T22:48:14+00:00

I am writing a generic linked list implementation in Java. The code is public

  • 0

I am writing a generic linked list implementation in Java. The code is

public class LinkedList<T> {
    private Node<T> top;
    private int no_of_items;

    private class Node<T extends Comparable<? super T>> {
        T data;
        Node<T> next;
    }

    public LinkedList() {
        top = null;
    }

    public boolean isEmpty() {
        return (top == null);
    }

    public void insert(T item) {
        Node<T> node = new Node<T>();
        node.data = item;
        node.next = top;
        if(isEmpty()) {
            top = node;
        } else {
            Node<T> n = top; 
            while(n.next != null) {
                n = n.next;
            }
            n.next = node;
        }
        no_of_items++;

    }
}

What I want is that T should be Comparable. While compiling this code I am getting an error where I initialize the Node.

Bound Mismatch the type T is not a valid substitute for the bounded parameter <T extends Comparable<? super T>> of the type LinkedList<T>.Node<T>

I am not able to figure out what is the issue here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:48:15+00:00Added an answer on June 17, 2026 at 10:48 pm

    I solved the error myself. The correct code would be

        public class LinkedList<T extends Comparable<? super T>> {
    private Node top;
    private int no_of_items;
    
    private class Node {
        T data;
        Node next;
    }
    
    public LinkedList() {
        top = null;
    }
    
    public boolean isEmpty() {
        return (top == null);
    }
    
    public void insert(T item) {
        Node node = new Node();
        node.data = item;
        node.next = top;
        if(isEmpty()) {
            top = node;
        } else {
            Node n = top; 
            while(n.next != null) {
                n = n.next;
            }
            n.next = node;
        }
        no_of_items++;
    
    }
        }
    

    The answer lies in that when we use a generic type T in a top class and we have non static inner class, the same T is visible in the inner class as well.

    Thanks,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For a CS class I am writing a linked list implementation of a linked
I am facing difficulty in writing a generic class in java whose generic members
I'm writing a linked list for a C++ class and am stuck trying to
I am writing a generic linked list in C++ using templates, and am experiencing
Hi I am writing a linked list data type. I have an inner class
I was writing a generic class to read RSS feed from various source and
I'm writing a generic class where I need to use Interlocked. T test1, test2;
I'm writing a generic matrix class in c++. I want to be able to
I'm writing an generic Array class and overloading operators for convenience. I've gotten my
My question is eerily similar to Writing a generic class to handle built-in types

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.