?
Solved

Stacks and Queues example

Posted on 2003-03-29
10
Medium Priority
?
186 Views
Last Modified: 2010-04-16
I have to make a program that uses the following Unit. I know that you can't provide me with the full source code so I expect just some guidelines not the actual program. Please tell me what the basic structure of the program should be. 10x a lot.
0
Comment
Question by:Terry007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 

Author Comment

by:Terry007
ID: 8231267
I will post the unit in 1 hour. Sorry about that.
0
 
LVL 15

Expert Comment

by:VGR
ID: 8231316
well, if Iwere your teacher, I would have done the reverse :
-here's a program using some queues&stacks (LIFO,FIFO files), write the Unit to implement them.

would be funnier
0
 

Author Comment

by:Terry007
ID: 8231632
Unit stack1;
 intrface
   type BaseT=integer;
   const StSize=100;
   function push(x:BaseT):boolean;
   function pop (var x:BaseT) boolean;
 implementation
  var S:array[1:StSize] of BaseT;
      top:integer;
  function push(x:BaseT):boolean;
   begin
    if top = StSize then
     push = false else
      begin inc top(top);
      S[top]:=x;
      push:=true;
    end;
  function pop(var x:BaseT):boolean;
   begin
    if top:=0 then pop:=false
     else
      begin
       x:=S[top]; Dec[top];pop:=true;
   begin
   top:=0;
   end;
  end;
end.


This is the unit that has to be used in a program using stack. The book I use ("Learn Pascal" by Sam A. Aboulrouls)
says nothing about that so I'd be glad if you could provide me with some ideas or links that could be usefull.
0
On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

 
LVL 101

Expert Comment

by:mlmcc
ID: 8231754
What are you trying to do with the program?

Basic stack program

if wanting to put something on
  check if stack has room
    push the item on
if wanting to take something off
  check stack has items
    pop it off

mlmcc
0
 

Author Comment

by:Terry007
ID: 8231897
The problem is that I don't know how can I put this Unit into use. What kind of program could use it. Please give me a simple example.
0
 
LVL 15

Expert Comment

by:VGR
ID: 8232071
something like :

program testqueues;
uses stack1;
Var v : BaseT;
Begin
  if push(12) Then WriteLn('suceeded in pushing 12 to the stack') Else  WriteLn('failed in pushing 12 to the stack');
  if pop(v) Then Begin
    WriteLn('suceeded in popping from the stack');
    WriteLn('value read = ',v,' (should be 12 ;-)');
  End Else  WriteLn('failed in popping from the stack');
End.
0
 

Author Comment

by:Terry007
ID: 8232328
10x VGR. Just one more thing. I so the stack is actually an abstract term. I mean it represents the status of a block of memory but it is not part of the language itself. :)
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 8232407
Tha is correct.  A stack is an abstract datatype used for a variety of algorithms.

Most arithmetic is done using stacks.  Operands are pushed onto the stack and popped off when needed by an operator.

Functions and procedures generally pass there parameters using the stack.

You can think of a stack as a last-in first-out data structure.  Information is put on the top and then retrieved from the top.

A queue is similarly an abstract datatype with other uses.  It is a first-in first-out structure.  Information is put in at one end and retrieved from the other.

You can look at a queue as the line at the store.  Everyone starts at the back and slowly moves to the front then gets checked out and moved out of the queue.

A stack is like the dishes at the buffet.  You take them off the top and the staff replaces them on the top.

mlmcc
0
 
LVL 15

Accepted Solution

by:
VGR earned 200 total points
ID: 8233023
in fact, the stack is a user datatype. It's no more "abstract" than "Extended" IMHO (I mean abstract in opposition to real/instanciated/existent/physical)

Even better, the stack is ***apparently*** not a language construction, while it is : it's the basic data structure that enables you to call a procedure or function, for example. It has to be coded in assembly if ever you write a procedure call or branchment in that language, other example.
It's a native data structure in FORTH, of course

Even worse, files (qtack and queues) structure is built in in almost all processors, from Intel to superscalar ones. It's used in your browser, in your IP communication layers, in your email client, etc
0
 

Expert Comment

by:tycordinal
ID: 8233212
stack can be used for:

1. remember the history of all websites you have visited, so that when you press Back button, the browser remember where you have visited last time, and you will be brought back to the most recently visited website.

queue can be used for:
1. arrange your email. when your emails come one by one into your mail box, the email software has to arrange them, so that you can see the most recent incoming email appear at the top.

hope u appreciate my examples. if so, please give me points.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Summer 2017 Scholarship Winners have been announced!
Hey fellow admins! This time, I have a little fairy tale for you. As many tales do, it starts boring and then gets pretty gory. I hope you like it. TL;DR: It is about an important security matter, you should read it if you run or administer Windows …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question