You are on page 1of 13

Stack

Name :Keywan Muhammedamin


Class :Stage 1 (group:B)
Course :Data Structure
Department:Software and Informatics

College of Engineering
Salahaddin University-Erbil
Academic Year 2019-2020
ABSTRUCT

In this report I am going to talk about one pf the data structures which is stack,
.first by giving you specific information and description of data structure
what is this term meaning, a simple explanation about it, giving it a definition in
general with its types in the introduction part of my report, along with this in the
introduction we will give you a general information about the our current subject
.which is stack
What is stack, explaining in a definition, what is its role in the programming of our
devices . What dose it affects on, how dose it helps us to handle our complex
problems, describing it which provides push and pop operations with usual
semantics also the working with the single stack and multiple stack and its
.application
What is the types of stack and focusing on each of its types, there basic operations,
.and when and where the single stack works as multiple if we take as an example
Explaining this and more by giving practically examples with C++ language,
.explaining it by coding and outputs and also by some pictures to explain it more

.And we are trying to present our report in a special order to be easy to get it
First an introduction to the subject then a background and review, method, design
.and outcomes, an at the end we are getting to a conclusion about our subject

CONTENTS
ABSTRACT 2

CONTENTS 3

INTRODUCTION 4-6

BACKGROUND 7-8

DESIGN 9-11

CONCLUSION 12

SOURCES 13
INTRODUCTION

Data Structure is a rigid implementation of a data type. It’s possible to analyze

the time and memory complexity of a Data Structure but not from a data type.

The Data Structure can be implemented in several ways and its

implementation may vary from language to language.

But in these report we are going to show the example and all over the

explanations with C++ language, and we chose this because that I came to

that, it will be more easier and understandable and also more clearly to

explain our topic, also to the whole data structure.

Data Structures include array, linked lists, stacks, binary trees, and hash

tables, among others.

Algorithms manipulate the data in these structures in various ways, such as

searching for a particular data item and sorting the data.

And now I am going to give a simple explanation of abstract data types and
what dose it means. The coming to our main topic, doing that to be more

undersatandable.

Abstract data types, commonly abbreviated ADTs, are a way of classifying

data structures based on how they are used and the behaviors they provide.

They do not specify how the data structure must be implemented but simply

provide a minimal expected interface and set of behaviors.

Stack is an abstract data type with a bounded (predefined) capacity. It is a


Simple data structure that allows adding and removing elements in a
particular order. Every time an element is added, it goes on the top of the
stack and the only element that can be removed is the element that is at the
.top of the stack, just like a pile of objects

Stack holds a collection of elements but different from the Array or List those
elements can only be manipulated from one end of the stack. This end is
.commonly known to be the top of the stack

The principle by which a stack is ordered is called LIFO (last-in first-out).The


last element inserted in the stack would be the first element removed from the
stack

Stack Representation
The following diagram depicts a stack and its operations −

A stack can be implemented by means of Array, Structure, Pointer, and


Linked List. Stack can either be a fixed size one or it may have a sense of
dynamic resizing. Here, we are going to implement stack using arrays, which
makes it a fixed size stack implementation.

Standard Stack Operations


 push() –  Place an item onto the stack. If there is no place for new
item, stack is in overflow state.

 pop() – Return the item at the top of the stack and then remove it. If
pop is called when stack is empty, it is in an underflow state.

 isEmpty() –  Tells if the stack is empty or not

 isfull() – Tells if the stack is full or not.

 peek() – Access the item at the i position

 count() – Get the number of items in the stack.

 change() – Change the item at the i position

 display() – Display all items in the stack

Some Applications of Stack Data Structure

 Balancing of symbols

 Infix to Postfix /Prefix conversion

 Redo-undo features at many places like editors, photoshop.

 Forward and backward feature in web browsers

 Used in many algorithms like Tower of Hanoi, tree traversals, stock


span problem, histogram problem.

 Other applications can be Backtracking, Knight tour problem or sudoku


solver taking as an example.

 In Graph Algorithms like Topological Sorting and Strongly Connected


Components

BACKGROUND
OF THE PRACTICAL WAY

Now we are going to explain stack in example and in pictures to understand


more about stacks role, and how dose it works. And giving you a background
about practical way
Here is an example of implementation of stack taking with arrays by the
.language of C++ in the end after your understanding to stack class

The Stack class

There is a standard C++ class called stack that implements the Stack ADT


(you can use it by including <stack>), but in this chapter I will use a
simplified version of it to illustrate the implementation of an ADT.

Then the syntax for constructing a new STACK is

    stack<ITEMTYPE> mystack;

Initially the stack is empty, as we can confirm with the empty method, which


returns a boolean:

    cout << mystack.empty ();

A stack is a generic data structure, which means that we can add to it any
type of item (ITEMTYPE). For our first example, we'll
use Node<ITEMTYPE> objects, as defined in the previous chapter. Let's start
by creating and printing a short list.

    LinkedList<int> list;
    list.push_front (3);
    list.push_front (2);
    list.push_front (1);
    list.print ();

The output is (1, 2, 3). To put a Node object onto the stack, use
the push method (assuming Node* node):
    mystack.push (*node);

The following loop traverses the list and pushes all the nodes onto the stack:

    stack<Node> mystack;
    for (Node* node = list.head; node != NULL; node = node->next) {
        mystack.push (*node);
  }

We can remove an element from the stack with the pop() function.

    mystack.pop ();

The return type from pop() is void. That's because the stack implementation


doesn't need to keep the item around because it is to be removed from the
stack.

To inspect the last-added element of a stack without removing it, we use


the top() function. The following loop is a common idiom for printing and
popping all the elements from a stack, stopping when it is empty:

    while (!mystack.empty ()) {
        cout << mystack.top().cargo << " ";
        mystack.pop();
  }

The output is 3 2 1. In other words, we just used a stack to print the elements
of a list backwards! Granted, it's not the standard format for printing a list,
but using a stack it was remarkably easy to do.

DESIGN

Now Using Arrays And Then A View From It


#include<iostream>

using namespace std;


#define MAX 1000 //max size for stack

class Stack {

   int top;

   public:

   int myStack[MAX]; //stack array

  

   Stack() { top = -1; }

   bool push(int x);

   int pop();

   bool isEmpty();

};

   //pushes element on to the stack

   bool Stack::push(int item)

   { if (top >= (MAX-1)) {

       cout << "Stack Overflow!!!";

       return false; }

else {

   myStack[++top] = item;

   cout<<item<<endl;

   return true; } }  

//removes or pops elements out of the stack

int Stack::pop()

{ if (top < 0) {

      cout << "Stack Underflow!!";

      return 0; }

else { int item = myStack[top--];

      return item;  } } 

//check if stack is empty

bool Stack::isEmpty()

{ return (top < 0); } 

// main program to demonstrate stack functions

int main() { class Stack stack;


    cout<<"The Stack Push "<<endl;

    stack.push(2);

    stack.push(4);

    stack.push(6);

    cout<<"The Stack Pop : "<<endl;

    while(!stack.isEmpty())

       { cout<<stack.pop()<<endl; } return 0; }

Output:
The Stack Push

The Stack Pop:

In the output, we can see that the elements are pushed into the stack in one
order and are popped out of the stack in the reverse order. This exhibits the LIFO
(Last in, First out) approach for the stack.

For the above array implementation of the stack, we can conclude that this is
very easy to implement as there are no pointers involved. But at the same time,
the size of the stack is static and the stack cannot grow or shrink dynamically.

********************************************************************************************
********************************************************************************************

:To understand more about how dose stack operation


CONCLUSION
At the end we can say that Stack is one of the efficient way to implement to
discipline a system. Stack is a container, where we insert our data in a pile. It is
.most useful where we track our last insertion record or use old data on a first level
.We can do PUSH and PULL operations on the basis of LIFO method

In programming world, we use stack to store data at a runtime on temporary


basis. In stack data structure, we used two types of operations, which were

1. PUSH
2. POP

The logic which is used behind these operations are Last In First Out(LIFO).
LIFO is a method, which tells us that the thing which is PUSH last should be
POP first.

And we can say in general, stacks are useful for processing nested structures
or for functions which call other functions (or themselves). A nested structure
is one that can contain instances of itself embedded within itself.

For example, algebraic expressions can be nested because a subexpression


of an algebraic expression can be another algebraic expression. Stacks are
used to implement functions, parsers, expression evaluation, and
backtracking algorithms.

REFERENCES
1-Data Structures and Algorithms Made Easy(BOOK)

2-Advanced Data Structures (BOOK)

3-Practical Data Structures with C++,C#, and JAVA (BOOK)

You might also like