You are on page 1of 3

CS 220 Data Structures – Fall 2023

Lab #3
Due Date: 11:59 p.m. Thursday 9/21/2023 – Absolute deadline
NOTE: All labs should be individual work. Submit via Blackboard answers to the problems
below.
1. Using the Java API, write a program that creates a linked list with three integer values
inserted to the end of the list. After each insertion, print the linked list to ensure that the
elements are added. Then delete the first element of the list and print the resulting list.
Submit your source code as well as screen shots to show that your program works.
import java.util.*;
import java.io.*;
public class Solution{

public static void main(String[] args){


try{
FileInputStream fs= new FileInputStream("Input.txt");
PrintStream ps= new PrintStream(new FileOutputStream("Output.txt",
false));
System.setIn(fs);
System.setOut(ps);
Scanner sc= new Scanner(System.in);
LinkedList<Integer> list= new LinkedList<>();
for(int i=0;i<3;i++){
list.add(2*i);
System.out.print("After "+ i+1+ " insertions, the list is: ");
System.out.println(list);
}
list.remove(0);
System.out.print("List after deleting the first element is: ");
System.out.println(list);
sc.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
Output:

Output is as expected. The program is running fine.

2. Give an algorithm for finding the second-to-last node in a singly linked list in which the
last node is indicated by a null next reference. No code is needed.

To find the second-to-last node in a singly linked list where the last node is indicated by a null
`next` reference, we can use the following algorithm:
1. Initialize two pointers, `current` and `prev`, and set them to point to the head of the linked list.
2. Traverse the linked list using the two pointers as follows:
a. Move the `current` pointer one node at a time while checking if `current.next` is not null.
This loop ensures that the `current` pointer will stop at the last node of the list.
b. During each iteration, update the `prev` pointer to point to the node that `current` is currently
pointing to.
3. After the loop terminates, the `prev` pointer will be pointing to the second-to-last node in the
linked list.
4.We can now access the data in the second-to-last node using `prev`.
This algorithm ensures that the `prev` pointer always lags one node behind the `current` pointer,
allowing you to identify the second-to-last node when you reach the end of the list.
3. Describe in detail how to swap two nodes x and y (and not just their contents) in a singly
linked list L given references only to x and y. No code is needed.
Swapping two nodes, `x` and `y`, in a singly linked list `L` when you have references only to `x`
and `y` (and not their predecessors) requires careful manipulation of the pointers in the linked
list.
We can do this using the following algorithm:
1. Traverse the linked list from the head node to find `x` and `y`. During this traversal, keep track
of the previous nodes to `x` and `y`, which we'll call `prevX` and `prevY`, respectively.
 Initialize two pointers, `current` and `prev`, to start at the head of the list.
 While traversing the list, look for nodes `x` and `y` by comparing their references
with `current`. Update `prev` to `current` and move `current` to the next node until we
find both `x` and `y`.
2. Once we've located `x` and `y`, we'll have references to `prevX`, `prevY`, `x`, and `y`.
3. Perform the actual swapping of the nodes:
 If `prevX` is `null`, it means `x` is the head of the list. In this case, update the head of
the list to `y` by setting `L.head = y`.
 Otherwise, update `prevX.next` to point to `y`, effectively bypassing `x`.
 If `prevY` is `null`, it means `y` is the head of the list. In this case, update the head of
the list to `x` by setting `L.head = x`.
 Otherwise, update `prevY.next` to point to `x`, effectively bypassing `y`.
4. Finally, update the `next` pointers of `x` and `y` to complete the swap. `x.next` should now
point to what `y` used to point to (i.e., `prevY.next`), and `y.next` should now point to what `x`
used to point to (i.e., `prevX.next`).

You might also like