Data Structures and Algorithm Analysis
import java.util.Scanner;
public class linkList
{
public static void main(String[] args)
{
Node start=null;
int option=0;
while (option !=5)
{
option = menu();
switch (option) {
case 1:
System.out.println("Option 1 selected");
start=createNodes();
break;
case 2:
System.out.println("Option 2 selected");
start=insertNodes(start);
break;
case 3:
System.out.println("Option 3 selected");
start=deleteNodes(start);
break;
case 4:
System.out.println("Option 4 selected");
traverseList(start);
break;
case 5:
System.out.println("Exit selected");
break;
default:
System.out.println("Invalid selection");
break; // This break is not really necessary
}
}
}
private static class Node
{
int number;
String name;
Node next;
Node(int d, String s) { number = d; name = s; next=null; }
Node() {number=0; name=null; next= null;}
}
public static Node createNodes()
{
Node newNode = new Node(5,"Mridula");
Node start = newNode;
Scanner scanner = new Scanner(System.in);
int num;
String nam;
int ans=1;
while (true)
{
Node newNode1=new Node();
System.out.println(" Enter number : ");
newNode1.number = scanner.nextInt();
System.out.println(" Enter name : ");
newNode1.name = scanner.next();
newNode.next = newNode1;
System.out.println(" Want to continue.... 0/1)");
ans=scanner.nextInt();
if (ans == 1) {newNode = newNode1; continue;}
else {newNode1.next = null; break;}
}
return start;
}
public static void traverseList(Node start)
{
Node topNode = start;
//System.out.println("Number : " + start.number + "Name : "+ start.name);
System.out.print("LinkedList Printing: \n");
// Traverse through the LinkedList
do {
// Print the data at current node
System.out.println("Number : " + topNode.number + "Name : "+ topNode.name);
//FILL UP HERE
}
while (topNode!= null);
}
public static Node insertNodes(Node start)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number to insert (0 to insert at beginning, -1 to insert at end, n for position --->");
System.out.println(" Select option: ");
int option = scanner.nextInt();
Node topNode = start;
Node newNode1=new Node();
System.out.println(" Enter number : ");
newNode1.number = scanner.nextInt();
System.out.println(" Enter name : ");
newNode1.name = scanner.next();
if (option ==0)
{
newNode1.next = start;
start = newNode1;
}
else
if (option == -1)
{
while (topNode.next !=null) {
// Print the data at current node
topNode = topNode.next;
// Go to next node
}
//FILL UP HERE
}
else
{
int cnt=0;
while (topNode.next !=null) {
// Print the data at current node
topNode = topNode.next;
cnt++;
if (cnt == option)
{
newNode1.next = topNode.next;
topNode.next = newNode1;
}
}
}
return start;
}
public static Node deleteNodes(Node start)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of node to delete (0 to delete first node, -1 to delete last node, n for position --->");
System.out.println(" Select option: ");
int option = scanner.nextInt();
Node topNode = start;
if (option ==0)
{
// FILL UP HERE
}
else
if (option == -1)
{
while (topNode.next.next !=null) {
// Print the data at current node
//FILL UP HERE
// Go to next node
}
//FILL UP HERE
}
else
{
int cnt=0;
while (topNode.next !=null) {
// Print the data at current node
topNode = topNode.next;
cnt++;
if (cnt == option )
{
// FILL UP HERE
}
}
}
return start;
}
public static int menu()
{
int swValue;
Scanner scanner = new Scanner(System.in);
// Display menu graphics
System.out.println("============================");
System.out.println("| MENU SELECTION DEMO |");
System.out.println("============================");
System.out.println("| Options: |");
System.out.println("| 1. Create a linked List |");
System.out.println("| 2. Insert into list |");
System.out.println("| 3. Delete from list |");
System.out.println("| 4. Traverse the list |");
System.out.println("| 5. Exit |");
System.out.println("============================");
System.out.println(" Select option: ");
swValue = scanner.nextInt();
return swValue;
}
}