6 Replies - 387 Views - Last Post: 07 February 2012 - 04:50 PM Rate Topic: -----

Topic Sponsor:

#1 SpartanGuy07  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 140
  • Joined: 08-September 11

Creating a Queue Class

Posted 07 February 2012 - 02:28 PM

My assignment is to create a Queue class from a singly linked list and an array list. I am getting an error from the compiler and I am not really sure how I can fix it.

I'll just post some snippets in hopes that someone will be able to help me figure out what the error means exactly.

Queue class
Spoiler


Singly Linked List Class
Spoiler


Node Class
Spoiler


The error I receive is:
Posted Image

I am not sure why the issue is occuring, but if it is a problem with the my method or code otherwise, I would appreciate someone explaining the problem to me.

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Creating a Queue Class

#2 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 106
  • View blog
  • Posts: 359
  • Joined: 28-November 09

Re: Creating a Queue Class

Posted 07 February 2012 - 02:30 PM

Your addlast is asking for a Node<E>, when it should be asking for E data. Your enqueue is trying to pass it E data, which is what is causing the error.

This post has been edited by ianian112: 07 February 2012 - 02:34 PM

Was This Post Helpful? 0
  • +
  • -

#3 SpartanGuy07  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 140
  • Joined: 08-September 11

Re: Creating a Queue Class

Posted 07 February 2012 - 02:52 PM

You say it should be asking for E data? Does that mean I need to redesign the addLast method?

We actually had to design the Singly Linked List last week, and write tests for it. So I know it works the way it was intended to with just simply adding the values into the list. Is there something I am doing wrong with the Queue on top of the Singly Linked Lists? Or like I said do I need to redesign things?

This post has been edited by SpartanGuy07: 07 February 2012 - 02:55 PM

Was This Post Helpful? 0
  • +
  • -

#4 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 106
  • View blog
  • Posts: 359
  • Joined: 28-November 09

Re: Creating a Queue Class

Posted 07 February 2012 - 03:02 PM

All you need to do in modify your addLast a littlebit

 public void addLast(Node<E> n) {



to

 public void addLast(E n) {
Node<E> a = new Node<E>(n);
}


Was This Post Helpful? 1
  • +
  • -

#5 SpartanGuy07  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 140
  • Joined: 08-September 11

Re: Creating a Queue Class

Posted 07 February 2012 - 03:08 PM

View Postianian112, on 07 February 2012 - 05:02 PM, said:

All you need to do in modify your addLast a littlebit

 public void addLast(Node<E> n) {



to

 public void addLast(E n) {
Node<E> a = new Node<E>(n);
}



OH RIGHT! I think I get it! Cause the new item will always be added to the end of the list, addLast should just always create a new node every time it is invoked right?

Wow I feel stupid that I didn't realize that! Thank you!
Was This Post Helpful? 0
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 719
  • View blog
  • Posts: 1,692
  • Joined: 05-May 05

Re: Creating a Queue Class

Posted 07 February 2012 - 03:12 PM

Like ian said, addLast expects a node not an instance of E. Create a node in enqueue with the value e and pass it to addLast. Ian was saying that your linked list should accept E and not a node, which is true, if you want to write an exportable list. As a client of your linked list, I shouldn't know anything about nodes. Instead I have to do this:

main() {
SLinkedList<Integer> llist = new SLinkedList<Integer>();
Node<Integer> elem = new Node<Integer>(2343);
llist.addLast(elem);
}



when the class should have been designed so that I can do this:

SLinkedList<Integer> llist = new SLinkedList<Integer>();
llist.addLast(2343);



What happens when I write code that depends on your Node implementation? Perhaps I want to keep track of the head of the list for odd reason.

Node<Integer> elem = new Node<Integer>(2343);
int first  = elem.getElement();



Later, you *learn why encapsulation is so important* and you decide to make your node class a private inner class of the linked list or you decide to remove the getter/setters. All of your client's codes break.
Was This Post Helpful? 0
  • +
  • -

#7 SpartanGuy07  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 140
  • Joined: 08-September 11

Re: Creating a Queue Class

Posted 07 February 2012 - 04:50 PM

Alright I understand what you mean. Thank you guys.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1