EENG212 Algorithms and Data Structures

Slides:



Advertisements
Similar presentations
INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Advertisements

Computer Science 112 Fundamentals of Programming II Applications of Stacks.
Stacks Example: Stack of plates in cafeteria.
Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Topic 15 Implementing and Using Stacks
CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)
Infix to postfix conversion Process the tokens from a vector infixVect of tokens (strings) of an infix expression one by one When the token is an operand.
Infix, Postfix, Prefix.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
Topic 15 Implementing and Using Stacks
Class 4: Queues. cis 335 Fall 2001 Barry Cohen What is a queue? n A stack is an ordered sequence of items. n As in lists and stacks, each node contains.
More About Stacks: Stack Applications Dan Nguyen CS 146, Spring 2004 Professor Sin-Min Lee.
Objectives of these slides:
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
The Stack Alexander G. Chefranov CMPE-231 Spring 2012.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
EENG212 Algorithms and 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.
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.
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.
EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+
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.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
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.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
Basic Data Structures (Stacks). 2 Basic Data Structures Stacks Queues Lists.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
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.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A3 – Basic Data Structures (Stacks)
Review Use of Stack Introduction Stack in our life Stack Operations
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Revised based on textbook author’s notes.
Infix to postfix conversion
MEMORY REPRESENTATION OF STACKS
Stacks.
CSC 172 DATA STRUCTURES.
Data Structures – Week #3
Stack application: postponing data usage
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Stacks – Calculator Application
Visit for more Learning Resources
Stacks – Calculator Application
Stacks – Calculator Application
PART II STACK APPLICATIONS
More About Stacks: Stack Applications
Stack and Queues Stack implementation using Array
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Stacks – Calculator Application
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Topic 15 Implementing and Using Stacks
Jordi Cortadella and Jordi Petit Department of Computer Science
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Data Structures – Week #3
Infix to postfix conversion
More About Stacks: Stack Applications
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

EENG212 Algorithms and Data Structures Applications of STACKS EASTERN MEDITERRANEAN UNIVERSITY

Applications of STACKS Stacks can be used to reverse a sequence. For example, if a string ”Computers” is entered by the user the stack can be used to create and display the reverse string “sretupmoC” as follows. The program simply pushes all of the characters of the string into the stack. Then it pops and display until the stack is empty.

#include<stdio.h> #include<stdlib.h> #define STACKSIZE 50 typedef struct{ int count; int top; char items[STACKSIZE];/*stack can contain up to 50 characters*/ }STACK; void push(STACK *, char); char pop(STACK *);

int main() { int i; STACK s; char p, A[20]; s.top = -1; /*indicates that the stack is empty at the beginning*/ s.count=0; printf("Input the string please:\n"); gets(A); /* alternatively you can use scanf("%s",A); */ /*pushing the characters into the stack*/ for(i=0;A[i] != '\0';i++){ p = A[i]; push(&s,p); } printf("The string in reverse order is:"); /*popping and printing the string in reverse order*/ while(s.count >= 0){ p=pop(&s); printf("%c",p); return 0;

void push(STACK *Sptr, char ps) /*pushes ps into stack*/ { if(Sptr->top == STACKSIZE-1){ printf("Stack is full\n"); Return; /*exit from the function*/ } else { Sptr->top++; Sptr->count++; Sptr->items[Sptr->top]= ps;

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

PALINDROME EXAMPLE The strings where the reading from the reverse and forward directions give the same word are called a palindrome. For example, the string ”radar” is an example for palindrome. Among many other techniques stack can be used to determine if a string is a palindrome or not. This is achieved by pushing all the letters of a given word into stack and checking if the letters popped are the same as the letter of the string. The following program determines if a given string is a palindrome or not?

#include<stdio.h> #include<stdlib.h> #define STACKSIZE 25 typedef struct{ int count; int top; char items[STACKSIZE];/*stack can contain up to 20 characters*/ }STACK; void push(STACK *, int); char pop(STACK *);

int main() { int i, check=0, size; STACK s; char p, A[20]; s.count=0; s.top = -1; /*indicates that the stack is empty at the beginning*/ printf("Enter the string\n"); gets(A); /* alternatively you can use scanf("%s",A); */ /*pushing the characters of the string into the stack*/ for(i=0; A[i]!='\0';i++){ p = A[i]; push(&s,p); 3 } size = s.count; /*popping and checking if the letters are equal in rev. & forw.direction*/ for(i=0;i<=size-1;i++){ p=pop(&s); if(p != A[i]) check ++; if(check == 0) printf("\nThe string %s is a palindrome\n",A); else printf("\nThe string %s is NOT a palindrome\n",A); return 0;

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

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

INFIX, POSTFIX AND PREFIX NOTATIONS Infix, Postfix and Prefix notations are used in many calculators. The easiest way to implement the Postfix and Prefix operations is to use stack. Infix and prefix notations can be converted to postfix notation using stack. The reason why postfix notation is preferred is that you don’t need any parenthesis and there is no prescience problem.

POSTFIX In Postfix notation the expression is scanned from left to right. When a number is seen it is pushed onto the stack; when an operator is seen the operator is applied to the two numbers popped from the stack and the result is pushed back to the stack. Ex: 6 5 2 3 + 8 * + 3 + * is evaluated as follows:

C code of Postfix Calculations #include<stdio.h> #include<stdlib.h> #define STACKSIZE 20 typedef struct{ int top; int items[STACKSIZE]; /*stack can contain up to 20 integers*/ }STACK; void push(STACK *, int); int pop(STACK *); int calculate(char []);

int main() { int result; char E[50]; printf("Enter your Postfix expression(don't use space character):\n"); scanf("%s",E); result = calculate(E); printf("The result of the Postfix expression %s=%d\n",E,result); return 0; }

int calculate(char exp[]) { STACK s; s.top =-1;/*indicates that the stack is empty at the beginning*/ int i,num1,num2,value; for(i=0; exp[i]!='\0';i++){ if(exp[i] >='0' && exp[i] <='9') /*checks if exp[i] has a digit*/ push(&s,(int)(exp[i] -'0')); /*converts digit into integer*/ else{ num1=pop(&s); num2=pop(&s); switch(exp[i]){ case '+': value=num2+num1;break; case '-': value=num2-num1;break; case '*': value=num2*num1;break; case '/': value=num2/num1;break; default : printf("Illegal Operator\n"); exit(1); } push(&s,value); return pop(&s);

void push(STACK *Sptr, int ps) /*pushes ps into stack*/ { if(Sptr->top == STACKSIZE-1){ printf("Stack is full\n"); exit(1); /*exit from the function*/ } else { Sptr->top++; Sptr->items[Sptr->top]= ps; int pop(STACK *Sptr) int pp; if(Sptr->top == -1){ printf("Stack is empty\n"); pp = Sptr->items[Sptr->top]; Sptr->top--; return pp;