Languages :: Pascal :: Stacks and Queues example |
|||
| By: VB guy |
Date: 29/03/2003 00:00:00 |
Points: 50 | Status: Answered Quality : Excellent |
|
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. |
|||
| By: VB guy | Date: 29/03/2003 05:27:00 | Type : Comment |
|
| I will post the unit in 1 hour. Sorry about that. |
|||
| By: VGR | Date: 29/03/2003 05:45:00 | Type : Comment |
|
| 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 |
|||
| By: VB guy | Date: 29/03/2003 19:18:00 | Type : Comment |
|
| 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. |
|||
| By: mlmcc | Date: 29/03/2003 19:55:00 | Type : Comment |
|
| 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 |
|||
| By: VB guy | Date: 29/03/2003 20:47:00 | Type : Comment |
|
| 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. |
|||
| By: VGR | Date: 29/03/2003 21:36:00 | Type : Comment |
|
| 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. |
|||
| By: VB guy | Date: 29/03/2003 21:56:00 | Type : Comment |
|
| 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. :) |
|||
| By: mlmcc | Date: 29/03/2003 22:38:00 | Type : Comment |
|
| 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 |
|||
| By: VGR | Date: 29/03/2003 23:17:00 | Type : Answer |
|
| 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 |
|||
| By: TheFalklands | Date: 30/03/2003 07:07:00 | Type : Comment |
|
| 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. |
|||
|
Do register to be able to answer |
|||
| Add This Article To: | |||
| |
|
|
|
| |
|
|
|








