给定链表的成对交换元素(Java解决方案)
我想解决以下任务: 给定一个单链表,编写一个函数来交换元素 成对的。例如,如果链表为1->2->3->4->5->6->7,则 函数应该将其更改为2->1->4->3->6->5->7,如果 链表是1->2->3->4->5->6,那么函数应该将其更改为 2->1->4->3->6->5 为此,我使用从这里开始的递归方法:,即交换前两个节点,然后对列表的其余部分进行递归。我的职能如下:给定链表的成对交换元素(Java解决方案),java,linked-list,Java,Linked List,我想解决以下任务: 给定一个单链表,编写一个函数来交换元素 成对的。例如,如果链表为1->2->3->4->5->6->7,则 函数应该将其更改为2->1->4->3->6->5->7,如果 链表是1->2->3->4->5->6,那么函数应该将其更改为 2->1->4->3->6->5 为此,我使用从这里开始的递归方法:,即交换前两个节点,然后对列表的其余部分进行递归。我的职能如下: private static ListNode reorder(ListNode l1) {
private static ListNode reorder(ListNode l1) {
if(l1 == null || l1.next == null)
return l1;
ListNode rest = l1.next.next;
//change head
ListNode newHead = l1.next;
//change next of second node
newHead.next = l1;
l1.next = reorder(rest);
return newHead;
}
但是在输入端123456,我有输出端143665?!我调试了它,但仍然看不出问题出在哪里。谁能解释一下为什么会这样?下面是全班同学:
public class Swap {
public static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public static void main(String[] args) {
ListNode l1 = new ListNode(1);
ListNode l2 = new ListNode(2);
ListNode l3 = new ListNode(3);
ListNode l4 = new ListNode(4);
ListNode l5 = new ListNode(5);
ListNode l6 = new ListNode(6);
ListNode l7 = new ListNode(7);
ListNode l8 = new ListNode(8);
ListNode l9 = new ListNode(9);
ListNode l10 = new ListNode(10);
l1.next = l2;
l2.next = l3;
l3.next = l4;
l4.next = l5;
l5.next = l6;
l7.next = l8;
l9.next = l10;
print(l1);
reorder(l1);
System.out.println();
print(l1);
}
private static void print(ListNode l1) {
ListNode current = l1;
while(current != null){
System.out.print(current.val + " ");
current = current.next;
}
}
private static ListNode reorder(ListNode l1) {
if(l1 == null || l1.next == null)
return l1;
ListNode rest = l1.next.next;
//change head
ListNode newHead = l1.next;
//change next of second node
newHead.next = l1;
l1.next = reorder(rest);
return newHead;
}
}
您的列表连接不好,您缺少以下链接:
l6.next = l7;
l8.next = l9;
您的列表连接不好,您缺少以下链接:
l6.next = l7;
l8.next = l9;
您的列表连接不好,您缺少以下链接:
l6.next = l7;
l8.next = l9;
您的列表连接不好,您缺少以下链接:
l6.next = l7;
l8.next = l9;
您正在打印从l1开始的列表,l1现在是第二个元素。你想打电话吗
print(reorder(l1));
您正在打印从l1开始的列表,l1现在是第二个元素。你想打电话吗
print(reorder(l1));
您正在打印从l1开始的列表,l1现在是第二个元素。你想打电话吗
print(reorder(l1));
您正在打印从l1开始的列表,l1现在是第二个元素。你想打电话吗
print(reorder(l1));
这是我做的一个递归方法,对我来说很有效。如果有任何问题,请告诉我
public void pairwiseSwap(Node node){
if(size() == 0){
System.out.println("empty");
return;
}
if(node.next == null){
System.out.println(node.value);
return;
}
Node one = node;
Node two = node.next;
System.out.println(two.value);
System.out.println(one.value);
if(two.next == null)
return;
pairwiseSwap(two.next);
}
这是我做的一个递归方法,对我来说很有效。如果有任何问题,请告诉我
public void pairwiseSwap(Node node){
if(size() == 0){
System.out.println("empty");
return;
}
if(node.next == null){
System.out.println(node.value);
return;
}
Node one = node;
Node two = node.next;
System.out.println(two.value);
System.out.println(one.value);
if(two.next == null)
return;
pairwiseSwap(two.next);
}
这是我做的一个递归方法,对我来说很有效。如果有任何问题,请告诉我
public void pairwiseSwap(Node node){
if(size() == 0){
System.out.println("empty");
return;
}
if(node.next == null){
System.out.println(node.value);
return;
}
Node one = node;
Node two = node.next;
System.out.println(two.value);
System.out.println(one.value);
if(two.next == null)
return;
pairwiseSwap(two.next);
}
这是我做的一个递归方法,对我来说很有效。如果有任何问题,请告诉我
public void pairwiseSwap(Node node){
if(size() == 0){
System.out.println("empty");
return;
}
if(node.next == null){
System.out.println(node.value);
return;
}
Node one = node;
Node two = node.next;
System.out.println(two.value);
System.out.println(one.value);
if(two.next == null)
return;
pairwiseSwap(two.next);
}
这是大多数链表重播问题的解决方案
package com.practice.ds.list;
final public class Node<T> {
public Node<T> next = null;
public Node<T> prev = null;
public T data = null;
public Node() {
}
public Node(T data) {
this.data = data;
}
@Override
public String toString() {
return "Node [next=" + next + ", prev=" + prev + ", data=" + data + "]";
}
}
package com.practice.ds.list;
public class LinkedList<T> {
private Node<T> head = null;
private Node<T> tail = null;
public void insertAtStart(T data) {
throwEmptyDataException(data);
Node<T> node = new Node<T>(data);
if(empty()) {
head = node;
tail = head;
}else {
node.next = head;
head = node;
}
display();
}
public void insertAtEnd(T data) {
throwEmptyDataException(data);
if(empty()) {
insertAtStart(data);
}else {
Node<T> node = new Node<T>(data);
tail.next = node;
tail = node;
display();
}
}
public void insertAtPosition(int position, T data) {
throwEmptyDataException(data);
if (position < 1 || position > size() || empty())
throw new IllegalArgumentException("Can't perform insertion. Please check your inputs");
Node<T> node = head;
Node<T> tempNode = node;
for (int i = 1; i <= size(); i++) {
if (i == position) {
if (node == head) {
insertAtStart(data);
return;
} else {
Node<T> newNode = new Node<T>(data);
tempNode.next = newNode;
newNode.next = node;
display();
break;
}
}
tempNode = node;
node = node.next;
}
}
public boolean delete(int position) {
if (empty() || position < 1 || position > size())
return false;
Node<T> node = head;
Node<T> tempNode = node;
for (int i = 1; i <= size(); i++) {
if(i == position) {
if(node == head) {
head = head.next;
return true;
}else if(node == tail) {
tempNode.next = null;
tail = tempNode;
return true;
}else {
tempNode.next = node.next;
return true;
}
}
tempNode = node;
node = node.next;
}
return false;
}
public T replace(int position, T data) {
throwEmptyDataException(data);
if (empty() || position < 1 || position > size())
return null;
Node<T> node = head;
for (int i = 1; i <= size(); i++) {
if(i == position) {
T replaceData = node.data;
node.data = data;
return replaceData;
}
node = node.next;
}
return null;
}
public boolean search(T data) {
Node<T> node = head;
while(node != null && !node.data.equals(data)) {
node = node.next;
}
return node != null;
}
public T middle() {
Node<T> slowPtr = head;
Node<T> fastPtr = head;
while(fastPtr != null && fastPtr.next != null) {
slowPtr = slowPtr.next;
fastPtr = fastPtr.next.next;
}
return empty() ? null : slowPtr.data;
}
public T getElementFromLast(int position) {
if(empty() || position < 1 || position > getSizeIteratively())
return null;
Node<T> firstPtr = head;
Node<T> secondPtr = head;
for(int i = 1;i<=size();i++) {
if(i > position)
firstPtr = firstPtr.next;
if(secondPtr.next == null)
return firstPtr.data;
secondPtr = secondPtr.next;
}
return null;
}
public void reverse() {
Node<T> prev = null;
Node<T> current = head;
Node<T> next = null;
while(current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
swapHeadTail();
displayIteratively();
}
public void reverseRecursively() {
reverseRecursively(head);
swapHeadTail();
display();
}
private Node<T> reverseRecursively(Node<T> node) {
if(node == null || node.next == null)
return node;
Node<T> secondNode = node.next;
node.next = null;
Node<T> reverseRest = reverseRecursively(secondNode);
secondNode.next = node;
return reverseRest;
}
private void swapHeadTail() {
Node<T> temp = head;
head = tail;
tail = temp;
}
public void swapPairwise() {
if(empty())
return;
Node<T> firstNode = head;
Node<T> secondNode = firstNode.next;
while(firstNode != null && secondNode != null) {
swap(firstNode.data, secondNode.data);
firstNode = firstNode.next;
if(firstNode != null)
secondNode = firstNode.next;
}
}
public void swap(T firstData, T secondData) {
throwEmptyException();
throwEmptyDataException(firstData);
throwEmptyDataException(secondData);
if(firstData.equals(secondData))
throw new IllegalArgumentException(firstData +" & "+ secondData+" both are the same. Can't swap");
Node<T> firstDataPtr = head;
Node<T> prevfirstDataPtr = firstDataPtr;
while (firstDataPtr != null && !firstDataPtr.data.equals(firstData)) {
prevfirstDataPtr = firstDataPtr;
firstDataPtr = firstDataPtr.next;
}
Node<T> secondDataPtr = head;
Node<T> prevSecondDataPtr = secondDataPtr;
while (secondDataPtr!= null && !secondDataPtr.data.equals(secondData)) {
prevSecondDataPtr = secondDataPtr;
secondDataPtr = secondDataPtr.next;
}
if(!(firstDataPtr == null || secondDataPtr == null)) {
// either first node or second node is head node
if (firstDataPtr == head)
head = secondDataPtr;
else if (secondDataPtr == head)
head = firstDataPtr;
// either first node or second node is tail node
if (firstDataPtr == tail)
tail = secondDataPtr;
else if (secondDataPtr == tail)
tail = firstDataPtr;
// getting the next pointer of both nodes
Node<T> nextFirstDataPtr = firstDataPtr.next;
Node<T> nextSecondDataPtr = secondDataPtr.next;
// swapping the nodes
prevfirstDataPtr.next = secondDataPtr;
secondDataPtr.next = nextFirstDataPtr;
prevSecondDataPtr.next = firstDataPtr;
firstDataPtr.next = nextSecondDataPtr;
// checking if both node is adjacent node
// if both nodes are adjacent re-adjust the pointer
if(nextFirstDataPtr == secondDataPtr) {
secondDataPtr.next = firstDataPtr;
} else if(nextSecondDataPtr == firstDataPtr) {
firstDataPtr.next = secondDataPtr;
}
} else
throw new IllegalArgumentException("Either "+firstData+" or "+secondData+" not present in the list");
displayIteratively();
}
public void setLastNodeAsFirstNode() {
if(empty() || head.next == null) {
return;
}
Node<T> node = head;
Node<T> prevNode = node;
while (node.next != null) {
prevNode = node;
node = node.next;
}
node.next = head;
head = node;
prevNode.next = null;
tail = prevNode;
display();
}
public int getSizeIteratively() {
if (empty())
return 0;
int size = 0;
Node<T> node = head;
while (node != null) {
++size;
node = node.next;
}
return size;
}
public int size() {
return size(head, 0);
}
private int size(Node<T> node, int size) {
return node != null ? size(node.next, ++size) : size;
}
public void displayIteratively() {
Node<T> node = head;
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}
public void display() {
display(head);
}
private void display(Node<T> node) {
if (node != null) {
System.out.print(node.data + " ");
display(node.next);
}
}
public void throwEmptyException() {
if (empty())
throw new IllegalArgumentException("List is empty!");
}
private void throwEmptyDataException(T data) {
if (data == null)
throw new IllegalArgumentException("data is null !");
}
public boolean empty() {
return head == null;
}
}
package com.practice.ds.list;
import java.util.Scanner;
public class LinkedListTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
LinkedList<Integer> list = new LinkedList<>();
boolean exit = false;
do {
System.out.println("\n----------------------------------------");
System.out.println("1. Insert at Start");
System.out.println("2. Insert at End");
System.out.println("3. Insert at Position");
System.out.println("4. Get the size of list");
System.out.println("5. Display the list");
System.out.println("6. Delete from the list ");
System.out.println("7. Replace the node ");
System.out.println("8. Search item position in the list ");
System.out.println("9. Find the middle of the list");
System.out.println("10. Get item from the last : ");
System.out.println("11. Reverse the list :: ");
System.out.println("12. Swap the node of the list");
System.out.println("13. Pairwise swap the list");
System.out.println("14. Make last node as first node");
System.out.println("15. Segregate even and odd node");
System.out.println();
int choice = scanner.nextInt();
switch (choice) {
case 1:
try {
System.out.println("Insert the node : ");
int node = scanner.nextInt();
list.insertAtStart(node);
} catch (Exception e) {
e.printStackTrace();
}
break;
case 2:
try {
System.out.println("Insert the node : ");
int node = scanner.nextInt();
list.insertAtEnd(node);
} catch (Exception e) {
e.printStackTrace();
}
break;
case 3:
try {
System.out.println("Enter the position :");
int position = scanner.nextInt();
System.out.println("Insert the node :");
int node = scanner.nextInt();
list.insertAtPosition(position, node);
} catch (Exception e) {
e.printStackTrace();
}
break;
case 4:
try {
System.out.println("Getting the size :: ");
System.out.println("1. Get Iteratively");
System.out.println("2. Get Recursively");
int input = scanner.nextInt();
switch (input) {
case 1:
System.out.println("The size of the list :: " + list.getSizeIteratively());
break;
case 2:
System.out.println("The size of the list :: " + list.size());
break;
default:
System.out.println("Invalid input...!");
break;
}
} catch (Exception e) {
e.printStackTrace();
}
break;
case 5:
try {
System.out.println("Displaying the list :: ");
System.out.println("1. Display Iteratively");
System.out.println("2. Display Recursively");
int input = scanner.nextInt();
switch (input) {
case 1:
list.displayIteratively();
break;
case 2:
list.display();
break;
default:
System.out.println("Invalid input...!");
break;
}
} catch (Exception e) {
e.printStackTrace();
}
break;
case 6:
try {
System.out.println("Enter the position ");
int position = scanner.nextInt();
System.out.println("is Delete :: " + list.delete(position));
} catch (Exception e) {
e.printStackTrace();
}
break;
case 7:
try {
System.out.println("Enter the position ");
int position = scanner.nextInt();
System.out.println("Insert the item ");
int data = scanner.nextInt();
list.replace(position, data);
} catch (Exception e) {
e.printStackTrace();
}
break;
case 8:
try {
System.out.println("Note: It will give first occurence of the item ");
System.out.println("Enter the item ");
int data = scanner.nextInt();
System.out.println(list.search(data));
} catch (Exception e) {
e.printStackTrace();
}
break;
case 9:
try {
System.out.println("The Middle node of the list is :: " + list.middle());
} catch (Exception e) {
e.printStackTrace();
}
break;
case 10:
System.out.println("Enter the position ");
try {
int position = scanner.nextInt();
System.out.println("Element is :: " + list.getElementFromLast(position));
} catch (Exception e) {
e.printStackTrace();
}
break;
case 11:
System.out.println("Reversing the list...");
System.out.println("1. Iteratively");
System.out.println("2. Recursively");
int key = scanner.nextInt();
switch (key) {
case 1:
try {
list.reverse();
} catch (Exception e) {
e.printStackTrace();
}
break;
case 2:
try {
list.reverseRecursively();
} catch (Exception e) {
e.printStackTrace();
}
break;
default:
System.out.println("Your choice is out of the box...! \ntry again...");
break;
}
break;
case 12:
try {
System.out.println("Enter first node ");
int firstNode = scanner.nextInt();
System.out.println("Enter second node ");
int secondNode = scanner.nextInt();
list.swap(firstNode, secondNode);
} catch (Exception e) {
e.printStackTrace();
}
break;
case 13:
try {
list.swapPairwise();
} catch (Exception e) {
e.printStackTrace();
}
break;
case 14:
try {
list.setLastNodeAsFirstNode();
} catch (Exception e) {
e.printStackTrace();
}
break;
default:
System.out.println("Your choice is out of the box...! \ntry again...");
break;
}
} while (!exit);
scanner.close();
}
}
package com.practice.ds.list;
最终公共类节点{
公共节点next=null;
公共节点prev=null;
公共T数据=null;
公共节点(){
}
公共节点(T数据){
这个数据=数据;
}
@凌驾
公共字符串toString(){
return“Node[next=“+next+”,prev=“+prev+”,data=“+data+””;
}
}
LinkedList.java
package com.practice.ds.list;
final public class Node<T> {
public Node<T> next = null;
public Node<T> prev = null;
public T data = null;
public Node() {
}
public Node(T data) {
this.data = data;
}
@Override
public String toString() {
return "Node [next=" + next + ", prev=" + prev + ", data=" + data + "]";
}
}
package com.practice.ds.list;
public class LinkedList<T> {
private Node<T> head = null;
private Node<T> tail = null;
public void insertAtStart(T data) {
throwEmptyDataException(data);
Node<T> node = new Node<T>(data);
if(empty()) {
head = node;
tail = head;
}else {
node.next = head;
head = node;
}
display();
}
public void insertAtEnd(T data) {
throwEmptyDataException(data);
if(empty()) {
insertAtStart(data);
}else {
Node<T> node = new Node<T>(data);
tail.next = node;
tail = node;
display();
}
}
public void insertAtPosition(int position, T data) {
throwEmptyDataException(data);
if (position < 1 || position > size() || empty())
throw new IllegalArgumentException("Can't perform insertion. Please check your inputs");
Node<T> node = head;
Node<T> tempNode = node;
for (int i = 1; i <= size(); i++) {
if (i == position) {
if (node == head) {
insertAtStart(data);
return;
} else {
Node<T> newNode = new Node<T>(data);
tempNode.next = newNode;
newNode.next = node;
display();
break;
}
}
tempNode = node;
node = node.next;
}
}
public boolean delete(int position) {
if (empty() || position < 1 || position > size())
return false;
Node<T> node = head;
Node<T> tempNode = node;
for (int i = 1; i <= size(); i++) {
if(i == position) {
if(node == head) {
head = head.next;
return true;
}else if(node == tail) {
tempNode.next = null;
tail = tempNode;
return true;
}else {
tempNode.next = node.next;
return true;
}
}
tempNode = node;
node = node.next;
}
return false;
}
public T replace(int position, T data) {
throwEmptyDataException(data);
if (empty() || position < 1 || position > size())
return null;
Node<T> node = head;
for (int i = 1; i <= size(); i++) {
if(i == position) {
T replaceData = node.data;
node.data = data;
return replaceData;
}
node = node.next;
}
return null;
}
public boolean search(T data) {
Node<T> node = head;
while(node != null && !node.data.equals(data)) {
node = node.next;
}
return node != null;
}
public T middle() {
Node<T> slowPtr = head;
Node<T> fastPtr = head;
while(fastPtr != null && fastPtr.next != null) {
slowPtr = slowPtr.next;
fastPtr = fastPtr.next.next;
}
return empty() ? null : slowPtr.data;
}
public T getElementFromLast(int position) {
if(empty() || position < 1 || position > getSizeIteratively())
return null;
Node<T> firstPtr = head;
Node<T> secondPtr = head;
for(int i = 1;i<=size();i++) {
if(i > position)
firstPtr = firstPtr.next;
if(secondPtr.next == null)
return firstPtr.data;
secondPtr = secondPtr.next;
}
return null;
}
public void reverse() {
Node<T> prev = null;
Node<T> current = head;
Node<T> next = null;
while(current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
swapHeadTail();
displayIteratively();
}
public void reverseRecursively() {
reverseRecursively(head);
swapHeadTail();
display();
}
private Node<T> reverseRecursively(Node<T> node) {
if(node == null || node.next == null)
return node;
Node<T> secondNode = node.next;
node.next = null;
Node<T> reverseRest = reverseRecursively(secondNode);
secondNode.next = node;
return reverseRest;
}
private void swapHeadTail() {
Node<T> temp = head;
head = tail;
tail = temp;
}
public void swapPairwise() {
if(empty())
return;
Node<T> firstNode = head;
Node<T> secondNode = firstNode.next;
while(firstNode != null && secondNode != null) {
swap(firstNode.data, secondNode.data);
firstNode = firstNode.next;
if(firstNode != null)
secondNode = firstNode.next;
}
}
public void swap(T firstData, T secondData) {
throwEmptyException();
throwEmptyDataException(firstData);
throwEmptyDataException(secondData);
if(firstData.equals(secondData))
throw new IllegalArgumentException(firstData +" & "+ secondData+" both are the same. Can't swap");
Node<T> firstDataPtr = head;
Node<T> prevfirstDataPtr = firstDataPtr;
while (firstDataPtr != null && !firstDataPtr.data.equals(firstData)) {
prevfirstDataPtr = firstDataPtr;
firstDataPtr = firstDataPtr.next;
}
Node<T> secondDataPtr = head;
Node<T> prevSecondDataPtr = secondDataPtr;
while (secondDataPtr!= null && !secondDataPtr.data.equals(secondData)) {
prevSecondDataPtr = secondDataPtr;
secondDataPtr = secondDataPtr.next;
}
if(!(firstDataPtr == null || secondDataPtr == null)) {
// either first node or second node is head node
if (firstDataPtr == head)
head = secondDataPtr;
else if (secondDataPtr == head)
head = firstDataPtr;
// either first node or second node is tail node
if (firstDataPtr == tail)
tail = secondDataPtr;
else if (secondDataPtr == tail)
tail = firstDataPtr;
// getting the next pointer of both nodes
Node<T> nextFirstDataPtr = firstDataPtr.next;
Node<T> nextSecondDataPtr = secondDataPtr.next;
// swapping the nodes
prevfirstDataPtr.next = secondDataPtr;
secondDataPtr.next = nextFirstDataPtr;
prevSecondDataPtr.next = firstDataPtr;
firstDataPtr.next = nextSecondDataPtr;
// checking if both node is adjacent node
// if both nodes are adjacent re-adjust the pointer
if(nextFirstDataPtr == secondDataPtr) {
secondDataPtr.next = firstDataPtr;
} else if(nextSecondDataPtr == firstDataPtr) {
firstDataPtr.next = secondDataPtr;
}
} else
throw new IllegalArgumentException("Either "+firstData+" or "+secondData+" not present in the list");
displayIteratively();
}
public void setLastNodeAsFirstNode() {
if(empty() || head.next == null) {
return;
}
Node<T> node = head;
Node<T> prevNode = node;
while (node.next != null) {
prevNode = node;
node = node.next;
}
node.next = head;
head = node;
prevNode.next = null;
tail = prevNode;
display();
}
public int getSizeIteratively() {
if (empty())
return 0;
int size = 0;
Node<T> node = head;
while (node != null) {
++size;
node = node.next;
}
return size;
}
public int size() {
return size(head, 0);
}
private int size(Node<T> node, int size) {
return node != null ? size(node.next, ++size) : size;
}
public void displayIteratively() {
Node<T> node = head;
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}
public void display() {
display(head);
}
private void display(Node<T> node) {
if (node != null) {
System.out.print(node.data + " ");
display(node.next);
}
}
public void throwEmptyException() {
if (empty())
throw new IllegalArgumentException("List is empty!");
}
private void throwEmptyDataException(T data) {
if (data == null)
throw new IllegalArgumentException("data is null !");
}
public boolean empty() {
return head == null;
}
}
package com.practice.ds.list;
import java.util.Scanner;
public class LinkedListTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
LinkedList<Integer> list = new LinkedList<>();
boolean exit = false;
do {
System.out.println("\n----------------------------------------");
System.out.println("1. Insert at Start");
System.out.println("2. Insert at End");
System.out.println("3. Insert at Position");
System.out.println("4. Get the size of list");
System.out.println("5. Display the list");
System.out.println("6. Delete from the list ");
System.out.println("7. Replace the node ");
System.out.println("8. Search item position in the list ");
System.out.println("9. Find the middle of the list");
System.out.println("10. Get item from the last : ");
System.out.println("11. Reverse the list :: ");
System.out.println("12. Swap the node of the list");
System.out.println("13. Pairwise swap the list");
System.out.println("14. Make last node as first node");
System.out.println("15. Segregate even and odd node");
System.out.println();
int choice = scanner.nextInt();
switch (choice) {
case 1:
try {
System.out.println("Insert the node : ");
int node = scanner.nextInt();
list.insertAtStart(node);
} catch (Exception e) {
e.printStackTrace();
}
break;
case 2:
try {
System.out.println("Insert the node : ");
int node = scanner.nextInt();
list.insertAtEnd(node);
} catch (Exception e) {
e.printStackTrace();
}
break;
case 3:
try {
System.out.println("Enter the position :");
int position = scanner.nextInt();
System.out.println("Insert the node :");
int node = scanner.nextInt();
list.insertAtPosition(position, node);
} catch (Exception e) {
e.printStackTrace();
}
break;
case 4:
try {
System.out.println("Getting the size :: ");
System.out.println("1. Get Iteratively");
System.out.println("2. Get Recursively");
int input = scanner.nextInt();
switch (input) {
case 1:
System.out.println("The size of the list :: " + list.getSizeIteratively());
break;
case 2:
System.out.println("The size of the list :: " + list.size());
break;
default:
System.out.println("Invalid input...!");
break;
}
} catch (Exception e) {
e.printStackTrace();
}
break;
case 5:
try {
System.out.println("Displaying the list :: ");
System.out.println("1. Display Iteratively");
System.out.println("2. Display Recursively");
int input = scanner.nextInt();
switch (input) {
case 1:
list.displayIteratively();
break;
case 2:
list.display();
break;
default:
System.out.println("Invalid input...!");
break;
}
} catch (Exception e) {
e.printStackTrace();
}
break;
case 6:
try {
System.out.println("Enter the position ");
int position = scanner.nextInt();
System.out.println("is Delete :: " + list.delete(position));
} catch (Exception e) {
e.printStackTrace();
}
break;
case 7:
try {
System.out.println("Enter the position ");
int position = scanner.nextInt();
System.out.println("Insert the item ");
int data = scanner.nextInt();
list.replace(position, data);
} catch (Exception e) {
e.printStackTrace();
}
break;
case 8:
try {
System.out.println("Note: It will give first occurence of the item ");
System.out.println("Enter the item ");
int data = scanner.nextInt();
System.out.println(list.search(data));
} catch (Exception e) {
e.printStackTrace();
}
break;
case 9:
try {
System.out.println("The Middle node of the list is :: " + list.middle());
} catch (Exception e) {
e.printStackTrace();
}
break;
case 10:
System.out.println("Enter the position ");
try {
int position = scanner.nextInt();
System.out.println("Element is :: " + list.getElementFromLast(position));
} catch (Exception e) {
e.printStackTrace();
}
break;
case 11:
System.out.println("Reversing the list...");
System.out.println("1. Iteratively");
System.out.println("2. Recursively");
int key = scanner.nextInt();
switch (key) {
case 1:
try {
list.reverse();
} catch (Exception e) {
e.printStackTrace();
}
break;
case 2:
try {
list.reverseRecursively();
} catch (Exception e) {
e.printStackTrace();
}
break;
default:
System.out.println("Your choice is out of the box...! \ntry again...");
break;
}
break;
case 12:
try {
System.out.println("Enter first node ");
int firstNode = scanner.nextInt();
System.out.println("Enter second node ");
int secondNode = scanner.nextInt();
list.swap(firstNode, secondNode);
} catch (Exception e) {
e.printStackTrace();
}
break;
case 13:
try {
list.swapPairwise();
} catch (Exception e) {
e.printStackTrace();
}
break;
case 14:
try {
list.setLastNodeAsFirstNode();
} catch (Exception e) {
e.printStackTrace();
}
break;
default:
System.out.println("Your choice is out of the box...! \ntry again...");
break;
}
} while (!exit);
scanner.close();
}
}
package com.practice.ds.list;
公共类链接列表{
私有节点头=null;
私有节点tail=null;
public void insertAtStart(T数据){
throwEmptyDataException(数据);
节点=新节点(数据);
if(空()){
头部=节点;
尾=头;
}否则{
node.next=头部;
头部=节点;
}
显示();
}
公共void插入(T数据){
throwEmptyDataException(数据);
if(空()){
insertAtStart(数据);
}否则{
节点=新节点(数据);
tail.next=节点;
尾=节点;
显示();
}
}
公共void插入位置(int位置,T数据){
throwEmptyDataException(数据);
如果(位置<1 | |位置>大小()| |空())
抛出新的IllegalArgumentException(“无法执行插入。请检查您的输入”);
节点=头部;
节点tempNode=节点;
对于(int i=1;i size())
返回false;
节点=头部;
节点tempNode=节点;
对于(int i=1;i size())
返回null;
节点=头部;
for(int i=1;i getSizeIteratively())
返回null;
节点firstPtr=头部;
节点二次ptr=头;
用于(int i=1;i位置)
firstPtr=firstPtr.next;
if(secondPtr.next==null)
返回firstPtr.data;
secondPtr=secondPtr.next;
}
返回null;
}
公共无效反向(){
Node prev=null;
节点电流=头;
Node next=null;
while(当前!=null){
下一个=当前。下一个;
current.next=prev;
prev=当前值;
当前=下一个;
}
天鹅尾巴();
迭代地显示();
}
public void reverseRecursively(){
反向(头);
天鹅尾巴();
显示();
}
私有节点反向递归(节点节点){
if(node==null | | node.next==null)
返回节点;
Node secondNode=Node.next;
node.next=null;
Node reverseRest=反向递归(secondNode);
secondNode.next=节点;
返回反向测试;
}
私人无效游尾蛇(){
节点温度=头;
头=尾;
尾=温度;
}
公共空间(airwise)(){
if(空())
返回;
Node firstNode=头部;
Node secondNode=firstNode.next;
while(firstNode!=null&&secondNode!=null){
交换(firstNode.data,secondNode.data);
firstNode=firstNode.next;
if(firstNode!=null)
secondNode=firstNode.next;
}
}
公共无效交换(T firstData,T secondData){
throwEmptyException();
throwEmptyDataException(firstData);
throwEmptyDataException(第二个数据);
if(第一个数据等于第二个数据))
抛出新的IllegalArgumentException(firstData+“&”+secondData+“两者都相同。无法交换”);