fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. ////////////////////////////////////////////
  5. /// ============= ARRAY ===================
  6. ////////////////////////////////////////////
  7.  
  8. class ArrayDS {
  9. private:
  10. int arr[100];
  11. int size;
  12. public:
  13. ArrayDS(){ size = 0; }
  14.  
  15. void insert(int x){
  16. if(size >= 100){
  17. cout << "Array full!\n";
  18. return;
  19. }
  20. arr[size++] = x;
  21. }
  22.  
  23. void remove(){
  24. if(size>0) size--;
  25. else cout << "Array empty!\n";
  26. }
  27.  
  28. bool search(int x){
  29. for(int i=0;i<size;i++)
  30. if(arr[i]==x) return true;
  31. return false;
  32. }
  33.  
  34. void print(){
  35. if(size==0) { cout << "Array empty!\n"; return; }
  36. for(int i=0;i<size;i++)
  37. cout<<arr[i]<<" ";
  38. cout<<endl;
  39. }
  40.  
  41. void printReverse(){
  42. if(size==0) { cout << "Array empty!\n"; return; }
  43. for(int i=size-1;i>=0;i--)
  44. cout<<arr[i]<<" ";
  45. cout<<endl;
  46. }
  47. };
  48.  
  49. ////////////////////////////////////////////
  50. /// ============ LINKED LIST =============
  51. ////////////////////////////////////////////
  52.  
  53. class LinkedList{
  54. private:
  55. struct Node{
  56. int data;
  57. Node* next;
  58. };
  59. Node* head;
  60.  
  61. public:
  62. LinkedList(){
  63. head = NULL;
  64. }
  65.  
  66. void insertBeg(int x){
  67. Node* n = new Node;
  68. n->data = x;
  69. n->next = head;
  70. head = n;
  71. }
  72.  
  73. void insertEnd(int x){
  74. Node* n = new Node;
  75. n->data = x;
  76. n->next = NULL;
  77.  
  78. if(head==NULL){
  79. head = n;
  80. return;
  81. }
  82.  
  83. Node* t = head;
  84. while(t->next != NULL) t = t->next;
  85. t->next = n;
  86. }
  87.  
  88. void deleteValue(int x){
  89. if(head==NULL){ cout<<"List empty!\n"; return; }
  90.  
  91. if(head->data==x){
  92. Node* t = head;
  93. head = head->next;
  94. delete t;
  95. return;
  96. }
  97.  
  98. Node* prev = head;
  99. Node* cur = head->next;
  100.  
  101. while(cur){
  102. if(cur->data == x){
  103. prev->next = cur->next;
  104. delete cur;
  105. return;
  106. }
  107. prev = cur;
  108. cur = cur->next;
  109. }
  110. cout<<"Value not found!\n";
  111. }
  112.  
  113. bool search(int x){
  114. Node* t = head;
  115. while(t){
  116. if(t->data==x) return true;
  117. t = t->next;
  118. }
  119. return false;
  120. }
  121.  
  122. void display(){
  123. if(head==NULL){ cout<<"List empty!\n"; return; }
  124. Node* t = head;
  125. while(t){
  126. cout<<t->data<<" ";
  127. t = t->next;
  128. }
  129. cout<<endl;
  130. }
  131. };
  132.  
  133. ////////////////////////////////////////////
  134. /// ================ STACK ================
  135. ////////////////////////////////////////////
  136.  
  137. class StackDS{
  138. private:
  139. int arr[100];
  140. int top;
  141. public:
  142. StackDS(){ top = -1; }
  143.  
  144. void push(int x){
  145. if(top>=99){ cout<<"Stack overflow!\n"; return; }
  146. arr[++top] = x;
  147. }
  148.  
  149. void pop(){
  150. if(top>=0) top--;
  151. else cout<<"Stack empty!\n";
  152. }
  153.  
  154. int topElement(){
  155. if(top>=0) return arr[top];
  156. cout<<"Stack empty!\n";
  157. return -1;
  158. }
  159.  
  160. void display(){
  161. if(top<0){ cout<<"Stack empty!\n"; return; }
  162. for(int i=top;i>=0;i--)
  163. cout<<arr[i]<<" ";
  164. cout<<endl;
  165. }
  166. };
  167.  
  168. ////////////////////////////////////////////
  169. /// ================= QUEUE ===============
  170. ////////////////////////////////////////////
  171.  
  172. class QueueDS{
  173. private:
  174. int arr[100];
  175. int front,rear;
  176. public:
  177. QueueDS(){
  178. front = rear = 0;
  179. }
  180.  
  181. void enqueue(int x){
  182. if(rear>=100){ cout<<"Queue full!\n"; return; }
  183. arr[rear++] = x;
  184. }
  185.  
  186. void dequeue(){
  187. if(front<rear) front++;
  188. else cout<<"Queue empty!\n";
  189. }
  190.  
  191. int frontElement(){
  192. if(front<rear) return arr[front];
  193. cout<<"Queue empty!\n";
  194. return -1;
  195. }
  196.  
  197. void display(){
  198. if(front>=rear){ cout<<"Queue empty!\n"; return; }
  199. for(int i=front;i<rear;i++)
  200. cout<<arr[i]<<" ";
  201. cout<<endl;
  202. }
  203. };
  204.  
  205. ////////////////////////////////////////////
  206. /// ================ MENUS ===============
  207. ////////////////////////////////////////////
  208.  
  209. int getIntInput(){
  210. int x;
  211. while(!(cin>>x)){
  212. cout<<"Invalid input! Enter a number: ";
  213. cin.clear();
  214. cin.ignore(1000,'\n');
  215. }
  216. return x;
  217. }
  218.  
  219. void arrayMenu(){
  220. ArrayDS a;
  221. int c,v;
  222.  
  223. while(true){
  224. cout<<"\nArray Menu\n";
  225. cout<<"1 insert\n2 delete\n3 print\n4 reverse\n5 search\n6 back\n";
  226. c = getIntInput();
  227.  
  228. if(c==6) return;
  229.  
  230. if(c==1){ cout<<"value:"; v = getIntInput(); a.insert(v); }
  231. else if(c==2) a.remove();
  232. else if(c==3) a.print();
  233. else if(c==4) a.printReverse();
  234. else if(c==5){
  235. cout<<"value:"; v = getIntInput();
  236. if(a.search(v)) cout<<"Found\n";
  237. else cout<<"Not Found\n";
  238. }
  239. }
  240. }
  241.  
  242. void linkedMenu(){
  243. LinkedList l;
  244. int c,v;
  245.  
  246. while(true){
  247. cout<<"\nLinkedList Menu\n";
  248. cout<<"1 insert beg\n2 insert end\n3 delete value\n4 search\n5 display\n6 back\n";
  249. c = getIntInput();
  250.  
  251. if(c==6) return;
  252.  
  253. if(c==1){ cout<<"value:"; v = getIntInput(); l.insertBeg(v); }
  254. else if(c==2){ cout<<"value:"; v = getIntInput(); l.insertEnd(v); }
  255. else if(c==3){ cout<<"value:"; v = getIntInput(); l.deleteValue(v); }
  256. else if(c==4){
  257. cout<<"value:"; v = getIntInput();
  258. if(l.search(v)) cout<<"Found\n";
  259. else cout<<"Not Found\n";
  260. }
  261. else if(c==5) l.display();
  262. }
  263. }
  264.  
  265. void stackMenu(){
  266. StackDS s;
  267. int c,v;
  268.  
  269. while(true){
  270. cout<<"\nStack Menu\n";
  271. cout<<"1 push\n2 pop\n3 top\n4 display\n5 back\n";
  272. c = getIntInput();
  273.  
  274. if(c==5) return;
  275.  
  276. if(c==1){ cout<<"value:"; v = getIntInput(); s.push(v); }
  277. else if(c==2) s.pop();
  278. else if(c==3) cout<<"Top="<<s.topElement()<<endl;
  279. else if(c==4) s.display();
  280. }
  281. }
  282.  
  283. void queueMenu(){
  284. QueueDS q;
  285. int c,v;
  286.  
  287. while(true){
  288. cout<<"\nQueue Menu\n";
  289. cout<<"1 enqueue\n2 dequeue\n3 front\n4 display\n5 back\n";
  290. c = getIntInput();
  291.  
  292. if(c==5) return;
  293.  
  294. if(c==1){ cout<<"value:"; v = getIntInput(); q.enqueue(v); }
  295. else if(c==2) q.dequeue();
  296. else if(c==3) cout<<"Front="<<q.frontElement()<<endl;
  297. else if(c==4) q.display();
  298. }
  299. }
  300.  
  301. ////////////////////////////////////////////
  302. /// ================= MAIN ================
  303. ////////////////////////////////////////////
  304.  
  305. int main(){
  306.  
  307. int choice;
  308.  
  309. while(true){
  310. cout<<"\n==== MAIN MENU ====\n";
  311. cout<<"1 Array\n2 LinkedList\n3 Stack\n4 Queue\n5 Exit\n";
  312. cout<<"Choice: ";
  313. choice = getIntInput();
  314.  
  315. switch(choice){
  316. case 1: arrayMenu(); break;
  317. case 2: linkedMenu(); break;
  318. case 3: stackMenu(); break;
  319. case 4: queueMenu(); break;
  320. case 5: return 0;
  321. default: cout<<"Invalid choice!\n";
  322. }
  323. }
  324. }
Success #stdin #stdout 0.01s 5296KB
stdin
4
1
10
1
20
4
3
5
5
stdout
==== MAIN MENU ====
1 Array
2 LinkedList
3 Stack
4 Queue
5 Exit
Choice: 
Queue Menu
1 enqueue
2 dequeue
3 front
4 display
5 back
value:
Queue Menu
1 enqueue
2 dequeue
3 front
4 display
5 back
value:
Queue Menu
1 enqueue
2 dequeue
3 front
4 display
5 back
10 20 

Queue Menu
1 enqueue
2 dequeue
3 front
4 display
5 back
Front=10

Queue Menu
1 enqueue
2 dequeue
3 front
4 display
5 back

==== MAIN MENU ====
1 Array
2 LinkedList
3 Stack
4 Queue
5 Exit
Choice: