I'm taking Data Structure & Algorithms subject and don't have any background. I saw the following exercise and don't know how to implement it in Java although I have already taken Java. Can anyone help.
For each of the following task:
Display all the integers in an array of integers.
Display all integers in a chain of linked nodes.
Display the nth integer in an array of integers.
Compute the sum of the first n even integers in an array of integers.
i. Write a program fragment using Java.
ii. Indicate the time requirement using big-Oh notation in the worst case
The integer in array part should be doable if you have programmed in Java before. That's pretty straightforward. There are 2 options: you either loop over the array, or you use a recursive function.
I wasn't that good in calculating execution times in big O notation, so I can't really help you with that. Have a look around the web, cause I'm sure you'll find some decent info on that topic
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks
Thanks for your help. I have a problem with this code, if you can pin point to the error that I'm having, I'd appreciate it:
Code:
/* Node.java Authors: Koffman & Wolz
* Represents a node with information and link fields.
*/
class Node
{
// Data fields for Node
private Object info; // data stored in the node
private Node link; // link to next node
// Methods
// Constructors
// postcondition: Creates a new empty node.
public Node() {
info = null;
link = null;
}
// postcondition: Creates a new node storing obj.
public Node(Object obj) {
info = obj;
link = null;
}
// postcondition: Creates a new node storing obj
// and linked to node referenced by next.
public Node(Object obj, Node next) {
info = obj;
link = next;
}
// accessors
public Object getInfo()
{
return info;
}
public Object getLink()
{
return link;
}
// mutators
public void setInfo(Object newInfo)
{
info = newInfo;
}
public void setLink(Node newLink)
{
link = newLink;
}
}
//////////////////////////////////////////
public class LinkedList {
Node head,tail,current;
public LinkedList() {
}
//For (Initially Nodes contains = 0,3,5,7) you can use this method inside class Linked List.
public void initial( )
{
Node newNode = new Node(new Integer(0));
head = newNode();
Node newNode = new Node(new Integer(3));
head = newNode( );
newNode.setLink(head);
newNode = new Node(new Integer(5));
newNode.setLink(head);
head = newNode( );
newNode = new Node(new Integer(7));
newNode.setLink(head);
head = newNode( );
}
//the methods to be used here are:
public void insertLast(int i) //insert at last of list
{
Node newNode= new Node(i); //makes new LinkedList
if (head == null)
head = newNode;
else
{
if (head.getLink() ==null) //head.link==null
head.setLink (newNode); //head.link = newNode
else
{
Node current = head;
//traversing nodes
while (current.getLink() !=null) //current.link !=null
current = current.getLink(); //current = current.link
current.setLink (newNode); //current.link= newNode
}
}
//.....................................................................
public void deleteByTarget(int source, int target) //deletes by target
{
if (head == null)
System.out.println("Cannot delete data");
else if (head.getInfo() == target)
head = head.getLink();
else
{
Node before = null;
Node current= head;
// current.info != target
while ( (current.getInfo() != target) && (current != null) )
{
before = current;
current = current.getLink(); // current = current.link
}
if (current.getInfo() == target) // current.info == target
before.setLink(current.getLink()); // before.link = current.link
else
System.out.println("The target does not exist");
}
//.................................................................................
public void getTotalElement( )
{
System.out.println ("Total Element is:");
if (head ==null)
System.out.println ("Zero");
else
{
current = head;
int count=0;
while (current!=null)
{
current = current.getLink();
count++;
}
System.out.println(count));
//.................................................................................
public void displayAllElement( )
{
if (head ==null)
System.out.println ("No Elements exist");
else
{
System.out.println("All Elements Display");
current = head;
while (current!=null)
{
System.out.println(current.getInfo());
current = current.getLink();
}
}
}
//.................................................................................
public void insertByTarget( )
Node newNode = new Node(new Integer(5))
if (head == null)
head = newNode;
else
{
if (head.getInfo()==target)
head.setLink(newNode);
else{
Node current= head;
while (current != null){
if(current.getInfo()!= target)//current.info!=target
current = current.getLink(); // current = current.link
else if(current.getInfo() == target){ //current.info == target
newNode.setLink(current.getLink()); //newNode.link=current.link
current.setLink(newNode); // current.link = newNode
break; }
if(current==null)
System.out.println("The target does not exist"); }
}
}
//.................................................................................
public int deleteLast( ) //delete last link
{
if (head == null)
System.out.println("List is empty");
else
{
if (head.getLink ==null)
head = null;
else
{
Node current = head;
while (current.getLink().getLink() !=null)
current = current.getLink();
current.setLink(null);
}
}
//.................................................................................
public void getFirstElement( ){
if (head == null)
System.out.println ("First Element is Null");
else
{
System.out.println("This is the First Element");
System.out.println(head.getInfo());
}
}
public class List {
public List() {
}
public static void main (String[] args)
{
LinkedList list = new LinkedList();
list.insertLast(new Integer(2));
list.deleteByTarget(new Integer(3), new Integer(3));
list. list.insertLast(new Integer(4));
list. list.insertLast(new Integer(5));
list.getTotalElement( );
list.displayAllElement( );
list.deleteLast( );
list. insertByTarget((new Integer(4), new Integer(5));
list. insertByTarget((new Integer(14), new Integer(4));
list. insertByTarget((new Integer(41), new Integer(14));
list.getLastElement( );
list.displayAllElement( );
}
//end main()
}
//end class ClassList
}
//////////////////////////////////////////////////////////////
/**The example of the output as shown below
--------------------Configuration: <Default>--------------------
AddFirst 3 successfull.
AddFirst 4 successfull.
AddLast 5 successfull.
AddLast 6 successfull.
Data : 4 3 5 6
Remove By Target 6 successfull.
Data : 4 3 5
AddFirst 7 successfull.
The target is not exists
Data : 7 4 3 5
The first removed data is 7
The last removed data is 5
The last removed data is 3
Total element is 1
Data : 4
First Element is 4
Last Element is 4
Process completed.
*/
Last edited by UnrealEd; 07-08-10 at 11:01 AM.
Reason: fixed [code] tags