DSA_Assignment2_Opt01 - Linked List
DSA_Assignment2_Opt01 - Linked List
Training Assignments
Version 1.1
Hanoi, 02/2021
Assignment Data Structures and Algorithms Issue/Revision: x/1
RECORD OF CHANGES
Contents
Assignment 2: Linked List..................................................................................................................4
Objectives:....................................................................................................................................4
Assignment Descriptions:...........................................................................................................4
CODE: DSA_Assignment2_Opt01
TYPE: N/A
LOC: N/A
DURATION: 180 MINUTES
Assignment Descriptions:
b) Write a code fragment to add a new node with key 17 just after the second node of the list.
Assume that there are at least two nodes on the list.
c) Write an iterative function count() that takes a link as input, and prints out the number of
elements in the linked list.
d) Write an iterative function max() that takes a link as input, and returns the value of the
maximum key in the linked list.
Assume all keys are positive, and return -1 if the list is empty.
class LinkedList {
static Node head;
static class Node {
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
Node function(Node node)
{
Node prev = null;
Node current = node;
Node next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
System.out.println(current + ” ”);
}
node = prev;
return node;
}
-- THE END --