0% found this document useful (0 votes)
28 views

Data Structure Lab 4

The document describes a LinkedList data structure implemented in Java. It includes Node and LinkedList classes that allow adding, removing, and traversing nodes in the list. Methods are provided to insert nodes at the front and back, delete from the front and back, find the number of occurrences of a value, count odd numbers, and add two linked lists together by traversing simultaneously and summing node values. A main method demonstrates creating and manipulating linked lists.

Uploaded by

tahaayyad0
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Data Structure Lab 4

The document describes a LinkedList data structure implemented in Java. It includes Node and LinkedList classes that allow adding, removing, and traversing nodes in the list. Methods are provided to insert nodes at the front and back, delete from the front and back, find the number of occurrences of a value, count odd numbers, and add two linked lists together by traversing simultaneously and summing node values. A main method demonstrates creating and manipulating linked lists.

Uploaded by

tahaayyad0
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

EX1:

public class Node {


int data;
Node next;

public Node() {
this.data = 0;
this.next = null;
}

public Node(int d) {
this.data = d;
this.next = null;
}
}

---------------------------------------
public class LinkedList {

private Node first;

public LinkedList() {
this.first = null;
}

public boolean isEmpty() {


return first == null;
}

public void insertAtFront(int d) {


Node newNode = new Node(d);
newNode.next = first;
first = newNode;
}

public void insertAtBack(int d) {


Node newNode = new Node(d);
if (isEmpty()) {
first = newNode;
} else {
Node current = first;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
}

public void deleteFromFront() {


if (!isEmpty()) {
first = first.next;
}
}

public void deleteFromBack() {


if (!isEmpty()) {
if (first.next == null) {
first = null;
} else {
Node current = first;
while (current.next.next != null) {
current = current.next;
}
current.next = null;
}
}
}

public int OccurrenceNb(int a) {


int count = 0;
Node current = first;
while (current != null) {
if (current.data == a) {
count++;
}
current = current.next;
}
return count;
}

public int OddNbs() {


int count = 0;
Node current = first;
while (current != null) {
if (current.data % 2 != 0) {
count++;
}
current = current.next;
}
return count;
}

public LinkedList AddTwoLists(LinkedList list) {


LinkedList result = new LinkedList();
Node current1 = this.first;
Node current2 = list.first;
int carry = 0;

while (current1 != null || current2 != null) {


int sum = carry;
if (current1 != null) {
sum += current1.data;
current1 = current1.next;
}
if (current2 != null) {
sum += current2.data;
current2 = current2.next;
}

carry = sum / 10;


sum %= 10;
result.insertAtBack(sum);
}

if (carry > 0) {
result.insertAtBack(carry);
}
return result;
}

public void display() {


Node current = first;
while (current != null) {
System.out.print(current.data + " --> ");
current = current.next;
}
System.out.println("null");
}
}
--------------------------------------------
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

LinkedList list1 = new LinkedList();

System.out.print("How many elements you want to insert at front? ");


int frontElements = scanner.nextInt();

System.out.println("Enter the " + frontElements + " elements:");


for (int i = 0; i < frontElements; i++) {
int data = scanner.nextInt();
list1.insertAtFront(data);
}

LinkedList list2 = new LinkedList();

System.out.print("How many elements you want to insert at back? ");


int backElements = scanner.nextInt();

System.out.println("Enter the " + backElements + " elements:");


for (int i = 0; i < backElements; i++) {
int data = scanner.nextInt();
list1.insertAtBack(data);
}

System.out.println("List 1:");
list1.display();

System.out.println("Delete the element at the front of List 1:");


list1.deleteFromFront();
list1.display();

System.out.println("Delete the element at the back of List 1:");


list1.deleteFromBack();
list1.display();

System.out.print("Enter a value to find the number of occurrences: ");


int valueToFind = scanner.nextInt();
int occurrences = list1.OccurrenceNb(valueToFind);
System.out.println("The number of occurrences of element " + valueToFind +
": " + occurrences + " time(s)");

int oddCount = list1.OddNbs();


System.out.println("The count of odd numbers in List 1: " + oddCount);
System.out.println("Create List 2:");
System.out.print("How many elements you want to insert at back for List 2?
");
int backElementsList2 = scanner.nextInt();

System.out.println("Enter the " + backElementsList2 + " elements for List


2:");
for (int i = 0; i < backElementsList2; i++) {
int data = scanner.nextInt();
list2.insertAtBack(data);
}

System.out.println("List 2:");
list2.display();

LinkedList sumList = list1.AddTwoLists(list2);


System.out.println("Sum of the two lists:");
sumList.display();

--------------------
EX2:

public class Task {

private String title;


private String description;
private String dueDate;

public Task(String title, String description, String dueDate) {


this.title = title;
this.description = description;
this.dueDate = dueDate;
}

public String getTitle() {


return title;
}

public String getDescription() {


return description;
}

public String getDueDate() {


return dueDate;
}

------------------------------------

public class Node {

private Task task;


private Node next;

public Node(Task task) {


this.task = task;
this.next = null;
}

public Task getTask() {


return task;
}

public Node getNext() {


return next;
}

public void setNext(Node next) {


this.next = next;
}

----------------------------------
public class TaskList {

private Node head;

public TaskList() {
head = null;
}

public void addTask(Task task) {


Node newNode = new Node(task);
if (head == null) {
head = newNode;
} else {
Node current = head;
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(newNode);
}
}

public void listTasks() {


Node current = head;
while (current != null) {
Task task = current.getTask();
System.out.println("Title: " + task.getTitle());
System.out.println("Description: " + task.getDescription());
System.out.println("Due Date: " + task.getDueDate());
System.out.println("---------------------------");
current = current.getNext();
}
}

public TaskList getTasksForDate(String targetDueDate) {


TaskList tasksForTargetDueDate = new TaskList();
Node current = head;
while (current != null) {
Task task = current.getTask();
if (task.getDueDate().equals(targetDueDate)) {
tasksForTargetDueDate.addTask(task);
}
current = current.getNext();
}
return tasksForTargetDueDate;
}

public void removeTask(String title) {


if (head == null) {
return;
}

if (head.getTask().getTitle().equals(title)) {
head = head.getNext();
return;
}

Node current = head;


while (current.getNext() != null) {
if (current.getNext().getTask().getTitle().equals(title)) {
current.setNext(current.getNext().getNext());
return;
}
current = current.getNext();
}
}
}

--------------------------

public static void main(String[] args) {


TaskList taskList = new TaskList();

taskList.addTask(new Task("Buy groceries", "Milk, eggs, bread", "2023-10-


10"));
taskList.addTask(new Task("Finish homework", "Math assignment", "2023-10-
15"));
taskList.addTask(new Task("Meeting", "Team meeting at 2 PM", "2023-10-
12"));
taskList.addTask(new Task("Read a book", "Chapter 5 of 'Data Structures'",
"2023-10-10"));

System.out.println("All Tasks:");
taskList.listTasks();

System.out.println("Tasks for date: 2023-10-10:");


TaskList tasksForDate = taskList.getTasksForDate("2023-10-10");
tasksForDate.listTasks();

System.out.println("After removing 'Buy groceries' task:");


taskList.removeTask("Buy groceries");
taskList.listTasks();
}

You might also like