473,472 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

need help

28 New Member
I need a help with the program to test the insertion, deletion, search, copyList, and the copy constructor operations for an object in the class UnorderedLinkedList.

These are my classes:



public class UnorderedLinkedList extends LinkedListClass
{

public UnorderedLinkedList()
{
super();
}

public UnorderedLinkedList(UnorderedLinkedList otherList)
{
super(otherList);
}

//Method to determine whether searchItem is in
//the list.
//Postcondition: Returns true if searchItem is found
// in the list; false otherwise.
public boolean search(DataElement searchItem)
{
LinkedListNode current; //variable to traverse the list
boolean found;

current = first; //set current to point to the first
//node in the list

found = false; //set found to false

while(current != null && !found) //search the list
if(current.info.equals(searchItem)) //item is found
found = true;
else
current = current.link; //make current point to
//the next node
return found;
}


//Method to delete deleteItem from the list.
//Postcondition: If found, the node containing
// deleteItem is deleted from the
// list. Also first points to the first
// node, last points to the last
// node of the updated list, and count
// is decremented by 1.
public void deleteNode(DataElement deleteItem)
{
LinkedListNode current; //variable to traverse the list
LinkedListNode trailCurrent; //variable just before current
boolean found;

if(first == null) //Case 1; the list is empty
System.err.println("Cannot delete from an empty list.");
else
{
if(first.info.equals(deleteItem)) //Case 2
{
first = first.link;

if(first == null) //the list had only one node
last = null;
count--;
}
else //search the list for the node with the given info
{
found = false;
trailCurrent = first; //set trailCurrent to point to
//the first node
current = first.link; //set current to point to the
//second node

while(current != null && !found)
{
if(current.info.equals(deleteItem))
found = true;
else
{
trailCurrent = current;
current = current.link;
}
}//end while

if(found) //Case 3; if found, delete the node
{
count--;
trailCurrent.link = current.link;

if(last == current) //node to be deleted was
//the last node
last = trailCurrent; //update the value of last
}
else
System.out.println("Item to be deleted is "
+ "not in the list.");
}//end else
}//end else
}//end deleteNode
}




public abstract class LinkedListClass
{
protected class LinkedListNode
{
DataElement info;
LinkedListNode link;
}

protected LinkedListNode first; //variable to store the
//address of the first
//node of the list
protected LinkedListNode last; //variable to store the
//address of the last
//node of the list
protected int count; //variable to store the number of nodes
//in the list


//default constructor
//Initializes the list to an empty state.
//Postcondition: first = null, last = null,
// count = 0
public LinkedListClass()
{
first = null;
last = null;
count = 0;
}

//Method to determine whether the list is empty.
//Postcondition: Returns true if the list is empty;
// false otherwise.
public boolean isEmptyList()
{
return (first == null);
}

//Method to initialize the list to an empty state.
//Postcondition: first = null, last = null,
// count = 0
public void initializeList()
{
first = null;
last = null;
count = 0;
}

//Method to output the data contained in each node.
public void print()
{
LinkedListNode current; //variable to traverse the list

current = first; //set current so that it points to
//the first node
while(current != null) //while more data to print
{
System.out.print(current.info + " ");
current = current.link;
}
}//end print

//Method to return the number of nodes in the list.
//Postcondition: The value of count is returned.
public int length()
{
return count;
}


//Method to return a reference of the object containing
//the data of the first node of the list.
//Precondition: The list must exist and must not be empty.
//Postcondition: The reference of the object that
// contains the info of the first node
// is returned.
public DataElement front()
{
DataElement temp = first.info.getCopy();
return temp;
}

//Method to return a reference of object containing
//the data of the last node of the list.
//Precondition: The list must exist and must not be empty.
//Postcondition: The reference of the object that
// contains the info of the last node
// is returned.
public DataElement back()
{
DataElement temp = last.info.getCopy();
return temp;
}

//Method to determine whether searchItem is in the list.
//Postcondition: Returns true if searchItem is found
// in the list; false otherwise.
public abstract boolean search(DataElement searchItem);


//Method to insert newItem in the list.
//Postcondition: first points to the new list
// and newItem is inserted at the
// beginning of the list. Also,
// last points to the last node and
// count is incremented by 1.
public void insertFirst(DataElement newItem)
{
LinkedListNode newNode; //variable to create the
//new node

newNode = new LinkedListNode(); //create the new node
newNode.info = newItem.getCopy(); //assign a copy of
//newItem to the node
newNode.link = first; //insert newNode before first
first = newNode; //make first point to the
//actual first node

if(last == null) //if the list was empty, newNode is
//also the last node in the list
last = newNode;

count++;
}

//Method to insert newItem at the end of the list.
//Postcondition: first points to the new list and
// newItem is inserted at the end
// of the list. Also, last points to
// the last node and
// count is incremented by 1.
public void insertLast(DataElement newItem)
{
LinkedListNode newNode; //variable to create the new node

newNode = new LinkedListNode(); //create the new node
newNode.info = newItem.getCopy(); //assign a copy of
//newItem to the node
newNode.link = null; //set the link field of
//newNode to null

if(first == null) //if the list is empty, newNode is
//both the first and last node
{
first = newNode;
last = newNode;
}
else //if the list is not empty, insert newNode after last
{
last.link = newNode; //insert newNode after last
last = newNode; //set last to point to the actual last node
}

count++;
}//end insertLast



//Method to delete deleteItem from the list.
//Postcondition: If found, the node containing
// deleteItem is deleted from the
// list. Also first points to the first
// node, last points to the last
// node of the updated list, and count
// is decremented by 1.
public abstract void deleteNode(DataElement deleteItem);

//Method to return a reference of the copy of otherList.
private void copy(LinkedListClass otherList)
{
LinkedListNode newNode; //variable to create a node
LinkedListNode current; //variable to traverse the list

first = null; //make this list empty

if(otherList.first == null) //otherList is empty
{
first = null;
last = null;
count = 0;
}
else
{
count = otherList.count;
current = otherList.first; //current points to the
//list to be copied

//copy the first element
first = new LinkedListNode(); //create the node
first.info = current.info.getCopy(); //copy the info
first.link = null; //set the link field of
//the node to null
last = first; //make last point to the first node
current = current.link; //make current point to the next
//node of the list being copied

//copy the remaining list
while(current != null)
{
newNode = new LinkedListNode();
newNode.info = current.info.getCopy();
newNode.link = null;
last.link = newNode;
last = newNode;
current = current.link;
}//end while
}//end else
}//end copy


//Method to return a reference of the copy of otherList.
public void copyList(LinkedListClass otherList)
{
if(this != otherList) //avoid self-copy
copy(otherList);
}

//copy constructor
public LinkedListClass(LinkedListClass otherList)
{
copy(otherList);
}//end copy constructor
}



import java.io.*;
import java.util.*;

public class TestProgUnorderedLinkedList
{
static BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws IOException
{
UnorderedLinkedList list
= new UnorderedLinkedList();
UnorderedLinkedList tempList =
new UnorderedLinkedList();
IntElement num = new IntElement();

StringTokenizer tokenizer;

int position;

System.out.print(" Enter 8 integers on the " + "same line: ");
System.out.flush();

tokenizer = new
StringTokenizer(keyboard.readLine());

num.setNum(Integer.parseInt(tokenizer.nextToken()) );

while (tokenizer.hasMoreTokens())
{
num.setNum(Integer.parseInt(tokenizer.nextToken()) );
list.insertLast(num);
}

System.out.println();
System.out.print("The list you " + "entered is: ");
list.print();
System.out.println();
System.out.println("Length of list: " + list.length());

tempList.copyList(list);

System.out.print("Enter the num to " + "be deleted: ");
System.out.flush();
num.setNum(Integer.parseInt(keyboard.readLine()));
System.out.println();

tempList.deleteNode(num);
System.out.println("After deleting " + num + " the node, tempList:");
tempList.print();
System.out.println();
System.out.println("Length of list: " + tempList.length());

System.out.print("Enter the search " + "item: ");
System.out.flush();

num.setNum(Integer.parseInt(keyboard.readLine()));
System.out.println();

if(tempList.search(num) != -1)
System.out.println("Item found in " + "the list");
else
System.out.println("Item not found");

System.out.print("The list temp: ");
tempList.print();
System.out.println();
System.out.println("Length of list: " + tempList.length());
}
}
Oct 16 '06 #1
1 1530
r035198x
13,262 MVP
You know with code tags, the code becomes more readable.
In java LinkedLists the trick is the design of the node. Here is how I would do it:
Expand|Select|Wrap|Line Numbers
  1. class LinkedListNode {
  2.     private Object data;
  3.     private Listnode next;
  4.  
  5.     public LinkedListNode(Object d) {
  6.     this(d, null);
  7.     }
  8.  
  9.     public LinkedListNode(Object d, LinkedListNode n) {
  10.     data = d;
  11.     next = n;
  12.     }
  13.  
  14.     public Object getData() {
  15.         return data;
  16.     }
  17.  
  18.     public LinkedListNode getNext() {
  19.         return next;
  20.     }
  21.  
  22.  
  23.     public void setData(Object ob) {
  24.         data = ob;
  25.     }
  26.  
  27.     public void setNext(LinkedListNode n) {
  28.         next = n;
  29.     }
  30. }
  31. public class TheList {
  32.     LinkedListNode head;
  33.     LinkedListNode tail;
  34.  
  35.     public static void main(String[] args) {
  36.         TheList linkedList = new TheList();
  37.     }
  38.     public TheList() {
  39.         head = null;
  40.         tail = null;
  41.     }
  42.     public TheList(LinkedListNode head, LinkedListNode tail) {
  43.         this.head head;
  44.         this.tail = tail;
  45.     }
  46.  
  47.     public void addLast(LinkedListNode node) {
  48.         if(head == null) {
  49.             head = node;
  50.         }
  51.         else if(tail == null) {
  52.             tail = node;
  53.         }
  54.         else {
  55.             tail.setNext(node);
  56.         }
  57.     }
  58.  
  59.     public boolean contains(LinkedListNode node) {
  60.         boolean found = false;
  61.         LinkedListNode current = head;
  62.         if(current == null) {
  63.             found = false;
  64.         }
  65.         else {
  66.             while(current != null && !found ) {
  67.                 if(current.getData().equals(node.getData())) {
  68.                     found = true;
  69.                 }
  70.                 current = current.getNext();
  71.             }
  72.         }
  73.         return found;
  74.     }
  75.  
  76.     public void addAfter(LinkedListNode nodeInList, LinkedListNode nodeToAdd) {
  77.         if(!contains(nodeToAdd)) {
  78.             System.out.println("Failed to add..");
  79.         }
  80.         else {
  81.             LinkedListNode current = head;
  82.             boolean positionReached = false;
  83.             while(!positionReached) {
  84.                 if(current.getData().equals(nodeInList)) {
  85.                     positionReached = true;
  86.                 }
  87.             }
  88.             LinkedListNode temp = current.getNext();
  89.             current.setNext(nodeToAdd);
  90.             nodeToAdd.setNext(temp);
  91.  
  92.         }
  93.     }
  94.     // the rest of the methods should now be easy
  95.  
  96. }
perhaps better approaches can be found here
http://www.cs.jhu.edu/~pari/600.107/Horstmann/slides/Ch19/ch19.html
http://www.javacommerce.com/displaypage.jsp?name=javadata2.sql&id=18214#Linked _Lists
Oct 16 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
5
by: John Flynn | last post by:
hi all i'm going to be quick i have an assignment due which i have no idea how to do. i work full time so i dont have the time to learn it and its due date has crept up on me .. As follows:...
0
by: xunling | last post by:
i have a question about answering ..... this topic is "need help" what do i have to write at te topic line, !after i have klicked the "answer message" button ive tried many possibilities,...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.