Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Data Structure Lab 5

The document defines two data structures: DoublyLinkedList and CircularLinkedList. DoublyLinkedList implements a doubly linked list with methods to insert and delete nodes from the front, back, or a given position. CircularLinkedList implements a circular linked list with similar methods as well as a method to return a new circular linked list containing only the even elements. Both classes include a main method to test the data structures by inserting elements from user input and displaying the results.

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)
14 views

Data Structure Lab 5

The document defines two data structures: DoublyLinkedList and CircularLinkedList. DoublyLinkedList implements a doubly linked list with methods to insert and delete nodes from the front, back, or a given position. CircularLinkedList implements a circular linked list with similar methods as well as a method to return a new circular linked list containing only the even elements. Both classes include a main method to test the data structures by inserting elements from user input and displaying the results.

Uploaded by

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

EX1:

public class Node {

public int value;


public Node previous;
public Node next;

Node() {
value = 0;
previous = null;
next = null;
}

Node(int val) {
value = val;
previous = null;
next = null;
}

public class DoublyLinkedList {

private Node head;


private Node tail;

DoublyLinkedList() {
head = null;
tail = null;
}

public boolean isEmpty() {


return head == null;
}

public void insertAtFront(int val) {


Node newNode = new Node(val);
if (isEmpty()) {
head = newNode;
tail = newNode;
} else {
head.previous = newNode;
newNode.next = head;
head = newNode;
head.previous = null;
}
}

public void insertAtBack(int val) {


Node newNode = new Node(val);
if (isEmpty()) {
head = newNode;
tail = newNode;
} else {
newNode.previous = tail;
tail.next = newNode;
tail = newNode;
tail.next = null;
}
}

public void displayForward() {


Node current = head;
System.out.print("null <-- ");
while (current != null) {
System.out.print(current.value + " --> ");
current = current.next;
}
System.out.println("null");
}

public void displayBackward() {


Node current = tail;
System.out.print("null <-- ");
while (current != null) {
System.out.print(current.value + " --> ");
current = current.previous;
}
System.out.println("null");
}

public void deleteFromFront() {


if (!isEmpty()) {
head = head.next;
head.previous = null;
}
}

public void deleteFromBack() {


if (!isEmpty()) {
tail = tail.previous;
tail.next = null;
}
}

public void deleteFromPosition(int p) {


if (p < 0) {
return; // Invalid position, do nothing
}

if (head == null) {
return;
} else {
Node current = head;

for (int i = 0; i < p; i++) {


current = current.next;
}
if (current == head) {
head = current.next;
head.previous = null;
} else if (current == tail) {
tail = current.previous;
tail.next = null;
} else {
current.previous.next = current.next;
current.next.previous = current.previous;
}
// Delete the middle node
current = null;
}
}

public int countOccurrences(int val) {


int counter = 0;
Node current = head;
while (current != null) {
if (current.value == val) {
counter++;
}
current = current.next;
}
return counter;
}

public void replaceOddEven() {


replaceOddEvenRecursion(head);
}

public void replaceOddEvenRecursion(Node current) {


if (current == null) {
return;
}
if (current.value % 2 == 0) {
current.value = 0;
} else {
current.value = 1;
}
replaceOddEvenRecursion(current.next);
}

public static void main(String[] args) {


// TODO Auto-generated method stub

Scanner scan = new Scanner(System.in);

DoublyLinkedList d = new DoublyLinkedList();

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


int n = scan.nextInt();

System.out.println("Insert " + n + " elements at front: ");


for (int i = 0; i < n; i++) {
int val = scan.nextInt();
d.insertAtFront(val);
}

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


int m = scan.nextInt();

System.out.println("Insert " + m + " elements at back: ");


for (int i = 0; i < m; i++) {
int val = scan.nextInt();
d.insertAtBack(val);
}
System.out.println("X--------------------------------------------------------------
------------------");

System.out.println("Doubly Linked List displayed in a forward direction:");


d.displayForward();

d.deleteFromPosition(0);
d.displayBackward();
d.displayForward();

System.out.println("---------------------------------------------------------------
-----------------");

System.out.println("Doubly Linked List displayed in a backward


direction:");
d.displayBackward();

System.out.println("---------------------------------------------------------------
-----------------");

System.out.println("Assume we are working with the Doubly Linked List


sorted in a forward direction:");
d.displayForward();

System.out.println("---------------------------------------------------------------
-----------------");

System.out.println("Result after deleting the first element:");


d.deleteFromFront();
d.displayForward();

System.out.println("---------------------------------------------------------------
-----------------");

System.out.println("Result after deleting the last element:");


d.deleteFromBack();
d.displayForward();

System.out.println("---------------------------------------------------------------
-----------------");

System.out.println("Result after deleting the element at position 3:");


d.deleteFromPosition(3);
d.displayForward();

System.out.println("---------------------------------------------------------------
-----------------");

System.out.print("Enter a number: ");


int val = scan.nextInt();
System.out.println(val + " occurred " + d.countOccurrences(val) + "
time(s).");
System.out.println("---------------------------------------------------------------
-----------------");

System.out.println("Result after replacing odd numbers with 1 and even


numbers with 0: ");
d.replaceOddEven();
d.displayForward();

}
}

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

EX2:

public class Node {

public int value;


public Node next;

public Node() {
value = 0;
next = null;
}

public Node(int val) {


value = val;
next = null;
}

public class CircularLinkedList {

public Node head;


public Node tail;

public CircularLinkedList() {
head = null;
tail = null;
}

public boolean isEmpty() {


return head == null;
}

public void insertAtFront(int val) {


Node newNode = new Node(val);
if (isEmpty()) {
head = newNode;
tail = newNode;
tail.next = head;
} else {
tail.next = newNode;
newNode.next = head;
head = newNode;
}
}

public void insertAtBack(int val) {


Node newNode = new Node(val);
if (isEmpty()) {
head = newNode;
tail = newNode;
tail.next = head;
} else {
Node current = head;
while (current.next != head) {
current = current.next;
}
current.next = newNode;
tail = newNode;
newNode.next = head;
}
}

public void display() {


Node current = this.head;
if (current != null) {
System.out.print(current.value + " --> ");
current = current.next;
}
while (current != head) {
System.out.print(current.value + " --> ");
current = current.next;
}
System.out.println();
}

public void deleteFromFront() {


if (isEmpty()) {
System.out.println("List is empty. Nothing to delete.");
return;
}

if (head == tail) {
head = null;
tail = null;
} else {
head = head.next;
tail.next = head;
}
}

public void deleteFromBack() {


if (isEmpty()) {
System.out.println("List is empty. Nothing to delete.");
return;
}

if (head == tail) {
head = null;
tail = null;
return;
}
Node current = head;
Node prev = null;

while (current.next != head) {


prev = current;
current = current.next;
}

prev.next = head;
tail = prev;
}

public CircularLinkedList evenCLL() {


CircularLinkedList eCLL = new CircularLinkedList();
Node current = this.head;

if (current == null) return eCLL;

if (current.value % 2 == 0)
eCLL.insertAtBack(current.value);

current = current.next;

while (current != head) {


if (current.value % 2 == 0)
eCLL.insertAtBack(current.value);
current = current.next;
}
return eCLL;
}

public static void main(String[] args) {


// TODO Auto-generated method stub

CircularLinkedList c = new CircularLinkedList();

Scanner scan = new Scanner(System.in);

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


int n = scan.nextInt();

System.out.println("Insert " + n + " elements at front: ");


for (int i = 0; i < n; i++) {
int val = scan.nextInt();
c.insertAtFront(val);
}

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


int m = scan.nextInt();

System.out.println("Insert " + m + " elements at back: ");


for (int i = 0; i < m; i++) {
int val = scan.nextInt();
c.insertAtBack(val);
}

System.out.println("---------------------------------------------------------------
-----------------");
System.out.println("Circular Linked List elements:");
c.display();

System.out.println("---------------------------------------------------------------
-----------------");

System.out.println("Result after deleting the first element:");


c.deleteFromFront();
c.display();

System.out.println("---------------------------------------------------------------
-----------------");

System.out.println("Result after deleting the last element:");


c.deleteFromBack();
c.display();

System.out.println("---------------------------------------------------------------
-----------------");

System.out.println("Circular Linked List with only even numbers:");


CircularLinkedList c2 = new CircularLinkedList();
c2 = c.evenCLL();
c2.display();
}
}

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

EX3:

public class Song {


private String songTitle;
private String artistName;

public Song() {
this.songTitle = null;
this.artistName = null;
}

public Song(String title, String artist) {


this.songTitle = title;
this.artistName = artist;
}

public String getTitle() {


return songTitle;
}

public String getArtist() {


return artistName;
}
@Override
public String toString() {
return "Song{" + "title=" + songTitle + ", artist=" + artistName + '}';
}
}

public class Node {


private Song song;
private Node nextNode;

public Node(Song song) {


this.song = song;
this.nextNode = null;
}

public Song getSong() {


return song;
}

public Node getNextNode() {


return nextNode;
}

public void setNextNode(Node nextNode) {


this.nextNode = nextNode;
}
}

import java.util.Random;

public class Playlist {

private Node firstNode;


private Node lastNode;
private int playlistSize;

public Playlist() {
firstNode = null;
lastNode = null;
playlistSize = 0;
}

public boolean isEmpty() {


return playlistSize == 0;
}

public void clear() {


firstNode = null;
lastNode = null;
playlistSize = 0;
}

public void play() {


if (isEmpty()) {
System.out.println("Playlist is empty.");
return;
}

Node current = firstNode;


do {
Song song = current.getSong();
System.out.println("Title: " + song.getTitle() + ", Artist: " +
song.getArtist());
current = current.getNextNode();
} while (current != firstNode);
}

public void addSong(Song song) {


Node newNode = new Node(song);
if (firstNode == null) {
firstNode = newNode;
lastNode = newNode;
firstNode.setNextNode(firstNode);
} else {
lastNode.setNextNode(newNode);
newNode.setNextNode(firstNode);
lastNode = newNode;
}
playlistSize++;
}

public void addSongAt(int index, Song newSong) {


if (index < 0 || index > playlistSize) {
System.out.println("Invalid index.");
return;
}

if (index == 0) {
Node newNode = new Node(newSong);
newNode.setNextNode(firstNode);
firstNode = newNode;
playlistSize++;
return;
}

Node current = firstNode;


Node previous = null;

int i = 0;
while (i < index) {
previous = current;
current = current.getNextNode();
i++;
}

Node newNode = new Node(newSong);


newNode.setNextNode(current);
previous.setNextNode(newNode);
playlistSize++;
}

public Song getRandomSong() {


if (firstNode == null) {
System.out.println("Playlist is empty.");
return null;
}

Random rand = new Random();


int randomIndex = rand.nextInt(playlistSize);

Node current = firstNode;


for (int i = 0; i < randomIndex; i++) {
current = current.getNextNode();
}

return current.getSong();
}

public Song getSongByTitle(String title) {


if (firstNode == null) {
return null;
}

Node current = firstNode;


while (current != lastNode) {
if (current.getSong().getTitle().equals(title)) {
return current.getSong();
}
current = current.getNextNode();
}

if (current.getSong().getTitle().equals(title)) {
return current.getSong();
}

return null;

public Song getSongByTitleRecursive(Node current, String title) {


if (current == null) {
return null;
}

if (current.getSong().getTitle().equals(title)) {
return current.getSong();
}

return getSongByTitleRecursive(current.getNextNode(), title);


}

public void removeSong(String title) {


Song songToRemove = getSongByTitle(title);

if (songToRemove == null) {
return;
}

Node current = firstNode;


Node previous = null;

while (current.getNextNode() != firstNode) {


if (current.getSong() == songToRemove) {
break;
}
previous = current;
current = current.getNextNode();
}

if (current.getSong() == songToRemove) {
if (current == firstNode) {
if (playlistSize == 1) {
firstNode = null;
lastNode = null;
} else {
firstNode = current.getNextNode();
previous.setNextNode(firstNode);
}
} else {
previous.setNextNode(current.getNextNode());
if (current == lastNode) {
lastNode = previous;
}
}
playlistSize--;
}
}
}
public static void main(String[] args) {
Playlist playlist = new Playlist();

playlist.addSong(new Song("Song 1", "Artist 1"));


playlist.addSong(new Song("Song 2", "Artist 2"));
playlist.addSong(new Song("Song 3", "Artist 3"));

System.out.println("Playlist Contents:");
playlist.play();

playlist.addSongAt(1, new Song("New Song 1", "New Artist 1"));

Song randomSong = playlist.getRandomSong();


System.out.println("The random song is: " + randomSong.getTitle() + " by "
+ randomSong.getArtist());

String songTitleToRemove = "Song 2";


playlist.removeSong(songTitleToRemove);

String songTitleToRetrieve = "Song 3";


Song songByTitle = playlist.getSongByTitle(songTitleToRetrieve);
if (songByTitle != null) {
System.out.println("Song found by title: " + songByTitle.getTitle() + "
by " + songByTitle.getArtist());
} else {
System.out.println("Song not found with the specified title.");
}

playlist.clear();
System.out.println("Is the playlist empty after clearing? " +
playlist.isEmpty());
}
}

You might also like