2016-06-22 32 views
-2

Ich habe Probleme, eine Hauptmethode für diesen Code zu entwickeln. Kann jemand helfen?Hauptmethode für diese C++ Verknüpfte Liste/Push/Pop-Programm

#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; 
    } 




} 

Die Programmausgabe sollte folgendes Gerüst folgen ...

Initialise stack (max size = 10) 

Write (“Testing Stack”) 

showstack() 

for (counter = 0, counter < MaxSize) 

    push(counter) 

showstack() 

for (counter = 0, counter < MaxSize) 



write(pop()) 

Die Hauptsache ich bin wirklich ein Problem mit mit ist wie/zu definieren, die tatsächlichen Stapel einzuleiten. Jede Hilfe würde viel helfen

Grundsätzlich Im versuchen, einen Stapel zu erstellen, drücken Sie Zahlen 1-10 darauf, zeigen Sie den Stapel und dann pop off es. Ich habe den Code für die Methoden, aber kann nicht herausfinden, wie man den tatsächlichen Stapel definieren/deklarieren, den ich von

+0

Ich habe keine Ahnung worum fragst du eigentlich? –

+0

Könnten Sie bitte Ihre Frage klären – Prasheel

+0

Sorry Jungs Ich habe richtig mehr am Ende, aber ich denke, es abgeschnitten ... Im Prinzip versuche ich, einen Stapel zu erstellen, drücken Sie Zahlen 1-10, zeigen Sie den Stapel und dann ab . Ich habe den Code für die Methoden, aber kann nicht herausfinden, wie man den tatsächlichen Stapel definiert/deklariert, den ich von – weeurey

Antwort

0

schieben/knallen werde. Es gibt viele, viele Möglichkeiten, dies zu tun. Der wahrscheinlich einfachste Ansatz, der eine Anpassung an eine breitere Verwendung ermöglicht, besteht darin, den Stapel in einer Klasse zu kapseln.

#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

ich die Einbeziehung der Header-Bibliothek entfernt haben, weil diese implentation nicht der Standard-Stack-Klasse benötigt, es sei denn, dass Sie die Vererbung implementieren müssen ...

#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; 
}