Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks Example: Stack of plates in cafeteria.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
Data Structures Queue Namiq Sultan 1. Queue A queue is an ordered collection of items into which items may be added at one end (rear) and from which items.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
 Balancing Symbols 3. Applications
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.
CMPT 225 Stacks.
Topic 15 Implementing and Using Stacks
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Topic 15 Implementing and Using Stacks
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Objectives of these slides:
The Stack Alexander G. Chefranov CMPE-231 Spring 2012.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
Stack Data Structure By : Imam M Shofi. What is stack? A stack is a limited version of an array. A stack is a limited version of an array. New elements,
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Chapter 7 Stacks Dr. Youssef Harrath
Data Structures and Algorithms
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Data Structures & Algorithms
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
CHP-3 STACKS.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2a. Simple Containers: The Stack.
Stacks.
Stacks Stack: restricted variant of list
Stacks.
Algorithms and Data Structures
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
Mark Allen Weiss, Addison Wesley
Stack and Queues Stack implementation using Array
Stack and Queues Stack implementation using Array
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
UNIT-I Topics to be covere d 1.Introduction to data structures.
Stacks and Queues 1.
Abstract Data Type Abstract Data Type as a design tool
Stacks Data structure Elements added, removed from one end only
Stack.
Data Structures and Algorithms 2/2561
Presentation transcript:

Data Structures Stack Namiq Sultan 1

Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations on these structures. Stack Queue Linked list Trees 2

Stack A stack is an ordered collection of items inot which new items may be added and from which items may be deleted at one end, called the top of the stack. Stack operations push pop This stack is called last-in first-out stack LIFO. pushpop 3

Representing stacks const int SIZE = 8; struct stack{ int item[SIZE}; int top; }; stack s; s.top = -1; // initialization Stack s s.item[0] s.item[7] s.top 4

Push and pop algorithms Push x if stack is full then overflow error else add x onto a stack end Pop if stack is empty then underflow error else remove item from a stack end Stack 5

Programming push operation void push(stack &st, int x) {if(st.top == SIZE-1) exit(1); else st.items[++st.top] = x; } Function call push(s, 4); push(s, 33); Stack ? ? ? ? ? ?

Programming pop operation int pop(stack &st) { if(st.top == -1) exit(1); else return st.items[st.top--]; } Function call y = pop(s); n = pop(s); Stack

// stack implementation using array #include using namespace std; const SIZE = 10; // Length of the stack struct stack{ int items[SIZE]; int top; }; void push(stack &, int); //prototype for push int pop(stack &); //prototype for pop void main () { stack stk; int item, value; int s; stk.top = -1; // intializing top to -1 Program 3.1 8

do{ cout >>>>"; cin >> s; //taking input switch(s){ case 1: //if PUSH cout<<"\n Enter item to push : "; cin>>item; push(stk, item); break; case 2: //if POP value = pop(stk); cout << value <<‘\n’; } }while (s != 0 ); } 9

void push(stack &st, int x) { if (st.top == SIZE-1){// if overflow cout << “Stack is full”; } st.items[++st.top]=x; //insert the specified value into the stack array } int pop(stack &st) { if (st.top == -1) {// if empty cout<<“Stack is empty”; } return st.items[st.top--]; //return the popped value } 10

// stack implementation using array and object-oriented programming #include using namespace std; const MAX = 100; class STACK{ int items[MAX]; int top; public: STACK() //constructor initializes top to -1 { top= -1; } void push(); void pop(); void traverse(); }; Program

void STACK::push() // add an element to the stack { int item; // if STACK is full then overflow if (top == MAX-1) cout<<"\nThe Stack Is Full"; else { cout<<"\nEnter The Element = "; cin>>item; items[++top]=item; } 12

void STACK::pop() //delete an element from the stack { int item; if (top == -1) cout<<"\nThe Stack Is Empty"; else { item=items[top--]; cout<<"\nThe Deleted Element Is = "<<item; } 13

//This function to print all the existing elements in the stack void STACK::traverse() { int i; if (top == -1) cout<<"\nThe Stack is Empty"; else { cout<<"\n\nThe Elements In The Stack are..."; for(i=top; i>=0; i--) cout<<"\n "<<items[i]; } 14

void main() { int choice; char ch; STACK ps; do{ //A menu for the stack operations cout<<"\n1. PUSH"; cout<<"\n2. POP"; cout<<"\n3. TRAVERSE"; cout<<"\nEnter Your Choice = "; cin>>choice; switch(choice){ case 1:ps.push(); break; 15

case 2: ps.pop(); break; case 3: ps.traverse(); break; default: cout<<"\nYou Entered Wrong Choice" ; } cout<< "\n\nPress (Y/y) To Continue = "; cin>> ch; }while(ch == 'Y' || ch == 'y'); } 16

Stacks, cont. More operations, general to most list data structures: –Create - Creates & initializes a new stack –Destroy - Destroys a stack, freeing up storage –Full - Function returning TRUE if stack is full –Empty - Function returning TRUE if stack has no elements –Clear - Removes all elements from stack –Size - Function returning number of elements in the stack 17

APPLICATIONS OF STACKS Stack is internally used by compiler when we implement (or execute) any recursive function. Stack is also used to evaluate a mathematical expression and to check the parentheses in an expression. 18

Evaluating Postfix Expression opndStk = the empty stack // scan the input string reading one element at a time into symb While(not end of input){ symb = next input character if(symb is an operand) push(opndStk, symb) else{ // symb is an operator opnd2 = pop(opndStk) opnd1 = pop(opndStk) value = result of applying symb to opnd1 and opnd2 push(opndStk, value) } // end else }// end while Return (pop(opndStk)) 19

Example: Postfix Expression Infix to postfix conversion: a+b*c-d  a+(b*c)-d  a+(bc*)-d  (abc*+)-d  ab*+d-  The only rules to remember during the conversion are that operations with highest precedence are converted first and that after a portion of the expression has been converted to postfix it is to be treated as a single operand.  Postfix expression requires no paranthesis 20

Example: Evaluate 452*+8-32^+ opndStkvalueopnd2opnd1symb 4 4,5 4,5,2 4, ,8 6 6,3 6,3,2 6, *+8-32^+452*+8-32^+ 21

Example: Brace matching Another use of stack is in compiler design, the need to match opened braces with closed braces. braceStk = the empty stack While(not end of input){ symb = next input character if symb is ‘(‘ push(braceStk, symb) if symb is ‘)’ if empty(braceStk) then error else x = pop(braceStk) }//end while If(!empty(braceStk)) error Else output(“Brace match”) 22