Upcoming SlideShare
×

# Data structures and algorithms lab3

1,699 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
1,699
On SlideShare
0
From Embeds
0
Number of Embeds
1,216
Actions
Shares
0
26
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Data structures and algorithms lab3

1. 1. DATA STRUCTURES AND ALGORITHMS LAB 3 Bianca Tesila FILS, March 2014
2. 2. OBJECTIVES Template functions  Template classes  Stack  Applications of stack 
3. 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. 4. TEMPLATES ‼Template functions: we can also use templates when we work in a procedural fashion.  Study ex2.cpp and ex2_swap.cpp
5. 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. 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. 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. 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. 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. 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. 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.