Chapter 1

advertisement
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.
Prof A Alkhorabi
6- 2
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.
Prof A Alkhorabi
6- 3
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.
Prof A Alkhorabi
6- 4
Stack concepts (2)
• Illustration (stack of books):
Initially:
Rob Roy
War & Peace
Moby Dick
Prof A Alkhorabi
After removing a book:
War & Peace
Moby Dick
After adding
“Misérables”:
After adding
“2001”:
Misérables
War & Peace
Moby Dick
2001
Misérables
War & Peace
Moby Dick
6- 5
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.
Prof A Alkhorabi
6- 6
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.
Prof A Alkhorabi
6- 7
Stack ADT: requirements
•
Requirements:
1) It must be possible to make a stack empty.
2) It must be possible to add (‘push’) an element to the
top of a stack.
3) It must be possible to remove (‘pop’) the topmost
element from a stack.
4) It must be possible to test whether a stack is full.
5) It should be possible to access the topmost element in a
stack without removing it.
Prof A Alkhorabi
6- 8
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.
Prof A Alkhorabi
6- 9
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.
}
Prof A Alkhorabi
6- 10
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].
topmost element
Invariant:
0
1
element element
depth=0
unoccupied
depth–1 depth
element
maxdepth–1
1
maxdepth–1
Empty stack:
Illustration
(maxdepth = 6):
Prof A Alkhorabi
1
0
2
Moby War & Rob
Dick Peace Roy
depth=3
4
5
6- 11
Implementation using arrays (2)
• C++ implementation:
class ArrayStack {
private: char Data;
int depth;
public:
////////// Constructor ///////////
ArrayStack (int maxDepth) {
Stack = new char[maxDepth];
depth = 0;
}
Prof A Alkhorabi
6- 12
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];
}
Prof A Alkhorabi
6- 13
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;
}
Prof A Alkhorabi
6- 14
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).
Prof A Alkhorabi
6- 15
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
Invariant:
element
element
element
Empty stack:
Illustration:
Prof A Alkhorabi
Rob
Roy
War &
Peace
Moby
Dick
6- 16
Implementation using SLLs (2)
• C++ Stack node implementation:
// Stack Node declaration
struct STCKNode{
char Data[10];
STCKNode* Under;
};
Prof A Alkhorabi
6- 17
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; } ;
Prof A Alkhorabi
6- 18
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;}
Prof A Alkhorabi
6- 19
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++; } }
Prof A Alkhorabi
6- 20
Implementation using SLLs (6)
//////////// Transformers - Continue ////////////
void Pop()
{
if (StackIsEmpty())
cout << "Stack is Empty!" << endl;
else {
Head = Head->Under;
Length--; }
}
}; // End of class SingleLinkedList
Prof A Alkhorabi
6- 21
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
Example:
Stack1.Pop();
Stack1.PrintStack();
//9
StckSLL2.CPP
"No of nodes in Stack = " << Stack1.size() << endl; }
Prof Acout<<
Alkhorabi
6- 22
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
Prof A Alkhorabi
6- 23
Download