Drill 3 Data Structures Laboratory
Drill 3 Data Structures Laboratory
Drill 3 Data Structures Laboratory
DRILL 3
STACKS
Name:______________________________
Student Number:______________________
Course/Section:________________________
Date of Performance:____________________
Date of Submission:_____________________
____________________
Professor
GRADE
Beginner (0-4)
Intermediate (5-7)
Skilled (8-10)
Promptness
Report is submitted a
week after the
laboratory
performance.
Report is submitted
on time.
Accuracy
Inappropriate use of
words, poor grammar
and ideas are not
clearly expressed
Does not point out
discussion well
Untidy
Numerous marks of
erasures
Sheets are either
folded or crampled.
Ink used was not
clear.
Appropriate choice of
language
Can express ideas
Discussion was well
versed.
Evident marks of
erasures
Appropriate use of ink.
Clean sheets
Presentation
Score
(over 10)
(over 20)
(over 20)
(over 10)
Completeness
Programming Skills
TOTAL
/100
______________________________________
Signature over Printed Name of Professor
I. DISCUSSION
STACKS
The elementary definition of a stack is an orderly pile arranged in layers. Consider a pile of books. One can place
another book in that stack by putting it at the top. To access the book at the bottom of the stack, most likely one
needs to remove every book at the top, then continue removing the books until the last book has been recovered.
In computer science, the stack is the Abstract Data Type (ADT) for the Last In First Out (LIFO) data structure. Similar
to an array, it can have any data type as an element, but is characterized solely by two basic operations: push and
pop. Below is the illustration of the two operations:
It is said to be restricted due to the fact that only few operations are allowed in it. Push adds a given node to the
top of the stack leaving the previous nodes below. Pop removes and returns the current top node of the stack.
With the two fundamental operations, it is clear that elements are removed from the stack in the reverse order to
the order of their inclusion in the list. Therefore, the lower elements are those that have been on the stack the
longest.
If illustrated using simple arrays and pointer notations, note that every element inside your stack can still be
accessed using its index, which does not correspond with its property that only the last element can be accessed at
a time. The best way to address this situation is to use the concepts of ADT to apply stacks using both arrays and
linked list. Here is a basic stack ADT implementation
template <class stackADT>
class Stacks_ex1
{
public:
Stacks_ex1<stackADT>(int size)
private:
};
}
void InitializeStack()
//Post-condition: The top of stack is set to
{
//a value equal to 0.
stackTop = 0;
}
bool isEmptyStack() const
//Post-condition: Return a value if top of stack
{
//is at its initial value. This denotes that the stack
return (stackTop == 0);
//is empty
}
bool isFullStack()
//Post-condition: Returns a value if top of stack
{
//reaches the assigned maximum stack size. This
return (stackTop == maxsize);
//denotes that the stack is full
}
void push(stackADT newItem)
//Post-condition: Performs the push operation
{
if (!isFullStack())
{
list[stackTop] = newItem;
stackTop++;
}
else
cout<<"Cannot add to a full stack";
}
void pop()
//Post-condition: Performs the pop operation
{
if(!isEmptyStack())
stackTop--;
else
cout<<"Cannot remove from an empty stack!\n ";
}
stackADT top()
//Post-condition: Returns the value of the element
{
//at the top of the stack. Note that to use assert()
assert(stackTop!=0);
//function, include the cassert library.
return list[stackTop - 1];
}
int stackTop;
int maxsize;
stackADT *list;
To implement stacks using arrays, we have the following stack ADT implementation.
template<class stacks>
struct node
{
};
stacks data;
node *link;
template<class stacks>
class Stacks_ex3
{
private:
node<stacks> *stackTop;
public:
Stacks_ex3<stacks>();
void initialize();
void push(stacks);
void pop();
bool isEmptyStack();
stacks top();
};
template<class stacks>
Stacks_ex3<stacks>::Stacks_ex3()
{
stackTop = NULL;
}
template<class stacks>
void Stacks_ex3<stacks>::initialize()
{
node<stacks> * temp;
while(stackTop != NULL)
{
temp = stackTop;
stackTop = stackTop -> link;
delete temp;
}
}
template<class stacks>
bool Stacks_ex3<stacks>::isEmptyStack()
{
return (stackTop == NULL);
}
template<class stacks>
void Stacks_ex3<stacks>::push(stacks newItem)
{
node<stacks> *newNode;
newNode = new node<stacks>;
newNode -> data = newItem;
newNode -> link = stackTop;
stackTop = newNode;
}
template<class stacks>
void Stacks_ex3<stacks>::pop()
{
node<stacks> * temp;
if (stackTop != NULL)
{
temp = stackTop;
stackTop = stackTop -> link;
delete temp;
}
else
cout<<"Stack is empty!"<<endl;
}
template<class stacks>
stacks Stacks_ex3<stacks>::top()
{
assert(stackTop != NULL);
return stackTop -> data;
}
APPLICATION: Postfix Expression
The usual notation for writing arithmetic expressions is called infix notation, in which the operator is between the
two operands. For example, in the expression a + b, the operator + is between operands a and b. The postfix
expression, on the other hand, is the one that has the notation operand1 operand2 operator. For instance, for infix
notation a + b, its postfix representation is a b +. Other examples include the following
Infix expression
Postfix expression
a+b*c
abc*+
The order of operation must be taken into consideration.
a*b+c
ab*c+
The first one to be evaluated or converted into postfix is
(a + b) * c
ab+c*
the operands with *, / and % as its operators (from left to
(a b ) * (c + d)
abcd+*
right) and + and (from left to right). When grouping symbols
(a + b) * (c d / e) + f
ab+cde/-*f+
are included, the first to be converted are expressions in
between the grouping symbols
// Stacks_ex1.cpp. Note that one needs to have Stacks_ex1.h which is composed of the program defined by the
//stackADT implementation. To successfully see the output of the program, file HighestGPAData.txt should be
//created at the specified path.
#include "stdafx.h"
#include "Stacks_ex1.h"
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
#include<conio.h>
using namespace std;
int main()
{
Stacks_ex1 <string> ex1(100);
double GPA, highestGPA;
string name;
ifstream infile;
infile.open("C:\\HighestGPAData.txt");
if(!infile)
{
}
cout<<fixed<<showpoint;
cout<<setprecision(2);
infile>>GPA>>name;
highestGPA = GPA;
while(infile)
{
if(GPA > highestGPA)
{
ex1.InitializeStack();
if(!ex1.isFullStack())
{
ex1.push(name);
}
highestGPA = GPA;
}
else if (GPA == highestGPA)
{
if (!ex1.isFullStack())
{
ex1.push(name);
}
else
}
infile>>GPA>>name;
}
cout<<"Highest GPA = "<<highestGPA<<endl;
cout<<"The students holding the highest GPA are: ";
while (!ex1.isEmptyStack())
{
cout<<ex1.top()<<endl;
ex1.pop();
}
_getch();
// Stacks_ex2.cpp. Note that one needs to have Stacks_ex1.h which is composed of the program defined by the
//stackADT implementation. This sample code will determine the total price of all the items on the list.
#include "stdafx.h"
#include "Stacks_ex1.h"
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
void main()
{
Stacks_ex2 <float> listOfPrices(5);
Stacks_ex2 <string> items(5);
float x=0, sum=0;
string name;
float price;
while (x<5)
{
cout<<"Enter name of Item "<<x+1<<":";
cin>>name;
items.push(name);
cout<<"Enter price:";
cin>>price;
listOfPrices.push(price);
x++;
}
x = 0;
while(x<5)
{
sum = sum + listOfPrices.top();
listOfPrices.pop();
x++;
}
cout<<"The sum of all the prices in the list is " <<sum;
_getch();
//Stacks_ex3.cpp: This one requires the user to have the linked list implementation of stacks as an ADT at Stacks_ex3.h
//stated above. This will convert an infix notation input into its corresponding postfix notation (disregarding the grouping
//symbols as well as the MDAS rule).
#include "stdafx.h"
#include "Stacks_ex3.h"
#include<iostream>
#include<conio.h>
#include<string>
#include<vector>
using namespace std;
void main()
{
Stacks_ex3<char> operation;
char temp = '$';
cout<<"Enter expression:";
char *st = new char[100];
char *stringtemp = new char[100];
gets(st);
int x=0, y=0, z;
while(st[x]!= NULL)
{
if (st[x] == '*' || st[x] == '+' || st[x] == '-' || st[x] == '//' || st[x] == '%')
{
temp = st[x];
}
else
{
if (temp=='$')
{
operation.push(st[x]);
}
else
{
z=0;
while (!operation.isEmptyStack())
{
stringtemp[z] = operation.top();
operation.pop();
z++;
}
operation.push(temp);
operation.push(st[x]);
while(z>0)
{
operation.push(stringtemp[z-1]);
z--;
}
temp = '$';
}
}
x++;
}
while (!operation.isEmptyStack())
{
cout<<operation.top();
operation.pop();
}
_getch();
IV. QUESTIONS
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________