-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoLinkedList.java
More file actions
134 lines (127 loc) · 5.27 KB
/
DoLinkedList.java
File metadata and controls
134 lines (127 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.io.*;
public class DoLinkedList
{
public static void main(String[] args)
{
char input1;
String inputInfo = new String();
int operation2;
String line = new String();
//create a linked list to be used in this method.
LinkedList list1 = new LinkedList();
try
{
// print out the menu
printMenu();
// create a BufferedReader object to read input from a keyboard
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader stdin = new BufferedReader (isr);
do
{
System.out.print("What action would you like to perform?\n");
line = stdin.readLine().trim(); //read a line
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1) //check if a user entered only one character
{
switch (input1)
{
case 'A': //Add String
System.out.print("Please enter a string to add:\n");
String str1 = stdin.readLine().trim();
list1.addElement(str1);
break;
case 'G': //Get a String at a certain index
System.out.print("Please enter an index of a string to get:\n");
inputInfo = stdin.readLine().trim();
int getIndex = Integer.parseInt(inputInfo);
try
{
String getString = (String)list1.getElement(getIndex);
System.out.print("The string at the index " + getIndex + " is "
+ getString + "\n");
}
catch(IndexOutOfBoundsException ex)
{
System.out.print("The index is not valid\n");
}
break;
case 'E': //Check if the linked list is empty
boolean empty = list1.isEmpty();
if (empty)
System.out.print("The linked list is empty\n");
else
System.out.print("The linked list is not empty\n");
break;
case 'I': //Get the last index of a given String
System.out.print("Please enter a string to search:\n");
String str2 = stdin.readLine().trim();
int searchIndex = list1.indexOfLast(str2);
if (searchIndex != -1)
System.out.print("The index of the last " + str2 + " is " + searchIndex
+ "\n");
else
System.out.print("The string " + str2
+ " does not exist in the linked list\n");
break;
case 'L': //List Strings
System.out.print(list1.toString());
break;
case 'Q': //Quit
break;
case 'R': //Remove a String
System.out.print("Please enter an index of a string to remove:\n");
inputInfo = stdin.readLine().trim();
int removeIndex = Integer.parseInt(inputInfo);
try
{
String removeString = (String)list1.removeElement(removeIndex);
System.out.print("The string at the index " + removeIndex + " was "
+ removeString + " and was removed\n");
}
catch(IndexOutOfBoundsException ex)
{
System.out.print("The index is not valid\n");
}
break;
case 'S': //Search and Replace
System.out.print("Please enter a string to replace:\n");
String original = stdin.readLine().trim();
System.out.print("Please enter a string used to replace with:\n");
String newStr = stdin.readLine().trim();
list1.searchAndReplace(original, newStr);
break;
case '?': //Display Menu
printMenu();
break;
default:
System.out.print("Unknown action\n");
break;
}
}
else
{
System.out.print("Unknown action\n");
}
} while (input1 != 'Q' || line.length() != 1);
}
catch (IOException exception)
{
System.out.print("IO Exception\n");
}
}
public static void printMenu()
{
System.out.print("Choice\t\tAction\n" +
"------\t\t------\n" +
"A\t\tAdd String\n" +
"G\t\tGet String\n" +
"E\t\tCheck if Empty\n" +
"I\t\tIndex of Last String\n" +
"L\t\tList Strings\n" +
"Q\t\tQuit\n" +
"R\t\tRemove String\n" +
"S\t\tSearch and Replace\n" +
"?\t\tDisplay Help\n\n");
}
}