0

I'm trying to implement a stack using a linked list and class template. I'm not getting any compiler errors but my logic may be wrong, kind of lost. I had a working program in just a single file only using a struct so I had difficulties with translating it over using multiple files and template classes. I will also include my single file cpp below, hopefully it helps. Any help will be greatly appreciated. I have a header.h file, functions.cpp and main. cpp.

Header.h

#define STACK_H
#include <iostream>

using namespace std;

//to implement a stack using linked list
template<class T>
class node{ 
  public:
  T data;
  node<T>*next;
};

template<class T>
class stack{
  private:
  node<T> *item;
  node<T> *top;
  public:
  stack(); // constructor
  void push( node<T> *); // to insert an item to the stack
  void pop(); // to remove an item from the stack
  void display(); // to display the stack elements on screen
  node<T> *newnode(int );
};

#include "functions.cpp"
  #endif

functions.cpp

#include <iostream>
#include "header.h"

#ifndef FUNCTIONS
#define FUNCTIONS

using namespace std;


template<class T>
stack <T> :: stack(){

node<T> *top = NULL;

}

template<class T>
void stack <T> :: push(node<T> * q){
if (top == NULL)
        top = q;
    else
    {
        q->next = top;
        top = q;

    }
}

template<class T>
void stack <T> :: pop(){
if (top == NULL) {
        cout << "Stack is empty";
    }
    else {
        cout << "Popped element is " << top->data;
        item = top;
        top = top->next;
        delete(item);
    }
}

template<class T>
void stack <T> :: display(){
node<T> *q;
    q = top;

    if (top == NULL) {
        cout << "Stack is empty!!";
    }
    else {
        while (q != NULL)
        {
            cout << q->data << " ";
            q = q->next;
        }
    }
}  

template<class T>
node<T> * stack <T> :: newnode(int x)
{

    item = new node<T>;
    item->data = x;
    item->next = NULL;
    return(item);
}

#endif

main.cpp

#include<iostream> 
#include "header.h"

using namespace std;

int main()
{
    int ch, x;

  stack <int> myStack;
    node<int> *nptr;


    do
    {
        cout << "\n\n1.Push\n2.Pop\n3.Print Stack\n4.Exit";
        cout << "\nPlease enter a function(1-4):";
        cin >> ch;

        if (ch == 1)
        {
            cout << "\nEnter data:";
            cin >> x;
            nptr = myStack.newnode(x);
            myStack.push( nptr);
        }

        else if (ch == 2)
        {
            myStack.pop();
        }

        else if (ch == 3)
        {
            myStack.display();
        }
        else cout << "\nInvalid Entry";

    } while (ch != 4);

    return 0;
}

Single file working program

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

nodeType *top = NULL;
nodeType *p;

nodeType* newnode(int x)
{
    p = new nodeType;
    p->data = x;
    p->next = NULL;
    return(p);
}

void push(nodeType *q)
{
    if (top == NULL)
        top = q;
    else
    {
        q->next = top;
        top = q;
    }
}

void pop() {
    if (top == NULL) {
        cout << "Stack is empty";
    }
    else {
        cout << "Popped element is " << top->data;
        p = top;
        top = top->next;
        delete(p);
    }
}

void printStack()
{
    nodeType *q;
    q = top;

    if (top == NULL) {
        cout << "Stack is empty!!";
    }
    else {
        while (q != NULL)
        {
            cout << q->data << " ";
            q = q->next;
        }
    }
}

int main()
{
    int ch, x;

    nodeType *nptr;


    do
    {
        cout << "\n\n1.Push\n2.Pop\n3.Print Stack\n4.Exit";
        cout << "\nPlease enter a function(1-4):";
        cin >> ch;

        if (ch == 1)
        {
            cout << "\nEnter data:";
            cin >> x;
            nptr = newnode(x);
            push(nptr);
        }

        else if (ch == 2)
        {
            pop();
        }

        else if (ch == 3)
        {
            printStack();
        }
        else cout << "\nInvalid Entry";

    } while (ch != 4);

    return 0;
}
  • 3
    Have you looked at https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file? – R Sahu Mar 13 '20 at 04:51
  • 1
    When you are talking to software engineers about a problem, get in the habit of identifying an actual problem and describing it accurately. You cannot vaguely indicate that something might be wrong, dump your code out, and expect someone to help. Well, you can if you want, but you are likely to be ignored. – paddy Mar 13 '20 at 05:25
  • Does this compile? You have circular includes. Normaly, the .h file never includes any cpp files. Cpp files include .h files and not vice versa. Here everything is including evrything. Put your class definition in a header file. Add include guards. here. In the cpp (without include guards), include your header .h – A M Mar 13 '20 at 06:52
  • What are these "difficulties" of which you speak? – JaMiT Mar 13 '20 at 07:08

0 Answers0