4 Replies - 1451 Views - Last Post: 22 January 2012 - 07:18 PM Rate Topic: -----

#1 murkyo0ocrimson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-September 11

Singly Linked Lists Within Another Linked List

Posted 22 January 2012 - 05:01 PM

I need to create a management system for an online bookstore using a singly linked list. The list is composed of the different books the website sells and each node (book) requires a title, price, and number of copies sold. Each book node also needs to have a waiting list, comprised of the customers' last names and how many copies they want to order. The waiting list needs to be another singly linked list that connects to each book node.

I'm not sure how to connect one linked list with another linked list, so any help would be appreciated. Some of the methods I need are adding a book to the list, ordering a book, selling a book, canceling an order, and displaying all the information about a book. I think I'll be able to write these once I understand what I need to do, but I've included them to give a broader scope on what I need this code to accomplish.

This is what I've got so far, using an inner Node class:

public class bookstore
{
	private class Node
	{
		String title;
		double price;
		int copiesSold;
		
		Node link;
		
		public Node()
		{
			title = "";
			price = 0.0;
			copiesSold = 0;
			
			link = null;
		}
		public Node(String t, double p, int cs, Node n)
		{
			title = t;
			price = p;
			copiesSold = cs;
			
			link = n;
		}
	}
	private Node head;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Singly Linked Lists Within Another Linked List

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,377
  • Joined: 27-December 08

Re: Singly Linked Lists Within Another Linked List

Posted 22 January 2012 - 06:24 PM

I would start by separating the Node and Book responsibilities. If you have a LinkedList or Persons as well, then a single Node and LinkedList class would make the most sense.
class Book{

    private String title;
    private int numCopies;
    private double price;

    //really though, it would be better to have 
    //the Bookstore class manage a 
    //Map<Book, LinkedList<Person>> for the waiting list
    private LinkedList<Person> waitingList;
 
}

class Person{
   
    private String name;
    private int numCopies;
}



Now your Node and LinkedList classes can (and should) be designed generically so you can better reuse them.
class Node<E>{

    private E element;
    private Node<E> next;
}

class LinkedList<E>{

    private Node<E> head, tail;
}



I have a LinkedList tutorial you may find helpful as well.

As for sorting, take a look at the Comparable interface, as well as the Insertion Sort and Mergesort algorithms. Insertion and Mergesorts are the most efficient with Linked Lists, b/c they don't require extra memory.

As for the Comparable interface, you would implement it on the class whose objects you want to sort. The Comparable interface contains the compareTo() method, which describes how the current Object and the parameter are related (or, which one is greater).

This post has been edited by macosxnerd101: 22 January 2012 - 07:17 PM
Reason for edit:: Fixed typo

Was This Post Helpful? 1
  • +
  • -

#3 ski-line900  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 30-May 11

Re: Singly Linked Lists Within Another Linked List

Posted 22 January 2012 - 06:30 PM

Hello, instead of storing the attributes for title, price, and copiesSold directly in the Node class we could make the Node class generic by adding <E> after the class name declaration. We could then store an E object in the node class, specified when the node is instantiated.
Then we could create a few classes to store the needed objects in: Bookstore, Book, and Customer. The Bookstore class could then contain a singly-linked list that stored Book objects, and the Book class could contain a singly-liked list object containing Customer objects, representing the waiting list.

Then the update methods for ordering a book, or canceling an order etc could be placed in the Bookstore class. To access the customer objects from the Bookstore class, you would access a Node in the Book singly-linked list, and then access the Customer singly-linked list object in contained in each Book object.

Just some ideas, this would make your Singly-Linked List and Node classes re-usable and able to store future classes. Also, if you need to make changes to the Customer or Book objects, or the overall design of the system this would make it much more manageable.

Hope it helps
Was This Post Helpful? 1
  • +
  • -

#4 murkyo0ocrimson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-September 11

Re: Singly Linked Lists Within Another Linked List

Posted 22 January 2012 - 07:16 PM

View Postmacosxnerd101, on 22 January 2012 - 06:24 PM, said:

class Book{

    private String title;
    private int numCopies;
    private int waitingList;

    //really though, it would be better to have 
    //the Bookstore class manage a 
    //Map<Book, LinkedList<Person>> for the waiting list
    private LinkedList<Person> waitingList;
 
}



Thanks for the help. I don't know much about Generics, so I'm sorry if this is elementary, but when I entered your code in my compiler, macosxnerd101, I got an error that read "Duplicate field bookstore.Book.waitingList" on both lines 5 and 10 above. Is this a typo in your code or something that I have to work with later on in my program?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,377
  • Joined: 27-December 08

Re: Singly Linked Lists Within Another Linked List

Posted 22 January 2012 - 07:18 PM

That third instance field should correspond to your price variable, double price. That was a typo on my part, sorry about that. I corrected it in my last post. :)

As for generics, for the purpose of your program, they're as simple as what I demonstrated in my last post.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1