CS112_20_Slides-ADTs2 - Abstract Data Types cont. Yinglin...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
S112 Fundamentals of Abstract Data Types— cont. Yinglin Wang CS Dept., SJTU
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
S112 Fundamentals of Stack provides a storage for a collection of data values; these values form a sequence; The values can only be removed or added at the end of the sequence from a stack. Lets consider the behavior of a stack
Background image of page 2
S112 Fundamentals of The element Last In must be element First Out. Abbreviated: Last In First Out (LIFO) equally: First In, Last out a 1 Push(stack, a 1 ) stack Feature of its Behavior :
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
S112 Fundamentals of a 1 Push(stack, a 1 ) stack
Background image of page 4
S112 Fundamentals of a 1 Push(stack, a 1 ) stack Push(stack, a 2 ) a 2
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
S112 Fundamentals of a 1 Push(stack, a 1 ) stack Push(stack, a 2 ) a 2
Background image of page 6
S112 Fundamentals of a 1 Push(stack, a 1 ) stack Push(stack, a 2 ) a 2 Push(stack, a 3 ) a 3
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
S112 Fundamentals of a 1 Push(stack, a 1 ) stack Push(stack, a 2 ) a 2 Push(stack, a 3 ) a 3 Pop(stack)
Background image of page 8
S112 Fundamentals of a 1 Push(stack, a 1 ) stack Push(stack, a 2 ) a 2 Push(stack, a 3 ) a 3 Pop(stack) The element Last In must be element First Out. Abbreviated: Last In First Out (LIFO) equally: First In, Last out Summary to the Behavior of a stack:
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
S112 Fundamentals of a 1 Push(stack, a 1 ) stack Push(stack, a 2 ) a 2 Push(stack, a 3 ) a 3 Pop(stack) If you need a storage device of such kind as stacks, what will you do? If you want to solve a problem: Read a series of integer numbers from keyboard and then print them in reverse order: e.g. read 12, 3, 6, 29 first, output 29, 6, 3, 12 later. you may use stacks to solve it:
Background image of page 10
S112 Fundamentals of stackT NewStack(void){ stackT stack; stack=New(stackT); stack->count=0; return (stack); } /* define a stack type typedef struct { int elements[100]; int count; } *stackT; /* declare a variable; stackT stack; /* pop a element from a stack int pop(stackT stack){ if(stack->count>0){ stack-> count— ; return stack-> elements[count ] } else{ printf(“error:stack is empty!”) } } /* push a element onto a stack
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 24

CS112_20_Slides-ADTs2 - Abstract Data Types cont. Yinglin...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online