2016-06-22 1 views
-2

이 코드의 주 방법을 고안하는 데 문제가 있습니다. 누구든지 도움이 될 수 있습니까?이 C++ 링크 된 목록/푸시/팝 프로그램의 기본 메소드

#include "stdafx.h" 
#include <cstdlib> 
#include<iostream> 
#include <queue> 
#include <stack> 
using namespace std; 


int main() 
{ 

return 0; 
} 




struct node { 
     int data; 
     node* next; 
    }; 

    node*head; 
node*tail; 

void push(node *& head, node *&tail, int data) { 
    if (head == NULL) { 
     node* n = new node; 
     n->data = data; 
     n->next = NULL; 
     head = n; 
     tail = n; 

    } 
    else if (head != NULL) { 
     node* n = new node; 
     n->data = data; 
     n->next = head; 
     head = n; 
    } 
} 

void showdata(node *& head) { 
    node* temp = new node; 
    temp = head; 
    if (temp == NULL) { 
     cout << "Empty" << endl; 
    } 
    else { 
     cout << "element: " << endl; 
     while (temp != NULL) { 
      cout << temp->data << endl; 
      temp = temp->next; 
     } 
    } 
} 

void pop(node *&head, node *& tail) { 
    if (head == NULL) { 
     cout << "Empty" << endl; 
    } 
    else if (head == tail) { 
     cout << "value: " << head->data << " was popped" << endl; 
     delete head; 
     head = NULL; 
     tail = NULL; 
    } 
    else { 
     node* delptr = new node; 
     delptr = head; 
     head = head->next; 
     cout << "value: " << delptr->data << " was popped" << endl; 
     delete delptr; 
    } 




} 

프로그램 출력/정의 실제 스택을 시작하는 방법입니다 함께

Initialise stack (max size = 10) 

Write (“Testing Stack”) 

showstack() 

for (counter = 0, counter < MaxSize) 

    push(counter) 

showstack() 

for (counter = 0, counter < MaxSize) 



write(pop()) 

중요한 것은

난 정말 문제가 있어요 ... 다음 발판을 따라야합니다. 어떤 도움이 많이 도움이 될 것입니다

기본적으로 스택을 만들려고합니다. 숫자 1-10을 눌러 스택을 표시 한 다음 끕니다. 필자는 메소드에 대한 코드를 가지고 있지만 실제로 푸시 할 스택/스택을 정의/선언하는 방법을 알아낼 수 없습니다.

+0

나는 단서가 없다 당신이 실제로 요구하는 것은 무엇입니까? –

+0

질문을 정리해 주시겠습니까 – Prasheel

+0

죄송합니다. 끝까지 더 잘 못알았습니다.하지만 나는 그걸 잘라 버린 것 같아요. 기본적으로 스택을 만들고, 1 ~ 10의 숫자를 푸시하고, 스택을 표시 한 다음 튕겨 내려고합니다. . 필자는 메서드에 대한 코드를 얻었지만 실제로 스택을 정의하거나 선언 할 방법을 알아 내지 못했습니다. – weeurey

답변

0

이렇게하는 방법은 여러 가지가 있습니다. 아마도 더 넓은 사용법에 적응할 수있는 가장 간단한 접근법은 스택을 클래스에 캡슐화하는 것입니다. 이 implentation가없는 한 상속을 구현해야, 표준 스택 클래스의 필요가 없기 때문에 내가 헤더 라이브러리의 포함을 제거했습니다

#include<iostream> 
// discarded the other headers. They are noise in this example. 
using namespace std; // avoid using this at file scope. It can result in many 
        // interesting and hard-to-find errors. 

//define a stack class 
class stack 
{ 
private: 
    // put the rest of your code in the stack class 
    struct node 
    { 
     int data; 
     node* next; 
    }; 

    node*head; 
    node*tail; 
    // need the following if the stack is to have and enforce a maximum size 
    int size; // how big is the stack currently? 
    int maxsize; // how big can the stack get? 

public: 
    // add a constructor and a destructor 
    stack(int maxsize): 
     head(NULL), // make sure head is NULL before it's used 
     tail(NULL), // make sure tail is NULL before it's used 
     size(0), // obviously nothing in the stack yet 
     maxsize(maxsize) // store the provided maximum stack size for later 
    { 
     // does nothing. All of the initialization is done in the member initializer list 
    } 
    ~stack() 
    { 
     // delete all of the nodes. 
     // leaving this up to OP, but one way is to add a destructor to node and 
     // allowing node to take care of the heavy lifting 
    } 
    void push(int data) 
    { 
     // add code here to test if size is less than maxsize before adding 
     if (head == NULL) 
     { 
      node* n = new node; 
      n->data = data; 
      n->next = NULL; 
      head = n; 
      tail = n; 

     } 
     else if (head != NULL) 
     { 
      node* n = new node; 
      n->data = data; 
      n->next = head; 
      head = n; 
     } 
     // don't forget to increment size 
    } 

    void showdata() 
    { 
     node* temp = new node; // why create a node... 
     temp = head;   // just to leak it by overwriting the pointer? 
     // use node * temp = head; instead. 
     if (temp == NULL) 
     { 
      cout << "Empty" << endl; 
     } 
     else 
     { 
      cout << "element: " << endl; 
      while (temp != NULL) 
      { 
       cout << temp->data << endl; 
       temp = temp->next; 
      } 
     } 
    } 

    void pop() // should return the value popped, not void 
    { 
     if (head == NULL) 
     { 
      cout << "Empty" << endl; 
     } 
     else if (head == tail) 
     { 
      cout << "value: " << head->data << " was popped" << endl; 
      delete head; 
      head = NULL; 
      tail = NULL; 
     } 
     else 
     { 
      node* delptr = new node; 
      delptr = head; 

      // same as last time: node* delptr = head; 
      head = head->next; 
      cout << "value: " << delptr->data << " was popped" << endl; 
      delete delptr; 
     } 
    } 
}; 
int main() 
{ 

    // Initialise stack (max size = 10) 
    stack teststack(10); // currently the code has no way to enforce the maximum stack size 

    //Write (“Testing Stack”) 
    cout << "Testing Stack" << endl; 

    //showstack() 
    teststack.showdata(); 

    //for (counter = 0, counter < MaxSize) 
    // leaving figuring out how to write a correct for loop to OP. 

    // push(counter) 
    teststack.push(1); 
    teststack.push(2); 

    //showstack() 
    teststack.showdata(); 

    //for (counter = 0, counter < MaxSize) 
    // same as above. 

    // write(pop()) 
    cout << teststack.pop() << endl; // currently not possible. Pop returns void 
    cout << teststack.pop() << endl; // currently not possible. Pop returns void 
} 
0

...

#include "stdafx.h" 
#include <cstdlib> 
#include<iostream> 
#include <queue> 
using namespace std; 

const int max_size = 10; 


class Stack{ 

public: 

    Stack(int max_size); 

    int max_size; 

    void Add(int data); 
    int Extract(); 

    void Show(); 

private: 

    struct node { 
     int data; 
     node* next; 
    }; 

    node*head; 
    node*tail; 

    void push(node *& head, node *&tail, int data) { 
     if (head == NULL) { 
      node* n = new node; 
      n->data = data; 
      n->next = NULL; 
      head = n; 
      tail = n; 

     } 
     else if (head != NULL) { 
      node* n = new node; 
      n->data = data; 
      n->next = head; 
      head = n; 
     } 
    } 

    void showdata(node *& head) { 
     node* temp = new node; 
     temp = head; 
     if (temp == NULL) { 
      cout << "Empty" << endl; 
     } 
     else { 
      cout << "element: " << endl; 
      while (temp != NULL) { 
       cout << temp->data << endl; 
       temp = temp->next; 
      } 
     } 
    } 

    void pop(node *&head, node *& tail) { 
     if (head == NULL) { 
      cout << "Empty" << endl; 
     } 
     else if (head == tail) { 
      cout << "value: " << head->data << " was popped" << endl; 
      delete head; 
      head = NULL; 
      tail = NULL; 
     } 
     else { 
      node* delptr = new node; 
      delptr = head; 
      head = head->next; 
      cout << "value: " << delptr->data << " was popped" << endl; 
      delete delptr; 
     } 

    } 
}; 

Stack::Stack(int _max_size) 
{ 
    max_size = _max_size; 
    head = NULL; 
    tail = NULL; 
} 



void Stack::Add(int data) 
{ 
    push(head, tail, data); 
} 

int Stack::Extract() 
{ 

    int poppedvalue = head->data; 
    pop(head, tail); 

    return poppedvalue; 
} 

void Stack::Show() 
{ 
    showdata(head); 
} 



int main() 
{ 
    //intialize 
    Stack stack(max_size); 

    cout << "Testing Stack" << endl;  

    stack.Show(); 

    for (int i = 0; i < stack.max_size; i++) 
     stack.Add(i); 

    stack.Show(); 

    for (int i = 0; i < stack.max_size; i++) 
     cout << stack.Extract() << endl; 

    return 0; 
}