Problem occurs within your loop-condition. It will continue as long as x>=0, since the it always fulfill it won't stop.
Ancient Dragon commented: Great work finding a solution to your problem :) +36
Problem occurs within your loop-condition. It will continue as long as x>=0, since the it always fulfill it won't stop.
>new_node->first->next = my_list->first;
my_list is a type, not an object. Perhaps you meant:new_node->first->next = my_this->first;
I did wrong there, thanks for pointing out...
I hit at problem again though. I have written a test code to see whether it will add to the linked list without problem. It will add the first set of values without problem. However it crashes during the second add.
I thought I got most of the requisite now.
1. create a new node
2. add values to the node
3. point the new nodes to the first node
4. change first node address to the new node
Have I missed out something?
void list_add(struct my_list* my_this, int key, double value)
{
// ADD YOUR CODE HERE (approx 23 lines)
cout << key << " " << value << endl;
my_list *new_node;
//allocate memory for next node
//typecast to (my_list) is needed here
new_node = (my_list*)malloc(sizeof(my_list));
if(my_this)
{
cout << "pang" << endl;
//adding key and value
new_node->first->key = key;
new_node->first->value = value;
new_node->first->next = my_this->first;
//point new_node to become the first element
my_this->first = new_node->first;
}
}
Ah sry, I was unsure whether it is C or C++. I guess C since it uses malloc...
anyway I've continued on it and got stuck again...
void list_add(struct my_list* my_this, int key, double value)
{
cout << key << " " << value << endl;
my_list *new_node;
//allocate memory for next node
//typecast to (my_list) is needed here
new_node = (my_list*)malloc(sizeof(my_list));
if(my_this)
{
new_node->first->key = key;
new_node->first->value = value;
new_node->first->next = my_list->first;
}
}
Now that I have assigned values to the new created node, I want to attach it to the linked list.
So I'm guessing this part
new_node->first->next = my_list->first;
needs to be attached to my_list->first. However doesnt work so well. It is complaining about "expected primary-expression...."
Hey, I am currently following a code skeleton to implement the missing code for some function. Below is only some part of the implementation I've done so far. Yes it is not much yet...
I got stuck for the implementation of the list_add() function.
What I want to do there is to create a new node, and allocate some memory to it so I afterward can insert values to it. However I get compilation error. I have trial error it for awhile now without founding any solution. So I hoped for if someone know what error it is.
The error I get is
error: invalid conversion from `void*' to `my_list*'
#include <iostream>
using namespace std;
struct list_item
{
int key; // identifies the data
double value; // the data stored
struct list_item* next; // a pointer to the next data
};
struct my_list
{
struct list_item* first; // a pointer to the first element of the list
};
void list_init(struct my_list* my_this)
{
my_this = NULL;
}
void list_add(struct my_list* my_this, int key, double value)
{
my_list *new_node;
//allocate memory for next node
new_node = malloc(sizeof(my_list));
}
int main()
{
my_list a;
list_init(a);
return 0;
}
Hi I've been struggling with this for a very long time now. And I can't seem to get it done.
I want to make an array, dynamic allocted array. Which act something like an vector (I know c++ has vector). So my approach was to create a list_add() function. Its purpose is to add new integer values to the array, and to increase the array size if it hits end of it.
Here's the problem. What I want to do in the code is to create a new dynamic allocated array, and rellocate the values from the old array to the new one. After that I want the new array to be recognized as the old one. However it does not seem to do it.
Can anyone point out to be if this is the correct approach, and what I did wrong in the code?
This is the test main
int main()
{
const int N = 7;
//declaring dynamic array allocation
int* list = new int[N];
int used = 0;//, a_val;
//and some scrap values
for(int i=0;i<11;i++)
{
list_add(list, used, N, i);
}
cout << endl << "Size: " << N << endl << endl;
cout << "Prints the list " << endl;
for(int i=0;i<used;i++)
{
cout << list[i] << ". ";
}
}
This is the function
bool list_add(int list[], int& space_used, int max_size, int value)
{
if(max_size-space_used > 0)
{
list[max_size-space_used-1] = value;
space_used++;
return true;
}
else
{
cout << …
Hello I've a question when creating a class with functions.
e.g.
function constructor() {
this.myFunc = function() {
return "Hello World";
}
}
do I have to write the function in that way? this.funcName = function() for every function in that class?
Because when I tried to write it in this way instead
function constructor() {
function() myFunc{
return "Hello World";
}
}
It does not work if it is bundled like function in a function. When I create an object of constructor, and then try to call the myFunc function.
aha, it was not so complicated then. thanks for the help ^^
hello all,
thanks for the tips, and help.
I rechecked the compile error, and found out that I could just overload operator== to make find(); search for the string
here's what I added to make it work
bool operator==( const Foo& f1, const string& str ) {
return f1.name == str;
}
I got another question though, is it possible to make the iterator to go through a defined intervall?
e.g. instead for v.begin(), v.end(), which is 0... end(). So I can go though from 5..end() ?
Hello,
I wonder what I need to do to use find() from <algorithm> to search in a vector containing some Struct, with two string vars.
I'm not asking for codes now, I just want the principles. I written this, and got compilation error.
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
struct Foo {
string name;
string age;
};
int main()
{
Foo f1;
f1.name = "Misco";
f1.age = "15";
Foo f2;
f2.name = "Foo";
f2.age = "1337";
vector<Foo> v;
v.push_back(f1);
v.push_back(f2);
find(v.begin(), v.end(), "Misco");
return 0;
}
Hello, I solved the problem by myself now.
What I needed to know was what the size of the object was. And the rest solved automatically.
It is binary file, i'm newbie in c++ sorry if I say something wrong.
Isn't there a way to interpret the binary file. And output only the strings I find in the file?
I've tried to fetch with this following code as well. Then it get the first name on the first loop, second loop I still get all scrap values that I don't want.
ifstream in("file.bin", ios::in | ios::binary);
string output;
in >> output
while(!in.eof()) {
cout << output << endl;
in >> output;
}
Hello,
I've been struggling with a problem for a while now. I'm trying to print out information which is contained in a binary file.
This what I get when I open it in notepad.
Marjorie Gestring D201 ð¿ ð¿ ð¿ ð¿ ð¿ Patricia McCormick D202 ð¿ ð¿ ð¿ ð¿ ð¿ Anna Andersson ick D203 ð¿ ð¿ ð¿ ð¿ ð¿ Fanny Duracks ick D204 ð¿ ð¿ ð¿ ð¿ ð¿ Ulrika Knape ick D205 ð¿ ð¿ ð¿ ð¿ ð¿ Greta Johanson ick D206 ð¿ ð¿ ð¿ ð¿ ð¿ Anna Lindberg ick D207 ð¿ ð¿ ð¿ ð¿ ð¿ Gunilla Rosenhoff D208 ð¿ ð¿ ð¿ ð¿ ð¿ Kathy Peiss nhoff D209 ð¿ ð¿ ð¿ ð¿ ð¿ Birgitta Conradson D210 ð¿ ð¿ ð¿ ð¿ ð¿ Lisa Regnell adson D211 ð¿ ð¿ ð¿ ð¿ ð¿ Leena Laine adson D212 ð¿ ð¿ ð¿ ð¿ ð¿ Paulina Suokas son D213 ð¿ ð¿ ð¿ ð¿ ð¿ Gerda Meyerson son D214 ð¿ ð¿ ð¿ ð¿ ð¿
I'm not trying to write anything complex right now. I just want to retrieve all the names and D-numbers for now.
What I wonder shouldn't this code print out every character it finds? If I want to get all the names and print it out nicely is it necessary to write a struct to contain the information?
char c;
in.read( (char*)&c, sizeof(char) );
while(!in.eof()) {
in.read( (char*)&c, sizeof(char) );
cout << c << endl;
}
*v
that part what I was looking for, thanks.
Yes I've already written a virtual function.
Anyway thanks, and thanks for correcting me as well ^^;
Hello,
This is probably a simple question.
I have a class A, and a class B, class C class... which inherits from class A.
Now I want to store it in a container e.g. vector
vector<*A> container;
I have also created some objects of B and C, and stored it in the container
B b;
C c;
container.push_back(&b);
container.push_back(&c);
Now for my question, I have overloaded << which take b, or c and print whatever it contains. Is it possible to fetch b, and c from the container so I can use <<?
As of now I have to written a print() function, which I call by
(*it)->print();
to print the information in the object. But is it possible to fetch from the container so I can use the overloaded << instead?
Hello,
I'm reading a chapter about virtual function. So what I know is that virtual function is equal to abstract functions.
It is enough to declare one function virtual to make the class into an abstract class. E.g.
class Base {
public:
Base();
virtual set();
}
Now I'm wondering about pure virtual function, and "normal" virtual function. I don't get what the book says about it. Seems to be that pure virtual function is equal to any virtual function you declare in the Base class. Or am I wrong?
thanks, it works now :)
Hello, I am having problem to define a constuctor of a class with private member which is also of a const type. When I try to compile it complains that the const members do not have initalized data. Hmm, anyway it works fine if I remove const. However is it possible to define a constructor with const members which is possible to set values to?
I know that const is values you can't change values on, but exercise says to use const. help is appreciated.:)
e.g. I want to create an object of a person
Bike bicycle (2, "Monark", "blue", 24.11);
and here is the constructor code
Bike::Bike(const int wheel, const string brand, string color, double wheelsize) {
w = wheel;
b = brand;
c = color;
ws = wheelsize;
}
and here is the class file
class Bike {
public:
Bike (const int, const string, string, double);
private:
const int w;
const string b;
string c;
double ws;
};
aha, stupid of me....
thanks for the help :)
I want to use my overloaded operator, inside another overloaded operator.
Hello,
I've written couple of overloaded operators in my file containing Set declarations. I want to use these operators within the declaration file. However it seems it isn't used. Instead it uses the standard operators.
e.g. i have two overloaded operators say <= and ==
bool Set::operator<=(const Set& b) const {
//codes....
return true;
}
bool Set::operator==(const Set& b) const {
if(b.header <= header && header <= header)
//codes....
return true;
}
Inside my overload for == which I use <= it doesn't use my overloaded version of <= instead it uses the standard version. Do I have to write a certain way to use my overloaded operator? or is it some other problems?
Yay, I solved it.
Read another article that made the picture more clear.... or maybe I just stumbled by luck. Here is the site anyway for future reference
http://www.codeproject.com/KB/cpp/linked_list.aspx
hi again,
I can not quite figure out how to fix it. The furthest I got to was to delete first node and make so it does not appear in the output
which I used this if-statement to do
if(value == 1) {
previous = header;
header = header->next;
delete (previous);
}
tried to understand the explanation from this webpage also, but it did not made me any smarter.
I have an ugly solution now which is to make integer larger than 1000 disappear in output. But yeah, it still exist in the linked-list T_T Is it possible for you to give me an example how it can be done?
Hello,
I've written a delete function to delete any integer value based on in parameter. The problem I have now is that this function deletes the values, but leaves the Node position intact with some huge int value instead.
So my question is, do I need to rearrange the Node, or why doesn't a simply delete works?
e.g. the node contains
1, 2, 3, 4, 5
and I delete '4' output becomes
1, 2, 3, 4130880, 5
void Set::del (int value) {
Node *temp = header;
while(temp->next != 0 && temp->value != value) {
temp = temp->next;
}
if(temp->value == value) {
delete temp;
}
}
Thanks, I think I have overall idea now. I have been searching for ages for a trigger solution. And never thought just having it auto query from java instead.
You would need to occassionally "poll" the DB (and hopefully you have a timestamp field to make it "easy").
Dunno what you meant by the word poll.
But do you mean I have to write a method that occassionally send a SQL query for example every 10min to the database. To check if a certain table has increased in row number?
Hi,
I wonder if it's possible to check whether a MySQL DB has updated. e.g. new information is added to a table?
and if it detect an update. it will perform search or something else.
Main question is if it is possible to listen for changes in the db.
The two parameter Node constructor you are using to declare new Nodes looks a little suspect. In particular I see no reason why you would want to pass a Node pointer, which is presumably what header is, to a new Node. Passing NULL, or 0, to assign to the next pointer of the new Node is reasonable, if that's what you are trying to do, but passing header doesn't sit well up front. I usually see the default constructor for the class would used to declare memory for a new Node, and the values of the Node data members would be assinged after declaration, but I can't be sure what you are trying to do. Post how you declared the Node class.
I'm not quite sure myself, since the Node class was already made. The assignment it self is to implement the functions defined in the header file for Set. I have attached the files relevant for my question.
If there are n elements of the array you want to change into a set, then you want to let i range from zero to less than n. n itself and n + 1 are invalid element indexes of the array.
Yes, that's but for some reason it will only add the two first values in the array if I skip the n+1. I'm guessing it is the Node starting values is zero.
New problems I've encountered is my overload for the + function for Sets. As of …
#include "node.h"
#include <cassert> //assert
Node::Node (int nodeVal, Node *nextPtr) : value (nodeVal), next (nextPtr)
{
// std::cout << "Konstruktor Node" << std::endl;
}
Node *Node::insert (int newValue) {
next = new Node (newValue, next);
assert (next != 0); //koll fr att next != 0
return next;
}
#ifndef NODE_H
#define NODE_H
#include <iostream>
class Node
{
public:
Node(int, Node*); //constructor
Node *insert(int value); //member function
private:
int value;
Node *next;
friend class Set;
friend std::ostream& operator << (std::ostream &os, const Set &theSet);
};
#endif
#include "set.h"
#include <istream>
using namespace std;
Set::Set() {
header = new Node(0,0);
// cout << "Empty constructor " << endl;
}
Set::Set(int a[], int n) {
header = new Node(0,0); //skapar ny lista
Node *temp = header; //temp tilldelas start vrdet fr listan
int i = 0;
while(i < n+1) {
temp->next = new Node(a[i],0); //nsta uttrymme p listan tilldelas vrde frn a[i]
temp = temp->next; //
i++;
}
// cout << "Created constructor" << endl;
}
Set::Set(const Set& b) {
header = new Node(0,header);
Node *temp = header;
Node *templist = b.header -> next;
while (templist != 0)
{
temp -> next = new Node (templist->value, 0);
temp = temp -> next;
templist = templist -> next;
}
}
Set::~Set() {
delete header;
}
bool Set::empty () const {
Node *temp = header;
temp = temp->next;
if(temp == 0) {
return true;
}
return false;
}
int Set::cardinality() const {
Node *temp = header;
temp = temp->next;
int count = 0;
while(temp->next != 0) {
temp = temp->next;
count++;
}
return count;
}
bool Set::member (int x) const {
Node *temp = header;
temp = temp->next;
while(temp->next != 0) {
if(temp->value == x) {
return true;
}
temp = temp->next;
}
return false;
}
Set Set::operator+ (const Set& b) {
Node *tmp = header;
Set out = b;
tmp = tmp->next;
int i = 0;
while (tmp->next != 0 && i != 9) {
out.insert(tmp->value);
tmp = tmp->next;
i++;
}
return out;
}
void Set::insert (int value) {
// Node *temp = header;
// while(temp->next != 0) {
// cout << "Occupied by: "<< temp->value << endl;
// temp = temp->next;
//
// }
// temp->next = new Node(value,temp->next);
Node *temp = header;
bool exist = false;
while (temp->next != 0) {
if(temp->value == value && !exist) {
cout << "Exist " << endl;
exist = true;
break;
}
temp = temp->next;
}
if(!exist)
header->next = new Node(value, header->next);
}
std::ostream& operator << (std::ostream& out, const Set& b) {
Node *temp = b.header;
temp = temp -> next;
if (temp == 0) {
out << "Empty";
} else {
while(temp->next != 0) {
out << temp->value << " ";
temp = temp->next;
}
}
return out;
}
/*
Name: set.h
Author: Kenneth Bjerner
Date: 04-02-04 11:31
Description: Definitionsfil fr klassen Set
*/
#ifndef SET_H
#define SET_H
#include "node.h"
#include <iostream>
class Set {
public:
Set ();
Set (int a[], int n);
Set (const Set& b);
~Set ();
bool empty () const;
int cardinality() const;
bool member (int x) const;
Set operator+ (const Set& b);
// Set operator* (const Set& b);
// Set operator- (const Set& b);
// Set operator+(int x) const;
// Set operator-(int x) const;
// bool operator<=(const Set& b) const;
// bool operator==(const Set& b) const;
// bool operator<(const Set& b) const;
// Set& operator=(const Set& b);
//
private:
Node *header;
friend std::ostream& operator << (std::ostream& os, const Set& b);
void insert (int value);
void del (int value);
};
#endif
Lerner:
Yep, i'm going to write a class that implement a sorted Set with unique values. You seems to know what I'm going to code. Btw, is that code example you shown a way to make constructor? Anyway here's the whole header file for Set that i'm supposed to implement.
#ifndef SET_H
#define SET_H
#include "node.h"
#include <iostream>
class Set {
public:
Set ();
Set (int a[], int n);
Set (const Set& b);
~Set ();
// bool empty () const;
// int cardinality() const;
// bool member (int x) const;
//
// Set operator+ (const Set& b);
// Set operator* (const Set& b);
// Set operator- (const Set& b);
// Set operator+(int x) const;
// Set operator-(int x) const;
// bool operator<=(const Set& b) const;
// bool operator==(const Set& b) const;
// bool operator<(const Set& b) const;
// Set& operator=(const Set& b);
//
private:
Node *header;
friend std::ostream& operator << (std::ostream& os, const Set& b);
void insert (int value);
void del (int value);
};
#endif
I guess I did the constructor implementation in a wrong way. Since I don't you use the void insert(int value) nor have I implemented it yet :/
and here's the Set.cpp file of what I've implemented so far. It does what I want. It adds to a values to a Node object. But I'm uncertain if I have done it correct. Because the question says that I'm supposed to call function with say and object R of the class Set. E.g. R.insert(value), …
Hmm.. I think i'm heading on the wrong direction. I'll be back when I've evaluated the question and tried a few thing.
Hi, I have some problem to implement the constructors from set.h. There was no problem to implement the empty constructor. But as for:
Set(int a[], int n);
I have some problem. It is supposed to use insert() function from node.cpp. And since the class Set is friend a friend to class Node it should have access to the private members in class Node right?
Well, anyway I tried to implement Set(int a[], int n);
with following code
Set::Set(int a[], int n) {
Set p;
p.insert(a[0]);
}
and I get compile error saying "undefined reference to Set::insert(int);
Below is the code I have in use.
set.h
#include "node.h"
#include <iostream>
class Set {
public:
Set ();
Set (int a[], int n);
Set (const Set& b);
~Set ();
private:
Node *header;
friend std::ostream& operator << (std::ostream& os, const Set& b);
void insert (int value);
void del (int value);
};
node.h
#include <iostream>
class Node {
public:
Node(int, Node*); //constructor
Node *insert(int value); //function
private:
int value;
Node *next;
friend class Set;
friend std::ostream& operator << (std::ostream &os, const Set &theSet);
};
node.cpp
#include "node.h"
#include <cassert> //assert
Node::Node (int nodeVal, Node *nextPtr) : value (nodeVal), next (nextPtr)
{
// std::cout << "Constructor Node" << std::endl;
}
Node *Node::insert (int newValue) {
next = new Node (newValue, next);
assert (next != 0);
return next;
}
no need I think I got it down now, since init -1 would be like init the array with some other starting number ; )
well thanks for the help!
urgh, that code from the book is quite complicated I think. Anyway I was trying to get your solution to take ZERO as a number as well. The only idea how to implement it I could come to was to init the array with some high value e.g. 9999.
Then rewrite the if-statement which print the array check an interval of acceptable integers. It is not a pretty solution, but can I bother and ask how you would have done it? ^^;
Thanks,
Just a question shouldn't it be the opposite?
that *q is the unique[x], and *r be the input[x]?
Since *r = tab ?
thanks, I was thinking something like that also. But I had it hard time to break it up where to start. Maybe I struggled to much with getting it work with pointers instead also... -p-
btw, that was an excercise from a book, and the solution they had for it was written like this. Could I bother you to explain if you know what the commented lines does?
#include <iostream>
using namespace std;
main()
{
int tab[1000], *p = tab;
cout << "skriv in ett antal heltal" << endl;
while (cin >> *p++)
;
p--;
cout << "Talen är: " << endl;
for (int *q = tab; q < p; q++)
{
int *r;
for (r = tab; r < q && *r != *q; r++); // What does this for-loop do?
if (r == q) // what does this if does?
cout << *q << ' ';
}
}
hello,
I've been pondering on a thing here for while now. The thing is that I want to read in several of integers, which I've fixed. And the problem now is that I want to output the integers I have input. The thing is that I only want to output unique numbers.
For example, I input:
1, 2, 3, 3, 4, 5, 1, 10, 9, 8
the output should be:
1, 2, 3, 4, 5, 10, 9, 8
-------------------------
Any tip would be helpful.
Yes I removed clear(); when I tried it before. Anyway it works now when I put it back :)
Thanks alot for the help!
hi,
i tried that before, but the problem with that is it will add all values into same vector<double> test. That's not want I wanted, I want to have the first double value in points, and the following two double values into unique vector<double> test for each user
Hello, I have some weird problem with this code.
The problem is that when I use >> to read data a text file to vector. It will only add the first double and discard the rest. Anyway if I use array instead of vector it works fine. Anyone know what the problem can be? I've used cout to check whetever it sends in correct data or not. And it does send in correct data.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Demo {
string name;
double points;
vector<double> test;
// double test[2];
};
istream& operator>> (std::istream& in, Demo &d);
istream& operator>> (std::istream& in, vector<double> d);
//istream& operator>> (istream& in, double d[]);
int main()
{
vector<Demo> d;
Demo tmp;
while(cin >> tmp) {
d.push_back(tmp);
}
cout << d[0].name << ", " << d[0].points << ", " << d[0].test[0] << ", " << d[0].test[1] << endl;
cout << d[1].name << ", " << d[1].points << ", " << d[1].test[0] << ", " << d[1].test[1] << endl;
cout << d[2].name << ", " << d[2].points << ", " << d[2].test[0] << ", " << d[2].test[1] << endl;
cout << d[3].name << ", " << d[3].points << ", " << d[3].test[0] << ", " << d[3].test[1] << endl;
cout << d[4].name << ", " << d[4].points << ", " << d[4].test[0] << ", " << d[4].test[1] << endl;
cout << d[5].name << ", " << d[5].points << ", " << d[5].test[0] << ", " << d[5].test[1] << endl;
return …