JavaExercises_1_DSA_List
JavaExercises_1_DSA_List
Lab 1. Lists
Writing Good Programs
The only way to learn programming is program, program and program. Learning
programming is like learning cycling, swimming or any other sports. You can’t learn
by watching or reading books. Start to program immediately. On the other hands, to
improve your programming, you need to read many books and study how the masters
program.
It is easy to write programs that work. It is much harder to write programs that not
only work but also easy to maintain and understood by others – I call these good
programs. In the real world, writing program is not meaningful. You have to write good
programs, so that others can understand and maintain your programs.
Pay particular attention to:
1. Coding style:
• Read Java code convention: ”Google Java Style Guide” or ”Java Code
Conventions - Oracle”.
• Follow the Java Naming Conventions for variables, methods, and classes
STRICTLY. Use CamelCase for names. Variable and method names begin
with lowercase, while class names begin with uppercase. Use nouns for
variables (e.g., radius) and class names (e.g., Circle). Use verbs for
methods (e.g., getArea(), isEmpty()).
• Use Meaningful Names: Do not use names like a, b, c, d, x, x1, x2, and
x1688 - they are meaningless. Avoid single-alphabet names like i, j, k.
They are easy to type, but usually meaningless. Use single-alphabet
names only when their meaning is clear, e.g., x, y, z for co-ordinates and i
for array index. Use meaningful names like row and col (instead of x and y,
i and j, x1 and x2), numStudents (not n), maxGrade, size (not n), and
upperbound (not n again). Differentiate between singular and plural nouns
(e.g., use books for an array of books, and book for each item).
• Use consistent indentation and coding style. Many IDEs (such as Eclipse /
NetBeans) can re-format your source codes with a single click.
3. The problems in this tutorial are certainly NOT challenging. There are tens of
thousands of challenging problems available – used in training for various
programming contests (such as International Collegiate Programming Contest
(ICPC), International Olympiad in Informatics (IOI)).
1
HaQT Data Structures and Algorithms
1 Exercises on Lists
1.1 Question 1
Write a Java program to implement a singly linked list of integer values with the following
operations:
• void addToHead(int x) - add a node with value x at the head of a list. • void
addToTail(int x) - add a node with value x at the tail of a list. • void addAfter(Node p,
int x) - add a node with value x after the node p. • void traverse() - traverse from
head to tail and dislay info of all nodes in the list. • int deleteFromHead() - delete the
head and return its info.
• int deleteAter(Node p) - delete the node after the node p and return its info. •
void delete(int x) - delele the first node whose info is equal to x.
• Node search(int x) - search and return the reference to the first node having info x. •
int count() - count and return number of nodes in the list.
• void dele(int i) - delete an i-th node on the list. Besure that such a node exists. •
void sort() - sort the list by ascending order of info.
• int[] toArray() - create and return array containing info of all nodes in the list. •
Merge two ordered singly linked lists of integers into one ordered list. • void
addBefore(Node p, int x) - add a node with value x before the node p. • Attach a
singly linked list to the end of another singly linked list.
• int max() - find and return the maximum value in the list.
• int min() - find and return the minimum value in the list.
• boolean sorted() - check and return true if the list is sorted, return false if the list is not
sorted.
2
HaQT Data Structures and Algorithms
• void insert(int x) - insert node with value x into sorted list so that the new list is sorted.
• Reverse a singly linked list using only one pass through the list.
• Check whether two singly linked list have the same contents.
1.2 Question 2
Write a Java program to implement a singly linked list of string values with 1 - 10 operations
in the above list.
1.3 Question 3
Write a Java program to implement a doubly linked list of integer values with the above
operations.
1.4 Question 4
Write a Java program to implement a circular linked list of integer values with the above
operations.