In dart list, we can could perform stack operations. Operations include PUSH,POP,PEEK,FIND,LENGTH etc on list elements.

image source: wikipedia.org

Two main operations:

  • Push, which adds an element to the collection, and
  • Pop, which removes the most recently added element that was not yet removed?

Additionally, a peek operation can, without modifying the stack, return the value of the last element added. Calling this structure a stack is by analogy to a set of physical items stacked one atop another, such as a stack of plates.

The order in which an element added to or removed from a stack is described as last in, first out, referred to by the acronym LIFO[last in first out]. As with a stack of physical objects, this structure makes it easy to take an item off the top of the stack, but accessing a datum deeper in the stack may require taking off multiple other items first.

In simple words, stack is a linear data structure where insertion(push) and deletion(pop) done from ‘Top of the Stack’(last end).

Real life examples: plates stack. Into a box we can adds new plates from upper side and we don’t remove any plates from middle or below. In stack same thing happened.

Methods

push() -> inserts new element into stack from top of the stack.
pop() -> removes existing element from top of the stack.
peek() -> gets top of the stack element without removing(deleting).
isEmpty() -> checks stack is empty or not.

Write a code, to perform PUSH, POP, PEEK & FIND operations into stack using integer list.

Code 1:

void main(){
  var listStack=[10,20,-30,40,-5];
  int find=40;
 
  print(‘stack is: $listStack’);
  // push -> add element at the end(top) of stack
  listStack.add(200);
  print(‘After push stack is: $listStack’);
  // pop -> remove element from the end(top) of stack
  listStack.removeLast();
  print(‘After pop stack is: $listStack’);
  // peek -> top of stack element display
  print(‘peek element of stack: ‘);
  print(listStack[listStack.length-1]);
  for (var i = 0; i < listStack.length; i++) {
    if(listStack[i]==find){
      print(‘item found!’);
    }
  }
}
output

Let’s see standard way for this …

Write a code, into String list stack to perform operations like – push, pop, peek, find element, length of stack.

Code 2:

import ‘dart:io’;
 
int main(){
  var lstStk=[];
// stack max-size: 6
int MAX=6;
 bool i=true;
 late String? val;int opt=0,cnt=0;
 
 print(‘Choose Option:’);
 while (i) {
   print(‘1. PUSH’);
   print(‘2. PEEK’);
   print(‘3. POP’);
   print(‘4. FIND ITEM’);
   print(‘5. display stack’);
   print(‘6. EXIT’);
   print(‘Enter Option: ‘);
   val=stdin.readLineSync();
    if (val!=null) {
      opt=int.parse(val);
    }
    switch(opt){
      case 1:
      if (lstStk.length==MAX) {
          print(‘stack overflow’);
          break;
        }
        print(‘Enter push element: ‘);
        val=stdin.readLineSync();
        /*if (val!=null) {
          opt=int.parse(val);
        }*/
       
        lstStk.add(val);
        print(‘element added successfully!’);
              break;
      case 2:
        print(‘peek element is ‘);
        print(lstStk[lstStk.length-1]);
        break;
      case 3:
         if(lstStk.isEmpty){
          print(‘stack underflow!’);
        
        }
        lstStk.removeLast();
        print(‘pop successfully!’);
              break;
      case 4:
         print(‘Enter element u find: ‘);
        val=stdin.readLineSync();
       
        for (var i = 0; i <lstStk.length; i++) {
          if (lstStk[i]==val) {
            print(‘element found!!’);
            cnt++;
           
          }
        }
        if (cnt==0) {
          print(‘element not found!’);
        }
        else{
          cnt=0;
        }
              break;
      case 5:
        if(lstStk.isEmpty){
          print(‘stack underflow!’);
        }
        else{
            print(‘stack is: ‘);
            print(lstStk);
        print(‘stack length is ‘);
        print(lstStk.length);
        }
       
        break;
      case 6:
        exit(0);
        break;
    }
 }
}
output

Queue

a queue is a collection of entities that are maintained in a sequence and can be modified by the addition of entities at one end of the sequence and the removal of entities from the other end of the sequence. By convention, the end of the sequence at which elements are added is called the back, tail, or rear of the queue, and the end at which elements are removed is called the head or front of the queue, analogously to the words used when people line up to wait for goods or services.

The operation of adding an element to the rear of the queue is known as enqueue, and the operation of removing an element from the front is known as dequeue. Other operations may also be allowed, often including a peek or front operation that returns the value of the next element to be dequeued without dequeuing it.

Real life example: when you visit BANK for deposit money into your account, you may face a long line out there. You can’t do your work at the first position of line; you need to wait for your turn. Which person first in the line he/she will do their job first. This is a perfect example of queue order.

The operations of a queue make it a first-in-first-out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed.

Methods

add() -> inserts new element into queue from rear. poll () -> removes existing element from front of the stack. peek() -> gets front of the queue element without removing(deleting). isEmpty() -> checks stack is empty or not.

There are 3 types of queues

  • Double ended queue(dequeue)
  • Priority queue
  • Circular queue

Write a code, to perform enqueue, dequeue, find element, queue length, queue overflow & queue empty(underflow) on integer queue list.

Code 3:

import ‘dart:io’;
 
void main(){
  var que=[];
  late String? val;
  int MAX=6,opt=0,cnt=0,find=0;
  bool i=true;
 
  print(‘Choose Option: ‘);
  while (i) {
    print(‘1. enqueue’);
    print(‘2. dequeue’);
    print(‘3. find element’);
    print(‘4. display queue’);
    print(‘5. exit’);
    print(‘6. enter option: ‘);
    val=stdin.readLineSync();
    if(val!=null){
      opt=int.parse(val);
    }
    switch(opt){
      case 1:
      if(que.length==MAX){
        print(‘queue overflow!’);
        break;
      }
      print(‘Enter enqueue value: ‘);
      val=stdin.readLineSync();
      if(val!=null){
        opt=int.parse(val);
      }
      // element added successfully from the end of queue
        que.add(opt);
        print(‘element inserted into queue successfully!!’);
        break;
      case 2:
      // element remove from queue from front-end(FIFO)
        que.removeAt(0);
        print(‘dequeue done successfully!’);
        break;
      case 3:
        print(‘Enter element u find: ‘);
        val=stdin.readLineSync();
        if(val!=null){
          find=int.parse(val);
        }
        for(int j=0;j<que.length;j++){
          if(find==que[j]){
            print(‘item found!’);
            cnt++;
           
          }
        }
        if(cnt==0){
          print(‘item not found!’);
        }
        else{
          cnt=0;
        }
        break;
      case 4:
        if(que.isEmpty){
          print(‘queue underflow!’);
        }
        print(‘Queue is: $que’);
        print(‘Queue size: ‘);
        print(que.length);
        break;
      case 5:
        exit(0);
        break;
    }
  }
}
output