此C ++链接列表/推送/弹出程序的主要方法

时间:2016-06-22 20:28:02

标签: c++ stack push pop

我无法为此代码设计主要方法。有人可以帮忙吗?

#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推入其中,显示堆栈然后将其弹出。我已经获得了方法的代码但是无法弄清楚如何定义/声明我将推送/弹出的实际堆栈

2 个答案:

答案 0 :(得分:0)

有许多方法可以做到这一点。可能允许适应更广泛使用的最简单方法是将堆栈封装在一个类中。

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

答案 1 :(得分:0)

我已删除了包含头库,因为此实现不需要标准堆栈类,除非您必须实现继承......

 class Vehicle(SoftDeleteModel):
    routes = models.ManyToManyField('RouteBoundary', 
                                     through='VehicleBoundaryMapProxy', 
                                     verbose_name=_('routes'))