0% found this document useful (0 votes)
77 views

Programming in C++ - Module 05

This document discusses stacks and their applications in C and C++. It begins by outlining the objectives and topics to be covered, which include understanding implementation of stacks in C and C++, and using stacks to solve problems like reversing a string and evaluating postfix expressions. Programming examples are provided to demonstrate reversing a string using stacks in C and C++. Another example shows evaluating a postfix expression using a stack in C. The key differences between implementing stacks in C versus C++ using the standard library are also highlighted.

Uploaded by

Pandu Patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Programming in C++ - Module 05

This document discusses stacks and their applications in C and C++. It begins by outlining the objectives and topics to be covered, which include understanding implementation of stacks in C and C++, and using stacks to solve problems like reversing a string and evaluating postfix expressions. Programming examples are provided to demonstrate reversing a string using stacks in C and C++. Another example shows evaluating a postfix expression using a stack in C. The key differences between implementing stacks in C versus C++ using the standard library are also highlighted.

Uploaded by

Pandu Patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Module 05

Partha Pratim
Das Module 05: Programming in C++
Objectives & Stack and its Applications
Outline

Stack in C
Reverse a String
Eval Postfix

Stack in C++
Partha Pratim Das
Reverse a String
Eval Postfix
Department of Computer Science and Engineering
Summary Indian Institute of Technology, Kharagpur
[email protected]

Tanwi Mallick
Srijoni Majumdar
Himadri B G S Bhuyan

NPTEL MOOCs Programming in C++ Partha Pratim Das 1


Module Objectives

Module 05

Partha Pratim Understanding implementation and use of stack in C


Das
Understanding stack in C++ standard library and its use
Objectives &
Outline

Stack in C
Reverse a String
Eval Postfix

Stack in C++
Reverse a String
Eval Postfix

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 2


Module Outline

Module 05

Partha Pratim Stack in C


Das
Reverse a String
Objectives & Evaluate a Postfix Expression
Outline
Stack in C++
Stack in C
Reverse a String Reverse a String
Eval Postfix
Evaluate a Postfix Expression
Stack in C++
Reverse a String
Eval Postfix

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 3


Understanding Stack in C

Module 05 Stack is a LIFO (last-In-First-Out) container that can


Partha Pratim
Das
maintain a collection of arbitrary number of data items –
all of the same type
Objectives &
Outline To create a stack in C we need to:
Stack in C Decide on the data type of the elements
Reverse a String
Eval Postfix
Define a structure (container) (with maximum size) for
Stack in C++ stack and declare a top variable in the structure
Reverse a String
Eval Postfix
Write separate functions for push, pop, top, and isempty
Summary
using the declared structure
Note:
Change of the data type of elements, implies
re-implementation for all the stack codes
Change in the structure needs changes in all functions
Unlike sin, sqrt etc. function from C standard library, we
do not have a ready-made stack that we can use
NPTEL MOOCs Programming in C++ Partha Pratim Das 4
Common C programs using stack

Module 05
Some common C programs that use stack:
Partha Pratim
Das Reversing a string
Objectives & Input: ABCDE
Outline Output: EDCBA
Stack in C
Reverse a String
Evaluation of postfix expression
Eval Postfix
Input: 1 2 3 * + 4 - (for 1 + 2 * 3 - 4)
Stack in C++
Reverse a String
Output: 3
Eval Postfix Stack states:
Summary
1 2 3 6 7 4 3
1 2 1 7
1

Identification of palindromes (w/ and w/o center-marker)


Conversion of an infix expression to postfix
Depth-first Search (DFS)

NPTEL MOOCs Programming in C++ Partha Pratim Das 5


Program 05.01: Reversing a string

Module 05 // FileName: Reverse_String.c void main() {


stack s;
Partha Pratim #include <stdio.h> s.top = -1;
Das
typedef struct stack { char ch, str[10] = "ABCDE";
char data [100];
Objectives & int top; int i, len = sizeof(str);
Outline } stack;
for(i = 0; i < len; i++) {
Stack in C
int empty (stack *p) { push(&s, str[i]);
Reverse a String
return (p->top == -1); }
Eval Postfix
}
Stack in C++ printf ("Reversed String: ");
Reverse a String int top (stack *p) {
Eval Postfix return p -> data [p->top]; while (!empty(&s)){
} printf("%c ", top(&s));
Summary pop(&s);
void push (stack *p, char x) { }
p -> data [++(p -> top)] = x; }
}

void pop (stack *p) {


if (!empty(p)) {
(p->top) = (p->top) -1;
}
}

Reversed String: EDCBA

NPTEL MOOCs Programming in C++ Partha Pratim Das 6


Program 05.02: Postfix Expression Evaluation

Module 05 // FileName: PostFix_Evaluation.c void main() {


stack s;
Partha Pratim #include<stdio.h> s.top = -1;
Das
typedef struct stack { // Postfix expression: 1 2 3 * + 4 -
char data [100]; char postfix[] = {’1’,’2’,’3’,’*’,’+’,’4’,’-’};
Objectives & int top;
Outline } stack; int i, op1, op2;
Stack in C
int empty (stack *p) { for(i = 0; i < 7; i++) {
Reverse a String
return (p->top == -1); char ch = postfix[i];
Eval Postfix
} if (isdigit(ch)) push(&s, ch-’0’);
Stack in C++ else {
Reverse a String int top (stack *p) { op2 = top(&s); pop(&s);
Eval Postfix return p -> data [p->top]; op1 = top(&s); pop(&s);
} switch (ch) {
Summary case ’+’:push(&s, op1 + op2);break;
void push (stack *p, char x) { case ’-’:push(&s, op1 - op2);break;
p -> data [++(p -> top)] = x; case ’*’:push(&s, op1 * op2);break;
} case ’/’:push(&s, op1 / op2);break;
}
void pop (stack *p) { }
if (!empty(p)) { }
(p->top) = (p->top) -1; printf("Evaluation %d\n", top(&s));
} }
}

Evaluation 3

NPTEL MOOCs Programming in C++ Partha Pratim Das 7


Understanding Stack in C++

Module 05

Partha Pratim C++ standard library provide a ready-made stack for any
Das
type of elements
Objectives &
Outline
To create a stack in C++ we need to:
Stack in C Include the stack header
Reverse a String Instantiate a stack with proper element type (like char)
Eval Postfix

Stack in C++
Use the functions of the stack objects for stack operations
Reverse a String
Eval Postfix

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 8


Program 05.03: Reverse a String in C++

Module 05 // FileName: Reverse_String_c++.cpp // FileName: Reverse_String.c


#include<iostream>
Partha Pratim #include<string.h>
Das #include<stack>
using namespace std;
Objectives & int main() { int main() {
Outline char str[10]= "ABCDE"; char str[10] = "ABCDE";
stack<char> s; stack s; s.top = -1;
Stack in C
int i; int i;
Reverse a String
Eval Postfix
for(i = 0; i < strlen(str); i++) for(i = 0; i < strlen(str); i++)
Stack in C++ s.push(str[i]); push(&s, str[i]);
Reverse a String
Eval Postfix cout << "Reversed String: "; printf ("Reversed String: ");

Summary while (!s.empty()) { while (!empty(&s)){


cout << s.top(); printf("%c ", top(&s));
s.pop(); pop(&s);
} }

return 0; return 0;
} }

• No codes for creating stack • Lot of code for creating stack


• No initialization • top to be initialized
• Clean interface for stack functions • Cluttered interface for stack functions
• Available in library – well-tested • Implemented by user – error-prone

NPTEL MOOCs Programming in C++ Partha Pratim Das 9


Program 05.04: Postfix Evaluation in C++

Module 05 // FileName:Postfix_Evaluation_c++.cpp
#include <iostream>
Partha Pratim #include <stack>
Das using namespace std;

int main() {
Objectives & // Postfix expression: 1 2 3 * + 4 -
Outline char postfix[] = {’1’,’2’,’3’,’*’,’+’,’4’,’-’}, ch;
stack<int> s;
Stack in C
Reverse a String
for(int i = 0; i < 7; i++) {
Eval Postfix
ch = postfix[i];
Stack in C++ if (isdigit(ch)) { s.push(ch-’0’); }
Reverse a String else {
Eval Postfix int op1 = s.top(); s.pop();
int op2 = s.top(); s.pop();
Summary switch(ch) {
case ’*’: s.push(op2 * op1); break;
case ’/’: s.push(op2 / op1); break;
case ’+’: s.push(op2 + op1); break;
case ’-’: s.push(op2 - op1); break;
}
}
}
cout << "\nEvaluation " << s.top();
return 0;
}

NPTEL MOOCs Programming in C++ Partha Pratim Das 10


Module Summary

Module 05

Partha Pratim C++ standard library provides ready-made stack. It works


Das
like a data type
Objectives &
Outline Any type of element can be used for C++ stack
Stack in C Similar containers as available in C++ standard library
Reverse a String
Eval Postfix include:
Stack in C++ queue
Reverse a String
Eval Postfix deque
Summary list
map
set
... and more

NPTEL MOOCs Programming in C++ Partha Pratim Das 11


Instructor and TAs

Module 05

Partha Pratim
Das Name Mail Mobile
Objectives &
Partha Pratim Das, Instructor [email protected] 9830030880
Outline Tanwi Mallick, TA [email protected] 9674277774
Stack in C
Srijoni Majumdar, TA [email protected] 9674474267
Reverse a String Himadri B G S Bhuyan, TA [email protected] 9438911655
Eval Postfix

Stack in C++
Reverse a String
Eval Postfix

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 12

You might also like