230 likes | 240 Views
Data Structures. 6 Stacks Data Structures Prof A Alkhorabi. 6 Stack ADTs. Stack concepts. Stack applications. A stack ADT: requirements, contract. Implementations of stacks: using arrays, linked lists. Linked Data Structures.
E N D
Data Structures 6Stacks Data Structures Prof A Alkhorabi
6Stack ADTs • Stack concepts. • Stack applications. • A stack ADT: requirements, contract. • Implementations of stacks: using arrays, linked lists.
Linked Data Structures • Linked data structures (or simply data structures), are structures represent objects linked in one of the following types: • Linear data structures: • Linked lists: single, double, circuliar. • Stack (LIFO). • Queue(FIFO). • Non-Linear data structures : • Tree: binary, multi-branch. • Graph.
Stack concepts (1) • A stack is a last-in-first-out sequence of elements (LIFO). • Elements can added and removed only at one end (the top of the stack). • The depth (or length) of stack is the number of elements it contains. • An empty stack has depth zero.
2001 Misérables Misérables Initially: After remov-ing a book: After adding “Misérables”: After adding “2001”: Rob Roy War & Peace War & Peace War & Peace War & Peace Moby Dick Moby Dick Moby Dick Moby Dick Stack concepts (2) • Illustration (stack of books):
Stack applications • Interpreter (e.g., the Java Virtual Machine) • maintains a stack containing intermediate results during evaluation of complicated expressions • also containing arguments and return addresses for method calls and returns. • Parser (e.g., a component of the Java compiler) • maintains a stack containing symbols encountered during parsing.
Example: text-file reversal • A text file is a sequence of (zero or more) lines. • To reverse the order of these lines, we must store them in a first-in-last-out sequence. • Text-file reversal algorithm: To output the lines of file in reverse order: 1. Make line-stack empty.2. For each line read from file, repeat: 2.1. Add line to the top of line-stack.3. While line-stack is not empty, repeat: 3.1. Remove a line from the top of line-stack into line. 3.2. Output line.4. Terminate.
Stack ADT: requirements • Requirements: • It must be possible to make a stack empty. • It must be possible to add (‘push’) an element to the top of a stack. • It must be possible to remove (‘pop’) the topmost element from a stack. • It must be possible to test whether a stack is full. • It should be possible to access the topmost element in a stack without removing it.
Stack ADT: contract (1) • Possible contract, expressed in C++ : class Stack { // Each Stack object is a stack whose elements are objects. /////////////// Accessors /////////////// boolean isEmpty (); // Return true if and only if this stack is empty. Object getTop (); // Return the element at the top of this stack.
Stack ADT: contract (2) • Possible contract (continued): ///////////// Transformers ///////////// void clear (); // Make this stack empty. void Push (char elem); // Add elem as the top element of this stack. void Pop (); // Remove and return the element at the top of this stack. }
topmost element unoccupied 0 1 depth–1 depth maxdepth–1 element element element Invariant: 1 maxdepth–1 depth=0 Empty stack: 1 4 0 depth=3 5 2 Illustration(maxdepth = 6): Moby Dick War & Peace Rob Roy Implementation of stacks using arrays (1) • Represent a bounded stack (depth maxdepth) by: • variable depth, containing the current depth • array elems of length maxdepth, containing the stacked elements in elems[0… depth–1].
Implementation using arrays (2) • C++ implementation: class ArrayStack { private: char Data;int depth; public: ////////// Constructor /////////// ArrayStack (int maxDepth) { Stack = new char[maxDepth]; depth = 0; }
Implementation using arrays (3) • C++ implementation (continued): ///////////// Accessors ///////////// boolean isEmpty () { if(depth == 0) return TRUE; else return FALSE; } char getLast () {if (depth == 0) { cout << “Stack is Empty” << endl; return 1; }return Stack[depth-1]; }
Implementation using arrays (4) • C++ implementation (continued): ///////////// Transformers ///////////// void clear () {for (int i = 0; i < depth; i++) Stack[i] = NULL; depth = 0; } void Push (char elem) {if (depth == elems.length) cout << “Stack is Full” << endl; else Stack[depth++] = elem; }
Implementation using arrays (5) • C++ implementation (continued): Pop () {if (depth == 0) cout << “Stack is Empty”; else Stack[--depth] = NULL;} • Analysis: • Operations isEmpty, getLast, Push, Pop have time complexity O(1). • Operation clear has time complexity O(n).
element element element Invariant: Empty stack: RobRoy War & Peace Moby Dick Illustration: Implementation of stacks using SLLs (1) • Represent an (unbounded) stack by a SLL, such that the first node contains the topmost element pointed to by pointer Head. topmost element
Implementation using SLLs (2) • C++ Stack node implementation: // Stack Node declaration struct STCKNode{ char Data[10]; STCKNode* Under; };
Implementation using SLLs (3) C++ Stack SLL implementation: // Stack declaration class StackSLL { private: STCKNode *Head; int Length; // depth public: //////////// Constructor //////////// StackSLL () { Head = NULL; Length = 0; } ;
Implementation using SLLs (4) C++ Stack SLL implementation: //////////// Accessors //////////// int size () { return Length; }; char* get (int i) { if (i < 0 || i >= Length) throw ; return node(i).Data; }; STCKNode node (int i); void PrintStack(); int StackIsEmpty() { if (Length <= 0) return TRUE; else return FALSE;} int StackIsFull() { if (Length >= STACKSIZE) return TRUE; else return FALSE;}
Implementation using SLLs (5) //////////// Transformers //////////// void Push(char *c) { STCKNode *p; p = new STCKNode; // Fill Node strcpy(p->Data,c); p->Under = NULL; if(StackIsFull()) cout << "Stack is FULL." << endl; else { if (StackIsEmpty()) Head = p; else { p->Under = Head; Head = p; } Length++; } }
Implementation using SLLs (6) //////////// Transformers - Continue //////////// void Pop() { if (StackIsEmpty()) cout << "Stack is Empty!" << endl; else { Head = Head->Under; Length--; } } }; // End of class SingleLinkedList
Implementation using SLLs (7) Possible implementation for main(): void main() { StackSLL Stack1; //1 Stack1.Push("Salem"); Stack1.PrintStack(); //2 cout<< "No of nodes in Stack = " << Stack1.size() << endl; Stack1.Push("Ahmed"); Stack1.PrintStack(); //3 Stack1.Push("Ali"); Stack1.PrintStack(); //4 Stack1.Push("Omar"); Stack1.PrintStack(); //5 Stack1.Pop(); Stack1.PrintStack(); //6 Stack1.Pop(); Stack1.PrintStack(); //7 cout<< "No of nodes in Stack = " << Stack1.size() << endl; Stack1.Pop(); Stack1.PrintStack(); //8 Stack1.Pop(); Stack1.PrintStack(); //9 cout<< "No of nodes in Stack = " << Stack1.size() << endl; } Example: StckSLL2.CPP
Implementation using SLLs (8) Output Salem No of nodes in Stack = 1 Ahmed Salem Ali Ahmed Salem Stack is Full. Ali Ahmed Salem No of nodes in Stack = 3 Ahmed Salem Salem No of nodes in Stack = 1 Stack is Empty!. No of nodes in Stack = 0