이중 연결 목록을 사용하여 스택을 구현하려고합니다. 내 스택 클래스 (push, pop) 함수는 이중 링크 목록 클래스의 멤버 함수에 대한 호출을 포함해야하지만 실제로 구현하는 데 문제가 있다는 것을 알고 있습니다.이중 연결 목록을 사용하는 C++ 스택
dlist.cpp :
#include <iostream>
#include <fstream>
#include <string>
#include "dlist.hpp"
using namespace std;
void dlist::appendNodeFront(int shares, float pps){
Node *n = new Node(shares, pps);
if(front == NULL){
front = n;
back = n;
}
else {
front->prev = n;
n->next = front;
front = n;
}
}
void dlist::appendNodeBack(int shares, float pps){
Node *n = new Node(shares, pps);
if(back == NULL){
front = n;
back = n;
}
else {
back->next = n;
n->prev = back;
back = n;
}
}
void dlist::display(){
Node *temp = front;
cout << "List contents: ";
while(temp != NULL){
cout << temp->value << " ";
temp = temp->next;
}
cout << endl;
}
void dlist::display_reverse(){
Node *temp = back;
cout << "List contents in reverse: ";
while(temp != NULL){
cout << temp->value << " ";
temp = temp->prev;
}
cout << endl;
}
void dlist::destroyList(){
Node *T = back;
while(T != NULL){
Node *T2 = T;
T = T->prev;
delete T2;
}
front = NULL;
back = NULL;
}
stack.cpp :
이#include <iostream>
#include <fstream>
#include <string>
#include "stack.hpp"
using namespace std;
stack::stack(){
int i;
for(i = 0; i < 1500; i++){
shares[i] = 0;
pps[i] = 0;
}
first = 0;
}
void stack::push(int num, float price){
if(first ==(1500-1)){
cout << "Stack is full" << endl;
return;
}
first++;
shares[first] = num;
pps[first] = price;
return;
}
void stack::pop(int *num, float *price){
if(first == -1){
cout << "Stack is empty" << endl;
return;
}
num = &shares[first];
price = &pps[first];
cout << shares[first] << endl;
cout << pps[first] << endl;
shares[first] = 0;
pps[first] = 0;
first--;
return;
}
스택에 푸시 기능은 기본적으로 appendNodeFront()
또는 appendNodeback()
를 호출해야 하는가? 어떤 도움이나 조언을 크게 주시면 감사하겠습니다!
밀어 일부 구성원을 변경할 수 있습니다 appendNodeback 때문에() 전화 할 수있는 올바른 기능입니다. pop 연산을 위해 스택에 삽입 된 마지막 요소를 제거해야하므로 removeLastNode() 함수를 구현해야합니다. – StaticBeagle
이러한 기능에는 여러 가지 책임이있는 것으로 보입니다. 예를 들어 노드를 만들고 어딘가에 추가합니다. 책임을 분리 할 때 상황이 더 간단해질 수 있습니다. 이 개념에도 [자체 Wikipedia 페이지] (https://en.wikipedia.org/wiki/Single_responsibility_principle)가 있습니다! 우와. –
두 가지 시나리오 : 머리에서 머리/팝을 누르거나 꼬리에서 꼬리/팝을 누릅니다. 요점은 삽입 된 마지막 요소가 제거 된 첫 번째 요소 (LIFO) 여야한다는 것입니다. – 0x499602D2