-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStacksAndQueues.java
More file actions
82 lines (78 loc) · 1.83 KB
/
Copy pathStacksAndQueues.java
File metadata and controls
82 lines (78 loc) · 1.83 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
package Java;
import java.util.*;
import java.io.*;
public class StacksAndQueues {
public static void main(String[] args) throws FileNotFoundException{
Scanner in=new Scanner(new FileReader("index3.txt"));
Stack<Queue<String>> s=new Stack<Queue<String>>();
int level=-1;
while(in.hasNext()){
String line=in.nextLine();
int ind=indentation(line);
if(ind>level){
level=ind;
s.push(new LinkedList<String>());
}
while(ind<level){
if(!s.empty())
if(!isSorted(s.pop())){
System.out.println(s);
System.out.println("no");
return;
}
level--;
}
if(!s.empty())
s.peek().add(line);
//System.out.println(indentation(line));
}
while(!s.empty()){
if(!isSorted(s.pop())){
System.out.println(s);
System.out.println("no");
return;
}
}
System.out.println("yes");
}
public static boolean isSorted(Queue<String> list){
String lastStr=list.poll();
String str=list.poll();
while(str!=null){
if(str.compareTo(lastStr)<0)
return false;
lastStr=str;
str=list.poll();
}
return true;
}
public static int indentation(String line){
int i;
for(i=0;i<line.length() && line.substring(i,i+1).equals("\t");i++){}
return i;
}
}
/*
1) A book’s index contains entries and sub-entries nested to several levels. Sub-entries are
indicated by deeper indentation. All the sub-entries of a given entry are preceded by the
same number of spaces; that number is greater than the indentation at the previous level.
Write a program that reads an index file (index.dat) and verifies that all the entries and
subentries are in alphabetical order. Skip empty lines.
class
abstract
accessors
constructors
overloaded
no-args
modifiers
method
private
public
static
stack
for handling nested structures
methods
push
pop
peekTop
*/