EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stacks using Linked Lists. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
1 Abstract Data Types. Objectives To appreciate the concept and purpose of abstract data types, or ADTs To understand both the abstract behavior and the.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
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.
Data Structures & Algorithms
CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
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.
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.
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.
Stacks.
Stacks CS 308 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof.
Ali Abdul Karem Habib Kufa University / mathematics & Science Of Computer.
The Stack Alexander G. Chefranov CMPE-231 Spring 2012.
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
UNIT - I Linear structures. What is an abstract data type? A data type consists of a collection of values together with a set of basic operations on these.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
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.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
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,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Infix to postfix conversion Scan the Infix expression left to right If the character x is an operand  Output the character into the Postfix Expression.
EENG212 Algorithms and Data Structures
Data Structures – Week #3 Stacks. 9.Mart.2012Borahan Tümer, Ph.D.2 Outline Stacks Operations on Stacks Array Implementation of Stacks Linked List Implementation.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
Data Structures & Algorithms
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Lab 6 Stack ADT. OVERVIEW The stack is one example of a constrained linear data structure. In a stack, the elements are ordered from most recently added.
Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Basic Data Structures (Stacks). 2 Basic Data Structures Stacks Queues Lists.
Stack Data Structure By Marwa M. A. Elfattah. Stack - What A stack is one of the most important non- primitive linear data structure in computer science.
STACK Data Structure
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Stacks This presentation shows – how to implement the stack – how it can be used in real applications.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A3 – Basic Data Structures (Stacks)
Chapter 12 – Data Structures
Stacks and Queues Chapter 4.
Stacks.
Stack and Queue APURBO DATTA.
Data Structures – Week #3
CSCE 3110 Data Structures & Algorithm Analysis
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
ECE 103 Engineering Programming Chapter 62 Stack Implementation
Stacks CS-240 Dick Steflik.
Stacks LIFO C C B B B B A A A A A Push (A) Push (B) Push (C) Pop Pop.
Stack.
Data Structures – Week #3
Infix to postfix conversion
CSCS-200 Data Structure and Algorithms
Abstract Data Types Stacks CSCI 240
Presentation transcript:

EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures

Stacks Outline Stacks Definition Basic Stack Operations Array Implementation of Stacks

What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the top of the stack (the most recently added items are at the top of the stack). The last element to be added is the first to be removed (LIFO: Last In, First Out).

BASIC STACK OPERATIONS Initialize the Stack. Pop an item off the top of the stack (delete an item) Push an item onto the top of the stack (insert an item) Is the Stack empty? Is the Stack full? Clear the Stack Determine Stack Size

Array Implementation of the Stacks The stacks can be implemented by the use of arrays and linked lists. One way to implement the stack is to have a data structure where a variable called top keeps the location of the elements in the stack (array) An array is used to store the elements in the stack

Stack Definition struct STACK{ int count; /* keeps the number of elements in the stack */ int top; /* indicates the location of the top of the stack*/ int items[STACKSIZE]; /*array to store the stack elements*/ }

Stacks

Stack Initialisation initialize the stack by assigning -1 to the top pointer to indicate that the array based stack is empty (initialized) as follows: You can write following lines in the main program: : STACK s; s.top = -1; :

Stack Initialisation Alternatively you can use the following function: void StackInitialize(STACK *Sptr) { Sptr->top=-1; }

Push Operation Push an item onto the top of the stack (insert an item)

Void push (Stack *, type newItem) Function: Adds newItem to the top of the stack. Preconditions: Stack has been initialized and is not full. Postconditions: newItem is at the top of the stack.

void push (STACK *, type newItem) void push(STACK *Sptr, int ps) /*pushes ps into stack*/ { if(Sptr->top == STACKSIZE-1){ printf("Stack is full\n"); return; /*return back to main function*/ } else { Sptr->top++; Sptr->items[Sptr->top]= ps; Sptr->count++; }

Pop operation Pop an item off the top of the stack (delete an item)

type pop (STACK *) Function: Removes topItem from stack and returns with topItem Preconditions: Stack has been initialized and is not empty. Postconditions: Top element has been removed from stack and the function returns with the top element.

Type pop(STACK *Sptr) int pop(STACK *Sptr) { int pp; if(Sptr->top == -1){ printf("Stack is empty\n"); return -1; /*exit from the function*/ } else { pp = Sptr->items[Sptr->top]; Sptr->top--; Sptr->count--; return pp; }

void pop(STACK *Sptr, int *pptr) { if(Sptr->top == -1){ printf("Stack is empty\n"); return;/*return back*/ } else { *pptr = Sptr->items[Sptr->top]; Sptr->top--; Sptr->count--; }

EXAMPLE The following program implements stack of size 10 using array representation. The 10 elements entered by the user are pushed into the stack and then the elements are popped back and displayed.

#include #define STACKSIZE 10 typedef struct{ int count; int top; int items[STACKSIZE]; /*stack can contain up to 10 integers*/ }STACK; void push(STACK *, int); int pop(STACK *); int main() { int p, i; STACK s; s.top = -1; /*indicates that the stack is empty at the beginning*/ s.count = 0; /* 0 items are in the stack*/

/*reading and pushing items into the stack*/ printf("Enter the %d stack items\n",STACKSIZE); for(i=0;i<= STACKSIZE-1;i++){ scanf("%d",&p); push(&s,p); } /*popping and printing the items in the stack*/ printf("\n\nStack contains the following items\n"); for(i=0;i<= STACKSIZE-1;i++){ p=pop(&s); printf("%d\t",p); } return 0; }

void push(STACK *Sptr, int ps) /*pushes ps into stack*/ { if(Sptr->top == STACKSIZE-1){ printf("Stack is full\n"); printf("There are %d items in the stack\n", Sptr- >count); return; /*return back to main function*/ } else { Sptr->top++; Sptr->items[Sptr->top]= ps; Sptr->count++; }

int pop(STACK *Sptr) { int pp; if(Sptr->top == -1){ printf("Stack is empty\n"); return -1 /*exit from the function*/ } else { pp = Sptr->items[Sptr->top]; Sptr->top--; Sptr->count--; return pp; }