Skip to content

Instantly share code, notes, and snippets.

@pteacher
Created March 1, 2017 13:00
Show Gist options
  • Save pteacher/4ee83b74ddd51828f1e41bad94aef08e to your computer and use it in GitHub Desktop.
Save pteacher/4ee83b74ddd51828f1e41bad94aef08e to your computer and use it in GitHub Desktop.
Реализуйте структуру данных "стек". Напишите программу, содержащую описание стека и моделирующую работу стека, реализовав все указанные здесь методы. Программа считывает последовательность команд и в зависимости от команды выполняет ту или иную операцию. После выполнения каждой команды программа должна вывести одну строчку. Возможные команды для…
import java.io.File;
import java.io.IOException;
import java.util.*;
/**
* Created by TEACHER on 22.02.2017.
*/
public class Collections {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
File file = new File("in.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
if (s.split(" ")[0].equals("push")) {
stack.push(Integer.parseInt(s.split(" ")[1]));
System.out.println("ok");
}
if (s.split(" ")[0].equals("size")) {
System.out.println(stack.size());
}
if (s.split(" ")[0].equals("clear")) {
stack.clear();
System.out.println("ok");
}
if (s.split(" ")[0].equals("back")) {
System.out.println(stack.peek());
}
if (s.split(" ")[0].equals("pop")) {
System.out.println(stack.pop());
}
if (s.split(" ")[0].equals("exit")) {
System.out.println("bye");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
static void generateRandomList(int length, List list) {
for (int i = 0; i < length; i++) {
list.add((int) (Math.random() * 100));
}
}
static void printList(LinkedList list) {
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
System.out.println();
}
}
@pteacher
Copy link
Author

pteacher commented Mar 1, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment