Home computickets Implement pascal stack

Implement pascal stack

Author

Date

Category

How to implement a Pascal stack with routines like:

  • pushing an item onto the stack,
  • popping (along with removing) an element from the stack with a certain position (for example: take an element with position 2, while the entire stack will move one position, since when an element is popped, the position is freed)

Answer 1, authority 100%

@VladD I just found an implementation via dynamic
made a normal stack:

Type
  PComp = ^ Comp; {stack}
  Comp = record
      sD: real;
      pNext: PComp;
  end;
  var
      pTop: PComp;
  Procedure CreateStack (var pTop: PComp); {stack creation procedure}
      begin
          New (pTop);
          pTop ^ .pNext: = nil
      end;
  Procedure AddComp (var pTopPComp; var sC: real); {add item}
      var
          pAux: PComp;
      begin
          New (pAux);
          pAux ^ .pNext: = pTop;
          pTop: = pAux;
          pTop ^ .sD: = sC
      end;
  Procedure GetComp (var pTop: PComp; var sC: real); {remove item}
      begin
          sC: = pTop ^ .sD;
          pTop: = pTop ^ .pNext
      end;

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions