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

Module 05

Uploaded by

yejataj548
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Module 05

Uploaded by

yejataj548
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Module M05

L
Partha Pratim
Das
Programming in Modern C++

E
Objectives &
Outline
Module M05: Stack and Common Data Structures / Containers

T
Stack in C
Common
Applications

P
Reverse a String
Eval Postfix
Partha Pratim Das

N
Stack in C++
Reverse a String
Eval Postfix
Department of Computer Science and Engineering
Data Structures / Indian Institute of Technology, Kharagpur
Containers
Containers in C++
[email protected]
Module Summary

All url’s in this module have been accessed in September, 2021 and found to be functional

Programming in Modern C++ Partha Pratim Das M05.1


Module Recap

Module M05

• Flexibility of defining customised sort algorithms to be passed as parameter to sort and

L
Partha Pratim
Das
search functions defined in the algorithm library

E
Objectives &
Outline • Predefined optimised versions of these sort and search functions can also be used

T
Stack in C
Common
• There are a number of useful functions like rotate, replace, merge, swap, remove
Applications
etc. in algorithm library

P
Reverse a String
Eval Postfix

N
Stack in C++
Reverse a String
Eval Postfix

Data Structures /
Containers
Containers in C++

Module Summary

Programming in Modern C++ Partha Pratim Das M05.2


Module Objectives

Module M05

• Understanding implementation and use of stack in C

L
Partha Pratim
Das
• Understanding stack in C++ standard library and its use

E
Objectives &
Outline • Understanding common containers in C++ standard library

T
Stack in C
Common
Applications

P
Reverse a String
Eval Postfix

N
Stack in C++
Reverse a String
Eval Postfix

Data Structures /
Containers
Containers in C++

Module Summary

Programming in Modern C++ Partha Pratim Das M05.3


Module Outline

Module M05

L
Partha Pratim
Das 1 Stack in C
Common Applications of Stack in C

E
Objectives &
Outline Reverse a String

T
Stack in C
Common
Evaluate Postfix Expressions
Applications

P
Reverse a String
Eval Postfix 2 Stack in C++

N
Stack in C++
Reverse a String
Reverse a String
Eval Postfix Evaluate Postfix Expressions
Data Structures /
Containers
Containers in C++ 3 Data Structures / Containers in C++
Module Summary
Containers in C++

4 Module Summary

Programming in Modern C++ Partha Pratim Das M05.4


Stack in C

Module M05

L
Partha Pratim
Das

E
Objectives &
Outline

T
Stack in C
Common
Applications

P
Reverse a String
Eval Postfix

N
Stack in C++
Reverse a String
Eval Postfix

Data Structures /
Containers
Containers in C++

Module Summary Stack in C

Programming in Modern C++ Partha Pratim Das M05.5


Stack in C

Module M05

• Stack is a LIFO (last-In-First-Out) container that can maintain a collection of arbitrary

L
Partha Pratim
Das
number of data items – all of the same type

E
Objectives &
Outline • To create a stack in C we need to:

T
Stack in C ◦ Decide on the data type of the elements
Common
Applications ◦ Define a structure (container) (with maximum size) for stack and declare a top

P
Reverse a String
Eval Postfix variable in the structure
◦ Write separate functions for push, pop, top, and isempty using the declared

N
Stack in C++
Reverse a String
Eval Postfix
structure
Data Structures /
Containers
• Note:
Containers in C++
◦ Change of the data type of elements, implies re-implementation for all the stack
Module Summary
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
Programming in Modern C++ Partha Pratim Das M05.6
Common C programs using stack

Module M05
Some common C programs that use stack:

L
Partha Pratim
Das
• Reversing a string

E
Objectives &
Outline ◦ Input: ABCDE
◦ Output: EDCBA

T
Stack in C
Common
Applications • Evaluation of postfix expression

P
Reverse a String
Eval Postfix ◦ Input: 1 2 3 * + 4 - (for 1 + 2 * 3 - 4)

N
Stack in C++ ◦ Output: 3
Reverse a String
Eval Postfix Stack states:
Data Structures / 1 2 3 6 7 4 3
Containers 1 2 1 7
Containers in C++ 1
Module Summary
• Identification of palindromes (w/ and w/o center-marker)
• Conversion of an infix expression to postfix
• Depth-first Search (DFS)

Programming in Modern C++ Partha Pratim Das M05.7


Program 05.01: Reversing a string

Module M05

#include <stdio.h> int main() {

L
Partha Pratim
Das stack s;
typedef struct stack { s.top = -1;

E
Objectives & char data [100];
Outline
int top; char ch, str[10] = "ABCDE";

T
Stack in C } stack;
Common int i, len = sizeof(str);
Applications
int empty(stack *p) { return (p->top == -1); }

P
Reverse a String
Eval Postfix
for(i = 0; i < len; i++)
int top(stack *p) { return p -> data [p->top]; } push(&s, str[i]);

N
Stack in C++
Reverse a String
void push(stack *p, char x) { printf("Reversed String: ");
Eval Postfix
p -> data [++(p -> top)] = x;
Data Structures / } while (!empty(&s)) {
Containers
printf("%c ", top(&s));
Containers in C++
void pop(stack *p) { pop(&s);
Module Summary if (!empty(p)) (p->top) = (p->top) -1; }
} }

Reversed String: EDCBA

Programming in Modern C++ Partha Pratim Das M05.8


Program 05.02: Postfix Expression Evaluation

Module M05
#include <stdio.h> void main() { stack s; s.top = -1;

L
Partha Pratim
Das typedef struct stack { // Postfix expression: 1 2 3 * + 4 -
char data [100]; char postfix[] = {’1’,’2’,’3’,’*’,’+’,’4’,’-’};

E
Objectives &
Outline
int top;
} stack; for(int i = 0; i < 7; i++) { char ch = postfix[i];

T
Stack in C if (isdigit(ch)) push(&s, ch-’0’);
Common
Applications
int empty(stack *p) { else {

P
Reverse a String return (p->top == -1); int op2 = top(&s); pop(&s);
Eval Postfix } int op1 = top(&s); pop(&s);
int top(stack *p) { switch (ch) {

N
Stack in C++
return p -> data [p->top]; case ’+’: push(&s, op1 + op2); break;
Reverse a String
Eval Postfix
} case ’-’: push(&s, op1 - op2); break;
void push(stack *p, char x) { case ’*’: push(&s, op1 * op2); break;
Data Structures / case ’/’: push(&s, op1 / op2); break;
Containers
p -> data [++(p -> top)] = x;
Containers in C++
} }
void pop(stack *p) { }
Module Summary }
if (!empty(p)) (p->top) = (p->top) -1;
} printf("Evaluation %d\n", top(&s));
}

Evaluation 3

Programming in Modern C++ Partha Pratim Das M05.9


Stack in C++

Module M05

L
Partha Pratim
Das

E
Objectives &
Outline

T
Stack in C
Common
Applications

P
Reverse a String
Eval Postfix

N
Stack in C++
Reverse a String
Eval Postfix

Data Structures /
Containers
Containers in C++

Module Summary Stack in C++

Programming in Modern C++ Partha Pratim Das M05.10


Understanding Stack in C++

Module M05

• C++ standard library provide a ready-made stack for any type of elements

L
Partha Pratim
Das
• To create a stack in C++ we need to:

E
Objectives &
Outline ◦ Include the stack header

T
Stack in C ◦ Instantiate a stack with proper element type (like char)
Common
Applications ◦ Use the functions of the stack objects for stack operations

P
Reverse a String
Eval Postfix

N
Stack in C++
Reverse a String
Eval Postfix

Data Structures /
Containers
Containers in C++

Module Summary

Programming in Modern C++ Partha Pratim Das M05.11


Program 05.03: Reverse a String in C++

Module M05

#include <stdio.h> #include <iostream>

L
Partha Pratim
Das #include <string.h> #include <cstring>
#include "stack.h" // User defined codes #include <stack> // Library codes

E
Objectives & using namespace std;
Outline

T
Stack in C int main() { char str[10] = "ABCDE"; int main() { char str[10]= "ABCDE";
Common stack s; s.top = -1; // stack struct stack<char> s; // stack class
Applications

P
Reverse a String
Eval Postfix
for(int i = 0; i < strlen(str); i++) for(int i = 0; i < strlen(str); i++)
push(&s, str[i]); s.push(str[i]);

N
Stack in C++
Reverse a String
printf("Reversed String: "); cout << "Reversed String: ";
Eval Postfix
while (!empty(&s)) { while (!s.empty()) {
Data Structures / printf("%c ", top(&s)); pop(&s); cout << s.top(); s.pop();
Containers
} }
Containers in C++
} }
Module Summary

• Lot of code for creating stack in stack.h • No codes for creating stack
• top to be initialized • No initialization
• Cluttered interface for stack functions • Clean interface for stack functions
• Implemented by user – error-prone • Available in library – well-tested

Programming in Modern C++ Partha Pratim Das M05.12


Program 05.04: Postfix Evaluation in C++
#include <iostream>
Module M05
#include <stack> // Library codes
using namespace std;

L
Partha Pratim
Das
int main() {

E
Objectives &
Outline
// Postfix expression: 1 2 3 * + 4 -
char postfix[] = {’1’,’2’,’3’,’*’,’+’,’4’,’-’}, ch;

T
Stack in C stack<int> s; // stack class
Common
Applications

P
Reverse a String
for(int i = 0; i < 7; i++) { ch = postfix[i];
Eval Postfix if (isdigit(ch)) { s.push(ch-’0’); }
else {

N
Stack in C++
int op1 = s.top(); s.pop();
Reverse a String
Eval Postfix
int op2 = s.top(); s.pop();
switch (ch) {
Data Structures / case ’*’: s.push(op2 * op1); break;
Containers
Containers in C++
case ’/’: s.push(op2 / op1); break;
case ’+’: s.push(op2 + op1); break;
Module Summary case ’-’: s.push(op2 - op1); break;
}
}
}
cout << "\nEvaluation " << s.top();
}

Programming in Modern C++ Partha Pratim Das M05.13


Data Structures / Containers in C++

Module M05

L
Partha Pratim
Das

E
Objectives &
Outline

T
Stack in C
Common
Applications

P
Reverse a String
Eval Postfix

N
Stack in C++
Reverse a String
Eval Postfix

Data Structures /
Containers
Containers in C++

Module Summary Data Structures / Containers in C++

Programming in Modern C++ Partha Pratim Das M05.14


Data Structures / Containers in C++

Module M05
• Like Stack, several other data structures are available in C++ standard library

L
Partha Pratim
Das • They are ready-made and work like a data type
• Varied types of elements can be used for C++ data structures

E
Objectives &
Outline • Data Structures in C++ are commonly called Containers:

T
Stack in C A container is a holder object that stores a collection of other objects (its elements)
Common
Applications
◦ They are implemented as class templates allowing great flexibility in the types supported as elements

P
Reverse a String The container
Eval Postfix
. manages the storage space for its elements

N
Stack in C++ . provides member functions to access them
Reverse a String
Eval Postfix
. supports iterators - reference objects with similar properties to pointers
Data Structures / ◦ Many containers have several member functions in common, and share functionalities - easy to learn
Containers and remember
Containers in C++
◦ stack, queue and priority queue are implemented as Container Adaptors
Module Summary
. Container adaptors are not full container classes, but classes that provide a specific interface
relying on an object of one of the container classes (such as deque or list) to handle the
elements
. The underlying container is encapsulated in such a way that its elements are accessed by the
members of the container adaptor independently of the underlying container class used
Programming in Modern C++ Partha Pratim Das M05.15
Data Structures / Containers in C++
Container Class Template Remarks
Module M05
Sequence containers: Elements are ordered in a strict sequence and are accessed by their position in the sequence

L
Partha Pratim array (C++11) Array class 1D array of fixed-size
Das
vector Vector 1D array of fixed-size that can change in size
deque Double ended queue Dynamically sized, can be expanded / contracted on both ends

E
Objectives &
Outline forward list (C++11) Forward list Const. time insert / erase anywhere, done as singly-linked lists
list List Const. time insert / erase anywhere, iteration in both directions

T
Stack in C
Common Container adaptors: Sequence containers adapted with specific protocols of access like LIFO, FIFO, Priority
Applications
LIFO stack Underlying container is deque (default) or as specified

P
Reverse a String
stack
Eval Postfix
queue FIFO queue Underlying container is deque (default) or as specified
priority queue Priority queue Underlying container is vector (default) or as specified

N
Stack in C++
Associative containers: Elements are referenced by their key and not by their absolute position in the container
Reverse a String
Eval Postfix
They are typically implemented as binary search trees and needs the elements to be comparable
Data Structures /
set Set Stores unique elements in a specific order
Containers multiset Multiple-key set Stores elements in an order with multiple equivalent values
Containers in C++ map Map Stores <key, value> in an order with unique keys
Module Summary multimap Multiple-key map Stores <key, value> in an order with multiple equivalent values
Unordered associative containers: Elements are referenced by their key and not by their absolute position in the container
Implemented using a hash table of keys and has fast retrieval of elements based on keys
unordered set (C++11) Unordered Set Stores unique elements in no particular order
unordered multiset (C++11) Unordered Multiset Stores elements in no order with multiple equivalent values
unordered map (C++11) Unordered Map Stores <key, value> in no order with unique keys
unordered multimap (C++11) Unordered Multimap Stores <key, value> in no order with multiple equivalent values
Programming in Modern C++ Partha Pratim Das M05.16
Module Summary

Module M05

• In C, stack needs to be coded by the user and works for a specific type of elements only

L
Partha Pratim
Das
• C++ standard library provides ready-made stack. It works like a data type

E
Objectives &
Outline • There are several containers in C++ standard library

T
Stack in C
Common
Applications

P
Reverse a String
Eval Postfix

N
Stack in C++
Reverse a String
Eval Postfix

Data Structures /
Containers
Containers in C++

Module Summary

Programming in Modern C++ Partha Pratim Das M05.17

You might also like