使用链表和类模板实现堆栈

时间:2020-03-13 04:47:18

标签: c++ class templates linked-list stack

我正在尝试使用链接列表和类模板来实现堆栈。我没有收到任何编译器错误,但是我的逻辑可能是错误的,有点迷失了。我仅使用结构在一个文件中就有一个工作程序,因此在使用多个文件和模板类进行翻译时遇到了困难。我还将在下面包含我的单个文件cpp,希望对您有所帮助。任何帮助将不胜感激。我有一个header.h文件,functions.cpp和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;
}

单个文件工作程序

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

0 个答案:

没有答案