0% found this document useful (0 votes)
19 views33 pages

Chap 3

The document discusses stacks and their implementation using linked lists in C++. It covers stack concepts and operations like push, pop and peek. It provides code templates for a stack class including functions for initialization, pushing and popping elements from the stack. Slide examples demonstrate how the stack and its operations work conceptually and in code.

Uploaded by

tienanhnvpl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views33 pages

Chap 3

The document discusses stacks and their implementation using linked lists in C++. It covers stack concepts and operations like push, pop and peek. It provides code templates for a stack class including functions for initialization, pushing and popping elements from the stack. Slide examples demonstrate how the stack and its operations work conceptually and in code.

Uploaded by

tienanhnvpl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

BK

TP.HCM
Ho Chi Minh City University of Technology BK
TP.HCM

Faculty of Computer Science and Engineering

Data Structures and Algorithms –


C++ Implementation

Huỳnh Tấn Đạt


Email: [email protected]
Home Page: http://www.cse.hcmut.edu.vn/~htdat/
Stacks
 Basic stack operations
 Linked-list implementation
 Stack applications
 Array implementation

Faculty of Computer Science and Engineering – HCMUT Slide 2


Linear List Concepts
 General list: no restrictions on where data can be
inserted/deleted, and on which operations can be used
on the list

 Restricted list: data can be inserted/deleted and


operations are performed only at the ends of the list

Faculty of Computer Science and Engineering – HCMUT Slide 3


Stack
 All insertions and deletions are restricted to one end
called the top
 Last-In First-Out (LIFO) data structure

Faculty of Computer Science and Engineering – HCMUT Slide 4


Basic Stack Operations

Push

Data
Top
Top

Stack Stack

Faculty of Computer Science and Engineering – HCMUT Slide 5


Basic Stack Operations

Overflow

Data Top

Stack

Faculty of Computer Science and Engineering – HCMUT Slide 6


Basic Stack Operations

Pop

Data
Top
Top

Stack Stack

Faculty of Computer Science and Engineering – HCMUT Slide 7


Basic Stack Operations

Underflow

Top
Stack

Faculty of Computer Science and Engineering – HCMUT Slide 8


Basic Stack Operations

Stack Top

Data
Top Top

Stack Stack

Faculty of Computer Science and Engineering – HCMUT Slide 9


Linked-List Implementation

Stack structure
top
Top 5

Conceptual Physical

Faculty of Computer Science and Engineering – HCMUT Slide 10


Linked-List Implementation

Stack stack
structure count <integer>
count top top <node pointer>
end stack

Stack node node


structure data <dataType>
data next next <node pointer>
end node

Faculty of Computer Science and Engineering – HCMUT Slide 11


Linked-List Implementation
template <class ItemType>
struct Node {
ItemType data;
Node<ItemType> *next;
};

template <class List_ItemType>


class Stack {
public:
Stack();
~Stack();

Faculty of Computer Science and Engineering – HCMUT Slide 12


Linked-List Implementation
void Push(List_ItemType dataIn);
int Pop(List_ItemType &dataOut);
int GetStackTop(List_ItemType &dataOut);
void Clear();
int IsEmpty();
int GetSize();
Stack<List_ItemType>* Clone();
void Print2Console();
private:
Node<List_ItemType>* top;
int count;
};

Faculty of Computer Science and Engineering – HCMUT Slide 13


Create Stack

Before After

? ? 0
count top count top

(no stack) (empty stack)

Faculty of Computer Science and Engineering – HCMUT Slide 14


Create Stack
Algorithm createStack (ref stack <metadata>)
Initializes metadata for a stack
Pre stack is structure for metadata
Post metadata initialized
1 stack.count = 0
2 stack.top = null
3 return
End createStack

Faculty of Computer Science and Engineering – HCMUT Slide 15


Linked-List Implementation
template <class List_ItemType>
Stack<List_ItemType>::Stack(){
this->top = NULL;
this->count = 0;
}

template <class List_ItemType>


Stack<List_ItemType>::~Stack(){
this->Clear();
}

Faculty of Computer Science and Engineering – HCMUT Slide 16


Push Stack

Before After
pNew red pNew red
data next data next
stack stack

2 blue 3 blue
count top data next count top data next

green green
data next data next

Faculty of Computer Science and Engineering – HCMUT Slide 17


Push Stack
Algorithm pushStack (ref stack <metadata>,
val data <dataType>)
Inserts (pushes) one item into the stack
Pre stack is a metadata structure to a valid stack
data contains data to be pushed into stack
Post data have been pushed in stack
Return true if successful; false if memory overflow

Faculty of Computer Science and Engineering – HCMUT Slide 18


Push Stack
1 if (stack full)
Before
1 success = false
pNew red
2 else
data next
1 allocate (pNew) stack
2 pNew -> data = data
2 blue
3 pNew -> next = stack.top
count top data next
4 stack.top = pNew
5 stack.count = stack.count + 1
green
6 success = true
data next
3 return success
End pushStack

Faculty of Computer Science and Engineering – HCMUT Slide 19


Push Stack
template <class List_ItemType>
void Stack<List_ItemType>::Push
(List_ItemType value){
Node<List_ItemType>* pNew = new
Node<List_ItemType>();
pNew->data = value;
pNew->next = top;
this->top = pNew;
this->count++;
}

Faculty of Computer Science and Engineering – HCMUT Slide 20


Pop Stack

Before After

dltPtr red dltPtr recycled

data next data next


stack stack

3 blue 2 blue
count top data next count top data next

green green
data next data next

Faculty of Computer Science and Engineering – HCMUT Slide 21


Pop Stack
Algorithm popStack (ref stack <metadata>,
ref dataOut <dataType>)
Pops the item on the top of the stack and returns it to caller
Pre stack is a metadata structure to a valid stack
dataOut is to receive the popped data
Post data have been returned to caller
Return true if successful; false if underflow

Faculty of Computer Science and Engineering – HCMUT Slide 22


Pop Stack
1 if (stack empty)
1 success = false Before
2 else
dltPtr red
1 dltPtr = stack.top
data next
2 dataOut = stack.top -> data stack
3 stack.top = stack.top -> next
3 blue
4 stack.count = stack.count - 1 count top data next
5 recycle (dltPtr)
6 success = true green
3 return success data next
End popStack

Faculty of Computer Science and Engineering – HCMUT Slide 23


Pop Stack
template <class List_ItemType>
int Stack<List_ItemType>::Pop(List_ItemType
&dataOut){
if (count == 0)
return 0;
Node<List_ItemType>* dltPtr = this->top;
dataOut = this->top->data;
this->top = this->top->next;
this->count--;
delete dltPtr;
return 1;
}

Faculty of Computer Science and Engineering – HCMUT Slide 24


Stack Top
Algorithm stackTop (val stack <metadata>,
ref dataOut <dataType>)
Retrieves the data from the top of the stack without
changing the stack
Pre stack is a metadata structure to a valid stack
dataOut is to receive top stack data
Post data have been returned to caller
Return true if successful; false

Faculty of Computer Science and Engineering – HCMUT Slide 25


Stack Top
1 if (stack empty)
1 success = false
2 else
1 dataOut = stack.top -> data
2 success = true
3 return success
End stackTop

Faculty of Computer Science and Engineering – HCMUT Slide 26


Stack Top
template <class List_ItemType>
int Stack<List_ItemType>::GetStackTop
(List_ItemType &dataOut){
if (count == 0)
return 0;
dataOut = this->top->data;
return 1;
}

Faculty of Computer Science and Engineering – HCMUT Slide 27


Destroy Stack
Algorithm destroyStack (ref stack <metadata>)
Releases all nodes back to memory
Pre stack is a metadata structure to a valid stack
Post stack empty and all nodes recycled
1 if (stack not empty)
1 loop (stack.top not null)
1 temp = stack.top
2 stack.top = stack.top -> next
3 recycle (temp)
2 stack.count = 0
3 return
End destroyStack
Faculty of Computer Science and Engineering – HCMUT Slide 28
Destroy Stack
template <class List_ItemType>
void Stack<List_ItemType>::Clear() {
Node<List_ItemType>* temp;
while (this->top != NULL){
temp = this->top;
this->top = this->top->next;
delete temp;
}
this->count = 0;
}

Faculty of Computer Science and Engineering – HCMUT Slide 29


Stack Empty
template <class List_ItemType>
int Stack<List_ItemType>::IsEmpty() {
return (count == 0);
}
template <class List_ItemType>
int Stack<List_ItemType>::GetSize() {
return count;
}

Faculty of Computer Science and Engineering – HCMUT Slide 30


Print a stack
template <class List_ItemType>
void Stack<List_ItemType>::Print2Console() {
Node<List_ItemType>* p;
p = this->top;
while (p != NULL){
printf("%d\t", p->data);
p = p->next;
}
printf("\n");
}

Faculty of Computer Science and Engineering – HCMUT Slide 31


Using Stacks
int main(int argc, char* argv[]){
Stack<int> *myStack = new Stack<int>();
int val;
myStack->Push(7);
myStack->Push(9);
myStack->Push(10);
myStack->Push(8);
myStack->Print2Console();
myStack->Pop(val);
myStack->Print2Console();
delete myStack;
return 0;
}

Faculty of Computer Science and Engineering – HCMUT Slide 32


Exercises
template <class List_ItemType>
Stack<List_ItemType>*
Stack<List_ItemType>::Clone() {
// ...
}

Faculty of Computer Science and Engineering – HCMUT Slide 33

You might also like