I'm trying to teach myself c++. to do so I made a challenge for myself to write a prime finder app. I've succeeded once in python (to learn python) with a less efficient algorithm. I'm using a doubly linked list to store the primes. currently I'm just trying to run this in a single thread but I made it doubly linked so I could multithread it later on.
anyway, TL;DR the debugger is showing the program getting stuck trying to assign a value to the start link's prm int in the Prime constructor I've done a bunch of searching but I can't figure out what I'm doing wrong. (also note the bings are debug messages)
#include <iostream>
#include <math.h>
#include <cmath>
using namespace std;
using std::cout;
struct PLink{
int prm;
PLink *next;
PLink *prev;
};
class Prime{
public:
PLink *start, *end;
Prime(){
start -> prm = 2;
end -> prm = 3;
start->next = end;
end->next = NULL;
start->prev = NULL;
end->prev = start;
addToEnd(5);
cout <<"cbing" << endl;
}
void insert(int val){
}
void addToEnd(int val){//adds a new prime to the end of the list
PLink *tmp = new PLink;
tmp->prm = val;
tmp->prev = end;
end->next = tmp;
tmp->next = NULL;
tmp = end;
cout << tmp->prm << endl;
cout << "addbing" << endl;
}
bool comp(int pot){ //compares the potential prime against known primes via modulo
int lim = sqrt(pot);
PLink * current = start;
bool check = false;
cout<<"bing " << pot << endl;
while (current->prm < lim && check == false){
if (pot%current->prm == 0) {
check = true;}
current = current->next;
}
return check; //false means its prime true means its not
}
};
int main()
{
Prime primeList;
int cap = 10000;
int beg = 5;
int count = 3;
bool toggle = false;
bool check = false;
cout << "2 \n3 \n5" << endl;
while(count < cap){
beg += 2;
cout << "bing" << endl;
if (toggle){
beg += 2;}
toggle = !toggle;
check = primeList.comp(beg);
if (check == false){
primeList.addToEnd(beg);
count++;
cout << "bing2" << endl;
}
}
};
using namespace std;
using std::cout;
the second using std::cout; is redundant, you can read some documents about C++ name visibility, like this:
http://www.cplusplus.com/doc/tutorial/namespaces/
http://www.tutorialspoint.com/cplusplus/cpp_namespaces.htm
Prime(){
start -> prm = 2;
end -> prm = 3;
start->next = end;
end->next = NULL;
start->prev = NULL;
end->prev = start;
addToEnd(5);
cout <<"cbing" << endl;
}
Note: when you declare a pointer like PLink *start, *end; C++ complier(say 'gcc' or clang) only allocate memory to store that pointer, but not allocate memory to store what your pointer is pointed(here it means your PLink object).
So, you should allocate memory for your PLink object pointed by these two pointers: PLink *start, *end;, that is to say, you have to change the above code to:
Prime(){
start = new PLink(); // use the default constructor generated by C++ complier since you haven't declared one in struct PLink
end = new PLink()
start -> prm = 2;
end -> prm = 3;
start->next = end;
end->next = NULL;
start->prev = NULL;
end->prev = start;
addToEnd(5);
cout <<"cbing" << endl;
}
Well, in order not to cause memory leak and double free the same pointer, you should carefully manipulate the object you created.
Related
Singly Linked List and Node classes and the start of the main function, where I wrote a brief outline of the code functionality. The issue is toward the end of the main function. I wrote '...' in place of what I believe to be irrelevant code because it simply parses strings and assigns them to the string temp_hold[3] array.
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
string value;
string attr;
string tagname;
Node *next;
Node(string c_tagname, string c_attr, string c_value) {
this->attr = c_attr;
this->value = c_value;
this->tagname = c_tagname;
this->next = nullptr;
}
};
class SinglyLinkedList {
public:
Node *head;
Node *tail;
SinglyLinkedList() {
this->head = nullptr;
this->tail = nullptr;
}
void insert_node(string c_tagname, string c_attr,string c_value) {
Node *node = new Node(c_tagname,c_attr, c_value);
if (!this->head) {
this->head = node;
} else {
this->tail->next = node;
}
this->tail = node;
}
};
int main(int argc, char **argv) {
/* storage is a vector holding pointers to the linked lists
linked lists are created and the linked list iterator sll_itr is incremented when
previous line begins with '</' and the currentline begins with '<'
linked lists have nodes, which have strings corresponding to tagname, value, and attribute
*/
SinglyLinkedList *llist = new SinglyLinkedList();
vector<SinglyLinkedList*> sllVect;
sllVect.push_back(llist);
auto sll_itr = sllVect.begin();
string temp_hold[3];
// to determine new sll creation
bool prev = false;
bool now = false;
//input
int num1, num2;
cin >> num1; cin >> num2;
//read input in
for (int i = 0; i <= num1; ++i) {
string line1, test1;
getline(cin, line1);
test1 = line1.substr(line1.find("<") + 1);
//determine to create a new linked list or wait
if (test1[0] == '/') {
prev = now;
now = true;
} else {
//make a node for the data and add to current linked list
if (i > 0) {
prev = now;
now = false;
//if last statement starts with '</' and current statment starts with '<'
// then start a new sll and increment pointer to vector<SinglyLinkedList*>
if (prev && !now) {
SinglyLinkedList *llisttemp = new SinglyLinkedList();
sllVect.push_back(llisttemp);
sll_itr++;
}
}
//parse strings from line
int j = 0;
vector<string> datastr;
vector<char> data;
char test = test1[j];
while (test) {
if (isspace(test) || test == '>') {
string temp_for_vect(data.begin(),data.end());
if (!temp_for_vect.empty()) {
datastr.push_back(temp_for_vect);
}
data.clear();
} else
if (!isalnum(test)) {
} else {
data.push_back(test);
}
j++;
test = test1[j];
}
//each node has 3 strings to fill
int count = 0;
for (auto itrs = datastr.begin(); itrs!=datastr.end(); ++itrs) {
switch (count) {
case 0:
temp_hold[count]=(*itrs);
break;
case 1:
temp_hold[count]=(*itrs);
break;
case 2:
temp_hold[count]=(*itrs);
break;
default:
break;
}
count++;
}
}
cout << "before storing node" << endl;
(*sll_itr)->insert_node(temp_hold[0], temp_hold[1], temp_hold[2]);
cout << "after" << endl;
}
cout << "AFTER ELSE" << endl;
return 0;
}
And here is the line that breaks the code. The auto sll_itr is dereferenced which means *sll_itr is now a SinglyLinkedList* and we can call the insert_node(string, string, string) to add a node to the current linked list. However when I keep the line, anything after the else statement brace does not run, which means the cout<<"AFTER ELSE"<< endl; does not fire. If I remove the insert_node line, then the program runs the cout<<"AFTER ELSE"<< endl; I am unsure what the issue is.
(*sll_itr)->insert_node(temp_hold[0],temp_hold[1],temp_hold[2]);
cout << "after" << endl;
} //NOT HANGING. This closes an else statement.
cout << "AFTER ELSE" << endl;
return 0;
}
Compiled as g++ -o myll mylinkedlist.cpp and then myll.exe < input.txt And input.txt contains
8 3
<tag1 value = "HelloWorld">
<tag2 name = "Name2">
</tag2>
</tag1>
<tag5 name = "Name5">
</tag5>
<tag6 name = "Name6">
</tag6>
Your linked list isn't the problem, at least not the problem here.
A recipe for disaster in the making: retaining, referencing, and potentially manipulating, an iterator on a dynamic collection that potentially invalidates iterators on container-modification. Your code does just that. tossing out all the cruft between:
vector<SinglyLinkedList*> sllVect;
sllVect.push_back(llist);
auto sll_itr = sllVect.begin();
....
SinglyLinkedList *llisttemp = new SinglyLinkedList();
sllVect.push_back(llisttemp); // HERE: INVALIDATES sll_iter on internal resize
sll_itr++; // HERE: NO LONGER GUARANTEED VALID; operator++ CAN INVOKE UB
To address this, you have two choices:
Use a container that doesn't invalidate iterators on push_back. There are really only two sequence containers that fit that description: std::forward_list and std::list.
Alter your algorithm to reference by index`, not by iterator. I.e. man your loop to iterate until the indexed element reaches end-of-container, then break.
An excellent discussion about containers that do/do-not invalidate pointers and iterators can be found here. It's worth a read.
Working on a program to solve the pouring problem:
I believe I am down to one last issue. My data structure is as follows:
I have an vector of Node pointers and each node contains a int array, and an address to the next node. In testing everything functions properly. The goal of this data structure is to basically function as an adjacency list. Where each node is linked to the nodes that it would have an edge to.
Currently my problem is when I am attempting to link these nodes to one another:
the LinkState function that I have should accomplish this, however it is instead resulting in the program running...forever.
The function should simply iterate through the individual nodes linked list and find where to connect the new node. Instead it is causing a node to constantly be leak to itself..which is leading to the runtime issue.
Sorry if this is a bit confusing. Any help would be greatly appreciated.
p.s. I know there are better ways to solve this problem like BFS, I'd like to stick to DFS.
#ifndef _POURINGLIST_H_
#define _POURINGLIST_H_
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
struct Node{
int state[3];
Node* next = NULL;
};
class PouringList{
Node* init;
vector<Node*> Head;
int max[3];
int steps;
public:
PouringList(){
//max values for comaprison
max[0] = 10;
max[1] = 7;
max[2] = 4;
//init values to begin DFS
init = new Node;
init->state[0] = 0;
init->state[1] = 7;
init->state[2] = 4;
};
//private methods not to be called by user
private:
//pours some good old h2o
Node pour(Node* curr_state, int A, int B){
int a = curr_state->state[A];
int b = curr_state->state[B];
int change = min(a, max[B]-b);
Node newState = *curr_state;
newState.state[A] = (a-=change);
newState.state[B] = (b+=change);
return newState;
}
//O(n) complexity used to check if a node is already in head
bool isIn(Node* find_me){
for(vector<Node*>::iterator i = Head.begin(); i != Head.end(); i++) {
if (equal(begin(find_me->state), end(find_me->state), begin((*i)->state)))
return true;
}
return false;
}
void printNode(Node* print){
for(int i = 0; i < 3; i++){
cout << print->state[i] << " ";
}
cout << endl;
}
int locate(Node* find_me){
for(vector<Node*>::iterator i = Head.begin(); i != Head.end(); i++) {
if (equal(begin(find_me->state), end(find_me->state), begin((*i)->state)))
return distance(Head.begin(), i);
}
return -1;
}
void LinkState(Node* head, Node * nxt){
Node* vert = Head[locate(head)];
while(vert->next != NULL){
vert = vert->next;
}
vert->next = nxt;
}
public:
void DFS(){
steps = 0;
//start exploring at initial value
explore(init);
}
void explore(Node* vertex){
//base case to end
if(!isIn(vertex)){
Head.push_back(vertex);
if(vertex->state[1] == 2 || vertex->state[2] == 2){
cout << steps << endl;
printNode(vertex);
return;
}
//generate all possible states and connects them to Head vertex
else{
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
Node conn1 = pour(vertex,i,j);
Node *conn = &conn1;
if(i!=j && !isIn(conn)){
cout << i << " adds water to " << j << endl;
LinkState(vertex, conn);
}
}
}
}
Node* Nextex = vertex;
//printNode(vertex);
while(Nextex != NULL){
//new neighbor
if(!isIn(Nextex)){
//printNode(Nextex);
explore(Nextex);
}
Nextex = Nextex->next;
}
}
//printNode(Nextex);
else{
cout <<"Dead end" << endl;
}
}
//start from init node and show path to solution
void display(){
Node *output;
for(int i = 0; i < Head.size(); i++){
output = Head[i];
while ( output != NULL){
printNode(output);
output = output->next;
}
cout << '#' <<endl;
}
}
};
#endif // _POURINGLIST_
basic driver:
#include "PouringList.h"
int main(){
PouringList s1;
s1.DFS();
}
Edit
I've attempted the suggested fix before (This is what I'm assuming you mean). It still lead to the programming running forever. Also I do not know enough about smartpointers to go and overhaul the application!
Node conn1 = pour(vertex,i,
Node *conn = new Node;
conn = &conn1;
You are storing the address of a local variable in your list.
In explore, you have
Node conn1 = pour(vertex,i,j);
Node *conn = &conn1;
then later pass conn to LinkState, which stores that pointer in your PouringList. All your added nodes will point at the same memory address.
What you should be doing is allocating a new Node and using that (preferably using some sort of smart pointer rather than storing raw pointers so the clean up will happen automatically).
I am trying to evaluate the time difference between looping in a linked list with an operation on each element in these two scenarios:
1) Doing the operation inside a function
2) Doing the operation without a function call in the same place
I was expecting that the variation with the function call with be a lot costlier due to the OS overhead of creating and destroying a stackframe for every call, but the results I got was just the opposite. I could not understand why. Could someone please explain what happened?
This is my program:
// ConsoleApplication4.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include<iostream>
#include<chrono>
#include<stdlib.h>
#define _CRT_SECURE_NO_WARNINGS
class linked_list_node
{
int a;
public :
std::string var;
bool eval()
{
if (var == "abc")
return true;
return false;
}
linked_list_node() { a = rand() % 100; if (a % 2 == 0) var = "abc"; }
linked_list_node* nxt;
std::string getVar() { return var; }
linked_list_node* getNext()
{
return nxt;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
linked_list_node *head = new linked_list_node();
linked_list_node *trav = head;
int len = 75000;
while (len != 0)
{
linked_list_node *n = new linked_list_node();
trav->nxt = n;
trav = n;
len--;
}
trav->nxt = NULL;
//traversal with function
int length = 0;
trav = head;
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
while (trav != NULL)
{
length++;
if (trav->eval())
std::cout << "";
trav = trav->nxt;
}
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::cout << "Time difference with function == " << std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count() << std::endl;
//traversal without function
trav = head;
length = 0;
begin = std::chrono::steady_clock::now();
while (trav != NULL)
{
length++;
if (trav->var =="abc")
std::cout << "";
trav = trav->nxt;
}
end = std::chrono::steady_clock::now();
std::cout << "Time difference without function = " << std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count() << std::endl;
getchar();
return 0;
}
These are my results :
Time difference with function == 18100
Time difference without function = 33700000
First difference:
In the function, you are comparing the value of var to"abc".
In the non-function call code, you are comparing the value of var to "ram".
The second difference, the most important one:
In the first case, you are using std::chrono::microseconds.
In the second case, you are using std::chrono::nanoseconds
After I fix those errors, I get consistently lower value for the second number than the first one.
You're measuring time in different units: std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin) vs std::chrono::duration_cast<std::chrono::microseconds>(end - begin).
I have to implement a queue from the scratch for an assignment without using any premade lib. It's working fine when I Enqueue e Dequeue, but when I Dequeue a unary queue (with just one element) and I print it, doesn't show that it is empty, the Print function actually print blank spaces, and when try to Enqueue new elements after Dequeuing the whole list it simply does not push new elements. Looks like that the reference to the first element got lost. If someone could help, here is the code:
structs:
typedef struct{
int number;
}TItem;
typedef struct cell{
struct cell *pNext;
TItem item;
}TCell;
typedef struct{
TCell *pFirst;
TCell *pLast;
}TQueue;
And the imlementations:
void Init(TQueue* pQueue){
pQueue->pFirst = new TCell;
pQueue->pLast = pQueue->pFirst;
pQueue->pFirst->pNext = NULL;
}
int Is_Empty(TQueue* pQueue){
return (pQueue->pFirst == pQueue->pLast);
}
int Enqueue(TQueue* pQueue, TItem x){
pQueue->pLast->pNext = new TCell;
pQueue->pLast = pQueue->pLast->pNext;
pQueue->pLast->item = x;
pQueue->pLast->pNext = NULL;
return 1;
}
int Dequeue(TQueue* pQueue, TItem* pX){
if(Is_Empty(pQueue))
return 0;
TCell* aux;
aux = pQueue->pFirst->pNext;
*pX = aux->item;
pQueue->pFirst->pNext = aux->pNext;
delete aux;
return 1;
}
void Print(TQueue* pQueue){
if(Is_Empty(pQueue) == 1)
cout << "EMPTY"<<endl;
TCell *temp;
temp = pQueue->pFirst->pNext;
cout << "Queue:"<<endl;
while( temp != NULL){
cout << temp->item.number << " ";
temp = temp->pNext;
}
}
PS: The memory allocation for the queue is made on the main block
Problem
Your Dequeue logic is invalid. All you need to do when dequeing is remove the first item in the queue.
Solution
int Dequeue(TQueue* pQueue, TItem* pX){
if(Is_Empty(pQueue))
return 0;
TCell* aux;
//aux = pQueue->pFirst->pNext;
aux = pQueue->pFirst;
//*pX = aux->item;
//pQueue->pFirst->pNext = aux->pNext;
pQueue->pFirst = aux->pNext;
delete aux;
return 1;
}
Also I don't know why you need to pass TItem* pX as you don't really use it.
Apologies if this is a silly / simple question.. but I'm very lost. I'm having trouble getting this program to run. I've written this program to read in 2 values, the first being a number of elements in a linked list, and the second to be the maximum random value that can be put into each element.
It should then use the merge sort algorithm included to sort and reprint the sorted list.
Ok, so I'm getting errors like:
base operand of `->' has non-pointer type `LIST'
and
request for member `element' in `conductor', which is of non-aggregate type `LIST *'
...(and a few others).
Yes this is for a class.. I've written the program but I'm not sure what I've done wrong here or why I'm getting errors? Any help is appreciated! Thank you
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <sys/time.h>
using namespace std;
typedef struct LIST {
int element;
LIST *next;
};
LIST split(LIST list)
{
LIST pSecondCell;
if (list == NULL)
return NULL;
else if (list.next == NULL)
return NULL;
else {
pSecondCell = list.next;
list.next = pSecondCell.next;
pSecondCell.next = split(pSecondCell->next);
return pSecondCell;
}
}
LIST merge(LIST list1, LIST list2)
{
if (list1 == NULL)
return list2;
else if (list2 == NULL)
return list1;
else if (list1.element <= list2.element) {
list1.next = merge(list1.next, list2);
return list1;
} else {
list2.next = merge(list1, list2.next);
}
}
LIST MergeSort(LIST list)
{
LIST SecondList;
if (list == NULL)
return NULL;
else if (list.next == NULL)
return list;
else {
SecondList = split(list);
return merge(MergeSort(list), MergeSort(SecondList));
}
}
int main(int argCount, char *argVal[])
{
int i, number, max;
struct timeval time1;
struct timeval time2;
//check for correct number of arguments
if (argCount != 3) {
cout << "Incorrect number of arguments" << endl;
return 0;
}
// initialize read in n and max values
number = atoi(argVal[1]);
max = atoi(argVal[2]);
// create list and fill with random numbers
LIST *conductor;
LIST *root = new LIST;
conductor = root;
for (i = 0; i < number; i++) {
conductor.element = rand() % max;
conductor.next = new LIST;
conductor = conductor.next;
}
// time how long it takes to sort array using mergeSort
gettimeofday(&time1, NULL);
mergeSort(root);
gettimeofday(&time2, NULL);
// print name, sorted array, and running time
cout << "Heather Wilson" << endl;
conductor = root;
for (i = 0; i < number - 2; i++) {
cout << conductor.element << ", ";
conductor = conductor.next;
}
double micro1 = time1.tv_sec * 1000000 + time1.tv_usec;
double micro2 = time2.tv_sec * 1000000 + time2.tv_usec;
cout << conductor.element << endl;
cout << "Running time: " << micro2 - micro1 << " microseconds" << endl;
return 0;
}
For base operand of->' has non-pointer type LIST'
Replace the -> with a .. You want to access a member of a local LIST, not a member of a pointed at object.
request for memberelement' in conductor', which is of non-aggregate type LIST *
This is the opposite. Replace the . with a ->. You want to access a member of the pointed at LIST, not a member of the pointer.
For clarification, I didn't read the code. There's too much of it. But those are the usual ways to address those specific errors. parapura seems to have actually read the code.
First: you should never have let the code grow this big with so many errors. You should start small and simple, then build up, testing at every stage, and never add to code that doesn't work.
Here's a stripped-down beginning of your code, with some bugs fixed:
#include <iostream>
using namespace std;
typedef struct LIST{
int element;
LIST *next;
};
int main(){
int i, number, max;
number = 5;
max = 100;
// create list and fill with random numbers
LIST *conductor;
LIST *root = new LIST;
conductor = root;
for(i=0; i<number; i++){
conductor->element = rand() % max;
cout << "element " << i << " is " << conductor->element << endl;
conductor->next = new LIST;
conductor = conductor->next;
}
conductor = root; // Forgot this, didn't you!
for(i=0; i<number-2;i++){
cout << conductor->element << ", ";
conductor = conductor->next;
}
return 0;
}
Take a look at this, verify that it works, make sure you understand the changes I made, then you can take a crack at implementing your split, merge and MergeSort functions and the I/O (one at a time, and testing at every stage, naturally).
I think all the places you are passing
LIST merge ( LIST list1 , LIST list2 )
it should be
LIST* merge ( LIST* list1 , LIST* list2 )