Stacks - 1 Lecture 13: Stacks PIC 10B Todd Wittman C o s m...

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: 1 Lecture 13: Stacks PIC 10B Todd Wittman C o s m ic W a rs : T h e G a th e rin g S h a d o w P h a n to m M e n a c e : 2 5 th R e w r ite T h e J a b b a W e a r s P r a d a W a t e r s h i p D o w n G r e e d o S h o t F i r s t The Stack Concept s A stack is a list of data that follows 2 rules: 1. Data can only be accessed by removing the top ( pop ). 2. A new data element can only be added to the top of the stack ( push ). s This means that all data in the middle of the stack is hidden. s Ex A stack of books s A stack is called a LIFO data structure: L ast I n, F irst O ut s We've actually encountered stacks before... Star W ars: The Book Portrait of the Artist as a Young Wookie T he P rincess Leia D iaries Bored of the R ings 2 Vectors Are Bad Stacks s Vectors have stack features because we are given the pop_back() and push_back() functions. s But vectors are more than stacks, because they can access the middle: v[i] s Adding/erasing from the back end of the vector should always be an O(1) operation. But it isn't. s Vectors are built on dynamic arrays. s But a dynamic array has to allocate a fixed number of blocks. int* v = new int[10]; s So how does a vector change its size? Resizing Vectors s The vector actually allocates a fixed number of sequential memory blocks. s This is called the vector capacity , not to be confused with the vector size. The capacity changes as the size grows. int* v = new int[10]; //capacity = 10 s If we do a push_back() so that the a larger capacity is needed, the vector does the following steps: r Allocate a new dynamic array with 50% more capacity....
View Full Document

Page1 / 9

Stacks - 1 Lecture 13: Stacks PIC 10B Todd Wittman C o s m...

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

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