Skip to content

Instantly share code, notes, and snippets.

@NishanthSpShetty
Created January 22, 2020 10:57
Show Gist options
  • Select an option

  • Save NishanthSpShetty/213561fc9fb9733fd01ce823f1ea04b6 to your computer and use it in GitHub Desktop.

Select an option

Save NishanthSpShetty/213561fc9fb9733fd01ce823f1ea04b6 to your computer and use it in GitHub Desktop.

Revisions

  1. NishanthSpShetty created this gist Jan 22, 2020.
    28 changes: 28 additions & 0 deletions stack.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    pub struct VMStack {
    internal_stack: Vec<i32>,
    capacity: usize,
    top: usize, //also size
    }

    impl VMStack {
    pub fn new(stack_size: usize) -> VMStack {
    VMStack { capacity: stack_size, top: 0, internal_stack: Vec::with_capacity(stack_size) }
    }

    pub fn push(&mut self, data: i32) {
    if self.top == self.capacity {
    panic!("Stack overflow Capacity {} , Size {} ", self.capacity, self.top)
    }
    self.internal_stack.push(data);
    self.top += 1;
    }

    pub fn pop(&mut self) -> i32 {

    if self.top == 0 {
    panic!("Stack underflow")
    }
    self.top-=1;
    self.internal_stack.pop().unwrap()
    }
    }