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

import-java

The document contains a Java implementation of a singly linked list with various methods for manipulating the list, such as adding, deleting, and searching for elements. It includes a main class that allows user interaction to perform different operations on the list based on user input. The code demonstrates functionalities like merging two lists, checking if the list is sorted, and calculating the sum and average of the list elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

import-java

The document contains a Java implementation of a singly linked list with various methods for manipulating the list, such as adding, deleting, and searching for elements. It includes a main class that allows user interaction to perform different operations on the list based on user input. The code demonstrates functionalities like merging two lists, checking if the list is sorted, and calculating the sum and average of the list elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

import java.util.

*;

class Node{

int info;

Node next;

Node(){

Node(int x){

info = x;

next = null;

Node(int x, Node p){

info = x;

next = p;

class MyList{

Node head, tail;

MyList(){

head = tail= null;

boolean isEmpty(){

return head == null;

void clear(){

head = tail= null;


}

void addToHead(int x){

if(isEmpty()){

head = tail = new Node(x);

} else {

Node q = new Node(x,head);

head = q;

void addToTail(int x){

if(isEmpty()){

head = tail = new Node(x);

} else {

Node q = new Node(x);

tail.next = q;

tail = q;

void deletePos(int x){

if (head == null)

return;

Node temp = head;

if(x == 0) {

head = temp.next;

return;

}
for (int i=0; temp!= null && i<x-1; i++)

temp = temp.next;

if(temp == null || temp.next == null)

return;

Node next = temp.next.next;

temp.next = next;

void deleteAfterOneElement(int target){

Node p = head;

while (p!=null && p.info != target){

p=p.next;

if(p!=null && p.next != null){

p.next = p.next.next;

if(p.next==null){

tail = p;

void addBeforeElement(int target, int addNumber){

Node current = head;

while (current != null && current.next != null && current.next.info != target){

current = current.next;

if (current != null && current.next != null){

Node newNode = new Node(addNumber);


newNode.next = current.next;

current.next = newNode;

void deleteElement(int target){

Node p = head;

Node prev = null;

while (p!=null && p.info != target){

prev = p;

p=p.next;

if(p!=null){

if(prev == null){

head = p.next;

if(head == null){

tail = null;

} else {

prev.next = p.next;

if(p.next == null){

tail = prev;

void traverse(){

Node p = head;
if(isEmpty()){

System.out.print("Empty list encountered.\n");

return;

System.out.print(p.info+" ");

while(p.next!=null){

p = p.next;

System.out.print(p.info + " ");

System.out.print("\n");

return;

void insertAfterNumber(int newnode_info, int target){

Node p = head;

while (p != null && p.info != target){

p = p.next;

if(p!= null){

Node q = new Node(newnode_info, p.next);

p.next = q;

if (p== tail){

tail = q;

}
boolean findPosition(int target){

Node p = head;

while(p != null){

if(p.info == target){

return true;

p = p.next;

return false;

int count(){

int count = 0;

Node p = head;

while (p != null){

count++;

p = p.next;

return count;

void insertSorted(int value){

Node newNode = new Node (value);

if(head == null || value < head.info){

newNode.next = head;

head = newNode;

return;

}
Node current = head;

while (current.next != null && current.next.info < value){

current = current.next;

newNode.next = current.next;

current.next = newNode;

void sortList(){

Node p = head, index = null;

int temp;

if(head == null){

return;

} else {

while(p != null){

index = p.next;

while(index != null){

if(p.info > index.info){

temp = p.info;

p.info = index.info;

index.info = temp;

index = index.next;

p = p.next;

}
String printList(){

String finalString = "";

Node p = head;

String t = p.info + " ";

finalString += t;

while(p.next!=null){

p = p.next;

t = p.info + " ";

finalString += t;

return finalString;

int maxValue(){

int maxValue = head.info;

Node current = head.next;

while(current != null){

if(current.info > maxValue ){

maxValue = current.info;

current = current.next;

return maxValue;

int minValue(){

int minValue = head.info;


Node current = head.next;

while(current != null){

if(current.info < minValue ){

minValue = current.info;

current = current.next;

return minValue;

int sum(){

Node p = head;

int sum = 0;

while (p != null){

sum += p.info;

p = p.next;

return sum;

double sumdb(){

Node p = head;

int sum = 0;

while (p != null){

sum += p.info;

p = p.next;

return sum;

}
boolean checkSort(){

Node p = head;

while(p != null && p.next != null){

if(p.info > p.next.info){

return false;

p=p.next;

return true;

void reverse(){

Node p = head;

Node prev = null;

Node nextNode;

while(p!= null){

nextNode = p.next;

p.next = prev;

prev = p;

p = nextNode;

head = prev;

void mergeList(MyList list2){

Node p1 = head;
Node p2 = list2.head;

MyList mergeList = new MyList();

while (p1 != null && p2 != null){

if (p1.info < p2.info){

mergeList.addToTail(p1.info);

p1 = p1.next;

else{

mergeList.addToTail(p2.info);

p2 = p2.next;

while(p1 != null){

mergeList.addToTail(p1.info);

p1 = p1.next;

while(p2 != null){

mergeList.addToTail(p2.info);

p2 = p2.next;

head = mergeList.head;

tail = mergeList.tail;

void attachList(MyList list2){


Node p = head;

if( p == null){

p = list2.head;

} else{

Node current = p;

while(current.next != null){

current = current.next;

current.next = list2.head;

boolean isEqual(MyList list2){

Node p1 = head;

Node p2 = list2.head;

Set<Integer> set1 = new HashSet<>();

Set<Integer> set2 = new HashSet<>();

while (p1 != null){

set1.add(p1.info);

p1=p1.next;

while (p2 != null){

set2.add(p2.info);

p2=p2.next;

if (set1.equals(set2) ){
System.out.println("yes");

return true;

}else{

System.out.println("no");

return false;

public class List{

public static void main(String[] args){

Scanner sc = new Scanner (System.in);

MyList List1 = new MyList();

MyList List2 = new MyList();

int option = sc.nextInt(), n, x;

n = sc.nextInt();

switch(option){

case 1:{

for(int i = 1; i<= n; i++){

List1.addToTail(sc.nextInt());

x= sc.nextInt();

System.out.print("1. Add "+x+" before the head of " + n +"-element list: ");

List1.traverse();

List1.addToHead(x);

List1.traverse();
break;

case 2:{

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

x = sc.nextInt();

System.out.print("2. Add " + x+" after the tail of "+n+"-element list: ");

List1.traverse();

List1.addToTail(x);

List1.traverse();

break;

case 3: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

int target = sc.nextInt();

int newnode_info = sc.nextInt();

System.out.print("3. Insert an element " + newnode_info + " after the element "+ target+ " in
the "+n+"-element list: ");

List1.traverse();

List1.insertAfterNumber(newnode_info, target);

List1.traverse();

break;

case 4: {
for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.print("4. Traverse the 13-element list: ");

List1.traverse();

List1.traverse();

break;

case 5: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.println("5. Delete the head of the 13-element list: ");

List1.traverse();

List1.deletePos(0);

List1.traverse();

break;

case 6: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.println("6. Delete the tail of the 12-element list: ");

List1.traverse();

List1.deletePos(11);

List1.traverse();

break;
}

case 7: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

x = sc.nextInt();

System.out.println("7. Delete the element after the element 1 of the 11-element list: ");

List1.traverse();

List1.deleteAfterOneElement(x);

List1.traverse();

break;

case 8: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

x = sc.nextInt();

System.out.print("8. Delete the element 3 in the 10-element list: ");

List1.traverse();

List1.deleteElement(x);

List1.traverse();

break;

case 9: {

for(int i = 1; i<=n; i++){


List1.addToTail(sc.nextInt());

x = sc.nextInt();

System.out.print("9. Search the element 6 in the 9-element list: ");

List1.traverse();

if(List1.findPosition(x)){

System.out.print(x);

};

break;

case 10:{

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.print("10. Count the number of the elements of the 9-element list: ");

List1.traverse();

System.out.print(List1.count());

break;

case 11:{

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.print("11. Delete the 3rd element in the 9-node list: ");

List1.traverse();

List1.deletePos(2);

List1.traverse();
break;

case 12:{

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.print("12. Sort in accending order the 8-node list: ");

List1.traverse();

List1.sortList();

List1.traverse();

break;

case 13:{

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.print("13. Delete the element 1 in the 8-node list: ");

List1.traverse();

List1.deleteElement(1);

List1.traverse();

break;

case 14: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

}
System.out.print("14. create and return array containing info of all nodes in the 7-node list: ");

List1.traverse();

break;

case 15: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

x = sc.nextInt();

for(int i = 1; i<=x; i++){

List2.addToTail(sc.nextInt());

System.out.print("15. Merge two ordered singly linked lists of integers into one ordered list: 7-
node list = " + List1.printList() +"; 5-node list: " + List2.printList());

List1.mergeList(List2);

List1.traverse();

break;

case 16: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

x = sc.nextInt();

int y = sc.nextInt();

System.out.print("16. add a node with value 14 before the node 15 in the 12-node list: ");

List1.traverse();
List1.addBeforeElement(15, 14);

List1.traverse();

break;

case 17: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

x = sc.nextInt();

for(int i = 1; i<=x; i++){

List2.addToTail(sc.nextInt());

System.out.println("17. Attach a singly linked list of 6 elements "+ List2.printList()+" to the end
of another singly linked list of 13 nodes: "+ List1.printList());

List1.attachList(List2);

List1.traverse();

break;

case 18: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.print("18. find and return the maximum value in the 19-node list: ");

List1.traverse();

System.out.println(List1.maxValue());

break;

}
case 19: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.print("19. find and return the minimum value in the 19-node list: ");

List1.traverse();

System.out.println(List1.minValue());

break;

case 20: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.print("20. return the sum of all values in the 19-node list: ");

List1.traverse();

System.out.println(List1.sum());

break;

case 21: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.print("21. return the average of all values in the 19-node list: ");

List1.traverse();

System.out.println(String.format("%.2f", List1.sumdb()/n));

break;
}

case 22: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.println("22. check and return true if the 19-node list "+ List1.printList()+"is sorted,
return false if the list is not sorted.");

System.out.print(List1.checkSort());

break;

case 23: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

System.out.println("23. sort the 19-node list: "+ List1.printList() +"then insert a node with value
40 into the sorted list so that the new list is a sorted list");

List1.sortList();

List1.traverse();

List1.insertSorted(40);

List1.traverse();

break;

case 24: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());
}

System.out.print("24. Reverse the singly linked list of 20 nodes: ");

List1.traverse();

List1.reverse();

List1.traverse();

break;

case 25: {

for(int i = 1; i<=n; i++){

List1.addToTail(sc.nextInt());

x = sc.nextInt();

for(int i = 1; i<=x; i++){

List2.addToTail(sc.nextInt());

System.out.print("25. Check whether two singly linked list have the same contents: 1st list of 5
elements: "+List1.printList()+"; 2nd list of 5 elements: "+List2.printList());

List1.isEqual(List2);

break;

You might also like