Anda di halaman 1dari 5

University Of Hail

College of Computer Science and Engineering


!
ICS-202 Data structure
Final Lab Exam
!

I.

This!is!a!liked!list!class,!which!is!missing!some!of!the!Methods!
implementation.!Please!implement!these!messing!methods:!:!
Add!(2!points)!
Remove!(2!points)!!
size(2!points).!

public class LinkedListDemo {


public static void main(String[] args) {
LinkedList listObject = new LinkedList();
listObject.add("1");
listObject.add("2");
listObject.add("3");
listObject.add("4");
listObject.add("5");
System.out.println("listObject - print linkedlist: " + listObject);
System.out.println("listObject.size() - print linkedlist size: " +
listObject.size());
System.out.println("listObject.get(3) - get 3rd element: " + listObject.get(2));
System.out.println("listObject.remove(2) - remove 2nd element: " +
listObject.remove(2));
System.out.println("listObject.get(4) - get 4th element: " + listObject.get(4));
System.out.println("listObject.size() - print linkedlist size: " +
listObject.size());
System.out.println("listObject - print linkedlist: " + listObject);
}
}
class LinkedList {
private Node head;
private int listCount;
public LinkedList() {
head = null;
listCount = 0;
}
public void add(Object data)
{
}
public Object get(int index)
{
if (index <= 0)
return null;
Node node = head;
for (int i = listCount; i > index; i--) {
if (node.getNext() == null)
return null;
node = node.getNext();
}
return node.getData();

University Of Hail
College of Computer Science and Engineering
!
ICS-202 Data structure
Final Lab Exam
!
}
public boolean remove(int index)
{
}
public int size()
{
}
public String toString() {
Node node = head;
String output = "";
while (node != null) {
output += "[" + node.getData().toString() + "]";
node = node.getNext();
}
return output;
}
private class Node {
Node next;
Object data;
public Node(Object dataValue) {
next = null;
data = dataValue;
}
public Node(Object dataValue, Node nextValue) {
next = nextValue;
data = dataValue;
}
public Object getData() {
return data;
}
public void setData(Object dataValue) {
data = dataValue;
}
public Node getNext() {
return next;
}
public void setNext(Node nextValue) {
next = nextValue;
}
}
}
!

University Of Hail
College of Computer Science and Engineering
!
ICS-202 Data structure
Final Lab Exam
!
!
!
!
!
II.

This!is!a!Binary!Tree!class,!which!is!missing!some!of!the!Methods!
implementation.!Please!implement!these!messing!methods:!:!!
AddNode(3!points)!
inOrderTraverseTree!(3!points)!

import java.lang.Math;
public class BinaryTree {
Node root;
public void addNode(int key, String name) {

}
public void inOrderTraverseTree(Node focusNode) {
if (focusNode != null) {

}
}

public Node findNode(int key) {


// Start at the top of the tree
Node focusNode = root;
// While we haven't found the Node
// keep looking
while (focusNode.key != key) {
// If we should search to the left
if (key < focusNode.key) {
// Shift the focus Node to the left child
focusNode = focusNode.leftChild;
} else {
// Shift the focus Node to the right child

University Of Hail
College of Computer Science and Engineering
!
ICS-202 Data structure
Final Lab Exam
!
focusNode = focusNode.rightChild;
}
// The node wasn't found
if (focusNode == null)
return null;
}
return focusNode;
}
public static void main(String[] args) {
BinaryTree theTree = new BinaryTree();
theTree.addNode(50, "Boss");
theTree.addNode(25, "Vice President");
theTree.addNode(15, "Office Manager");
theTree.addNode(30, "Secretary");
theTree.addNode(75, "Sales Manager");
theTree.addNode(85, "Salesman 1");

theTree.inOrderTraverseTree(theTree.root);
}
}
class Node {
int key;
String name;
Node leftChild;
Node rightChild;
Node(int key, String name) {
this.key = key;
this.name = name;
}
public String toString() {
return name + " has the key " + key;

University Of Hail
College of Computer Science and Engineering
!
ICS-202 Data structure
Final Lab Exam
!
}
}!

Anda mungkin juga menyukai