SlideShare a Scribd company logo
1 of 11
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

Applications of stack
Applications of stackApplications of stack
Applications of stackeShikshak
 
CS106 Lab 9 - 1D array
CS106 Lab 9 - 1D arrayCS106 Lab 9 - 1D array
CS106 Lab 9 - 1D arrayNada Kamel
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringSri Harsha Pamu
 
Talk on Standard Template Library
Talk on Standard Template LibraryTalk on Standard Template Library
Talk on Standard Template LibraryAnirudh Raja
 
Evaluation of prefix expression with example
Evaluation of prefix expression with exampleEvaluation of prefix expression with example
Evaluation of prefix expression with exampleGADAPURAMSAINIKHIL
 
Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]Palak Sanghani
 
Prelim Project OOP
Prelim Project OOPPrelim Project OOP
Prelim Project OOPDwight Sabio
 
Write a program that initializes an array - of - double and then copies the c...
Write a program that initializes an array - of - double and then copies the c...Write a program that initializes an array - of - double and then copies the c...
Write a program that initializes an array - of - double and then copies the c...licservernoida
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answersAkash Gawali
 
Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7ashhadiqbal
 
C language presentation
C language presentationC language presentation
C language presentationbainspreet
 
Stack organization
Stack organizationStack organization
Stack organizationchauhankapil
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 

What's hot (20)

Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Queue
QueueQueue
Queue
 
5.program structure
5.program structure5.program structure
5.program structure
 
CS106 Lab 9 - 1D array
CS106 Lab 9 - 1D arrayCS106 Lab 9 - 1D array
CS106 Lab 9 - 1D array
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
 
Talk on Standard Template Library
Talk on Standard Template LibraryTalk on Standard Template Library
Talk on Standard Template Library
 
Evaluation of prefix expression with example
Evaluation of prefix expression with exampleEvaluation of prefix expression with example
Evaluation of prefix expression with example
 
Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]
 
Prelim Project OOP
Prelim Project OOPPrelim Project OOP
Prelim Project OOP
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 
R: Apply Functions
R: Apply FunctionsR: Apply Functions
R: Apply Functions
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Write a program that initializes an array - of - double and then copies the c...
Write a program that initializes an array - of - double and then copies the c...Write a program that initializes an array - of - double and then copies the c...
Write a program that initializes an array - of - double and then copies the c...
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
 
MCRL2
MCRL2MCRL2
MCRL2
 
Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7
 
C language presentation
C language presentationC language presentation
C language presentation
 
Scilab vs matlab
Scilab vs matlabScilab vs matlab
Scilab vs matlab
 
Stack organization
Stack organizationStack organization
Stack organization
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 

Viewers also liked

Data structures and algorithms lab11
Data structures and algorithms lab11Data structures and algorithms lab11
Data structures and algorithms lab11Bianca Teşilă
 
Difference between c# generics and c++ templates
Difference between c# generics and c++ templatesDifference between c# generics and c++ templates
Difference between c# generics and c++ templatesUmar Ali
 
Data structures and algorithms lab8
Data structures and algorithms lab8Data structures and algorithms lab8
Data structures and algorithms lab8Bianca Teşilă
 
Data structures and algorithms lab1
Data structures and algorithms lab1Data structures and algorithms lab1
Data structures and algorithms lab1Bianca Teşilă
 
Introduction to computer architecture and organization
Introduction to computer architecture and organizationIntroduction to computer architecture and organization
Introduction to computer architecture and organizationMuhammad Ishaq
 
Data structures and algorithms lab5
Data structures and algorithms lab5Data structures and algorithms lab5
Data structures and algorithms lab5Bianca Teşilă
 
Chapter 1 introduction to computers
Chapter 1   introduction to computersChapter 1   introduction to computers
Chapter 1 introduction to computershaider ali
 
0. Course Introduction
0. Course Introduction0. Course Introduction
0. Course IntroductionIntro C# Book
 

Viewers also liked (11)

Data structures and algorithms lab11
Data structures and algorithms lab11Data structures and algorithms lab11
Data structures and algorithms lab11
 
Difference between c# generics and c++ templates
Difference between c# generics and c++ templatesDifference between c# generics and c++ templates
Difference between c# generics and c++ templates
 
Data structures and algorithms lab8
Data structures and algorithms lab8Data structures and algorithms lab8
Data structures and algorithms lab8
 
Data structures and algorithms lab1
Data structures and algorithms lab1Data structures and algorithms lab1
Data structures and algorithms lab1
 
Introduction to computer architecture and organization
Introduction to computer architecture and organizationIntroduction to computer architecture and organization
Introduction to computer architecture and organization
 
Data structures and algorithms lab5
Data structures and algorithms lab5Data structures and algorithms lab5
Data structures and algorithms lab5
 
USMLE and Canadian Exams
USMLE and Canadian ExamsUSMLE and Canadian Exams
USMLE and Canadian Exams
 
Chapter 1 introduction to computers
Chapter 1   introduction to computersChapter 1   introduction to computers
Chapter 1 introduction to computers
 
0. Course Introduction
0. Course Introduction0. Course Introduction
0. Course Introduction
 
Ppt 1
Ppt 1Ppt 1
Ppt 1
 
DSA-2012-Lect00
DSA-2012-Lect00DSA-2012-Lect00
DSA-2012-Lect00
 

Similar to Data structures and algorithms lab3

Similar to Data structures and algorithms lab3 (20)

LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
 
Data structures and algorithms lab4
Data structures and algorithms lab4Data structures and algorithms lab4
Data structures and algorithms lab4
 
Lab Manual-OOP.pdf
Lab Manual-OOP.pdfLab Manual-OOP.pdf
Lab Manual-OOP.pdf
 
104332 sri vidhya eng notes
104332 sri vidhya eng notes104332 sri vidhya eng notes
104332 sri vidhya eng notes
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
 
stack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptxstack_presentaton_HUSNAIN[2].pojklklklptx
stack_presentaton_HUSNAIN[2].pojklklklptx
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks
StacksStacks
Stacks
 
01-intro_stacks.ppt
01-intro_stacks.ppt01-intro_stacks.ppt
01-intro_stacks.ppt
 
Lec2
Lec2Lec2
Lec2
 
Introduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdfIntroduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdf
 
CD3291 2.5 stack.pptx
CD3291 2.5 stack.pptxCD3291 2.5 stack.pptx
CD3291 2.5 stack.pptx
 
Objectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docxObjectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docx
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docxAssg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
 
Stacks
StacksStacks
Stacks
 
Stacks in Data Structure
Stacks in Data StructureStacks in Data Structure
Stacks in Data Structure
 
stack presentation
stack presentationstack presentation
stack presentation
 

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.