The program simply pushes all of the characters of the string into the stack. Then it pops and display until the stack is empty.

Similar documents
Prepared By:- Dinesh Sharma Asstt. Professor, CSE & IT Deptt. ITM Gurgaon

Data Structures Week #3. Stacks

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Stacks. Revised based on textbook author s notes.

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK

Formal Languages and Automata Theory, SS Project (due Week 14)

UNIT 2: STACK & RECURSION Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.

Outline. Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications. ADT for stacks

Stacks. CONTENTS 3.1 Introduction 1. Stack as an abstract data type 2. Representation of a Stack as an array 3.2Applications of Stack.

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Write a C program to add two Complex numbers using functions illustrating-

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Lecture Data Structure Stack

PA3 Design Specification

STACKS AND QUEUES. Problem Solving with Computers-II

Infix to Postfix Conversion

Stacks Calculator Application. Alexandra Stefan

BBM 201 DATA STRUCTURES


BBM 201 DATA STRUCTURES

Stack Abstract Data Type

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering

Types of Data Structures

Top of the Stack. Stack ADT

Stack & Queue on Self-Referencing Structures

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Top of the Stack. Stack ADT

Application of Stack (Backtracking)

CS 171: Introduction to Computer Science II. Stacks. Li Xiong

Stack and Its Implementation

printf("this program adds the value 10 to a given integer number.\n\n");

Outline. Stacks. 1 Chapter 5: Stacks and Queues. favicon. CSI33 Data Structures

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

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

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

Stacks and their Applications

Stack & Queue on Self-Referencing Structures

STACKS 3.1 INTRODUCTION 3.2 DEFINITION

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

EEE Algorithms & Data Structures Final Exam Instructor: Dr. Hasan Demirel

Computer Programming for Engineering Applica4ons. Intro to Programming 10/17/13 ECE 175. The Stack Data Structure. Examples from Real Life

Stacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal.

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Associate Professor Dr. Raed Ibraheem Hamed

NCS 301 DATA STRUCTURE USING C

Data Structure - Stack and Queue-

1. Algorithm. Data Structures & Algorithms. How many times you weight? Algo 2 : Half Cutting. How many times you weight? Algo.1 :Two coins at a time

BBM 201 DATA STRUCTURES

15. Stacks and Queues

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

The Stack and Queue Types

Lecture No.04. Data Structures

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Stacks and Queues. CSE Data Structures April 12, 2002

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

Chapter 9 STACK, QUEUE

Computer Science BS Degree - Data Structures (I2206) Abstract Data Types (ADT)

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

Initialisation of an array is the process of assigning initial values. Typically declaration and initialisation are combined.

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

CSCE 210 Dr. Amr Goneid Exercises (1): Stacks, Queues. Stacks

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

#06 More Structures LIFO FIFO. Contents. Queue vs. Stack 3. Stack Operations. Pop. Push. Stack Queue Hash table

Stacks. Chapter 3. Exercises. 1. See Figure 3-1. FIGURE 3-1 Solution to Exercise 1

Postfix (and prefix) notation

Review: Expressions, Variables, Loops, and more.

Linear Data Structure

March 13/2003 Jayakanth Srinivasan,

Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

BBM 201 DATA STRUCTURES

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

Lab 7 1 Due Thu., 6 Apr. 2017

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

Unit-2. Stacks: Introduction-Definition-Representation of Stack-Operations on Stacks- Applications of Stacks.

Data Structure. Chapter 3 Stacks and Queues. Department of Communication Engineering National Central University Jhongli, Taiwan.

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

COMPUTER ARCHITECTURE AND PARALEL PROCESSING STUDY NOTES

Overview of today s lecture. Quick recap of previous C lectures. Introduction to C programming, lecture 2. Abstract data type - Stack example

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3

Solution for Data Structure

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

1. Stack Implementation Using 1D Array

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

Stacks and Queues. Stack - Abstract Data Type. Stack Applications. Stack - Abstract Data Type - C Interface

Stacks Fall 2018 Margaret Reid-Miller

ABSTRACT DATA TYPES (ADTS) COMP1927 Computing 2 16x1 Sedgewick Chapter 4

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 16 EXAMINATION Model Answer Subject Code:

Fundamentals of Programming. Lecture 14 Hamed Rasifard

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

Chapter 19: Program Design. Chapter 19. Program Design. Copyright 2008 W. W. Norton & Company. All rights reserved.

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

Lecture 12 ADTs and Stacks

Transcription:

EENG212 Algorithms & Data Structures Fall 0/07 Lecture Notes # Outline Stacks Application of Stacks Reversing a String Palindrome Example Infix, Postfix and Prefix Notations APPLICATION 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 0 typedef struct int count; int ; char items[stacksize];/*stack can contain up to 0 characters*/ STACK; void push(stack *, char); char pop(stack *); int main() int i; STACK s; char p, A[20]; s. = -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-> == STACKSIZE-1) printf("stack is full\n"); 1

else exit(1); /*exit from the function*/ Sptr->++; Sptr->count++; Sptr->items[Sptr->]= ps; char pop(stack *Sptr) char pp; if(sptr-> == -1) printf("\nstack is empty\n"); else pp = Sptr->items[Sptr->]; Sptr->--; 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 sack 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 2 typedef struct int count; int ; 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. = -1; 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); /*indicates that the stack is empty at the beginning*/ 2

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-> == STACKSIZE-1) else char pop(stack *Sptr) char pp; if(sptr-> == -1) printf("stack is full\n"); Sptr->++; Sptr->count++; Sptr->items[Sptr->]= ps; printf("stack is empty\n"); else pp = Sptr->items[Sptr->]; Sptr->--; Sptr->count--; return pp; 3

INFIX, POSTFIX AND PREFIX NOTATIONS Infix Postfix Prefix A+B AB+ +AB A+B-C AB+C- -+ABC (A+B)*(C-D) AB+CD-* *+AB-CD 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. 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: 2 3 + 8 * + 3 + * is evaluated as follows: - The first 4 numbers are pushed onto the stack : 3 2 - Next + is read, so 3 and 2 are popped and their sum (3+2=) is pushed onto the stack: - Next 8 is pushed onto the stack: - Next * is read, so 8 and are popped and their product (8*=40) is pushed onto the stack: 8 40 - Next + is read, so 40 and are popped and their product (40+=4) is pushed onto the stack: - Next 3 is pushed onto the stack: 3 4 40 - Next + is read, so 3 and 4 are popped and their sum (3+4=48) is pushed onto the stack: 48 4

- Finally * is read, so 48 and are popped and their product (+48=288) is pushed onto the stack: ` 288 Note that 2 3 + 8 * + 3 + * = 288 where the corresponding infix notation is:((3+2)*8++3)* The following program is an example of the evaluation of a given Postfix expression. Note that the user may enter any postfix expression as a string. The program reads the characters in the string one by one and if the character is a digit (number) than it converts the character into an integer otherwise uses the related operation. #include<stdio.h> #include<stdlib.h> #define STACKSIZE 20 typedef struct int ; 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[0]; 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. =-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-> == STACKSIZE-1) else int pop(stack *Sptr) int pp; if(sptr-> == -1) printf("stack is full\n"); Sptr->++; Sptr->items[Sptr->]= ps; printf("stack is empty\n"); else pp = Sptr->items[Sptr->]; Sptr->--; return pp;