vendredi 24 juin 2016

Main method for this C++ Linked list/push/pop program


I'm having trouble devising a main method for this code. Can anyone be of assistance?

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




}

The program output should follow the following scaffold...

Initialise stack ( max size = 10 )

Write (“Testing Stack”)

showstack()

for ( counter = 0, counter < MaxSize)

   push(counter)

showstack()

for ( counter = 0, counter < MaxSize)



write(pop())

The main thing I'm really having a problem with is how to define/initiate the actual stack. Any help would help alot

Basically Im trying to create a stack, push numbers 1-10 onto it, display the stack and then pop it off. Ive got the code for the methods but cant figure out how to define/declare the actual stack that I will be pushing to/popping from


Aucun commentaire:

Enregistrer un commentaire