Skip to content

Commit

Permalink
[Stack] Add print()
Browse files Browse the repository at this point in the history
  • Loading branch information
deunlee committed May 31, 2020
1 parent 1837e1d commit f19f548
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Stack/ArrayStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ namespace Deun {
*/
const T& peek();

/**
* 스택을 출력합니다.
*/
void print();

/**
* 스택을 초기화합니다.
* 모든 원소가 삭제됩니다.
Expand Down
18 changes: 18 additions & 0 deletions Stack/ArrayStack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ namespace Deun {
return array[count - 1];
}

template <typename T>
void ArrayStack<T>::print() {
using namespace std;
cout << "ArrayStack(size=" << size << ", count=" << count << ")" << endl;
if (!count) {
cout << "(empty)" << endl;
return;
}
cout << "(top) ";
for (int i = count - 1; i >= 0; i--) {
cout << array[i];
if (i) {
cout << " / ";
}
}
cout << endl;
}

template <typename T>
void ArrayStack<T>::clear() {
count = 0;
Expand Down
5 changes: 5 additions & 0 deletions Stack/LinkedListStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ namespace Deun {
*/
const T& peek();

/**
* 스택을 출력합니다.
*/
void print();

/**
* 스택을 초기화합니다.
* 모든 원소가 삭제됩니다.
Expand Down
27 changes: 22 additions & 5 deletions Stack/LinkedListStack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,31 @@ namespace Deun {
return head->data;
}

template <typename T>
void LinkedListStack<T>::print() {
using namespace std;
cout << "LinkedListStack(size=inf, count=" << count << ")" << endl;
if (!head) {
cout << "(empty)" << endl;
return;
}
StackNode<T>* current = head;
cout << "(head)";
while (current) {
cout << " -> " << current->data;
current = current->next;
}
cout << endl;
}

template <typename T>
void LinkedListStack<T>::clear() {
StackNode<T>* front = head;
StackNode<T>* current = head;
StackNode<T>* next;
while (front) {
next = front->next;
delete front;
front = next;
while (current) {
next = current->next;
delete current;
current = next;
}
head = nullptr;
count = 0;
Expand Down
3 changes: 3 additions & 0 deletions Stack/Stack.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __DEUN_STACK_HPP__
#define __DEUN_STACK_HPP__

#include <iostream>
#include <new>

namespace Deun {
Expand All @@ -22,6 +23,8 @@ namespace Deun {
virtual bool push(const T& element) = 0;
virtual T pop() = 0;
virtual const T& peek() = 0;

virtual void print() = 0;
virtual void clear() = 0;
};

Expand Down

0 comments on commit f19f548

Please sign in to comment.