• No results found

Data Structures and Algorithms C++ Implementation

N/A
N/A
Protected

Academic year: 2021

Share "Data Structures and Algorithms C++ Implementation"

Copied!
33
0
0

Loading.... (view fulltext now)

Full text

(1)

Data Structures and Algorithms –

C++ Implementation

Ho Chi Minh City University of Technology Faculty of Computer Science and Engineering

BK TP.HCM BK TP.HCM

Huỳnh Tấn Đạt

Email: [email protected]
(2)

Stacks

Basic stack operations

Linked-list implementation

Stack applications

(3)

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 operations are performed only at the ends of the list

(4)

Stack

All insertions and deletions are restricted to one end called the top

(5)

Basic Stack Operations

Top Push Data Top Stack Top Stack
(6)

Basic Stack Operations

Top

Overflow

Data

(7)

Basic Stack Operations

Pop Data Top Stack Top Stack
(8)

Basic Stack Operations

Underflow

Top

(9)

Basic Stack Operations

Top Top Stack Top Data Top Stack Top Stack
(10)

Linked-List Implementation

Top Stack structure top 5 Conceptual Physical
(11)

Linked-List Implementation

Stack

structure count top

stack

count <integer>

top <node pointer>

end stack end stack

Stack node

structure data next

node

data <dataType>

next <node pointer>

(12)

Linked-List Implementation

template <class ItemType>

struct Node {

ItemType data;

Node<ItemType> *next; };

template <class List_ItemType>

class Stack { public:

Stack(); ~Stack();

(13)

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(); Stack<List_ItemType>* Clone(); void Print2Console(); private: Node<List_ItemType>* top; int count; };

(14)

Create Stack

Before After

0

count top

? ?

count top count top

(no stack) (empty stack)

(15)

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

1 stack.count = 0

2 stack.top = null

3 return

(16)

Linked-List Implementation

template <class List_ItemType>

Stack<List_ItemType>::Stack(){ this->top = NULL;

this->count = 0; }

template <class List_ItemType>

template <class List_ItemType> Stack<List_ItemType>::~Stack(){

this->Clear(); }

(17)

Push Stack

Before red data next After red data next stack stack pNew pNew 2 count top blue data next green data next 3 count top blue data next green data next
(18)

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

data contains data to be pushed into stack

Post data have been pushed in stack

(19)

Push Stack

1 if (stack full)

1 success = false

2 else

1 allocate (pNew)

2 pNew -> data = data

3 pNew -> next = stack.top

Before 2 blue red data next stack pNew

3 pNew -> next = stack.top

4 stack.top = pNew 5 stack.count = stack.count + 1 6 success = true 3 return success 2 count top blue data next green data next

(20)

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->data = value;

pNew->next = top;

this->top = pNew;

this->count++;

}

(21)

Pop Stack

recycled Before red data next dltPtr After data next dltPtr stack stack 3 count top blue data next green 2 count top blue data next green
(22)

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

dataOut is to receive the popped data

Post data have been returned to caller

(23)

Pop Stack

1 if (stack empty)

1 success = false

2 else

1 dltPtr = stack.top

2 dataOut = stack.top -> data

3 stack.top = stack.top -> next

Before

red

data next dltPtr

stack 3 stack.top = stack.top -> next

4 stack.count = stack.count - 1 5 recycle (dltPtr) 6 success = true 3 return success 3 count top blue data next green

(24)

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; dataOut = this->top->data; this->top = this->top->next; this->count--; delete dltPtr; return 1; }

(25)

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

dataOut is to receive top stack data

Post data have been returned to caller

(26)

Stack Top

1 if (stack empty)

1 success = false

2 else

1 dataOut = stack.top -> data

2 success = true

3 return success

3 return success

(27)

Stack Top

template

<

class

List_ItemType>

int Stack<List_ItemType>::GetStackTop

(List_ItemType

&

dataOut){

if (count == 0)

return 0;

dataOut = this->top->data;

dataOut = this->top->data;

return 1;

}

(28)

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 loop (stack.top not null)

1 temp = stack.top

2 stack.top = stack.top -> next

3 recycle (temp)

(29)

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;

this->top = this->top->next;

delete temp;

}

this->count = 0;

}

(30)

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;

return count; }

(31)

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"); }

(32)

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->Push(8); myStack->Print2Console(); myStack->Pop(val); myStack->Print2Console(); delete myStack; return 0;

(33)

Exercises

template

<

class

List_ItemType>

Stack<List_ItemType>

*

Stack<List_ItemType>::Clone() {

// ...

References

Related documents

Understanding the need for balanced backup and recovery solutions, Oracle offers optimized architectures to protect business information stored in Oracle’s engineered

The Hybrid model combines features of Pools and Bilateral Contracts (Shahidehpour et al., 2002). In this model, a Pool isn’t mandatory, and customers can either negotiate a

Stacks: Abstract Data Type, Primitive Stack operations: Push &amp; Pop, Array and Linked Implementation of Stack in C, Application of stack: Prefix and Postfix

Transition to the next state, depending upon what the next symbol in the input is, and what the top of the stack is, and (potentially) push more symbols on the top of the stack.

The main goal of the present work was to reveal how the presence of L-alanine (L-Ala), L- phenylalanine (L-Phe) and L-tryptophan (L-Trp) amino acids modified the micellization

Se foi no mundo supra - sensível que até então os valores encontraram legitimidade, trata-se agora de suprimir o solo mesmo a partir do qual eles foram colo- cados, para então

• Data mined using SQL queries as well as large-scale advanced analytics (e.g., machine learning, graph analysis, etc.).. • Interactive and iterative computations

In the Next-Generation Green Data Center, this concept is extended to configurations of basic equipment like servers and storage units with the aim of generating various types