You are on page 1of 6

COMSATS University Islamabad, Virtual Campus

Data Structures
Assignment # 2
Fall 2023

Submitted To:

Prof. Imran Latif

Submitted By:

Muhammad Saad

Registration #:

FA22-BCS-076

Submission Date:

10/26/2023

Section:

D
Q-1. Write and test this method: Node concat(Node list1, Node list2)//returns: a new list that
contains a copy of list1, followed by a copy of list2;For example, if list1 is {22,33,44,55} and list2
is {66,77,88,99}, then concat (list1,list2) will return the new list {22,33,44,55, 66,77,88,99}.
Answer:
public static Node concat(Node list1, Node list2) {
if (list1 == null) {
return list2;
}
if (list2 == null) {
return list1;
}
Node head = new Node(list1.data);
Node tail = head;
while (list1.Next != null) {
list1 = list1.Next;
tail.Next = new Node(list1.data);
tail = tail.Next;
}
while (list2 != null) {
tail.Next = new Node(list2.data);
tail = tail.Next;
list2 = list2.Next;
}
return head;
}
Q-2: Write and test this method:
void replace (Node list, int I, int x)//replaces the ith element with x;
For example, if list is {22,33,44,55, 66,77,88,99}, then replace (list, 2, 50) will change list to
{22,33,50,55, 66,77,88,99}.
Answer:

void replace (Node list, int I, int x)


{

Node current=list;

int count=0;

while (current! =null) {

count++;

if (count == i)

current.data = x;

current=current.Next;

Q-3Give an algorithm for finding the second-to-last node in a singly linked list.

Algorithm: FindSecondToLast(Singly Linked List)

Input: A singly linked list

Output: The second-to-last node in the list or null if there isn't one

Steps:

1. Initialize two pointers, first and second, and set them both to the head of the list.

2. If the list is empty or contains only one node, return null (there's no second-to-last node).

3. Move the first pointer to the second node in the list by advancing it twice: first = first.next followed by
first = first.next.

4. While first.next is not null:

Move the first pointer one step further by setting first = first.next.

Move the second pointer one step further by setting second = second.next.

5. Return the node pointed to by the second pointer as the second-to-last node.

Q-4Give an implementation of the size( ) method for the Circularly LinkedList class, assuming
that we did not maintain size as an instance variable.

Answer:
public int size() {

if (head == null) {

return 0;

int count = 1;

Node current = head.getNext();

while (current != head) {

count++;

current = current.getNext();

return count;

Q-5 Implement a rotate(int d) method in the SinglyLinkedList class, which rotates the given
linked list at specified index.
Given linked list 10 20 30 40 50 60 Where d=4
Rotated Linked list 50 60 10 20 30 40
Answer:

public static void rotate(int d) {

if (Head == null || d <= 0) {

return;

Node current = Head;

int count = 1;

while (count < d && current != null) {

current = current.Next;

count++;

if (current == null) {

return;
}

Node newHead = current.Next;

current.Next = null;

// Traverse the list to find the current tail and update its next pointer

current = newHead;

while (current.Next != null) {

current = current.Next;

current.Next = Head;

// Update the head to the new head

Head = newHead;

Q-6 Add and test a method for the LinkedQueue class that reverses the order of the elements in the
queue.

Answer :

public void reverse() {

Stack<T> stack = new Stack<>();

while (!isEmpty()) {

stack.push(dequeue());

while (!stack.isEmpty()) {

enqueue(stack.pop());

Q-7Add and test a method for the LinkedQueue class that removes and returns the element that
is second from the front, if it exists.
Answer :
public int removeSecond() {

if (front == null || front.next == null || front.next.next == null) {

return -1;

int data = front.next.data;

front.next = front.next.next;

return data;

You might also like