long report ds
TRANSCRIPT
-
8/13/2019 Long Report Ds
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 eperiment and present technical report.
!. To complete every tasks in this eperiment effectively as individual or in group.
Theo!
Stack:
The stack is one eample 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 toinsert 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. <hough the stack data structure is narrowly defined# it is soetensively 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. <hough all programs share
the same definition of stack / a sequence of homogenous data items with insertion and removaldone at each end / the type of data items stored in stacks varies from program to program. 0omeuse stacks of integers others use stack of characters# floating point numbers and so forth.
0tack can be implemented by using various functions for the stack operation. owever it is
laborious and errorprone. The better approach is by using the 55 class.
LI"O ( $last in first out% is eactly how a stack operates. The last value put on a stack is
-
8/13/2019 Long Report Ds
2/21
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 eist on the stack.
6elow is the clearly illustration for the operation in stack
"i)ue *#ush and #o$ o$eation+
,h! $eo$'e need stack in thei 'i&e-
1st 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 test% on a stack
and if you need to reverse something# 8ust pop it off the top of the undo stack and push it onto theredo stack.
2ndsituation (
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 enqueueoperation to insert data items and the dequeue operation to remove data items. & sequence of
enqueues and dequeues is shown below(
-
8/13/2019 Long Report Ds
3/21
"I"O : $first in first out% is eactly 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/a$'e o& Queue o$eation
icu'a Queue
:iven an array & of a default size $; 1% with two references backandfront# originally set to 1
and < respectively. 7ach time we insert $enqueue% a new item# we increase the back inde when
we remove $dequeue% an item we increase the front inde. ere is a picture that illustrates themodel after a few steps(
&s you see from the picture# the queue logically moves in the array from left to right. &fterseveral moves backreaches the end# leaving no space for adding new elements
owever# there is a free space before the front inde. =e shall use that space for enqueueing
new items# i.e. the net entry will be stored at inde
-
8/13/2019 Long Report Ds
4/21
,inally# when backreachesfront# the queue is full. There are two choices to handle a fullqueue(a% throw an eception 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 eample# ?>! is 3%. 6y using the modulo
operator# we can view the queue as a circular array# where the @wrapped around@ can besimulated as @back > arrayAsize@. "n addition to the back and front indees# we maintain another
inde( cur for counting the number of elements in a queue. aving this inde simplifies a logic
of implementation.
3.0 E2ui$ent List 4 e2uieent
,or this lab session# we need(
1. &n editor for 55 programming language installed in your system $i.e. Bicrosoft
Cisual 0tudio 55# 6orland 55# ,ree# etc.%
2. ,undamental of 55 programming skill
5.0 Task:
-
8/13/2019 Long Report Ds
5/21
Task 1: Stack c'ass i$'eentation
The following is the class declaration of the stack(
Dinclude EiostreamF
Ddefine defaultsize 2
-
8/13/2019 Long Report Ds
6/21
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 $urrentosition is not negative%. "f it is# then we output a warning that thestack 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
eists on the stack before we remove it. "f it is does eist# we remove the value on the
top of the stack by decrementing the stack counter and returning the value from the
method. "f the urrentosition is negative# an error message is displayed $showingthat 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 isroom 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 codeJ
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.
-
8/13/2019 Long Report Ds
7/21
bool stack (( isempty$%
G
LLset your codeJ
LEA
h. The final method is clear. "t simply empties the stack by resetting the urrentosition variable
to its initial value.
void stack (( clear$%
G
LLset your codeJ
Q%ESTION :
ompile the following reversedigitprogram to run and test your stack class.
TEST #O;A8
void main$%
G
stack reversedigit
int inAdataint outAdata
coutEE@top stack( @EEreversedigit.top$%EEendlLLline !
coutEE@enter digits to reverse( @EEendlLLline McoutEE@$enter N
-
8/13/2019 Long Report Ds
8/21
coutEE@top stack( @EEreversedigit.top$%EEendlLL
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. 7plain 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. 7plain the operation of the command in line 1O through line 22.
Task : Queue c'ass i$'eentation
The following is the class declaration of the queue(
Dinclude EiostreamF
Ddefine defaultsize !class queue
G
public(
queue$%queue $int queuesize%
Hqueue $%
int first $%int remove $%
void insert $int value%
void clear $%bool isempty $%
private(
int Iarray
int frontint rear
int arraysize
int currentsizeJ
Q%ESTION 1:
"mplement the operations $methods% of the queue class based on these descriptions(
ONST%TO
a. & constructor for the 2ueue must set up an empty queue. The first constructor assumes
-
8/13/2019 Long Report Ds
9/21
that the size of the 2ueue is set to the de&au'tsi
-
8/13/2019 Long Report Ds
10/21
TEST #O;A8
void main$%G
queue line
line.remove$% LLline 4
line.clear$% LLline !line.insert$12% LLline M
line.insert$23% LLline Oline.insert$1% LLline ?
line.insert$RR% LLline R
coutEEline.first$%EEendl LLline 1.0 esu't
Task 1: Stack c'ass i$'eentation
"u'' odin)
-
8/13/2019 Long Report Ds
12/21
-
8/13/2019 Long Report Ds
13/21
O%T#%T
-
8/13/2019 Long Report Ds
14/21
TAS? Q%E%E LASS I8#LE8ENTATION
"u'' codin)
6.0 iscussion @ onc'usion
1. onclude the findings throughout your lab eperiment and answer all the questionsin this lab sheet.
=hat is the output of line 4S :ive your comments.
Underflow# because thereKs no data in queue
a. 7plain the operation of the command in line M through line R.
-
8/13/2019 Long Report Ds
15/21
-
8/13/2019 Long Report Ds
16/21
O%T#%T
-
8/13/2019 Long Report Ds
17/21
-
8/13/2019 Long Report Ds
18/21
Task : Queue c'ass i$'eentation
a. ,hat is the out$ut o& 'ine 5- ;ive !ou coents.
Underflow# because thereKs no data in queue
b. E/$'ain the o$eation o& the coand in 'ine 6 thou)h 'ine D.
Using the concept ,",- $first in first out%
, line.insert$12% LLline M Q
LLnumber 12 is insert first
line.insert$23% LLline O Q , LLnumber 23 is insert and queue
line.insert$1% LLline ? Q , LLnumber 1 is insert and queue
line.insert$RR% LLline R Q , LLLnumber RR is insert and queue
c. ,hat is the out$ut o& 'ine 10- ;ive !ou coents.
The output of line 1< is 1because the command ask to display the first queue data .
d. E/$'ain the o$eation o& the coand in 'ine 11 thou)h 'ine 15.
The command will do looping with condition that if the line is not empty# it will removethe data Wthe looping end when the line is empty.
e. 7o( to have a 2ueue (ith the si
-
8/13/2019 Long Report Ds
19/21
,irst we write the given program for stack class implementation# based on the program
of stack we implement the operations of the class based on below types with its description that
can be read at the lab sheet(
constructor
destructor
top
pop
push
isempty
clear
&fter done the continuous coding# we compile and correct if the the is any errordisplayed. =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 needfollowing implement and thus the implement coding are different with the previous one.
constructor
destructor
insert
remove isempty
first
clear
=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 eecute it. The output is printscreen
for report purpose. =e answer the question given.
-
8/13/2019 Long Report Ds
20/21
ONL%SION
-verall# " had achieved the ob8ective goal. " able to illustrate how stack and queue datastructures 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 resultfrom this eperiment and present technical report and complete every tasks in this eperiment
effectively as individual or in group.
&fter review back the topic on stack and queue# " decided to differentiate them in a tableas shown below(
0T&X 9U7U7
Y simplest one
Y easy to implement.
Y insertion or deletion from one place only
Y fast operatiom
Y stack is a recursive function
only can be inserted and removed from the top
Y & queue is a firstin firstout structure $,",-%.
7lements are added at one end $tail% and
removed from the other end $head%.
Y 9ueue behavior is subsumed by
-rderedollection and there is no need for
class 9ueue.
-
8/13/2019 Long Report Ds
21/21