Become familiar with the concept of stacks as a data structures.
Practice implementation of C structures and enumerations.
Practice using pseudo code and D structures to document program design.
Concept of a stack
A stack is a data structure to hold one or more items such that the last item added is the first item taken out. The effect is that the most recently added item is always at the very top. A common acronym for this property is Last In First Out (LIFO).
There are 2 operations that define the behaviour of a stack:
push – This operation adds an item to the very top of the stack.
pop – This operation removes the item at the very top of the stack.
Helper functions for a stack
These 2 operations help make it easier to use a stack:
top – This operation shows the item at the very top of the stack. It does not remove the item.
size – This operation counts the number of items currently in the stack.
Basic implementation of a stack
The following source code is a basic implementation of a stack with a fixed size. In the example, the stack size is fixed to 15.
#include <stdio.h> #include <stdlib.h>
#define MAX_STACK_SIZE 15
struct stack { int currentStackSize; double currentStackItems[ MAX_STACK_SIZE ]; };
struct stack numbers; /* Stack currently in use by program */
void push(double); /* Adds a number to the top of the stack */ double pop(); /* Remove a number from the top of the stack */ double top(); /* Look at the number currently at the top of the stack */ int size(); /* Count the numbers currently in the stack */
int main(void) { /* Add your source code here */ }
Following is source code for the main() function of a program which provides a partial solution for Exercise 1. The stack functions are still required outside of the main() function.
Incremental program that stops when a specific character is found
Following is source code for the main() function of a program which provides a partial solution for all the exercises. The stack functions are still required outside of the main() function.
Accepts input numbers. Use double as the data type to match the stack data type.
Store the numbers into the stack.
Stops reading input numbers when it finds a period by itself, e.g. .
When the input ends, then read back and display all the numbers currently in the stack.
The program above will reverse the order of the input numbers.
For example, if the input is:
5 3.0 2 1 .
The output display would be:
1.00 2.00 3.00 5.00
#include <stdio.h> #include <stdlib.h>
#define MAX_STACK_SIZE 15
struct stack { int currentStackSize; double currentStackItems[ MAX_STACK_SIZE ]; };
struct stack numbers; /* Stack currently in use by program */
void push(double); /* Adds a number to the top of the stack */ double pop(); /* Remove a number from the top of the stack */ double top(); /* Look at the number currently at the top of the stack */ int size(); /* Count the numbers currently in the stack */
int main(void) { char token[64]; char operator; double result = 0.0;
Exercise 2: Calculator for addition operation using Postfix notation
The stack data structure is ideal for implementing a calculator using Postfix notation, which does not require the use of parentheses. This format is also called Reverse Polish Notation (RPN). Read the description here:
struct stack { int currentStackSize; double currentStackItems[ MAX_STACK_SIZE ]; };
struct stack numbers; /* Stack currently in use by program */
void push(double); /* Adds a number to the top of the stack */ double pop(); /* Remove a number from the top of the stack */ double top(); /* Look at the number currently at the top of the stack */ int size(); /* Count the numbers currently in the stack */
int main(void) { char token[64]; char operator; double operand1, operand2, sum, result = 0.0;
Exercise 3: Calculator using Postfix notation with 4 basic operations
Add source code to the program from Exercise 2 for implementing the remaining basic algebraic operations:
Subtraction
Multiplication
Division
Be careful about the order of the operands!
For example, if the input is:
5 1 2 + 4 * + 3 - .
The output display would be:
-14.00
#include <stdio.h> #include <stdlib.h>
#define MAX_STACK_SIZE 15
struct stack { int currentStackSize; double currentStackItems[ MAX_STACK_SIZE ]; };
struct stack numbers; /* Stack currently in use by program */
void push(double); /* Adds a number to the top of the stack */ double pop(); /* Remove a number from the top of the stack */ double top(); /* Look at the number currently at the top of the stack */ int size(); /* Count the numbers currently in the stack */
struct stack { int currentStackSize; double currentStackItems[ MAX_STACK_SIZE ]; };
struct stack numbers; /* Stack currently in use by program */
void push(double); /* Adds a number to the top of the stack */ double pop(); /* Remove a number from the top of the stack */ double top(); /* Look at the number currently at the top of the stack */ int size(); /* Count the numbers currently in the stack */
int main(void) { char token[64]; char operator; double operand1, operand2, result, calculation = 0.0; long iOperand1, iOperand2, iResult;