DSA Assignment - 2
DSA Assignment - 2
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
class Sllst {
Node head;
newNode.next = head;
head = newNode;
if (head == null) {
head = newNode;
} else {
temp = temp.next;
temp.next = newNode;
if (head != null) {
head = head.next;
if (head == null) {
return;
if (head.next == null) {
head = null;
return;
temp = temp.next;
temp.next = null;
temp = temp.next;
System.out.println("null");
if (temp.data == key) {
return true;
temp = temp.next;
return false;
}
public class Llst {
list.insatbeg(10);
list.insatbeg(20);
list.insatend(30);
list.insatend(40);
list.display();
list.delatbeg();
list.display();
list.delatend();
list.display();
System.out.println(list.search(30));
System.out.println(list.search(50));
class Node {
int data;
Node(int data) {
this.data = data;
class DoublyLinkedList {
Node head;
if (head != null) {
head.prev = newNode;
newNode.next = head;
head = newNode;
if (head == null) {
head = newNode;
return;
temp = temp.next;
}
temp.next = newNode;
newNode.prev = temp;
if (head.next == null) {
head = null;
return;
head = head.next;
head.prev = null;
if (head.next == null) {
head = null;
return;
temp = temp.next;
temp.prev.next = null;
}
public void displayForward() {
temp = temp.next;
System.out.println("null");
temp = temp.next;
temp = temp.prev;
System.out.println("null");
if (temp.data == key) {
return true;
}
temp = temp.next;
return false;
list.insertAtBeginning(10);
list.insertAtBeginning(20);
list.insertAtEnd(30);
list.insertAtEnd(40);
list.displayForward();
list.deleteAtBeginning();
list.displayForward();
list.deleteAtEnd();
list.displayForward();
list.displayBackward();
System.out.println(list.search(30));
System.out.println(list.search(50));
import java.util.ArrayList;
import java.util.List;
class Passenger {
String name;
int age;
String seatnum;
this.name = name;
this.age = age;
this.seatnum = seatnum;
return "Name: " + name + ", Age: " + age + ", Seat: "
+ seatnum;
}
class Compartment {
int compnum;
List<Passenger> passengers;
Compartment(int compnum) {
this.compnum = compnum;
System.out.println(passenger);
class Train {
List<Compartment> compartments;
Train(int initialCompartmentCount) {
compartments.add(new Compartment(i));
compartments.add(new Compartment(currentSize +
i));
compartment.displayPassengers();
compartment.addPassenger("Passenger" + i, 30,
"Seat" + i);
train.delinkCompartment(9, 12);
train.linkCompartment(3);
train.displayTrainDetails();