Skip to content

Commit c5e4535

Browse files
committed
queue using stack
1 parent 7638390 commit c5e4535

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

queue/stack_queue.dart

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
void main(List<String> args) {
2+
// Stackqueue stackqueue=Stackqueue();
3+
// stackqueue.enqueue(20);
4+
// stackqueue.enqueue(30);
5+
// stackqueue.enqueue(40);
6+
// stackqueue.dequeue();
7+
// // print(stackqueue.enqueuestack.elementList);
8+
// stackqueue.dequeue();
9+
// stackqueue.dequeue();
10+
// stackqueue.enqueue(20);
11+
// stackqueue.dequeue();
12+
Priority priority=Priority();
13+
priority.insert(10);
14+
priority.insert(3);
15+
priority.printall();
16+
}
17+
18+
19+
class Priority{
20+
List list=[];
21+
//[1,2,5]
22+
//[1,2,5,5]
23+
void insert(var item){
24+
if(list.isEmpty){
25+
list.add(item);
26+
}else{
27+
list.add(null);
28+
for(int i=list.length-1;0<=i;i--){
29+
try{
30+
if(list[i]>item){
31+
list[i+1]=list[i];
32+
list[i]=item;
33+
}else{
34+
list[i+1]=item;
35+
break;
36+
}
37+
}catch (e){
38+
continue;
39+
}
40+
}
41+
}
42+
}
43+
44+
void printall(){
45+
print(list);
46+
}
47+
}
48+
49+
50+
class Stackqueue{
51+
Stack enqueuestack=Stack();
52+
Stack dequeuestack=Stack();
53+
54+
void enqueue(var item){
55+
enqueuestack.push(item);
56+
}
57+
58+
void dequeue(){
59+
if(isallEmpty()==true){
60+
throw "not data found for dequeue";
61+
}else{
62+
while(!enqueuestack.isEmpty()){
63+
dequeuestack.push(enqueuestack.pop());
64+
}
65+
print("pop value:-${dequeuestack.pop()}");
66+
while(!dequeuestack.isEmpty()){
67+
enqueuestack.push(dequeuestack.pop());
68+
}
69+
}
70+
}
71+
72+
bool isallEmpty(){
73+
if(enqueuestack.isEmpty()==true&&dequeuestack.isEmpty()==true){
74+
return true;
75+
}else{
76+
return false;
77+
}
78+
}
79+
}
80+
81+
class Stack {
82+
List elementList = [];
83+
84+
void push(var item) {
85+
elementList.add(item);
86+
}
87+
88+
pop() {
89+
var result = elementList.removeLast();
90+
return result;
91+
}
92+
93+
String peek() {
94+
return elementList.last;
95+
}
96+
97+
bool isEmpty(){
98+
if(elementList.isEmpty){
99+
return true;
100+
}else{
101+
return false;
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)