SlideShare a Scribd company logo
DATA STRUCTURES AND ALGORITHMS
LAB 3

Bianca Tesila

FILS, March 2014
OBJECTIVES
Template functions
 Template classes
 Stack
 Applications of stack

TEMPLATES

‼Short reminder: why do we need templates?

Study ex1.cpp: KeyStorage class from the previous
lab
 Pay attention to the way of defining the class
methods

TEMPLATES

‼Template functions: we can also use templates when

we work in a procedural fashion.


Study ex2.cpp and ex2_swap.cpp
STACK



instance of an abstract data type (ADT) that
formalizes the concept of restricted collection (LIFO
= last in first out)



ADT vs. Data Structures: ADT is in the logical level
and data structure is in the implementation level:
ADT: the stack itself
 Data Structure: the stack implemented with an array

STACK: ACCESS TO ELEMENTS

‼ In a stack, all the operations take
place at the top of the top of the
stack.
Consequently, we can have access
to stack elements only through the
top of the stack.
STACK: BASIC OPERATIONS


push(x)




Adds the element x at the top of the stack

pop()
Removes the element from the top of the stack and returns it
 Returns an error if the stack is empty

STACK: BASIC OPERATIONS


peek()
 Returns (but does not remove) the element at the top of the stack



isEmpty()


Returns 1 if the stack is empty and 0 otherwise
STACK: ARRAY-BASED IMPLEMENTATION



Study stack_basic.cpp



Test the basic operations for a stack of char elements



Implement the following functionalities:
 display all the elements of a stack
 sort the elements of the stack
Test the new methods!
STACK: APPLICATIONS
‼Exercise: Implement a class named LargeStack which can stock
two arbitrary values of type T (use class templates). The class can
have just two members:
Stack<T> Smain, Saux;
Smain is the main stack which allows stocking the values added to
LargeStack.
 Saux is the auxiliary stack which has to be empty before and after the
call to a function of LargeStack class.


LargeStack class has the following main functions:
 void push(T x): add an element x to the top of the Smain stack
 T pop() : delete and return the element of the top of Smain stack
 void swap(int i): exchange the values from level i and j of Smain
stack. The levels are numbered starting with 0. If an error
occurs, the stack won’t be modified. You can use Saux stack for
temporary stocking of any values you want.
HOMEWORK


Finish all the lab exercises.



Using a stack, implement an algorithm for converting a
number from a base to another base.
For instance, 510 = 1012 .



Using a stack, check whether a given string is palindrome or
not.

More Related Content

What's hot (20)

PDF
Applications of stack
eShikshak
 
DOC
5.program structure
Shankar Gangaju
 
PPSX
CS106 Lab 9 - 1D array
Nada Kamel
 
PDF
Lec16-CS110 Computational Engineering
Sri Harsha Pamu
 
ODP
Talk on Standard Template Library
Anirudh Raja
 
PPTX
Evaluation of prefix expression with example
GADAPURAMSAINIKHIL
 
PDF
Lec 6 14_aug [compatibility mode]
Palak Sanghani
 
PDF
Prelim Project OOP
Dwight Sabio
 
DOCX
Best,worst,average case .17581556 045
university of Gujrat, pakistan
 
PPTX
R: Apply Functions
DataminingTools Inc
 
PPTX
Queue ppt
SouravKumar328
 
PDF
Write a program that initializes an array - of - double and then copies the c...
licservernoida
 
DOC
1183 c-interview-questions-and-answers
Akash Gawali
 
PPTX
MCRL2
kashif kashif
 
DOCX
Comp 220 ilab 7 of 7
ashhadiqbal
 
PPTX
C language presentation
bainspreet
 
PDF
Scilab vs matlab
Dadan Bagenda
 
PPTX
Stack organization
chauhankapil
 
PPT
Operator Overloading
Nilesh Dalvi
 
Applications of stack
eShikshak
 
5.program structure
Shankar Gangaju
 
CS106 Lab 9 - 1D array
Nada Kamel
 
Lec16-CS110 Computational Engineering
Sri Harsha Pamu
 
Talk on Standard Template Library
Anirudh Raja
 
Evaluation of prefix expression with example
GADAPURAMSAINIKHIL
 
Lec 6 14_aug [compatibility mode]
Palak Sanghani
 
Prelim Project OOP
Dwight Sabio
 
Best,worst,average case .17581556 045
university of Gujrat, pakistan
 
R: Apply Functions
DataminingTools Inc
 
Queue ppt
SouravKumar328
 
Write a program that initializes an array - of - double and then copies the c...
licservernoida
 
1183 c-interview-questions-and-answers
Akash Gawali
 
Comp 220 ilab 7 of 7
ashhadiqbal
 
C language presentation
bainspreet
 
Scilab vs matlab
Dadan Bagenda
 
Stack organization
chauhankapil
 
Operator Overloading
Nilesh Dalvi
 

Viewers also liked (11)

PPTX
Data structures and algorithms lab11
Bianca Teşilă
 
PDF
Difference between c# generics and c++ templates
Umar Ali
 
PPTX
Data structures and algorithms lab8
Bianca Teşilă
 
PPTX
Data structures and algorithms lab1
Bianca Teşilă
 
PPTX
Introduction to computer architecture and organization
Muhammad Ishaq
 
PPTX
Data structures and algorithms lab5
Bianca Teşilă
 
PPTX
USMLE and Canadian Exams
Kuwait Medical Society, UK & Ireland
 
PPTX
Chapter 1 introduction to computers
haider ali
 
PPT
0. Course Introduction
Intro C# Book
 
PPT
Ppt 1
shanmugamsara
 
PDF
DSA-2012-Lect00
Haitham El-Ghareeb
 
Data structures and algorithms lab11
Bianca Teşilă
 
Difference between c# generics and c++ templates
Umar Ali
 
Data structures and algorithms lab8
Bianca Teşilă
 
Data structures and algorithms lab1
Bianca Teşilă
 
Introduction to computer architecture and organization
Muhammad Ishaq
 
Data structures and algorithms lab5
Bianca Teşilă
 
USMLE and Canadian Exams
Kuwait Medical Society, UK & Ireland
 
Chapter 1 introduction to computers
haider ali
 
0. Course Introduction
Intro C# Book
 
DSA-2012-Lect00
Haitham El-Ghareeb
 
Ad

Similar to Data structures and algorithms lab3 (20)

PPT
Unit i(dsc++)
Durga Devi
 
PPT
Stacks
Malainine Zaid
 
PDF
02 Stack
Budditha Hettige
 
PPTX
Stacks Data structure.pptx
line24arts
 
DOCX
ObjectivesMore practice with recursion.Practice writing some tem.docx
vannagoforth
 
PPT
Data Structures
Dr.Umadevi V
 
PPT
Lecture5
Muhammad Zubair
 
PPTX
stack.pptx
mayankKatiyar17
 
PPTX
Week2-stacks-queues.pptx
VandanaBharti21
 
PPTX
Stack in Sata Structure
Muhazzab Chouhadry
 
PDF
Prof.sujata chechare patil stack and queue iceem engg college
sujatachecharepatil
 
PPTX
Review of basic data structures
Deepa Rani
 
PDF
Ds lab handouts
Ayesha Bhatti
 
PDF
Stacks
Sadaf Ismail
 
PPT
Lecture 2c stacks
Victor Palmar
 
PDF
Data Structures and Files
KanchanPatil34
 
PPT
01 stack 20160908_jintaek_seo
JinTaek Seo
 
PPT
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
sumitbardhan
 
PDF
Chapter 5 Stack and Queue.pdf
GirT2
 
PPTX
DATA STRUCTURE AND COMPUTER ALGORITHMS LECTURE 2
emathemathematics
 
Unit i(dsc++)
Durga Devi
 
Stacks Data structure.pptx
line24arts
 
ObjectivesMore practice with recursion.Practice writing some tem.docx
vannagoforth
 
Data Structures
Dr.Umadevi V
 
Lecture5
Muhammad Zubair
 
stack.pptx
mayankKatiyar17
 
Week2-stacks-queues.pptx
VandanaBharti21
 
Stack in Sata Structure
Muhazzab Chouhadry
 
Prof.sujata chechare patil stack and queue iceem engg college
sujatachecharepatil
 
Review of basic data structures
Deepa Rani
 
Ds lab handouts
Ayesha Bhatti
 
Stacks
Sadaf Ismail
 
Lecture 2c stacks
Victor Palmar
 
Data Structures and Files
KanchanPatil34
 
01 stack 20160908_jintaek_seo
JinTaek Seo
 
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
sumitbardhan
 
Chapter 5 Stack and Queue.pdf
GirT2
 
DATA STRUCTURE AND COMPUTER ALGORITHMS LECTURE 2
emathemathematics
 
Ad

More from Bianca Teşilă (6)

PDF
Akka Streams - An Adobe data-intensive story
Bianca Teşilă
 
PPTX
Data structures and algorithms lab10
Bianca Teşilă
 
PPTX
Data structures and algorithms lab9
Bianca Teşilă
 
PPTX
Data structures and algorithms lab7
Bianca Teşilă
 
PPTX
Data structures and algorithms lab6
Bianca Teşilă
 
PPTX
Data structures and algorithms lab4
Bianca Teşilă
 
Akka Streams - An Adobe data-intensive story
Bianca Teşilă
 
Data structures and algorithms lab10
Bianca Teşilă
 
Data structures and algorithms lab9
Bianca Teşilă
 
Data structures and algorithms lab7
Bianca Teşilă
 
Data structures and algorithms lab6
Bianca Teşilă
 
Data structures and algorithms lab4
Bianca Teşilă
 

Data structures and algorithms lab3

  • 1. DATA STRUCTURES AND ALGORITHMS LAB 3 Bianca Tesila FILS, March 2014
  • 2. OBJECTIVES Template functions  Template classes  Stack  Applications of stack 
  • 3. TEMPLATES ‼Short reminder: why do we need templates? Study ex1.cpp: KeyStorage class from the previous lab  Pay attention to the way of defining the class methods 
  • 4. TEMPLATES ‼Template functions: we can also use templates when we work in a procedural fashion.  Study ex2.cpp and ex2_swap.cpp
  • 5. STACK  instance of an abstract data type (ADT) that formalizes the concept of restricted collection (LIFO = last in first out)  ADT vs. Data Structures: ADT is in the logical level and data structure is in the implementation level: ADT: the stack itself  Data Structure: the stack implemented with an array 
  • 6. STACK: ACCESS TO ELEMENTS ‼ In a stack, all the operations take place at the top of the top of the stack. Consequently, we can have access to stack elements only through the top of the stack.
  • 7. STACK: BASIC OPERATIONS  push(x)   Adds the element x at the top of the stack pop() Removes the element from the top of the stack and returns it  Returns an error if the stack is empty 
  • 8. STACK: BASIC OPERATIONS  peek()  Returns (but does not remove) the element at the top of the stack  isEmpty()  Returns 1 if the stack is empty and 0 otherwise
  • 9. STACK: ARRAY-BASED IMPLEMENTATION  Study stack_basic.cpp  Test the basic operations for a stack of char elements  Implement the following functionalities:  display all the elements of a stack  sort the elements of the stack Test the new methods!
  • 10. STACK: APPLICATIONS ‼Exercise: Implement a class named LargeStack which can stock two arbitrary values of type T (use class templates). The class can have just two members: Stack<T> Smain, Saux; Smain is the main stack which allows stocking the values added to LargeStack.  Saux is the auxiliary stack which has to be empty before and after the call to a function of LargeStack class.  LargeStack class has the following main functions:  void push(T x): add an element x to the top of the Smain stack  T pop() : delete and return the element of the top of Smain stack  void swap(int i): exchange the values from level i and j of Smain stack. The levels are numbered starting with 0. If an error occurs, the stack won’t be modified. You can use Saux stack for temporary stocking of any values you want.
  • 11. HOMEWORK  Finish all the lab exercises.  Using a stack, implement an algorithm for converting a number from a base to another base. For instance, 510 = 1012 .  Using a stack, check whether a given string is palindrome or not.