Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Allen Weiss, Addison Wesley

Similar presentations


Presentation on theme: "Mark Allen Weiss, Addison Wesley"— Presentation transcript:

1 Mark Allen Weiss, Addison Wesley
Stack & Queue Lecturers : Boontee Kruatrachue Room no Kritawan Siriboon Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss, Addison Wesley

2 Stack Lecturers : Boontee Kruatrachue Room no Kritawan Siriboon Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss, Addison Wesley

3 push pop top Stack LIFO List ( LastInFirstOut )
An ordered collection of items. One end is called a top of the stack. Items are inserted onto the top of the stack. => push Items are deleted from the top of the stack . => pop

4 Stack Implementations
Array Implementation Linked Stack

5 Array Implementation An array. 2 1 How can we make a stack?
What is it? An ordered collection of items. What data type can we use? What can keep many items? What can make items in ordered? An array. 2 1 Now, we have place for items of a stack. Next, can stack operations be done?

6 Stack Top With array, can we do stack operations, push, pop,...? Where can we push in the array ? At index 3. Why? We push on the top. We need a data structure for top. Where is the top element in each picture? At index 2,3. int top; What type of data can be top -> index ? 2 1 2 1 3

7 top= Stack Data Structure typedef char T;
#define SIZE 6 //constant SIZE T item[SIZE]; int top; 5432 1 top= C B A item A B C 1 2 3 4 5 top 2

8 Stack Operations

9 What if the array is full ?
push( ) What if the array is full ? Check Overflow pushing variable i ++ top; item[top] = i; item[++top] = i; push D D C B A 2 1 top = 3 top = 2 1 C B A item item

10 What if the array is empty ?
pop( ) What if the array is empty ? Check Underflow int oldTop = top; top --; return item[oldTop]; return item[top--]; pop Need to erase D? D D C B A 2 1 D C B A 3 2 1 No. Only change top. top=3 oldTop= top=

11 Try pop until empty top = 2 1 C B A top = 2 1 C B A isEmpty( )
What happens when stack empty? Try pop until empty return (top==-1); top = 2 1 C B A top = 2 1 C B A top = -1

12 2 2 1 1 top = A init( ) Before pushing, a stack is empty.
top = -1 2 1 test : push(A) top = A ++ top; item[top] = ‘A’;

13 top = 3 2 D 1 C B A isFull( ) What happens when stack full?
D C B A return (top==SIZE-1); top = SIZE-1

14 Stack Implementations
Array Implementation Linked Stack

15 Linked Stack top Check if it support every operations. pop ? push ?

16 Queue Lecturers : Boontee Kruatrachue Room no Kritawan Siriboon Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss, Addison Wesley

17 แถวคอย Queue FIFO List FirstInFirstOut front / head rear/tail deQueue
(delete) enQueue (insert) An ordered collection of items. There are 2 ends, head (front) and tail (rear). Items are inserted to the tail (rear). => enQueue Items are deleted from the head (front). => deQueue

18 Queue Implementations
Linked Queue Array Implementation

19 Linked Queue How do they link? Support every operations ? enQueue ?
front rear front rear How do they link? Support every operations ? front rear enQueue ? (insert) deQueue ? (delete) Every operations ?

20 Queue Implementations
Linked Queue Array Implementation

21 Array Implementation C D E A B C D E 1 f r 4 = r Straight array f = 2
f = 2 3 4 = r Circular array A B C D E f r Not full. But cannot enqueue ! No place left at the rear side ! Can we enqueue at the other side ? Think as an array is a circle.

22 Array Implementation C D E void increase(int &r) void increase(int &i)
#define SIZE 5 C D E 1 f = 2 3 4 = r r++ does not work ! void increase(int &r) if (i == (SIZE -1)) i = 0; else i++; void increase(int &i) if (r == (SIZE -1)) r = 0; else r++; Also f++ does not work !

23 isEmpty( ) A B E r f f r f f f f = 1 r = 1 r = 4 4 2 f = 2 3 3
A B E f r f f r f f 1 3 4 2 r = f = 1 2 3 4 r = f = isEmpty( ): can’t just check r>f ? or f>r ? It’s circular. Same as isFull( ).

24 isFull( ) A B C D E A F B C D E F B C D E A B E C D f r r f f r 1 3 4
A B C D E A F B C D E f r r f f r 1 3 4 2 r = f = F B C D E 1 2 3 4 r = f = A B E C D isFull( ), also can’t just check r>f ? or f>r ?

25 isEmpty(), isFull( ) ? A B C D E A B E C D f r r f 1 2 3 4 r = f = 1 2
A B C D E f r r f 1 2 3 4 r = f = 1 2 3 4 r = f = A B E C D isEmpty() & isFull( ), has the same condition ie. increase(r) makes it equal front. How to check isEmpty() or isFull()?

26 isEmpty( ) / isFull( ) Solution
#define MAX 50 typedef int T; struct queue{ T data[MAX]; int front, rear; int count; }; enQ deQ init isFull isEmpty count++ count-- count = 0 count == SIZE count == 0

27 Queue Operations enQ isFull deQ isEmpty init

28 Algorithms

29 Evaluate Postfix Expression
Stack Applications Parenthesis Matching Evaluate Postfix Expression Infix to Postfix Conversion (Reverse Polish Notation) Function Call (clearly see in recursion)

30 Are the parenthesises correct? (a+b-c)*[d+e]/{f*(g+h)}
Parenthesis Matching Are the parenthesises correct? (a+b-c)*[d+e]/{f*(g+h)} [(a+b-c}*[d+e]/{f*(g+h)} (a+b-c *[d+e]/{f*(g+h) }

31 [ (a+b)*{ (d+e)-3}] Parenthesis Matching Match ? Count : [ { ( ] } ) ?
No. We can do it since we were young. How ?

32 [ (a+b)*{ (d+e)-3}] Parenthesis Matching Simulate what you do. random
How to program this algorithm ? random What data structures are needed ? [ (a+b)*{ (d+e)-3}] / / / / / / / / Difficult ! Random position (until found match?). Scan out both ways. if Left side = open paren & Right side = close paren. If match : Check out. goto 2 else end process : Not match. else goto 1

33 [ (a+b)*{ (d+e)-3}] [ ( ) { ( ) }] [ ( ) { ( ) }] Parenthesis Matching
Clear out what are irrelevant. [ ( ) { ( ) }] [ ( ) { ( ) }] Parenthesis matching’s nature is a stack. How ? The close parenthesis matches the previous open parenthesis.

34 [ (a+b)*{ (d+e)-3}] Parenthesis Matching ↑ ( { ( [ Scan left to right.
Open paren : push to a stack. Close paren : check if match ? with top in stack match pop out. else done : not match !

35 Design Tools + + Code if (i == (SIZE -1)) Pseudocode i = 0; else i++;
if (i is at the last item) i = 0; else i++; Warnier – Orr Diagram i is last item i = 0 i is last item i++ + + Or if then A not A

36 Warnier-Orr Diagram Design Tool
Init empty stack s [ (a+b)*{ (d+e)-3}] read ch error=false ch = non_paren Scan (not EOF && not error) ch = open paren + ch = close paren s.push(ch) s.empty() + error =true(no match-open-paren) open = s.pop() Paren matching match(open,ch) notmatch(open,ch) error = true (missmatch) error + no match-open-paren / missmatch s.empty() + MATCH ( MISSMATCH open paren exceed [

37 Warnier-Orr Diagram Design Tool
Init empty stack s read ch error=false Scan (until EOF || error) ch = open paren + ch = close paren s.push(ch) s.empty() + error =true(no match-open-paren) + open = s.pop() Paren matching notmatch(open,ch) error = true (missmatch) error + no match-open-paren / missmatch + s.empty() + MATCH + MISSMATCH open paren exceed

38 Evaluate Postfix Expression
Stack Applications Parenthesis Matching Evaluate Postfix Expression Infix to Postfix Conversion (Reverse Polish Notation) Function Call (clearly see in recursion)

39 Evaluate Postfix Notation
Infix Form Prefix Form Postfix Form (Polish notation) a + b + a b a b + a + b * c + a * b c a b c * +

40 Evaluate Postfix Notation
Evaluate Postfix Notation’s nature is a stack. * * = ? 3 input: 6523 input: + Push : 6, 5, 2, 3 o2 <- pop#1 o1 <- pop#2 push S 5 6 S 2 5 + 6 5 What ‘s next ?

41 * * = ? input: 6523 Push : 3 2 5 6 s input: + pop#1 → 3 pop#2 → 2 5 push 2+3 6 s input: 8 8 push 8 5 6 input: * pop#1 → 8 40 pop#2 → 5 5 push 5*8 6 input: - pop#1 → 40 pop#2 → 5 -35 push 6 input: 3 push 3 3 -35 6 input: + pop#1 → 3 pop#2 → -35 -32 push 6 input: * pop#1 → -32 pop#2 → 6 push 6 * -32 - 192

42 Evaluate Postfix Expression
Stack Applications Parenthesis Matching Evaluate Postfix Expression Infix to Postfix Conversion (Reverse Polish Notation) Function Call (clearly see in recursion)

43 Infix to Postfix Conversion
a * b + c ab*c+ a*b+c ===> Notice : output: operands’ order is the same as input’s. output stack

44 Infix to Postfix Conversion a*b+c ---- >
input: stack: output: a*b+c a a*b+c * a a*b+c * ab a*b+c ab* a*b+c ab*c a*b+c ab*c+

45 Infix to Postfix Conversion a+b*c ---- >
input: stack: output: a+b*c a a+b*c a a+b*c ab * a+b*c abc a+b*c abc*+

46 Infix to Postfix Conversion
abc*+d- a+b*c-d => input: stack: output: a+b*c-d a a+b*c-d + a a+b*c-d + ab * a+b*c-d + abc a+b*c-d - abc*+ a+b*c-d - abc*+d a+b*c-d abc*+d-

47 Infix to Postfix Conversion
abc*+de/f+g*- a+b*c-(d/e+f)*g => a+b*c-(d/e+f)*g + ab * a+b*c-(d/e+f)*g + abc a+b*c-(d/e+f)*g - abc*+ ( a+b*c-(d/e+f)*g - abc*+d / a+b*c-(d/e+f)*g - abc*+de

48 Infix to Postfix Conversion (cont.) a+b*c-(d/e+f)*g =>
a+b*c-(d/e+f)*g - abc*+de + a+b*c-(d/e+f)*g - abc*+de/f a+b*c-(d/e+f)*g - abc*+de/f+ * a+b*c-(d/e+f)*g - abc*+de/f+g a+b*c-(d/e+f)*g - abc*+de/f+g*-

49 Evaluate Postfix Expression
Stack Applications Parenthesis Matching Evaluate Postfix Expression Infix to Postfix Conversion (Reverse Polish Notation) Function Call (clearly see more in recursion)

50 Stack of Function Call x 3 p i 8 3 5 3 k 8 *p 9 10 3 int i = 5;
int i = k; pass by reference is tecnically the same as pass address but for writing convenience (no dereference) int& ref = k; ref is another name of k void f() { int k = 8; sth(k); sth( 5); Add1(&k); Ad1(k); } void sth(int i){ int x = 3; i = x; } int *p = &k; //pass data void Add1(int *p) { (*p)++; } //pass address //pass by reference int& ref = k; void Ad1(int& ref){ ref++; } x 3 3 Add1( ) p Stack top i 8 3 5 3 Stack frame Ad1( ) sth( ) Stack top k f( ) 8 *p Stack frame ref 9 10

51 Struct Stack #define SIZE 6 typedef int T; typedef struct stack {
T item[SIZE]; int top; } stack; #define SIZE 6 typedef int T; void push (stack *sp, T d){ if (!isFull(*sp)) sp->item[++sp->top] = d; } void push (int item[], int *pTop, T d ){ (*pTop)++; item[*pTop] = d; } int main() { stack s, s1; init(&s); push(&s, 5); int main(){ T item[SIZE], item2[SIZE]; int top, top2; init(&top); push( item, &top,5);

52 Lab Questions

53 Common Misunderstanding
int *p = &i; int *p; p = &i; *p = &i; int *q = p; int *q; q = p; q p i 8 q points to what p points to 59 59 s and u are pointers 59 int *s, t, *u; int *s; int t; int *u; t is int

54 Lab : Scramble List

55 h I t1 love h2 you Opps! very much t2 C++ Bottom Up Lift it.
Try to print h2. love Where ? h2 you Take the bottom up. Opps! very much t2 C++

56 Riffle Shuffle h1 h2 h1 h2 4 1 4 1 5 2 5 2 t2 t2 6 3 6 3 7 7 8 8 t1 t1
Riffle Shuffle each node of each packet from the top. put the rest at the back of the result packet. 8 8 t1 t1 9 9 Lift up to 2 packets.

57 Riffle Shuffle Riffle Shuffle each node of each packet from the top.
4 1 4 1 5 5 2 2 t2 t2 6 6 3 3 7 7 Riffle Shuffle each node of each packet from the top. put the rest at the back of the result packet. 8 8 t1 t1 9 9 Lift up to 2 packets.

58 FILE *fp = fopen("D:/zfile.txt","w");
fprintf(fp, "369"); 369 fclose(fp); fp = fopen("D:/zfile.txt", "a"); 369 EOF fprintf(fp, "Append"); 369Append EOF fclose(fp); fp = fopen("D:/zfile.txt", "r"); 369Append EOF int i; fscanf(fp,"%d",&i); i 369 369Append EOF char c = fgetc(fp); c A 369Append EOF

59 FILE * fopen( const char * filename, const char * mode ); int fputc( int c, FILE *fp ); int fputs( const char *s, FILE *fp ); int fprintf(FILE *fp,const char *format, ...); int fgetc( FILE * fp ); char *fgets( char *buf, int n, FILE *fp ); int fscanf(FILE *fp, const char *format, ...);

60 C string H e l o W o r l d char s[12]={'H','e','l','l','o','\0'};
char s1[]="World"; printf("%s%s\n",s,s1); //output:HelloWorld S H e l o \0 1 2 3 4 5 6 7 8 9 10 11 S1 W o r l d \0 1 2 3 4 5


Download ppt "Mark Allen Weiss, Addison Wesley"

Similar presentations


Ads by Google