import-java
import-java
*;
class Node{
int info;
Node next;
Node(){
Node(int x){
info = x;
next = null;
info = x;
next = p;
class MyList{
MyList(){
boolean isEmpty(){
void clear(){
if(isEmpty()){
} else {
head = q;
if(isEmpty()){
} else {
tail.next = q;
tail = q;
if (head == null)
return;
if(x == 0) {
head = temp.next;
return;
}
for (int i=0; temp!= null && i<x-1; i++)
temp = temp.next;
return;
temp.next = next;
Node p = head;
p=p.next;
p.next = p.next.next;
if(p.next==null){
tail = p;
current = current.next;
current.next = newNode;
Node p = head;
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()){
return;
System.out.print(p.info+" ");
while(p.next!=null){
p = p.next;
System.out.print("\n");
return;
Node p = head;
p = p.next;
if(p!= null){
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;
newNode.next = head;
head = newNode;
return;
}
Node current = head;
current = current.next;
newNode.next = current.next;
current.next = newNode;
void sortList(){
int temp;
if(head == null){
return;
} else {
while(p != null){
index = p.next;
while(index != null){
temp = p.info;
p.info = index.info;
index.info = temp;
index = index.next;
p = p.next;
}
String printList(){
Node p = head;
finalString += t;
while(p.next!=null){
p = p.next;
finalString += t;
return finalString;
int maxValue(){
while(current != null){
maxValue = current.info;
current = current.next;
return maxValue;
int minValue(){
while(current != null){
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;
return false;
p=p.next;
return true;
void reverse(){
Node p = head;
Node nextNode;
while(p!= null){
nextNode = p.next;
p.next = prev;
prev = p;
p = nextNode;
head = prev;
Node p1 = head;
Node p2 = list2.head;
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;
if( p == null){
p = list2.head;
} else{
Node current = p;
while(current.next != null){
current = current.next;
current.next = list2.head;
Node p1 = head;
Node p2 = list2.head;
set1.add(p1.info);
p1=p1.next;
set2.add(p2.info);
p2=p2.next;
if (set1.equals(set2) ){
System.out.println("yes");
return true;
}else{
System.out.println("no");
return false;
n = sc.nextInt();
switch(option){
case 1:{
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:{
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: {
List1.addToTail(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());
List1.traverse();
List1.traverse();
break;
case 5: {
List1.addToTail(sc.nextInt());
List1.traverse();
List1.deletePos(0);
List1.traverse();
break;
case 6: {
List1.addToTail(sc.nextInt());
List1.traverse();
List1.deletePos(11);
List1.traverse();
break;
}
case 7: {
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: {
List1.addToTail(sc.nextInt());
x = sc.nextInt();
List1.traverse();
List1.deleteElement(x);
List1.traverse();
break;
case 9: {
x = sc.nextInt();
List1.traverse();
if(List1.findPosition(x)){
System.out.print(x);
};
break;
case 10:{
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:{
List1.addToTail(sc.nextInt());
List1.traverse();
List1.deletePos(2);
List1.traverse();
break;
case 12:{
List1.addToTail(sc.nextInt());
List1.traverse();
List1.sortList();
List1.traverse();
break;
case 13:{
List1.addToTail(sc.nextInt());
List1.traverse();
List1.deleteElement(1);
List1.traverse();
break;
case 14: {
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: {
List1.addToTail(sc.nextInt());
x = sc.nextInt();
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: {
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: {
List1.addToTail(sc.nextInt());
x = sc.nextInt();
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: {
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: {
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: {
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: {
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: {
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: {
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: {
List1.addToTail(sc.nextInt());
}
List1.traverse();
List1.reverse();
List1.traverse();
break;
case 25: {
List1.addToTail(sc.nextInt());
x = sc.nextInt();
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;