[go: up one dir, main page]

0% found this document useful (0 votes)
6 views7 pages

Lab 4 Dsa

Uploaded by

zoya.23bce9358
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Lab 4 Dsa

Uploaded by

zoya.23bce9358
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

NAME- SYED SADIA ZOYA

REG NO- 23BCE9358

ASSIGNMENT- 4
1. import java.util.Scanner;

class Node {
int data;
Node next;

public Node(int data) {


this.data = data;
this.next = null;
}
}

class LinkedList {
private Node head;

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

public void addNode(int data) {


Node newNode = new Node(data);

if (head == null) {
head = newNode;
} else {

Node current = head;


while (current.next != null) {
current = current.next;
}

current.next = newNode;
}
}

public void displayList() {


if (head == null) {
System.out.println("The list is empty.");
return;
}

Node current = head;


System.out.print("Linked List: ");

while (current != null) {


System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
}

public class SinglyLinkedListDemo {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
LinkedList linkedList = new LinkedList();

System.out.println("Enter integers to add to the


linked list (enter -1 to stop):");
while (true) {
System.out.print("Enter an integer: ");
int data = scanner.nextInt();
if (data == -1) {
break;
}

linkedList.addNode(data);
}

linkedList.displayList();

scanner.close();
}
}

OUTPUT:
2. import java.util.Scanner;

class SongNode {
String title;
SongNode next;

public SongNode(String title) {


this.title = title;
this.next = null;
}
}

class Playlist {
private SongNode head;

public Playlist() {
this.head = null;
}

public void addSong(String title) {


SongNode newSong = new SongNode(title);

if (head == null) {
head = newSong;
} else {

SongNode current = head;


while (current.next != null) {
current = current.next;
}
current.next = newSong;
}
}

public void displayPlaylist() {


if (head == null) {
System.out.println("The playlist is empty.");
return;
}

SongNode current = head;


System.out.println("Playlist:");

while (current != null) {


System.out.println(current.title);
current = current.next;
}
}
}

public class PlaylistManager {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Playlist playlist = new Playlist();

System.out.println("Enter song titles to add to the playlist (enter 'done'


to stop):");
while (true) {
System.out.print("Enter song title: ");
String title = scanner.nextLine();
if (title.equalsIgnoreCase("done")) {
break;
}

playlist.addSong(title);
}

playlist.displayPlaylist();

scanner.close();
}
}

You might also like