Data Structures (INE2011) Electronics and Communication Engineering Hanyang University Haewoon Nam Lecture 4 1
Stacks Insertion and deletion are made at one end () Last input first output (LIFO) Inserting an element Push Pop 1. Push A0120C0C 00000194 00000001 2
Stacks Insertion and deletion are made at one end () Last input first output (LIFO) Inserting an element Push Pop 1. Push 2. Push 70C00A14 A0120C0C 00000194 3
Stacks Insertion and deletion are made at one end () Last input first output (LIFO) Inserting an element Push Pop 1. Push 2. Push 70C00A14 3. Push 00000005 A0120C0C 70C00A14 4
Stacks Insertion and deletion are made at one end () Last input first output (LIFO) Inserting an element Push Pop 1. Push 2. Push 70C00A14 3. Push 00000005 4. Pop 00000005 70C00A14 5
Stacks Insertion and deletion are made at one end () Last input first output (LIFO) Inserting an element Push Pop 1. Push 2. Push 70C00A14 3. Push 00000005 4. Pop 00000005 70C00A14 6
Stacks and Local Variables in A Function #include <iostream> int add2 (int i32val1, int i32val2) { int i32out = 0; i32out = i32val1 + i32val2; return i32out; } i32out 00000005 00000005 00000005 70C00A14 00000010 7
Stacks and A Function Memory map #include <iostream> int add2 (int i32val1, int i32val2) { int i32out = 0; i32out = i32val1 + i32val2; return i32out; } main( ) add2( ) 00000005 int main ( ) { int i32result = 0; 70C00A14 i32result = add2(3, 5); std::cout << The result is << i32result; } 8
Class Stack template<class T> class Stack { public: Stack(int stackcapacity = 10); ~Stack() {delete [] stack;} bool IsEmpty() const; T& Top() const; void Push(const T& item); void Pop(); private: T *stack; // array for stack elements int ; // position of element int capacity; // capacity of stack array }; template<class T> Stack<T>::Stack(int stackcapacity) :capacity(stackcapacity) { if (capacity < 1) throw Stack capacity must be > 0 ; stack = new T[capacity]; = -1; } template<class T> inline bool Stack<T>::IsEmpty() const {return == -1} template<class T> inline T& Stack<T>::Top() const { if (IsEmpty()) throw Stack is empty ; return stack[]; } 9
Class Stack template<class T> void Stack<T>::Push(const T& x) {// Add x to the stack. if ( == capacity - 1) {ChangeSize1D(stack, capacity, 2*capacity); capacity *= 2; } // add at stack stack[++] = x; } void Stack<T>::Pop() { if (IsEmpty()) throw Stack is empty. Cannot delete. ; stack[--].~t(); // destructor for T } 0 1 2 3 4 a b c d 0 1 2 3 4 a b c d e 0 1 2 3 4 a b c d e push pop 10
Queues Insertion and deletion are made at different ends First input first output (FIFO) Adding an element Insert item at the rear of the queue Delete item at the front of the queue 1. Add A0120C0C 00000194 00000001 front, rear 11
Queues Insertion and deletion are made at different ends First input first output (FIFO) Adding an element Insert item at the rear of the queue Delete item at the front of the queue 1. Add A0120C0C 00000194 2. Add 70C00A14 rear front 12
Queues Insertion and deletion are made at different ends First input first output (FIFO) Adding an element Insert item at the rear of the queue Delete item at the front of the queue 1. Add 2. Add 70C00A14 3. Add 00000005 A0120C0C 70C00A14 rear front 13
Queues Insertion and deletion are made at different ends First input first output (FIFO) Adding an element Insert item at the rear of the queue Delete item at the front of the queue 1. Add 2. Add 70C00A14 3. Add 00000005 4. Delete 00000005 70C00A14 rear front 14
Queues Insertion and deletion are made at different ends First input first output (FIFO) Adding an element Insert item at the rear of the queue Delete item at the front of the queue 1. Add 2. Add 70C00A14 3. Add 00000005 4. Delete 00000005 70C00A14 rear front 15
Linear Queues Linear queues 0 1 2 3 4 a b c d Newest element Oldest element front push rear pop 0 1 2 3 4 5 6 7 8 9 a b c d e 0 1 2 3 4 5 a b c d e front rear front rear 16
Circular Queues Circular queues [0] [1] [2] [3] [4] [5] [0] [1] [2] [3] [4] [5] a b c d f c d e front rear rear front [2] [3] [2] [3] b c c [1] a [0] d [5] [4] [1] [0] f d e [5] [4] 17
Circular Queues Empty queue Full queue [0] [1] [2] [3] [4] [5] [0] [1] [2] [3] [4] f a b c d [5] e front = rear front = rear [2] [3] [2] [3] b c [1] [0] [5] [4] [1] a [0] f d e [5] [4] 18