티스토리 뷰

기존에 CLinkedList.c 와 CLinkedList.h를 이용하여

스택을 구현해보았다.


(*여기서 SPush할때 데이터를 머리에 추가가아니라 꼬리에 추가 CLinkedList::LInsert(data);를 하면 큐 구조가 되는거 같음)


ListBaseStack.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef LISTBASESTACK_H
#define LISTBASESTACK_H
 
#include "Node.h"
#include "CLinkedList.h"
 
class ListStack : public CLinkedList
{
private:
    Node *tail;
public:
    bool SIsEmpty();
 
    void SPush(LData data);
    LData SPop();
    LData Peek();
};
#endif // LISTBASESTACK_H

cs



ListBaseStack.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "ListBaseStack.h"
#include "CLinkedList.h"
#include <iostream>
 
bool ListStack::SIsEmpty()
{
    if(CLinkedList::LCount() == 0)
        return true;
    else
        return false;
}
 
void ListStack::SPush(LData data)
{
    CLinkedList::LInsertFront(data);
}
 
LData ListStack::SPop()
{
    LData data;
    CLinkedList::LFirst(&data);
    return CLinkedList::LRemove();
}
 
LData ListStack::Peek()
{
    return tail->getdata();
}
 
cs



ListBaseStackMain.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include"ListBaseStack.h"
#include<iostream>
 
using namespace std;
 
int main()
{
    ListStack Stack;
 
 
    Stack.SPush(1);
    Stack.SPush(2);
    Stack.SPush(3);
    Stack.SPush(4);
    Stack.SPush(5);
    
    while (!Stack.SIsEmpty())
    {
        cout << Stack.SPop() << endl;
    }
    return 0;
}
cs


Comments