You are on page 1of 6

9/13/12 Stacks in C++ | C/c++ Programming Resources | Source Code Mania.

COM

HOM E

TUTOR IA LS

PR OG R A M M ING LECTUR ES

PR OG R A M M ING BOOKS

S OUR CE CODE

F OR UM S

JOIN US

Search

Circular Linked Lists

Applications of Stack in data structures

Ads by Google

C++ Stack C
CATEGOR IES

PHP Programming Resources C/C++ Programming Resources JAVA Programming Resources .NET Programming Resources Android Programming Resources iPhone Programming Resources

Stacks in C++
L EAVE A C O MMENT

Stack is an ordered group of homogeneous items. Items are added to and removed from the top of the stack (the most recently added items are at the top of the stack). last item to be added is the first to be The removed (LIFO: Last In, First Out). A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted at one end called the TOP of the stack. The implementation of stack provides for the insertion and deletion of items (at run time), so that a stack is a dynamic, constantly changing object.

R ECENT POS TS

Navigation Bar Controller iPhone Development Using View Controller iPhone Development UIKit Views and Animation iPhone Development Views and Drawing iPhone Development Model View Controller (MVC) iPhone Development

POPULAR POS TS (LAS T 30 DAYS )

Applications of Stack in data structures 1864 view(s) File Handling using Input-Output Streams in Java 1580 view(s) Advanced Java Tutorial (For Intermediate) 1348 view(s) Attendance Management System 1230 view(s) String Manipulation in Java 1008 view(s) JAVA Graphical User Interface (GUI) 974 view(s) Sockets and Network Programming in Java 917 view(s)

How does a stack change? items may be put on top of the stack. In which case the top of the stack New moves upward to correspond to the new highest element. Items which are at the top of the stack may be removed. In which case the top of the stack moves downward to correspond to the new highest element.

sourcecodemania.com/stacks-in-cpp/

1/6

9/13/12 Stacks in C++ | C/c++ Programming Resources | Source Code Mania.COM Circular Linked Lists 914 view(s) UDP Datagram Sockets in Java 891 view(s) Advanced Data Structures Tutorial using C++ 805 view(s)

Which end is top? We must decide which end of the stack is designated as its top. common data A structure in computing. items are popped and pushed (retrieved and stored) from the top of the Data stack. Stacks normally have a maximum size. It is an error to push items onto a full stack, or pop items off an empty stack.

Applications of Stack
Parsing of algebraic expression Banking Transaction View (You view the last transaction first)

TAGS

C/C++ language Classes


Android

C-Sharp

CSS Data

structures

Design Pattern Eclipse


Graphics Design Books HTML

iPhone

JAVA Java
JAVA GUI

Collections

JavaScript Mobile

Programming Books
PDF PHP pointers

Object Oriented
Programming

Programming Books Programming


Languages Books
RaphaelJS

Insertion and Deletion in Stack


C is the current top element of the stack. If any new items are added to the stack, they are placed on top of C If any new items are deleted C is deleted first.

Source Code Web Development Books


Threads Tutorial

Operations on Stack
A Stack normally has the following methods: push(item) pop( ) isEmpty() isFull() top( ) //Push an item onto the stack //Pop the top item off the stack //Return true if stack is empty // Return true if stack is full //Return value of top item

There are several ways to implement a Stack in C++.

Implementing a Stack in C++


First, if we want to store letters, we can use type char. since a stack usually holds a bunch of items Next, with the same type (e.g., char), we can use an array to hold the contents of the stack. some point well At have to decide how big this array is; keep in mind that a normal array has a fixed size. Lets choose the array to be of size 4 for now. So, an array getting A, then B, will look like: A [0] B [1] [2] [3]

How do we know that which element to pop when user asks a pop operation. As not all the spaces of array
sourcecodemania.com/stacks-in-cpp/ 2/6

9/13/12 Stacks in C++ | C/c++ Programming Resources | Source Code Mania.COM

are filled. e need another variable (usually Int) to keep track of last pushed index. for each push we add W So one to top and for each pop we deduct one from top. A [0] B [1] [2] [3]

What if the stack is full or what if the stack is empty? Solution: Before any push check if the stack is already filled or not. If it is filled then generate error message e.g Stack Overflow. Before pop check if stack is not already empty

Sample Program (Push Pop using Array)


1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2
sourcecodemania.com/stacks-in-cpp/

#nld<oi.> icuecnoh #nld <otemh icue isra.> #eielnt 1 dfn egh 0 ittp-; n o=1 itsaklnt] n tc[egh; vi mi( od an) { itc; n h d{ o cu < ed < ":ps" ot < nl < 1 uh; cu < ed < ":pp; ot < nl < 2 o" cu < ed < ":ei" ot < nl < 3 xt; cu < ed < "ne coc" ot < nl < etr hie; cn> c; i > h sic(h wthc) { cs 1 ae : ps(; uh) lsSak) ittc(; bek ra; cs 2 ae : cu < "aappd "< pp) ot < dt oe= < o(; lsSak) ittc(; bek ra; cs 3ei() ae :xt0; } }wie1; hl() gth) ec(; } vi ps(n dt) od uhit aa { i(o+=lnt) ftp1=egh { cu < "tc oefo\ Cno etrnwdt" ot < sak vrlwn ant ne e aa; rtr; eun } tp+ o+; saktp=aa tc[o]dt;
3/6

9/13/12 Stacks in C++ | C/c++ Programming Resources | Source Code Mania.COM

4 3 4 4 4 5 4 6 4 7 4 8 4 9 5 0 5 1 5 2 5 3 5 4 5 5 5 6 5 7 5 8 5 9 6 0 6 1 6 2 6 3

} itpp) n o( { ittmVr n epa; i(o=-)/ecnas mk iEpy) ftp=1/w a lo ae smt( { cu < "tc i udrlw(mt); ot < sak s nefo Epy" rtr(1; eun-) } tmVr=saktp; epa tc[o] tpo-; rtr tmVr eun epa; } vi lsSak) od ittc( { cu < ed < "h saki"< ed ; ot < nl < Te tc s < nl frititpi=;-) o(n =o;>0icu < saki < ed; ot < tc[] < nl }

Stacks in Problem Solving


Consider a mathematical expression that includes several sets of nested parenthesis, e.g
(x+( ( +) ) y a b)

W e want to ensure that parenthesis are nested correctly and the expression is valid. Validation 1. There is an equal number of right and left parentheses 2. Every right parenthesis is preceded by a matching left parenthesis Expressions
( B +)*C Vld A ai A +B Ivld ) nai (+] Ivld AB nai

Rules Each left parentheses is the opening scope Each right parenthesis is a closing scope Parentheses count = 0 at the end means that no scopes have been left open and left and right parentheses exactly match. parentheses count at any time should never become negative. IF negative then it means The expression is invalid.

Related Posts
The OpenGL Programming Guide Introduction to Recursion in C++ Functions with parameters in C++ Functions in C++ Repetition Control Statements in C++ C++ Expressions and Language Constructs Introduction to C++ Basics Mobile Developers Guide to the Parallel Universe Silverlight for Windows Phone Object-oriented Programming in C# for C and Java programmers
Tagged with: C/C++ language Data structures

Ads by Google

C++

Stack

C Source Code

If you enjoyed this article, please consider sharing it!

sourcecodemania.com/stacks-in-cpp/

4/6

9/13/12 Stacks in C++ | C/c++ Programming Resources | Source Code Mania.COM

Leave a Reply
Your email address will not be published. Name

Email

Website

Comment

You may use these HTML tags and attributes:


< he="tte"><brtte"><coy tte"><><lcqoect=" <ie <oe <e dttm=" a rf" il=" ab il=" arnm il=" b bokut ie"> ct> cd> dl aeie"> <m <>< ct=" <tie <tog e> i q ie"> srk> srn>

Type the tw o w ords:

Post Comment

LOOKING FOR S OMETHING?

V IS IT OUR FR IENDS !

AR CHIV ES

Use the form below to search the site:


Search

A few highly recommended friends...

All entries, chronologically... September 2012 August 2012

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

July 2012 June 2012 May 2012 February 2012 January 2012 December 2011 November 2011 October 2011 September 2011 August 2011

Please install and activate the "Twitter for WordPress" plugin to use this section.

sourcecodemania.com/stacks-in-cpp/

5/6

9/13/12 Stacks in C++ | C/c++ Programming Resources | Source Code Mania.COM

Source Code Mania.COM

PA G ES

THE LA TES T

M OR E

Copyright 2011 SourcecodeMania.COM

Forums Join Us Programming Lectures Search Results Tutorials

Navigation Bar Controller iPhone Development


Navigation controller is one of the most common parts of iPhone applications. As []

Thanks for dropping by! Feel free to join the discussion by leaving comments, and stay updated by subscribing to the RSS feed.

Powered By: ICT Innovations, Inc

sourcecodemania.com/stacks-in-cpp/

6/6

You might also like