Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

CS212T: DATA STRUCTURE TERM: 1ST TERM(2022)

LAB: 04

General notes:
● Each exercise is a separate project and should be saved in a
different folder. The name of the folder is the exercise number.
● Write your name, ID, and PC number as a comment on the
main class.
● Zip your folders into one folder and upload your answer to the
blackboard before the end of the class. Late submission is
considered absent.
Exercise 1:
A. Write a class City which contains the following data and methods:
Data:
● Name (string)
● Population (integer)

Methods:
● Get and set for each data item
● Print City Info
B. Write a class Node which contains the following data and methods:
Data:
● Data (City)
● Next (Node)

Methods:
● Get and set for each data item

C. Write a class LinkedListOfCities which contains the following data and


methods:
Data:
● Head (Node)
Methods:
● Get and set for Head
● void insertNode(City c), this method inserts a city at the end of linked list.
● City getMostPopulatedCity(Node n, Node mostPopulated), this method
recursively calculate, and then returns, the most populated city.
In the Main Function:
The user should be able to choose one option either to add a new city, get the most
populated city or exit the program.
Lab Exercise 2:
Write a Java program that:
A. Ask the user to input the size of an integer linked list
B. Read list’s elements from user
C. Ask the user to input two numbers to be swapped in the linked list
D. Then call a method “ swap(LinkedList<Integer> list, int element1, int
element2)” which will swap the two elements in the linked list.
E. Finally, print the linked list elements before and after swapping.

You might also like