110 lines
2.2 KiB
C++
110 lines
2.2 KiB
C++
|
/**
|
|||
|
* File: linkedlist_stack.cpp
|
|||
|
* Created Time: 2022-11-28
|
|||
|
* Author: qualifier1024 (2539244001@qq.com)
|
|||
|
*/
|
|||
|
|
|||
|
#include "../utils/common.hpp"
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ֵ<EFBFBD>ջ */
|
|||
|
class LinkedListStack {
|
|||
|
private:
|
|||
|
ListNode *stackTop; // <20><>ͷ<EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>Ϊջ<CEAA><D5BB>
|
|||
|
int stkSize; // ջ<>ij<EFBFBD><C4B3><EFBFBD>
|
|||
|
|
|||
|
public:
|
|||
|
LinkedListStack() {
|
|||
|
stackTop = nullptr;
|
|||
|
stkSize = 0;
|
|||
|
}
|
|||
|
|
|||
|
~LinkedListStack() {
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD>ڵ㣬<DAB5>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
|
|||
|
freeMemoryLinkedList(stackTop);
|
|||
|
}
|
|||
|
|
|||
|
/* <20><>ȡջ<C8A1>ij<EFBFBD><C4B3><EFBFBD> */
|
|||
|
int size() {
|
|||
|
return stkSize;
|
|||
|
}
|
|||
|
|
|||
|
/* <20>ж<EFBFBD>ջ<EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
|
|||
|
bool isEmpty() {
|
|||
|
return size() == 0;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><>ջ */
|
|||
|
void push(int num) {
|
|||
|
ListNode *node = new ListNode(num);
|
|||
|
node->next = stackTop;
|
|||
|
stackTop = node;
|
|||
|
stkSize++;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><>ջ */
|
|||
|
int pop() {
|
|||
|
int num = top();
|
|||
|
ListNode *tmp = stackTop;
|
|||
|
stackTop = stackTop->next;
|
|||
|
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
|
|||
|
delete tmp;
|
|||
|
stkSize--;
|
|||
|
return num;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>Ԫ<EFBFBD><D4AA> */
|
|||
|
int top() {
|
|||
|
if (isEmpty())
|
|||
|
throw out_of_range("ջΪ<EFBFBD><EFBFBD>");
|
|||
|
return stackTop->val;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><> List ת<><D7AA>Ϊ Array <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
vector<int> toVector() {
|
|||
|
ListNode *node = stackTop;
|
|||
|
vector<int> res(size());
|
|||
|
for (int i = res.size() - 1; i >= 0; i--) {
|
|||
|
res[i] = node->val;
|
|||
|
node = node->next;
|
|||
|
}
|
|||
|
return res;
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
/* Driver Code */
|
|||
|
int main() {
|
|||
|
/* <20><>ʼ<EFBFBD><CABC>ջ */
|
|||
|
LinkedListStack *stack = new LinkedListStack();
|
|||
|
|
|||
|
/* Ԫ<><D4AA><EFBFBD><EFBFBD>ջ */
|
|||
|
stack->push(1);
|
|||
|
stack->push(3);
|
|||
|
stack->push(2);
|
|||
|
stack->push(5);
|
|||
|
stack->push(4);
|
|||
|
cout << "ջ stack = ";
|
|||
|
printVector(stack->toVector());
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>Ԫ<EFBFBD><D4AA> */
|
|||
|
int top = stack->top();
|
|||
|
cout << "ջ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> top = " << top << endl;
|
|||
|
|
|||
|
/* Ԫ<>س<EFBFBD>ջ */
|
|||
|
top = stack->pop();
|
|||
|
cout << "<EFBFBD><EFBFBD>ջԪ<EFBFBD><EFBFBD> pop = " << top << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD> stack = ";
|
|||
|
printVector(stack->toVector());
|
|||
|
|
|||
|
/* <20><>ȡջ<C8A1>ij<EFBFBD><C4B3><EFBFBD> */
|
|||
|
int size = stack->size();
|
|||
|
cout << "ջ<EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD> size = " << size << endl;
|
|||
|
|
|||
|
/* <20>ж<EFBFBD><D0B6>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
|
|||
|
bool empty = stack->isEmpty();
|
|||
|
cout << "ջ<EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><EFBFBD> = " << empty << endl;
|
|||
|
|
|||
|
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
|
|||
|
delete stack;
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|