 Download Download Presentation LISTS

# LISTS

Télécharger la présentation ## LISTS

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. LISTS

2. What is a list? • A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element has a unique predecessor (except first) and a unique successor (except last) Examples: grocery, to-do, address

3. What is a list? (cont.) • Lists may be sorted: 1 2 3 4 5 • or not sorted: 4 2 3 5 1 • Order always matters in a list: • 3 1 4 5 2 and 1 3 4 5 2 • are not the same lists

4. Define list head and tail. • Head - the first element of a list Head([4, 2, 3, 5, 1]) = 4 • Tail - the remaining elements of a list without the head Tail([4, 2, 3, 5, 1]) = [2, 3, 5, 1]

5. STACKS

6. What is a stack? • A collection of data items in which • elements are • added • removed • from only one end (top) • LIFO (last-in-first-out)

7. Stack Examples • Coins • Trays • Kleenex

8. Spring-loaded Stack of Plates • If plate is added to stack, • those below it are pushed down and can't be accessed • If plate is removed from stack, • those below it pop up one position • The stack empty when no more plates in it • The stack is full if no more room for more

9. Stack Operations • a) Add an element to the top of the stack: • Push • b) Take an element off the top of the stack: • Pop

10. Stack operations (cont.) • c) Determine if the stack is full: • If a stack is full, an attempt to push another element results in overflow. • d) Determine if the stack is empty: • If a stack is empty, an attempt to pop another element results in underflow.

11. Stack Example Stack1.Push(2) 2 3 Stack1.Push(3) 2 5 3 Stack1.Push(5) 2 3 X = Stack1.Pop() 2 X = 5 X = Stack1.Pop() 2 X = 3 3 Stack1.Push(X) 2 X = 3

12. Stack Application • A program is to be written to • convert nonnegative integers • from base ten to binary • using repeated division by 2 • Successive remainders give the binary digits, but in reverse order. • How does one print them in correct order?

13. 0 r 1 2 | 1 r 1 2 | 3 r 0 2 | 6 r 1 2 | 13 r 0 2 | 26 Remainders 0 and 1 are generated in right-to-left order. We need to "stack" them up, then print them out from top to bottom. 26 is equivalent to what base 2 number?

14. BASE-CONVERSION ALGORITHM • Create an empty stack to hold the remainders. • While Number != 0 do the following: • Calculate Remainder that results when Number is divided by 2. • Push Remainder onto the stack of remainders. • Replace Number by integer quotient of Number divided by 2. • End while. • While the stack of remainders is not empty: • Remove the Remainder from the top of the stack of remainders. • Display Remainder. • End while.

15. /* Program that uses a stack to convert the base-ten representation of a positive integer to base two. Input(keyboard): A positive integer. Output(screen): Base-two representation of the number. */ #include <iostream.h> #include "Stack.h" int main(void) { unsigned Number, // the number to be converted Remainder; // remainder when Number divd by 2 Stack<int> // stack of remainders StackOfRemainders; char Response;

16. do { cout << "Enter positive integer to convert: "; cin >> Number; while (Number != 0 && !StackOfRemainders.IsFull()) { Remainder = Number % 2; StackOfRemainders.Push(Remainder); Number /= 2; } cout << "Base two representation: "; while ( !StackOfRemainders.IsEmpty() ) { Remainder = StackOfRemainders.Pop() cout << Remainder; } cout << endl; cout << "\nMore (Y or N)? "; cin >> Response; } while (Response == 'Y' || Response == 'y'); return 0; }

17. Designing a Stack Class • Identifying operations needed to manipulate the object being modeled by the class • Operations are described independently of how class will be implemented • At this point, we don’t know what data members will be available.

18. Class Design • Construction: • Takes an uninitialized stack and produces an initialized stack: • Constructor: (Stack) --> Stack

19. Full operation: • Takes a stack and produces False or True depending on whether the stack has room for more values: • IsFull: (Stack) --> int • Empty operation: • Takes a stack and produces False or True depending on whether stack contains values: • IsEmpty: (Stack) --> int

20. Push operation: • Takes a stack and a value and adds the value to the top of the stack: • Push: (Stack, Value) --> Stack • Pop operation: • Takes a stack and removes and returns the value at the top of the stack: • Pop: (Stack) --> Value

21. Stack Specification

22. Stack Application Infix to Postfix

23. Infix to Postfix Algorithm 1. Scan the string from left to right. 2. If a symbol is an operand, write it. 3. If a symbol is an operator a) pop and write every operator from stack that has precedence higher or equal new operator b) push the operator onto the stack 4. If opening left parand is seen, push it on the stack. 5. On right parand • Pop and write all operators to the left parand • Discard both parands 6. When string has been scanned, pop and write remaining operators on the stack

24. Programming Assignment • Write a program which converts an infix notation expression into a postfix expression. • Use four operators: + - * / plus ( ) • For operands use letters: A, B, C, D, ... • A + B * C - E ==> A B C * + E -

25. THE END