I am unable to make any logical algorithm to access middle of stack. I am using stack implemented with linkedlists, I have following functions available for stack like push,pop,top.
I'm doing a refresher for C++ and have gotten to operator overloading. I'm trying to perform an operator overload with the insertion (<<) operator, but I have encountered a problem.
Here's my class [In a header file "Shinigami.h"]
#include<string> namespace K{ class Quincy; class Shinigami{ friend std::ostream& operator<<(std::ostream&, const Shinigami&);
[Code] .....
If the operator function is a friend of the 'Shinigami' class, why doesn't it recognize any of it's private members? I need it to be in this file because I'm doing a bit of association with the 'Quincy' class.
I thought it was the namespace, but I included that.
I am trying to program a stack based linked list. Something very weird is happening with my program. Whenever i want to print with cout<< in specific places, it actually changes the data of the stack. Also, using endl after the cout also changes the data of the program.
#include <iostream> using namespace std; class node { public: int number; node* nextPtr=NULL;
So im trying to write a code that will take data in from a user and when that user enters specific character then i want to pop the top of the stack and place that node on a new stack. Then if the user enters a different specific character then i want to place what was just popped off the stack back on to the original stack. Anyways i'm just testing what i have without all the complicated stuff with the specific characters, but i get an error and i'm not not well versed in exception handling. So this is what i have so far. the push seems to work well but the pop seems to be giving me problems.
Stack::Stack(){ top = NULL; bottom = NULL; } //method to push a line of text onto the stack void Stack::push(string data)
Use the linkedStackType class (Linked list implementation of stack) and write a function reverseRows to reverse the order of rows in the matrix. Note that reverseRows is not a member function of the Matrix class therefore only the public interface of matrix can be used.
template<class Type> struct nodeType { Type info; nodeType<Type> *link;
I'm a little confused by my programming assignment this week. I've been working at it Wednesday and I've made progress but I'm still confused as to how I'm supposed to do this. The class I made is called Stack, and it's derived from a template class called StackADT. We also utilize a class called unorderedLinkedList, which is derived from a class called linkedList.
We're supposed to implement all of the virtual functions from stackADT in the Stack class. The Stack data is stored in a an unorderedLinkedList, so what I'm confused by is how to implement a few of the Stack functions because there are no functions in unorderedLinkedList which we could call to manipulate the data.
As you can see from my attached code, I'm really confused by how I'm supposed to implement the pop() and top() functions, and I also think my initializeList() function is wrong. We don't have any similar functions in unorderedLinkedList to call, so I'm at a loss of how i'd access my unorderedLinkedList. My initial thought was to call the similar functions in the class that unorderedLinkedList was derived from, linkedList, but I'm unsure of this is what we're supposed to do, or if theres actually a way to access my unorderedLinkedList without having to use the functions from the base class.
NOTE: We're not allowed to modify stackADT, unorderedLinkedList, and linkedList.
Stack.h
#include "stackADT.h" #include "unorderedLinkedList.h" template<class Type> class Stack: public stackADT<Type>{ template <class T> struct nodeType { T info; nodeType<T> *link;
Say I have a class that requires dynamic allocation to implement a few of the operators. Take "+" for example; I need to create a new object to hold the sum of the two parameters whose size is not known at compile time.
I'm pretty sure the standard way to indicate a failure inside the overloading function would be to throw an exception. However I am currently involved in an embedded(ish) project where the spec. says no exceptions are to be used.
I think I have 2 options:
1. Return an "invalid" object (with a flag indicating an error has occurred) and check for this after each operation.
a = b + c if (a.err) // handle error or
2. To forsake operator overloading entirely and think up a new way of doing things where all functions that involve dynamic allocation can return error codes. but this seems rather terrible too as I may end up with something like:
objA a if (add(&a, b, c) == -1) // assuming b and c are initialized before this snippet starts // handle error
Is there a number 3 that I haven't thought of? It seems that not allowing exceptions is fairly common even in the non-embedded world [URL] so how is this normally done? or is operator overloading usually avoided when exceptions are not allowed?
We're trying to overload the delete[] operator with specific arguments. Which is the right way to call it? We use the GNU compiler and obtain compiler errors with all of these samples:
#include<iostream> using namespace std; typedef unsigned int P;
I'm trying to come up with the union of two Vector ADT bags, so I have to overload the '+' operator, but I'm getting a bunch of error messages saying:
VectorBag.cpp: In instantiation of ‘VectorBag<ItemType> VectorBag<ItemType>::operator+(VectorBag<ItemType>) [with ItemType = int]’: proj2.cpp:161:42: required from here VectorBag.cpp:81:24: error: no match for ‘operator[]’ (operand types are ‘VectorBag<int>’ and ‘int’) newBag.add(anotherBag[i]); ^ Here is the function to overload the operator:
template<class ItemType> VectorBag<ItemType> VectorBag<ItemType>::operator+(VectorBag<ItemType> anotherBag) { VectorBag<ItemType> newBag; for (int i = 0; i < anotherBag.getCurrentSize(); i++) newBag.add(anotherBag[i]); }
The add() function is pre-defined by me somewhere else in the code. It basically does push_back().
I want to overload prefix and postfix increment(++) operators with friend function. I also have to use the constructors for this. How can I do this? in C++
i am doing some practice problems and i can't seem to figure out how to do this. basically we have a students number of test scores, then the name followed by the scores they have in a text file. Then we have to make a class with a constructor, copy constructor, destructor, and overload the = operator and the input and output operator. Are we suppose to call the text file in the input overload operator?
Here is what i have so far.
This is my header file.
#ifndef STUDENTTESTSCORES_H #define STUDENTTESTSCORES_H #include <string> #include <iostream> using namespace std; class StudentTestScores{ private: string studentName;
[Code]...
i am 100% sure the overloading the input is wrong
here is the implementation of the constructor copy constructor and desctructor
#include <iostream> #include "StudentTestScores.h" using namespace std; StudentTestScores::StudentTestScores(string name = "", int numScores = 0) { studentName = name; numTestScores = numScores; if (numScores <= 0) testScores = NULL; else
[Code]...
and here is the notepad file
3 Justin Bieber491.469.184.681.081.5 Miley Cyrus380.080.090.083.3 Kim K490.575.661.481.677.2
The program is suppose to use all the information and read from the notepad and output the exact things as the notepad file
Consider the class specification below. Write the prototype (i.e. header) of a member function to overload the insertion operator (i.e. <<). The << operator is to output the data members of an instance of class StudentTestScores into an output stream. Your definition should allow for chaining of output operations (e.g. cout << x << y; where x and y are of type StduentTestScires).
#include <string> using namespace std; class StudentTestScores{ private: string studentName; float *testScores; // used to point to an array of test scores int numTestScores; // number of test scores
I'm having some issues with my code. For the produce function i am getting an error saying 'no instance of overload function produce() matches the argument list' and also for the lines buffer[head].data = message; buffer[head].time = current_time i get an error saying 'expression must have pointer to object type.
In the code i'm not sure if i passed the variables to the function correctly. I have attached the code .....
I have this code that I need to memorize for my final. Memorizing code is easy for me, but I'm trying pretty hard to fundamentally understand the functions, and what they are doing (even using pen and paper, to draw and trace).For example, in the push function below, I understand everything, except why I'm setting ptr = p. I feel like p should be equal to NULL, then the next node I push should be equal to p, etc.
Stack & Stack::push(double x) { Node * p = NULL; try { p = new Node; }
[code].....
Also, are LL Queues that hard to implement once you can do them w/stacks - That will probably be something I have to code for my final, as well. Below is the full code for my Stack class.
#include <iostream> #include <string> #include <stdlib.h> using namespace std; class Stack }
I do some research about getting the coordinates from the list items but without success. I would like to make that when i press down my mouse button that little blocks fall "out" my mouse and stack on top of eachother.
that first works. When i press the mouse button i draw squares and the fall to the bottom of the screen, but the stack part not.
here is my code:
using System; using System.Collections.Generic; using System.ComponentModel;
I'm trying to write a function that takes two linked lists and creates a third one with only the common elements.
It assumes the first list (the caller) has no dups, but it doesn't seem to be working. The program doesn't crash, it just hangs when it is supposed to display L3 (the third list)..everything else runs and is displayed fine.
Write the definition of the class dayType that implements the day of the week in a program. The class dayType should store the day, such as Sunday for Sunday. The program should be able to perform the following operations on an object of type dayType:
a. Set the day b. Print the day c. Return the day d. Return the next day e. Return the previous day f. Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add 4 days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday. g. Add the appropriate constructors
Write the definitions of the functions to implement the operations for the class dayType.
Line 23 error: prototype for 'void dayType::setDay(int)' does not match any in class 'dayType' Line 11 error: candidate is: void dayType::setDay() Line 36 error: prototype for 'void dayType::getDay(int) const' does not match any in class 'dayType' Line 13 error: candidate is: void dayType::getDay()
I know this is probably a really easy mistake but I am new to Programming and I have changed this code for days to no avail.
I have a linked list comprised of chars like so...
Code:
node1 - "p" node2 - "o" node3 - "p"
I need a function that will take in three perameters...node *replaceChar(node *head, char key, char *str)Stipulations of this function. head is the head of the list, 'key' and 'str' are guaranteed to contain alphanumeric characters only (A-Z, a-z, and 0-9). str can range from 1 to 1023 characters (inclusively). So if I call this function with these perameters..
Code:
node *head == /*the head of the list to be examined*/ char key == "p"char *str == "dog"The new list will look like this... node1 - 'd' node2 - 'o' node3 - 'g' node4 - 'o' node5 - 'd' node6 - 'o' node7 - 'g'
All instances of 'p' were replaced with 'dog' I have a toString function which takes in a string and converts it to a linked list and returns the head. So assume that you can call the function on str = "dog" so...
Code:
toString(str) == /*this will return the head to the list made from the str*/
If it's unclear what my question is...I am stumped on how to write the replaceChar function the one that takes in three perameters..
I have tried to understand the concept of linked lists and I have read the assigned chapter 2 times. My teacher is a little laid back when it comes to teaching! This is only a portion of my program. This function is supposed to add 2 binary numbers 11101+1101 and store the result in the temp list. The answer I get is 10000.I don't think that it is adding the carry.