push(object)
: inserts an elementpop()
: removes the last inserted elementtop()
: returns the last inserted element without removing itsize()
: returns the number of element sstoredempty()
: indicates whether no elements are storedμ€ν κ°μ²΄λ₯Ό μ μΈνκΈ° μν΄μλ βstackβμ΄λΌκ³ λΆλ¦¬λ μ μ νμΌμ λ¨Όμ ν¬ν¨ν΄μΌ νλ€. STL λ²‘ν° ν΄λμ€μ λ§μ°¬κ°μ§λ‘ μ€ν ν΄λμ€λ std λ€μμ€νμ΄μ€μ ν¬ν¨λκΈ° λλ¬Έμ std::stack
μΌλ‘ μ¬μ©νλμ§ using
λ¬Έμ μ¬μ©ν΄μΌ νλ€. μλ₯Ό λ€μ΄, λ€μ νλ‘κ·Έλ¨μ μ μμ μ€νμ μ μΈνλ€.
#include <stack>
using std::stack // stackμ μ κ·Όν μ μκ² νλ€.
stack<int> myStack; // μ μμ μ€ν
size()
: μ€ν λ΄μ κ°μ²΄μ κ°μλ₯Ό λ°ννλ€.empty()
: μ€νμ΄ λΉμ΄ μμΌλ©΄ μ°Έμ, κ·Έλ μ§ μμΌλ©΄ κ±°μ§μ λ°ννλ€.push(e)
: κ°μ²΄ eλ₯Ό μ€νμ μ΅μμμ μ½μ
νλ€.pop()
: μ€νμ μ΅μμμ μλ κ°μ²΄λ₯Ό μ€νμμ μ κ±°νλ€.top()
: μ€νμ μ΅μμ μμμ λ νΌλ°μ€λ₯Ό λ°ννλ€.μ€ν μ°μ°μ μ°λ¦¬ μ μμ STL ꡬνμ μ€μν μ°¨μ΄κ° μλ€. STL ꡬνμλ μ€μν μ°¨μ΄κ° μμ. STL ꡬνμλ λΉ μ€νμ λν top
λλ pop
μ°μ°μ κ²°κ³Όκ° μ μλμ΄ μμ§ μλ€.
μ¦, μ€λ₯(exception)κ° λ°μλμ§ μλλ€. μ€λ₯κ° λ°μλμ§ μλλΌλ, λΉμ μ νλ‘κ·Έλ¨μ΄ μ€λ¨λ κ°λ₯μ±μ΄ λλ€. λ°λΌμ νλ‘κ·Έλλ¨Έκ° κ·Έλ¬ν λΆλ²μ μΈ μ κ·Όμ νμ§ μλ κ²μ΄ νμνλ€.
StackEmpty
// An informal Stack interface(not a complete C++ class)
template <typename E>
class Stack
{
public:
int size() const;
bool empty() const;
const E &top() const throw(StackEmpty);
void push(const E &e);
void pop() throw(StackEmpty);
}
#include <iostream>
#include <string>
using namespace std;
// Code Fragment 2.4: RuntimeException
class RuntimeException
{ // generic run-time exception
private:
string errorMsg;
public:
RuntimeException(const string &err) { errorMsg = err; }
string getMessage() const { return errorMsg; }
};
// Code Fragment 5.2: Exception thrown by functions pop and top when called on an empty stack.
// Exception thrown on performing top or pop of an empty stack.
class StackEmpty : public RuntimeException
{
public:
StackEmpty(const string &err) : RuntimeException(err) {}
};
class StackFull : public RuntimeException
{
public:
StackFull(const string &err) : RuntimeException(err) {}
};
// Code Fragment 5.1: An informal Stack interface (not a complete C++ class).
template <typename E>
class Stack
{ // an interface for a stack
public:
int size() const; // number of items in stack
bool empty() const; // is the stack empty?
const E &top() const throw(StackEmpty); // the top element
void push(const E &e); // push x onto the stack
void pop() throw(StackEmpty); // remove the top element
};
// Code Fragment 5.4 : The class ArrayStack, which implements the Stack interface.
template <typename E>
class ArrayStack
{
enum
{
DEF_CAPACITY = 100
}; // default stack capacity
public:
ArrayStack(int cap = DEF_CAPACITY); // constructor from capacity
int size() const; // number of items in the stack
bool empty() const; // is the stack empty?
const E &top() const throw(StackEmpty); // get the top element
void push(const E &e) throw(StackFull); // push element onto stack
void pop() throw(StackEmpty); // pop the stack
// . . .housekeeping functions omitted
private: // member data
E *S; // array of stack elements
int capacity; // stack capacity
int t; // index of the top of the stack
};
// Code Fragment 5.5: Implementations of the member functions of class ArrayStack (excluding housekeeping functions).
template <typename E>
ArrayStack<E>::ArrayStack(int cap)
: S(new E[cap]), capacity(cap), t(-1) {} // constructor from capacity
template <typename E>
int ArrayStack<E>::size() const
{
return (t + 1);
} // number of items in the stack
template <typename E>
bool ArrayStack<E>::empty() const
{
return (t < 0);
} // is the stack empty?
template <typename E> // return top of stack
const E &ArrayStack<E>::top() const throw(StackEmpty)
{
if (empty())
throw StackEmpty("Top of empty stack");
return S[t];
}
template <typename E> // push element onto the stack
void ArrayStack<E>::push(const E &e) throw(StackFull)
{
if (size() == capacity)
throw StackFull("Push to full stack");
S[++t] = e;
}
template <typename E> // pop the stack
void ArrayStack<E>::pop() throw(StackEmpty)
{
if (empty())
throw StackEmpty("Pop from empty stack");
--t;
}
int main()
{
// Code Fragment 5.6 : An example of the use of the ArrayStack class.
// The contents of the stack are shown in the comment following the operation.
// The top of the stack is indicated by an asterisk (β*β).
ArrayStack<int> A; // A = [ ], size = 0
A.push(7); // A = [7*], size = 1
A.push(13); // A = [7, 13*], size = 2
cout << A.top() << endl;
A.pop(); // A = [7*], outputs: 13
A.push(9); // A = [7, 9*], size = 2
cout << A.top() << endl; // A = [7, 9*], outputs: 9
cout << A.top() << endl;
A.pop(); // A = [7*], outputs: 9
ArrayStack<string> B(10); // B = [ ], size = 0
B.push("Bob"); // B = [Bob*], size = 1
B.push("Alice"); // B = [Bob, Alice*], size = 2
cout << B.top() << endl;
B.pop(); // B = [Bob*], outputs: Alice
B.push("Eve"); // B = [Bob, Eve*], size = 2
}
SinglyLinkedList.h
#include <string>
using std::string;
// Code Fragment 3.18: A node in a generic singly linked list.
template <typename E>
class SNode
{ // singly linked list node
private:
E elem; // linked list element value
SNode<E> *next; // next item in the list
friend class SLinkedList<E>; // provide SLinkedList access
};
// Code Fragment 3.19: A class definition for a generic singly linked list.
template <typename E>
class SLinkedList
{ // a singly linked list
public:
SLinkedList(); // empty list constructor
~SLinkedList(); // destructor
bool empty() const; // is list empty?
const E &front() const; // return front element
void addFront(const E &e); // add to front of list
void removeFront(); // remove front item list
private:
SNode<E> *head; // head of the list
};
// Code Fragment 3.20: Other member functions for a generic singly linked list.
template <typename E>
SLinkedList<E>::SLinkedList() // constructor
: head(NULL)
{
}
template <typename E>
bool SLinkedList<E>::empty() const // is list empty?
{
return head == NULL;
}
template <typename E>
const E &SLinkedList<E>::front() const // return front element
{
return head->elem;
}
template <typename E>
SLinkedList<E>::~SLinkedList() // destructor
{
while (!empty())
removeFront();
}
template <typename E>
void SLinkedList<E>::addFront(const E &e)
{ // add to front of list
SNode<E> *v = new SNode<E>; // create new node
v->elem = e; // store data
v->next = head; // head now follows v
head = v; // v is now the head
}
template <typename E>
void SLinkedList<E>::removeFront()
{ // remove front item
SNode<E> *old = head; // save current head
head = old->next; // skip over old head
delete old; // delete the old head
}
Stack_with_a_Singly-linked_List.cpp
#include <iostream>
#include <string>
#include "SinglyLinkedList.h"
using namespace std;
// Code Fragment 2.4: RuntimeException
class RuntimeException
{ // generic run-time exception
private:
string errorMsg;
public:
RuntimeException(const string &err) { errorMsg = err; }
string getMessage() const { return errorMsg; }
};
// Code Fragment 5.2: Exception thrown by functions pop and top when called on an empty stack.
// Exception thrown on performing top or pop of an empty stack.
class StackEmpty : public RuntimeException
{
public:
StackEmpty(const string &err) : RuntimeException(err) {}
};
class StackFull : public RuntimeException
{
public:
StackFull(const string &err) : RuntimeException(err) {}
};
κ΄νΈκ²μ¬ μ€νμ μ¬μ©νμ¬ μμμ κ΄νΈ μ¬μ©μ΄ μ¬λ°λ₯Έμ§ νμΈν μ μλ€. κ΄νΈμλ μΈ μ’ λ₯κ° μλ€ β()β, β{}β, β[]β μ¬λ κ΄νΈλ₯Ό μΌμͺ½, λ«λ κ΄νΈλ₯Ό μ€λ₯Έμͺ½μ΄λΌ νννκ² λ€.
μ¬λ°λ₯Έ κ΄νΈμ μ¬μ©μ λ€μκ³Ό κ°μ 쑰건μ κ°μ§λ€:
μκ³ λ¦¬μ¦:
λ€μμ μ μκ³ λ¦¬μ¦μ ꡬνμ΄λ€:
bool ParenMatch(char X[])
{
int i = 0; prev;
char ch;
while(X[i] != '\0')
{
ch = X[i++];;
if(ch == '[' || ch =='{' || ch == '(') push(ch);
else if (ch == ']' || ch == '}' || ch ++ ')')
{
if(empty()) return false;
prev = top();
pop();
if((ch == ']' && prev != '[')
|| (ch == '}' && prev != '{')
|| (ch == ')' && prev != '('))
return false;
}
}
if (empty()) return false;
return true;
}