Nothing to build: Eclipse Issues for C++ - c++

I have some experience with Java and Eclipse, but I'm new to C++, and trying to teach myself. I apologize if this is a simple question, or one that has already been asked (though I looked around for a while.) I'm on a Windows 8.
I'm trying to make a sorted linked list (which is relatively unimportant.) I get:
Info: Nothing to build for Working.
Here's my code:
/*
* SortedList class
*/
#include <string>
#include <fstream>
#include<iostream>
#include "SortedList.h"
using namespace std;
//the ListNode Structure
struct ListNode {
string data;
ListNode *next;
};
//the head of the linked list and the pointer nodes
ListNode head;
ListNode *prev, *current;
// insert a string into the list in alphabetical order
//now adds a string to the list and counts the size of the list
int Insert(string s){
//make the new node
ListNode temp;
temp.data = s;
//the node to traverse the list
prev = &head;
current = head.next;
int c = 0;
//traverse the list, then insert the string
while(current != NULL){
prev = current;
current = current->next;
c++;
}
//insert temp into the list
temp.next = prev->next;
prev->next = &temp;
return c;
}
//Return the number of times a given string occurs in the list.
int Lookup(string s){
return 0;
}
//prints the elements of the list to ostream
void Print(ostream &output){
}
int main( int argc, char ** argv ) {
cout << Insert("a") << endl;
cout << Insert("b") << endl;
cout << Insert("d") << endl;
}
And here's my header:
using namespace std;
#ifndef SORTEDLIST_H_
#define SORTEDLIST_H_
class SortedList {
public:
// constructor
SortedList();
// modifiers
int Insert(string s);
// other operations
int Lookup(string s) const;
void Print(ostream &output) const;
private:
struct ListNode {
string data;
ListNode *next;
};
// pointer to the first node of the list
ListNode head;
ListNode *prev, *current;
};
#endif /* SORTEDLIST_H_ */
Any help would be greatly appreciated.

Why don't you use std::deque (in header deque)? It probably has all the functionality you are seeking, it is fully tested and optimised. If you need a deque with a bit more of functionality, create a class that inherit from it and add the functions you need. Have a look to http://en.cppreference.com/w/cpp/containe and pick up the container that best suits your needs.
As a general advise, if something you need is already available in some good and stable library (STL, boost, GSL, Armadillo or similar), much better use it rather than to write+debug+optimise it yourself from scratch. As a general advise, focus your efforts on the code that is unique to your application, and reuse what has already be done (BUT only if it has been well tested, do not use crappy half cooked libraries).

Related

How do you write a function that returns a node which value corresponds to a value stored in a variable?

I stumbled across this question in an old textbook I bought ages ago, whilst strictly speaking it is not too difficult, I could not find a post here that simply answered this one question. As such I thought "Hey perhaps someone starting out might be confused about this", and so I put together the following code:
#include <iostream>
using namespace std;
// Structures
struct charNode {
char Node;
charNode *next;
};
// Functions
charNode* getCharNode(char c) {
return ; //<----- Return Node Here
}
What this needs is to be put in a class or else you must have a global variable that points to the head of that singly linked list.
An example class could look like this:
#include <iostream>
class singly_linked_list {
struct charNode {
char Node;
charNode *next;
};
public:
// find the charNode with the value `c`:
charNode* getCharNode(char c) {
for(charNode* curr = head; curr != nullptr; curr = curr->next) {
if(curr->Node == c) return curr;
}
return nullptr;
}
// add member functions to add/remove charNode's from the list etc
// and implement one of "the rule of 3" or "the rule of 5"
private:
charNode* head = nullptr;
};
You can implement getCharNode() function like in following code. I used this function for an example of creating singly linked list of chars. Also created extra function print_list() which outputs linked list to console.
Notice that I did only allocation of nodes (new operator), and didn't do deallocation (delete), I left this task for you to do, if you care about memory leaks.
Try it online!
#include <iostream>
// Structures
struct charNode {
charNode(char value, charNode * _next = nullptr)
: Node(value), next(_next) {}
char Node;
charNode *next;
};
// Functions
charNode* getCharNode(char c, charNode * next = nullptr) {
return new charNode(c, next);
}
void print_list(charNode const * node) {
if (!node)
return;
std::cout << node->Node << " ";
print_list(node->next);
}
int main() {
charNode * list = getCharNode('a',
getCharNode('b', getCharNode('c')));
print_list(list);
}
Output:
a b c

What are some good-enough ways I can insert an object (Packet) into a binary search tree?

I'm trying to insert a Packet object into this binary search tree. But the problem is, I don't really know of a good-enough way of doing this or how to go about doing it. I'm looking for some pointers in the right direction and to be shown what to do to tackle this problem.
Please:
Ignore my usage of namespace std; because this is for educational
purposes and I'm not rly (as of now) looking to go further than that!
Help me with my specific question and if possible, show me how I
could fix this problem.
<< Take a look at my code >>
Main.cpp:
#include <iostream>
#include "BST.h"
#include "Packet.h"
// IGNORE the USAGE of namespace std. as this is purely a testing program for educational purposes.
// It is NOT implementation for a real program.
using namespace std;
int main() {
cout << "-------------------------------------------------------" << endl;
cout << "Testing BST" << endl;
cout << "-------------------------------------------------------" << endl;
BST test1;
Packet packetTest(123, "This is a packet of cheese.", 12.95, 10);
// test1.insert(How should I choose to insert Packet? That's the question.);
system("pause");
}
BST.h:
#pragma once
#include "Packet.h"
using namespace std;
class BST {
struct Node {
Node() : rlink(nullptr), llink(nullptr) {};
~Node() {};
// Store packet here (for instance Packet *data or something)...
Node *rlink, *llink;
};
public:
BST();
// void insert(How should I choose to insert Packet? That's the question.);
void insert(Node *&p, Node *newNode);
void preorderTraversal() const;
void destroyTree();
~BST();
private:
Node * root;
void destroyTree(Node *&p);
void preorderTraversal(const Node *p) const;
};
BST.cpp (need guidance here, see below code to see what I mean):
#include "BST.h"
#include <iostream>
BST::BST() : root(nullptr) {}
// Need guidance here. What should I do for this function? How can I insert this object called Packet into the BST?
/*void BST::insert(How should I choose to insert Packet? That's the question.) {
Node *newNode = new Node;
...
insert(root, newNode);
}*/
void BST::insert(Node *&p, Node *newNode) {
if (p == nullptr) {
p = newNode;
}/*else if (p's data's getPartId() > newNode's data's getPartId()){
insert(p->llink, newNode);
}*/else {
insert(p->rlink, newNode);
}
}
void BST::preorderTraversal() const {
if (root == nullptr) {
cerr << "There is no tree.";
}
else {
preorderTraversal(root);
}
}
void BST::preorderTraversal(const Node *p) const {
if (p != nullptr) {
// cout << p->data->getPartId() << " "; Need to handle Packet's data here. But we need to implement Packet insection first!
preorderTraversal(p->llink);
preorderTraversal(p->rlink);
}
}
void BST::destroyTree(Node *&p) {
if (p != nullptr) {
destroyTree(p->llink);
destroyTree(p->rlink);
delete p;
p = nullptr;
}
}
void BST::destroyTree() {
destroyTree(root);
}
BST::~BST() {
destroyTree(root);
}
Packet.h:
#pragma once
#include <string>
using namespace std;
class Packet {
public:
Packet(int partId, string description, double price, int partCount) :
partId(partId), description(description), price(price), partCount(partCount) {}
int getPartId() const { return partId; }
private:
int partId;
string description;
double price;
int partCount;
};
This was my previous implementation of insert in BST.cpp:
void BST::insert(Packet &data) {
Node *newNode = new Node;
newNode->data = &data;
insert(root, newNode);
}
As you can see, I don't believe that this is ideal. I mean I had to use & reference twice. Is there a more elegant solution and may I get guidance in regards to that?
Q: How can I insert this object called Packet into the BST?
A: To create a relationship between the BST and Packet class, you must define one in some sort of way. Best practice calls for an association which imposes the least amount of coupling between the related classes.
I have implemented an association in your solution in the place I found most suitable ie. the rlink and llink pointers of struct Node of class BST.
// Store packet here (for instance Packet *data or something)...
Packet* rlink, * llink;
A relationship is the only way you will be able to access getPartId() from a Node or BST object. Albeit the Packet class does not manage any resources so it does not require memory management, association is just a fancy word for a loosely coupled relationship between classes, which is the case here.
Be careful when calling functions recursively, as you have in void BST::insert(Node *&p, Node *newNode). You shouldn't call a function recursively without an exit condition and never really use recursion unless you have to as iterations are a stack-memory saving alternative. I saw no need for recursion in your insert function so I took it out. I'm hoping what I replaced them with is of some use to you:
void BST::insert(Packet& p) {
Packet* newPacket = new Packet(p);
insert(root, newPacket);
}
void BST::insert(Node*& p, Packet* newPacket) {
if (p == nullptr) {
p = new Node;
p->llink = newPacket;
}else if ((p->llink->getPartId()) > newPacket->getPartId()){
p->llink = newPacket;
}else {
p->rlink = newPacket;
}
}
I then went on to say:
void BST::preorderTraversal(const Node* p) const {
if (p != nullptr) {
cout << p->llink->getPartId() << " \n";
}
}
void BST::destroyTree(Node*& p) {
if (p != nullptr) {
delete p;
p = nullptr;
}
}
As I said, a relationship is the only way you will be able to access getPartId() from a Node or BST object.
Regarding the comments, I agree. Encapsulation requires keeping all data members private and only exposing methods when you have to. My solution allows you to keep the function
private:void insert(Node*& p, Packet* newPacket);
As you have kept Node completely hidden by overloading the preorderTraversal()
Good job and hope I helped!

Linked List Addresses are not in regular intervals

I am trying to figure out how the newNode addresses change each iteration of appendNode. When I convert the addresses from hexadecimal to decimal I get the values 14683824, 14683848, 14685096. While the first two addresses seem to increase by 24 the third address increases by 1248. I can't figure out why this is occurring.
//Main Program
#include <iostream>
#include "numberList.h"
using namespace std;
int main()
{
numberList list;
list.appendNode(2.5);
list.appendNode(3.4);
list.appendNode(5.6);
// list.displayList();
return 0;
}
//append.cpp
#include <iostream>
#include "numberList.h"
using namespace std;
void numberList ::appendNode(double num)
{
listNode *newNode;
listNode *nodePtr;
newNode = new listNode;
newNode->value = num;
newNode->next = nullptr;
cout << newNode << endl; //The addresses of the newNode dont have even
intervals
if(!head)
{
head = newNode;
}
else
{
nodePtr = head;
while(nodePtr->next)
{
nodePtr = nodePtr->next;
}
}
}
//numberList.h
#ifndef APPENDNODE_NUMBERLIST_H
#define APPENDNODE_NUMBERLIST_H
class numberList
{
private:
struct listNode
{
double value;
struct listNode *next;
};
listNode *head;
public:
numberList()
{
head = nullptr;
}
//~numberList();
void appendNode(double);
void displayList() const;
void insertNode(double);
void deleteNode(double);
};
#endif //APPENDNODE_NUMBERLIST_H
Output:
0xe00eb0
0xe00ec8
0xe013a8
The standard doesn't give you any guarantees about the location of memory you allocate. new could give you locations that are right next to each other, but it'd also be allowed to give you locations on a completely random basis. What specifically you end up with depends on
The algorithm used for allocation by new
The pattern of previous allocations
So why did it happen this time? In general, it makes sense for successive allocations to end up close together because that decreases the chance of a cache miss, but new has no obligation to do that. In fact, because new occasionally has to request memory from the operating system, and because new has to use a bit of memory to keep track of what's been allocated, it's not possible to guarantee that all allocations happen at regular intervals.

How to pass the value from superclass to subclass in c++

I have made a superclass named "tree". I have constructed the tree in this class. Now, I want to pass the root of the constructed tree to another class which is a subclass of tree. But when I try to pass it, the subclass calls the supercalss constructor and sets it to NULL;
struct node
{
struct node *left;
struct node *right;
int val;
};
struct node *create(int val)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->val = val;
temp->left = temp->right = NULL;
return temp;
};
class tree
{
public:
struct node *root;
tree()
{
root = NULL;
}
void createtree()
{
root = create(5);
}
void preorder()
{
preorderp(root);
}
void preorderp(struct node *p)
{
if(!p) {
return;
}
cout<<p->val<<' ';
preorderp(p->left);
preorderp(p->right);
}
};
This is the definition of my tree class. It just creates a tree with one node having value 5. Now I want to pass the new root created to a subclass of tree.
class treeiterator:public tree
{
struct node *p;
stack<struct node *> s;
public:
treeiterator()
{
p = root;
push(root);
}
bool hasnext();
int next();
private:
void push(struct node *root);
};
I create an object for tree first and then do createtree. Now, when I create an object for treeiterator, it's member p gets sets to NULL since supercalss constructor is also called. How can I just access the tree created in the superclass in subclass?
Full code:
#include <bits/stdc++.h>
using namespace std;
struct node
{
struct node *left;
struct node *right;
int val;
};
struct node *create(int val)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->val = val;
temp->left = temp->right = NULL;
return temp;
};
class tree
{
public:
struct node *root;
tree()
{
root = NULL;
}
void createtree()
{
root = create(5);
}
void preorder()
{
preorderp(root);
}
void preorderp(struct node *p)
{
if(!p) {
return;
}
cout<<p->val<<' ';
preorderp(p->left);
preorderp(p->right);
}
};
class treeiterator:public tree
{
struct node *p;
stack<struct node *> s;
public:
treeiterator()
{
p = root;
push(root);
}
bool hasnext();
int next();
private:
void push(struct node *root);
};
void treeiterator::push(struct node *t)
{
while(t) {
s.push(t);
t = t->left;
}
}
bool treeiterator::hasnext()
{
return s.empty()?1:0;
}
int treeiterator::next()
{
struct node *t = s.top();
int val = t->val;
s.pop();
if(t->right) {
push(t->right);
}
return val;
}
int main()
{
tree t;
t.createtree();
t.preorder();
treeiterator it;
while(it.hasnext()) {
cout<<it.next()<<' ';
}
}
Because of inheritance every treeiterator is also a tree. This means
treeiterator treeIt;
treeIt.createtree();
will do what OP wants. There is no need to make a separate tree and moving the root around.
However this is a bit odd in the world of C++ because OP is under-using the constructor. For example, node could be:
struct node
{
node *left;
node *right;
int val;
node(int inval):
val(inval),
left(nullptr),
right(nullptr)
// the above is a Member Initializer List. It makes sure all of your
// members are initialized before the body of the constructor runs.
{
}
};
That bit after the : in the constructor is the Member Initializer List.
Now when you allocate a node it's initialized and ready to be linked. For tree
class tree
{
public:
struct node *root; // almost certainly should not be public.
// also should be a std::unique_ptr<node>
tree(int inval)
{
root = new node(5); // note new in place of malloc. new allocates
// storage and calls constructors. malloc should
// only be used in C++ in rare edge-cases.
}
/* obsolete
void createtree()
{
root = create(5);
}
*/
...
};
tree is assigned a root on allocation. treeiterator is a wee bit trickier because it must call tree's constructor to set up root.
class treeiterator:public tree
{
struct node *p; // Don't see the point off this
stack<struct node *> s; // or this, but that's another question
public:
treeiterator(int inval):
tree(inval) // call's tree's constructor
{
}
bool hasnext();
int next();
private:
void push(struct node *root);
};
Allocating a treeiterator now guarantees that it is all ready to go with no further work.
treeiterator treeIt(5); // all done.
All of the above is covered within the first few chapters of any good C++ programming text. I recommend getting one and reading it, because right now it looks like you are trying to write bad C.
Off topic 1:
You are going to quickly find that this code is in violation of the Rule Of Three. What is The Rule of Three? If you don't know, read the link. It will save you much time and hair-pulling
Off Topic 2:
#include <bits/stdc++.h>
using namespace std;
Is a ticking time bomb. The first line includes the entire standard library, but only in GCC. Your code is now doing far, far more work than it need to to compile, is no longer standard C++, and is not portable to other compilers and may well break with the next revision of GCC. Don't use anything in bits. It internal compiler-specific stuff with no guarantees what-so-ever.
More here: Why should I not #include <bits/stdc++.h>?
The second line takes everything in the std namespace and places it in the global namespace. This leads to fun games like is reverse or std::reverse being called? Often this leads to insane and arcane compiler messages because the poor compiler is confused as hell, but sometimes it's not confused and picks the best choice among the many and silently breaks something else. Great fun debugging.
More here: Why is "using namespace std" considered bad practice?
Together you have the entire standard library pulled into your file AND stripped of it's proper namespace. This results in a vast minefield of potential hidden pain that is not worth any perceived time savings. One of the resulting bugs could cost more clean up than years of typing a few extra lines per file and characters.
No one want to clean up code with this stupid a mistake, so doing this in a professional setting can be costly.
First, you should not have root has public. This is a gross OO error. If you want it to be available to subclasses you should make it protected.

Single linked lists C++

Hey guys,
I'm studying for a midterm right now and am working on trying to create a simple program using single linked list. All I want it to do is insert "1", "2", "3", "4" into the list and print it out. Please take a look at the following code:
#include <iostream>
#include <string>
using namespace std;
class node{
public:
node(int data);
friend class slist;
private:
int data;
node *next;
};
node::node(int data){
data = data;
next = NULL;
}
class slist{
public:
slist(){
head = NULL;
}
void insert(int item);
void output();
private:
node* head;
};
void slist::insert(int item){
node* newnode = new node(item);
if(head == NULL)
{
head = newnode;
}
else
{
newnode->next = head;
head = newnode;
}
}
void slist::output(){
node* p = head;
while (p->next != NULL){
cout << p->data << " ";
p = p->next;
}
cout << p->data << " ";
}
int main(){
slist FINAL;
FINAL.insert(1);
FINAL.insert(2);
FINAL.insert(3);
FINAL.insert(4);
FINAL.output();
return 0;
}
It compiles fine, but it prints out (I'm guessing) pointers instead of the actual numbers. Can anyone tell me why?
Thanks so much!
There is a bug in the node constructor. The argument data conflicts with the memeber variable named data. This should fix it:
node::node(int data){
this->data = data;
next = NULL;
}
The better option is to rename the argument to something else. Also consider using an initialization list:
node::node(int d) : data(d), next(NULL) { }
node::node(int data) {
data = data;
next = NULL;
}
The line data = data is a problem for you. Rename the parameter to be different from the member. This ought to give you a series of garbage values (not pointers) for the member variable data.
Note: this is one of the reasons for naming convention distinguishing members variables, being it traling or leading _, m_ or wherever the balance is b/w info and aesthetics.
Well, I think both instances of data in
data = data;
are local; so the data member of node never gets anything assigned to it. What's getting printed are undefined values, not addresses. That's in any case stylistically terrible; there are many conventions here but a good one is _data for the class member, and data for the function argument. Make that simple change and see if that doesn't clear up your problem.