Question

can some help me why it is erroring. I cannot find the mistake in the code....

can some help me why it is erroring. I cannot find the mistake in the code. It is to implment a circular double linked list. please and thank you

//////////////////////////////////////////////////////////////////////////

HEADER FILE

////////////////////////////////////////////////////////////////////////

#pragma once
#include <cstdlib>



template <typename T>

class Node
{
public:
   T val;
   Node<T> *next;
   Node(T v)
   {
       val = v;
       next = NULL;
   }
};

///////////////////////////////////////////////////////////////////////////////////

STACK.CPP FILE

///////////////////////////////////////////////////////////////////////////////////

#include "Stack.h"

template <typename T>

class Stack
{
private:
   //head of the stack
   Node<T> *stack;
   // size or number of the values within the stack
   int size;
public:
   Stack()
   {
       size = 0;
   }
   //push function
   void push(T val);
   {
       // create a node of type Node<T> pointer
       Node<T> *node = new Node<T>(val);
       if (stack == NULL)
       {
           stack = node;
       }
       else
       {
           //if not linked to the current node to the stack
           //and make the head of stack as node
           node->next = stack;
           stack = node;
       }
       size++;
   }
   T pop()
   {
       //iif stack is empty return null
       if (isEmpty())
       {
           return NULL;
       }
       else
       {
           //if it isnt set the head of the stack to the next node , then store
           // the head node value to return
           T del = stack->val;
           stack = stack->next;
           size--;
           return del;
       }
   }
   bool isEmpty()
   {
       return size == 0 ? true : false;
   }
};

////////////////////////////////////////////////////////////////////////////

/DRIVER FILE....................

////////////////////////////////////////////////////////////////////////////

#include <fstream>
#include <iostream>
#include "Stack.h"

using namespace std;

int main()
{
   Stack<string> stack;
   string fileName = "input.txt";
   ifstream input(fileName.c_str());
   if (input == 0)
   {
       cout << "\nError: inp.txt is not found";
       return 0;
   }

   string line;
   cout << "pushing following lines from inp.txt into Stack\n\n";
   while (getline(inp, line))
   {
       cout << line << endl;
       Stack.push(line);
   }
   inp.close();

   ofstream out("out.txt");
   string top = " ";
   cout << " \n Writing following lines to out.txt \n\n";
   while (!stack.isEmpty())
   {
       //pop a value from stack and write it to file
       top = stack.pop();
       cout << top << " \n";
   }
   out.close();
   return 0;
}

/////////////////////////////////////////////

INPUT FILE

/////////////////////////////////

//input.txt -----------

1 2 3 4 5 6 7 8
one two
three four five
six seven eight nine

//output.txt -----------

six seven eight nine
three four five
one two
1 2 3 4 5 6 7 8

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ PROGRAM

(Stack.h )File

#include <cstdlib>

// Create template class Node
template <typename T>
class Node
{
public:
   // Node members
T val;
Node<T> *next;
Node(T v) // create constructor for Node
{
val = v;
next = NULL;
}
};

(MyStack.h) File

#include "Stack.h"

template <typename T>

class Stack
{
private:
//head of the stack
Node<T> *stack;
// size or number of the values within the stack
int size;
public:
Stack()
{
size = 0;
}
//push function
void push(T val)
{
// create a node of type Node<T> pointer
Node<T> *node = new Node<T>(val);
if (stack == NULL)
{
stack = node;
}
else
{
//if not linked to the current node to the stack
//and make the head of stack as node
node->next = stack;
stack = node;
}
size++;
}
T pop()
{
//iif stack is empty return null
if (isEmpty())
{
return NULL;
}
else
{
//if it isnt set the head of the stack to the next node , then store
// the head node value to return
T del = stack->val;
stack = stack->next;
size--;
return del;
}
}
bool isEmpty()
{
return size == 0 ? true : false;
}
};

(driver.cpp) File

#include <fstream>
#include <iostream>
#include "MyStack.h" // You should include MyStack.h header file (You declare only Stack.h header file)

using namespace std;

int main()
{
Stack<string> stack;
string fileName = "d:\\Myinput1.txt"; // declare file name in specific path
ifstream input(fileName.c_str());
if (input == 0)
{
cout << "\nError: inp.txt is not found";
return 0;
}

string line;
cout << "pushing following lines from inp.txt into Stack\n\n";
while (getline(input, line)) // in getline() function using input(input file object ie "input") you declare "inp"
{
cout << line << endl;
stack.push(line); // call Stack object "stack" you declare "Stack"
}
input.close();

ofstream out("d:\\Myout1.txt"); // declare file name in specific path
string top = " ";
cout << " \n Writing following lines to out.txt \n\n";
while (!stack.isEmpty())
{
//pop a value from stack and write it to file
top = stack.pop();
cout << top << " \n";
}
out.close();
return 0;
}

OUTPUT

pushing following lines from inp.txt into Stack

1 2 3 4 5 6 7 8
one two
three four five
six seven eight nine

Writing following lines to out.txt

six seven eight nine
three four five
one two
1 2 3 4 5 6 7 8

Add a comment
Know the answer?
Add Answer to:
can some help me why it is erroring. I cannot find the mistake in the code....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • can someone please fix this error in the stack file at line 18 ( class Stack2:public...

    can someone please fix this error in the stack file at line 18 ( class Stack2:public Stack { ) this is thew error ----> expected class name before '{' token Queue.h --> #include "Stack.hpp" template <typename ITEM> class Queue { private: ITEM item; Stack <ITEM> * s1= new Stack<ITEM>; Stack <ITEM> * s2= new Stack<ITEM>; public: Queue(){}; bool enqueue (ITEM item); bool dequeue (ITEM &item); bool check = true; ~Queue(); }; //this is the class which contains the functions of...

  • how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 }...

    how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 } class Stack { Node head; int size; Stack() //default constructor { this.head=null; this.size=0; } //Input = data //Output = void (just adds value to list) // method pushes elements on stack // time: O(1) // space: O(1) public void push(int data) { Node node=new Node(data); node.next=head; head=node; size++; } //Input = none //Output = top of stack // method pops value from top of...

  • PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite...

    PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define...

    // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define Stack_h #include <stdio.h> #include <string> #include <iostream> using namespace std; class Stack { public: Stack(); ~Stack(); bool empty(); string top(); void push(const string &val); void pop(); void display(ostream &out); private: class Node { public: string word; Node *next; }; Node *tos; }; #endif Header file provided above. PLS create source code for functions declared in the header. If changes are need no make in...

  • Im writing a method to evaluate a postfix expression. Using my own stack class. Here is my code but I keep getting a classcastexception where it says java.lang.Character cannot be cast to java.lang,In...

    Im writing a method to evaluate a postfix expression. Using my own stack class. Here is my code but I keep getting a classcastexception where it says java.lang.Character cannot be cast to java.lang,Integer. Im not sure how to fix this. public class Evaluator { public static void evaluatePost(String postFix)    {        LinkedStack stack2 = new LinkedStack();        int val1;        int val2;        int result;        for(int i = 0; i < postFix.length(); i++)        {            char m = postFix.charAt(i);            if(Character.isDigit(m))            {                stack2.push(m);            }            else            {               ...

  • C++: I need implement this code using Double Linked List using the cosiderations 1. head point...

    C++: I need implement this code using Double Linked List using the cosiderations 1. head point to null in an empty list 2. There is not need of a tail pointer /*This class implements the singly linked list using templates Each list has two attributes:    -head: first node in the list    -tail: last node in the list #include "circDLLNode.h" template class { public:    //Default constructor: creates an empty list    ();    //Destructor: deallocate memory    ~();  ...

  • Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac...

    Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac -g P4Program.java P4Program.java:94: error: package list does not exist Iterator i = new list.iterator(); ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. Note: Below there are two different classes that work together. Each class has it's own fuctions/methods. import java.util.*; import java.io.*; public class P4Program{ public void linkedStackFromFile(){ String content = new String(); int count = 1; File...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT