DATA STRUCTURE - STACK



Similar documents
DATA STRUCTURE - QUEUE

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Data Structures Using C++ 2E. Chapter 5 Linked Lists

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

Chapter 3: Restricted Structures Page 1

7.1 Our Current Model

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Analysis of a Search Algorithm

CmpSci 187: Programming with Data Structures Spring 2015

List, Stack and Queue. Tom Chao Zhou CSC2100B Data Structures Tutorial 3

Stacks. Linear data structures

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Linked Lists Linked Lists, Queues, and Stacks

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Outline. The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack. Stacks 2

Data Structures and Algorithms Lists

Data Structures and Algorithms C++ Implementation

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

DATA STRUCTURES USING C

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Module 2 Stacks and Queues: Abstract Data Types

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

Programming with Data Structures

Chapter Objectives. Chapter 7. Stacks. Various Types of Stacks LIFO. Empty Stack. Stacks

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Data Structures and Algorithms Stacks and Queues

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

STACKS,QUEUES, AND LINKED LISTS

St S a t ck a ck nd Qu Q eue 1

CS 2412 Data Structures. Chapter 2 Stacks and recursion

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Application of Stacks: Postfix Expressions Calculator (cont d.)

Data Structures Using C++

Universidad Carlos III de Madrid

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Common Data Structures

Data Structures and Algorithms

Abstract Data Types. Chapter 2

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

Lecture Notes on Stacks & Queues

Data Structures and Algorithms V Otávio Braga

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

CompSci-61B, Data Structures Final Exam

Linked List as an ADT (cont d.)

Data Structures in the Java API

C++ INTERVIEW QUESTIONS

Data Structures and Data Manipulation

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Stacks. Data Structures and Data Types. Collections

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Java Collection Framework hierarchy. What is Data Structure? Chapter 20 Lists, Stacks, Queues, and Priority Queues

3 Abstract Data Types and Search

Introduction to Stacks

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Sequential Data Structures

Cpt S 223. School of EECS, WSU

Computer Systems Architecture

Basic Data Structures and Algorithms

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Introduction to Data Structures

CHAPTER 7: The CPU and Memory

Algorithms and Abstract Data Types

Data Structures and Algorithms

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Unordered Linked Lists

Glossary of Object Oriented Terms

Chapter 7D The Java Virtual Machine

Stacks and queues. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

Algorithms and Data Structures

TREE BASIC TERMINOLOGIES

Ordered Lists and Binary Trees

Object Oriented Software Design II

Common Operating-System Components

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

Lecture 2: Data Structures Steven Skiena. skiena

Stack Allocation. Run-Time Data Structures. Static Structures

Introduction to Object-Oriented Programming

Sequences in the C++ STL

! stack, queue, priority queue, dictionary, sequence, set e.g., a Stack is a list implements a LIFO policy on additions/deletions.

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

PARALLELIZED SUDOKU SOLVING ALGORITHM USING OpenMP

Queues. Manolis Koubarakis. Data Structures and Programming Techniques

GCE Computing. COMP3 Problem Solving, Programming, Operating Systems, Databases and Networking Report on the Examination.

Transcription:

DATA STRUCTURE - STACK http://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm Copyright tutorialspoint.com A stack is an abstract data type ADT, commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example deck of cards or pile of plates etc. A real-world stack allows operations at one end only. For example, we can place or remove a card or plate from top of the stack only. Likewise, Stack ADT allows all data operations at one end only. At any given time, We can only access the top element of a stack. This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the element which is placed insertedoradded last, is accessed first. In stack terminology, insertion operation is called PUSH operation and removal operation is called POP operation. Stack Representation Below given diagram tries to depict 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. Basic Operations Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from these basic stuffs, a stack is used for the following two primary operations push pushing storing an element on the stack. pop removing accessing an element from the stack. When data is PUSHed onto stack. To use a stack efficiently we need to check status of stack as well. For the same purpose, the

following functionality is added to stacks peek get the top data element of the stack, without removing it. isfull check if stack is full. isempty check if stack is empty. At all times, we maintain a pointer to the last PUSHed data on the stack. As this pointer always represents the top of the stack, hence named top. The top pointer provides top value of the stack without actually removing it. First we should learn about procedures to support stack functions peek Algorithm of peek function begin procedure peek return stack[top] Implementation of peek function in C programming language int peek() { return stack[top]; isfull Algorithm of isfull function begin procedure isfull if top equals to MAXSIZE return true return false Implementation of isfull function in C programming language bool isfull() { if(top == MAXSIZE) return true; return false; isempty Algorithm of isempty function begin procedure isempty if top less than 1 return true return false

Implementation of isempty function in C programming language is slightly different. We initialize top at -1, as index in array starts from 0. So we check if top is below zero or -1 to determine if stack is empty. Here's the code bool isempty() { if(top == -1) return true; return false; PUSH Operation The process of putting a new data element onto stack is known as PUSH Operation. Push operation involves series of steps Step 1 Check if stack is full. Step 2 If stack is full, produce error and exit. Step 3 If stack is not full, increment top to point next empty space. Step 4 Add data element to the stack location, where top is pointing. Step 5 return success. if linked-list is used to implement stack, then in step 3, we need to allocate space dynamically. Algorithm for PUSH operation A simple algorithm for Push operation can be derived as follows begin procedure push: stack, data if stack is full return null top top + 1 stack[top] data Implementation of this algorithm in C, is very easy. See the below code

void push(int data) { if(!isfull()) { top = top + 1; stack[top] = data; { printf("could not insert data, Stack is full.\n"); Pop Operation Accessing the content while removing it from stack, is known as pop operation. In array implementation of pop operation, data element is not actually removed, instead top is decremented to a lower position in stack to point to next value. But in linked-list implementation, pop actually removes data element and deallocates memory space. A POP operation may involve the following steps Step 1 Check if stack is empty. Step 2 If stack is empty, produce error and exit. Step 3 If stack is not empty, access the data element at which top is pointing. Step 4 Decrease the value of top by 1. Step 5 return success. Algorithm for POP operation A simple algorithm for Pop operation can be derived as follows begin procedure pop: stack if stack is empty return null data stack[top] top top - 1 return data Implementation of this algorithm in C, is shown below int pop(int data) {

if(!isempty()) { data = stack[top]; top = top - 1; return data; { printf("could not retrieve data, Stack is empty.\n"); For a complete stack program in C programming language, please click here. Loading [MathJax]/jax/output/HTML-CSS/jax.js