Вы находитесь на странице: 1из 21

LAB SESSION 6: Stack and Queue 1.0 Objectives 1. To illustrate how stack and queue data structures work.

2. To develop suitable algorithms for solving the problems of data configuration and manipulation in stack and queue. 3. To analyze the difference and similarity between the operations that can be implemented in stack and queue. 4. To discuss the result from this e periment and present technical report. !. To complete every tasks in this e periment effectively as individual or in group. Theo ! Stack: The stack is one e ample of a constrained linear data structure. "n a stack# the data items are ordered from most recently added $the top% to least recently added $the bottom%. &ll insertions and deletions are performed at the top of the stack. 'ou use the push operation to insert a data item onto the stack and the pop operation to remove the top most stack data item. & sequence of pushes and pops is shown below(

These constraints on insertion and deletion produce the )last in# first out* $+",-% behavior that characterizes a stack. &lthough the stack data structure is narrowly defined# it is so e tensively used by systems software that support for a primitive stack. .rimitive stack is one of the basic data items of most computer architectures. The stack is one of the most frequently used data structures. &lthough all programs share the same definition of stack / a sequence of homogenous data items with insertion and removal done at each end / the type of data items stored in stacks varies from program to program. 0ome use stacks of integers1 others use stack of characters# floating point numbers and so forth. 0tack can be implemented by using various functions for the stack operation. 2owever it is laborious and error3prone. The better approach is by using the 455 class. LI"O ( $last in first out% is e actly how a stack operates. The last value put on a stack is

the first value removed from a stack. #o$ ( an operation on a stack that inserts a value to the top of the stack. #ush ( an operation on a stack that removes the top value from the stack. %nde &'o(( occurs when a value is attempted to be removed from a stack# but no values e ist on the stack. 6elow is the clearly illustration for the operation in stack

"i)u e *#ush and #o$ o$e ation+ ,h! $eo$'e need stack in thei 'i&e1st situation ( 'ou are writing a notepad application. 0tack is good to use because it able to undo and redo feature. 7verytime the users enters something# save the state $ in this case# the tes t% on a stack and if you need to reverse something# 8ust pop it off the top of the undo stack and push it onto the redo stack. 2nd situation ( 0tacks are also used for reversing things# if you push something# say a 0tring onto a 0tack one character at a time# and then construct a 0tring from the members popped off the 0tack# then the 0tring is reversed. Q%E%E 9ueue is another constrained linear data structure. The data items in a queue are ordered from least recently added $the front% to the most recently added $the rear%. "nsertions are performed at the rear of the queue and deletions are performed at the front. 'ou use enqueue operation to insert data items and the dequeue operation to remove data items. & sequence of enqueues and dequeues is shown below(

"I"O : $first in first out% is e actly how a queue operates. "t describes the order values are inserted and removed into a data structure. " ont : the pointer to where items are removed when dealing with a queue. Queue : a simple data structure that allows insertions and deletions following a ,",behavior. .ea : the pointer to where items are added when dealing with a queue. E/a0$'e o& Queue o$e ation 1i cu'a Queue :iven an array & of a default size $; 1% with two references back and front# originally set to 31 and < respectively. 7ach time we insert $enqueue% a new item# we increase the back inde 1 when we remove $dequeue% an item 3 we increase the front inde . 2ere is a picture that illustrates the model after a few steps(

&s you see from the picture# the queue logically moves in the array from left to right. &fter several moves back reaches the end# leaving no space for adding new elements

2owever# there is a free space before the front inde . =e shall use that space for enqueueing new items# i.e. the ne t entry will be stored at inde <# then 1# until front. 0uch a model is called a ( a$ a ound 2ueue or a ci cu'a 2ueue

,inally# when back reaches front# the queue is full. There are two choices to handle a full queue(a% throw an e ception1 b% double the array size. The circular queue implementation is done by using the modulo operator $denoted >%# which is computed by taking the remainder of division $for e ample# ?>! is 3%. 6y using the modulo operator# we can view the queue as a circular array# where the @wrapped around@ can be simulated as @back > arrayAsize@. "n addition to the back and front inde es# we maintain another inde ( cur 3 for counting the number of elements in a queue. 2aving this inde simplifies a logic of implementation. 3.0 E2ui$0ent List 4 .e2ui e0ent ,or this lab session# we need( 1. &n editor for 455 programming language installed in your system $i.e. Bicrosoft Cisual 0tudio 455# 6orland 455# 43,ree# etc.% 2. ,undamental of 455 programming skill

5.0 Task:

Task 1: Stack c'ass i0$'e0entation The following is the class declaration of the stack( Dinclude EiostreamF Ddefine defaultsize 2< class stack G public( stack$%1 stack$int stacksize%1 Hstack$%1 int top$%1 int pop$%1 void push $int value%1 void clear$%1 bool isempty$%1 private( int Iarray1 int 4urrent.osition1 J Q%ESTION 1: "mplement the operations $methods% of the stack class based on these descriptions( 1ONST.%1TO. a. & constructor for the stack must allocate the proper amount of memory and initialize the top of the stack. "f an ob8ect of type stack is created with no parameters# then the stacksKs internal array is initializes to 2< indicated by the defaultsize define statement. stack ( ( stack$% G LLset your code J b. The second constructor# stack $int stacksize%# allows the programmer to set the size. "f the size of array is less than zero# it will sets the size to the default size. This ensures that an empty or negative size stack cannot be specified. stack ( ( stack$int stacksize% G LLset your code J 6EST.%1TO.

c. The destructor for a stack simply frees any memory allocated by the stack constructors. stack (( Hstack$% G LLset your code J TO# d. The top method simply return the value at the top of the stack. The method can be improved# which is before returning the value# we can check the stack to make sure it is not empty $4urrent.osition is not negative%. "f it is# then we output a warning that the stack has been accessed illegally. int stack (( top$% G LLset your code J #O# e. The pop method operates similar to the top method. =e need to ensure that a value e ists on the stack before we remove it. "f it is does e ist# we remove the value on the top of the stack by decrementing the stack counter and returning the value from the method. "f the 4urrent.osition is negative# an error message is displayed $showing that the stack is underflow / )stack underflow*%. int stack (( pop$% G LLset your code J #%S7 f. The push method operates in reverse of the pop method. =e need to ensure that there is room remaining in the stack for the value. "f there is# the value is placed on the stack. -therwise# an error message is generated $)stack overflow*%. void stack (( push$int value% G LLset your code J ISE8#T9 g. The isempty method simply returns true if the stack empty. This is important because it allows the user to determine if there is a value to remove before issuing the pop method.

bool stack (( isempty$% G LLset your code J 1LEA. h. The final method is clear. "t simply empties the stack by resetting the 4urrent.osition variable to its initial value. void stack (( clear$% G LLset your code J Q%ESTION :: 4ompile the following reversedigit program to run and test your stack class. TEST #.O;.A8 void main$% G stack reversedigit1 int inAdata1 int outAdata1 coutEE@top stack( @EEreversedigit.top$%EEendl1LLline ! coutEE@enter digits to reverse( @EEendl1LLline M coutEE@$enter N<N to stop%@EEendl1LLline O do LLline ? G cinFFinAdata1 reversedigit.push$inAdata%1 J while $PinAdataQQ<%1 LLline 11 coutEE@top stack( @EEreversedigit.top$%EEendl1LLline 12 reversedigit.push$R2%1 LLline 13 reversedigit.push$3%1 reversedigit.push$1<<%1 LLline 1! coutEE@top stack( @EEreversedigit.top$%EEendl1LLline 1M while$Preversedigit.isempty$%% LLline 1O G outAdataQreversedigit.pop$%1 coutEEoutAdataEE@ @1 J coutEEendl1 LLline 22 reversedigit.clear$%

coutEE@top stack( @EEreversedigit.top$%EEendl1LL J 6ased on your output# answer the following questions( a. =hat is the output of line !S =hat is the usage of the command in line !S b. 7 plain the operation of the command in line ? through line 11. c. =hat is the output of line 12S :ive your comments. d. =hat is the output of line 1MS :ive your comments. e. 7 plain the operation of the command in line 1O through line 22. Task :: Queue c'ass i0$'e0entation The following is the class declaration of the queue( Dinclude EiostreamF Ddefine defaultsize ! class queue G public( queue$%1 queue $int queuesize%1 Hqueue $%1 int first $%1 int remove $%1 void insert $int value%1 void clear $%1 bool isempty $%1 private( int Iarray1 int front1 int rear1 int arraysize1 int currentsize1 J1 Q%ESTION 1: "mplement the operations $methods% of the queue class based on these descriptions( 1ONST.%1TO. a. & constructor for the 2ueue must set up an empty queue. The first constructor assumes

that the size of the 2ueue is set to the de&au'tsi<e. 7nough memory space for the queue must be allocated $by using a dynamic memory allocation%. The current number of items in the queue is set to zero. 0et the & ont and ea pointers to the proper inde es $& ont should point to the first element of the array# and ea should point to the last element%. b. The set up for the second constructor is similar with the first constructor. 2owever it takes a parameter that allows the user to specify the default size of the 2ueue. 6EST.%1TO. c. The destructor for a stack simply frees any memory allocated by the 2ueue constructors. INSE.T d. To insert a value into the 2ueue# there must be room for it. ,irst# make sure that the cu entsi<e of the queue is less than the ma imum size. "f so# increment the ea pointer so that is point to the ne t available cell of the array. "f the ea pointer is set to the last cell of the array# then set the ea pointer to zero# thus implementing a circular array. -nce the ea pointer is set to the ne t available cell# copy the insertion value to the cell and increment the current number of items by one. .E8O=E e. To remove a value from the 2ueue# there must be a value on the queue. Therefore# we must check to make sure that a value e ists by comparing the current number of items to zero. Teturn the value pointed by the & ont inde and then increment the & ont inde . "f the & ont inde is already at the last element of the array# set it to zero. ISE8#T9 f. The ise0$t! method simply returns true if the number of items in the 2ueue is equal to zero1 otherwise it returns &a'se. "I.ST g. The &i st method operates similarly to the e0ove method# e cept that when it returns the first value of the 2ueue# it does not remove it from the 2ueue. 1LEA. h. The final method is c'ea . "t used to reset the 2ueue. The c'ea method will reset the inde es and counters of the class so that the 2ueue appears empty. The actual contents of the 2ueue will remain# but will be ignored. Q%ESTION :: 3.2.1 4ompile the following program to run and test your queue class.

TEST #.O;.A8 void main$% G queue line1 line.remove$%1 LLline 4 line.clear$%1 LLline ! line.insert$12%1 LLline M line.insert$23%1 LLline O line.insert$1%1 LLline ? line.insert$RR%1 LLline R coutEEline.first$%EEendl1 LLline 1< while$Pline.isempty$%% LLline 11 G coutEEline.remove$%EE@ @1 J LLline 14 line.clear$% line.remove$%1 J 6ased on your output# answer the following questions( a. =hat is the output of line 4S :ive your comments. b. 7 plain the operation of the command in line M through line R. c. =hat is the output of line 1<S :ive your comments. d. 7 plain the operation of the command in line 11 through line 14. e. 2ow to have a queue with the size of 1<S 0how your code.

>.0 .esu't Task 1: Stack c'ass i0$'e0entation "u'' 1odin)

O%T#%T

TAS? : Q%E%E 1LASS I8#LE8ENTATION "u'' codin)

6.0 6iscussion @ 1onc'usion 1. 4onclude the findings throughout your lab e periment and answer all the questions in this lab sheet.

=hat is the output of line 4S :ive your comments. Underflow# because thereKs no data in queue a. 7 plain the operation of the command in line M through line R.

O%T#%T

6.0

6IS1%SSION

Based on !ou out$utA ans(e the &o''o(in) 2uestions: Task 1 : Stack c'ass i0$'e0entation a. ,hat is the out$ut o& 'ine >- ,hat is the usa)e o& the co00and in 'ine >The command in line ! is to show that the stack is empty. Underflow because the top stack is <. b. E/$'ain the o$e ation o& the co00and in 'ine B th ou)h 'ine 11. The command in line ? through line 11 is to pushed the data in the stack that user have insert until the user enter < to stop the insertion process. c. d. e. ,hat is the out$ut o& 'ine 1:- ;ive !ou co00ents. The output is < because the last data that was push is <. ,hat is the out$ut o& 'ine 16- ;ive !ou co00ents. The output is 1<< because the last data that was pushed and on top is 1<<. E/$'ain the o$e ation o& the co00and in 'ine 1C th ou)h 'ine ::. ,unction while is compare the data with isempty function. "t VpopK all the data and show at the output until the stack become underflow.

Task :: Queue c'ass i0$'e0entation a. ,hat is the out$ut o& 'ine 5- ;ive !ou co00ents. Underflow# because thereKs no data in queue b. E/$'ain the o$e ation o& the co00and in 'ine 6 th ou)h 'ine D. Using the concept ,",- $first in first out% , T 12 line.insert$23%1 LLline O Q , 12 2 3 line.insert$1%1 LLline ? Q , 12 2 3 line.insert$RR%1 LLline R Q , 12 2 3 1 1 T RR LLLnumber RR is insert and queue T LLnumber 1 is insert and queue T line.insert$12%1 LLline M Q LLnumber 12 is insert first LLnumber 23 is insert and queue

c. ,hat is the out$ut o& 'ine 10- ;ive !ou co00ents. The output of line 1< is 1: because the command ask to display the first queue data . d. E/$'ain the o$e ation o& the co00and in 'ine 11 th ou)h 'ine 15. The command will do looping with condition that if the line is not empty# it will remove the data Wthe looping end when the line is empty. e. 7o( to have a 2ueue (ith the si<e o& 10- Sho( !ou code.

,irst we write the given program for stack class implementation# based on the program of stackA we implement the operations of the class based on below types with its description that can be read at the lab sheet( 33constructor 33destructor 33top 33pop 33push 33isempty 33clear &fter done the continuous coding# we compile and correct if the the is any error displayed. =e add the following given coding on question 2 for reversedigit and run and test the stack class. =e printscreen the output for the report and answer the question given. The answer for each task can be seen at discussion part. +ater# we continue the task 2 where the step for same task 2 9ueue class implementation is also same only the coding and the types of implement coding is different which is queue need following implement and thus the implement coding are different with the previous one. 33constructor 33destructor 33insert 33remove isempty 33first 33clear =hen we finished create the coding# we compile it and correct the error that displayed in the bo . Then# we continue type the question 2 coding and e ecute it. The output is printscreen for report purpose. =e answer the question given.

1ON1L%SION

-verall# " had achieved the ob8ective goal. " able to illustrate how stack and queue data structures work# able to develop suitable algorithms for solving the problems of data configuration and manipulation in stack and queue# able to analyze the difference and similarity between the operations that can be implemented in stack and queue# also discussing the result from this e periment and present technical report and complete every tasks in this e periment effectively as individual or in group. &fter review back the topic on stack and queue# " decided to differentiate them in a table as shown below( 0T&4X Y simplest one Y easy to implement. Y insertion or deletion from one place only Y fast operatiom Y stack is a recursive function 3only can be inserted and removed from the top 9U7U7 Y & queue is a first3in first3out structure $,",-%. 7lements are added at one end $tail% and removed from the other end $head%. Y 9ueue behavior is subsumed by -rdered4ollection and there is no need for class 9ueue.

Вам также может понравиться