-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntListTest.java
More file actions
82 lines (81 loc) · 2.7 KB
/
IntListTest.java
File metadata and controls
82 lines (81 loc) · 2.7 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
// ***************************************************************
// IntListTest.java
//
// Driver to test IntList methods.
// ***************************************************************
import java.util.Scanner;
public class IntListTest {
private static Scanner scan;
private static IntList list = new IntList();
//----------------------------------------------------------------
// Creates a list, then repeatedly prints the menu and does what
// the user asks until they quit.
//----------------------------------------------------------------
public static void main(String[] args) {
scan = new Scanner(System.in);
printMenu();
int choice = scan.nextInt();
while (choice != 0) {
dispatch(choice);
printMenu();
choice = scan.nextInt();
}
}
//----------------------------------------
// Does what the menu item calls for.
//----------------------------------------
public static void dispatch(int choice) {
int newVal;
switch (choice) {
case 0:
System.out.println("Bye!");
break;
case 1: //add to front
System.out.println("Enter integer to add to front");
newVal = scan.nextInt();
list.addToFront(newVal);
break;
case 2: //add to end
System.out.println("Enter integer to add to end");
newVal = scan.nextInt();
list.addToEnd(newVal);
break;
case 3: //remove first element
list.removeFirst();
break;
case 4: //print
list.print();
break;
case 5:
System.out.println("The length of the linked list is: " + list.length());
break;
case 6:
list.removeLast();
break;
case 7:
System.out.println("Please enter a number to replace, and the number it's replacing:");
newVal = scan.nextInt();
int oldVal = scan.nextInt();
list.replace(oldVal, newVal);
break;
default:
System.out.println("Sorry, invalid choice");
}
}
//-----------------------------------------
// Prints the user's choices
//-----------------------------------------
public static void printMenu() {
System.out.println("\n Menu ");
System.out.println(" ====");
System.out.println("0: Quit");
System.out.println("1: Add an integer to the front of the list");
System.out.println("2: Add an integer to the end of the list");
System.out.println("3: Remove an integer from the front of the list");
System.out.println("4: Print the list");
System.out.println("5: Print the length of the list");
System.out.println("6: Remove the last integer from the list");
System.out.println("7: Replace one integer in the list with another");
System.out.print("\nEnter your choice: ");
}
}