0% found this document useful (0 votes)
78 views3 pages

Intermediate Object-Oriented Programming: Practice Class 09 (Week 10)

The document describes a LinkedList class and Node class that are used to implement a linked list data structure. It provides code for the classes and tasks the reader with drawing the state of a linked list and variables after code segments are executed, writing an algorithm to reverse the order of nodes in a linked list, and representing a linked list where nodes contain Person objects instead of name and age fields.

Uploaded by

Shubham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views3 pages

Intermediate Object-Oriented Programming: Practice Class 09 (Week 10)

The document describes a LinkedList class and Node class that are used to implement a linked list data structure. It provides code for the classes and tasks the reader with drawing the state of a linked list and variables after code segments are executed, writing an algorithm to reverse the order of nodes in a linked list, and representing a linked list where nodes contain Person objects instead of name and age fields.

Uploaded by

Shubham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Intermediate Object-Oriented Programming

Practice Class 09 (Week 10)


Linked Lists
______________________________________________________________________
Task 1
Study the class below.
public class LinkedList
{
private Node head;
//Constructor
public LinkedList(){...}

//Adds a Node object to the front of the Linked List


public void addFirst(Node newNode){...}

//Returns the Node object at the front of the Linked List,


//does NOT remove it
public Node getFirst(){...}

//Removes and returns the Node object at the front


public Node removeFirst(){...}

//Returns the number of Nodes in the list, 0 if empty


public int size(){...}

//Returns a String description of the object


public String toString(){...}
}

public class Node


{
private String name;
private int age;
private Node next;
//Constructor
public Node(String name, int age){...}

//Sets the next field to the Node object pass in as parameter


public void setNext(Node node){...}

//Returns name
public String getName(){...}

//Returns age
public int getAge(){...}

1
//Returns next
public Node getNext(){...}

//Returns a String description of the object


public String toString(){...}
}

Using the class definitions above and the following declarations, draw the state of
the linked list and
the variables
after the execution of each code fragment (the segments are executed one after the
other).

LinkedList myList = new LinkedList();

a) Node newNode = new Node("Bob", 11);

b) myList.addFirst(newNode);

c) newNode = new Node("Zac", 9);


myList.addFirst(newNode);
newNode = new Node("Jane", 7);
myList.addFirst(newNode);
newNode = null;

d) newNode = new Node("Peter", 12);


Node temp = myList.getFirst();

e) while (true) // repeatedly examine temp


// there are two possible cases
{
if (temp.getNext() == null)
{
break;
}
else
{
temp = temp.gextNext();
}
}

temp.setNext(newNode);
newNode = null;

2
f) temp = myList.getFirst();

while (true)
{
if(temp.getNext().getName().equals(Bob)
{
temp.setNext(temp.getNext().getNext());
return;
}
else
{
temp = temp.getNext();
}
}

Task 2

a) Write an algorithm (pseudo code), which can be used to implement a method in


the LinkedList class, that will reverse the order of the nodes in the linked list. For
example, the following original linked list:
head
Jane Bob Jill Peter Zoe
7 11 5 15 9
null

becomes:
head
Zoe Peter Jill Bob Jane
9 15 5 11 7
null

Note: There are several ways to solve this problem. For your solution, try to make the
most use of the existing method of the given class.

b) Code your algorithm as a method of the given LinkedList class.

Task 3
If we change the Node class from Task 1 so that instead of having a name and age
attribute, it has a data attribute that is an instance of a user-defined class Person, draw
a representation of the resulting linked list.

You might also like