In this program I am cloning a graph in c++.
This is the question that i am coding.
This is my program and following it the issue and problem area.
#include <vector>
#include <unordered_map>
#include <queue>
#include <iostream>
using namespace std;
#define neighbours neighbors
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> neighbors;
Node() {
val = 0;
neighbors = vector<Node*>();
}
Node(int _val) {
val = _val;
neighbors = vector<Node*>();
}
Node(int _val, vector<Node*> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
#define neighbours neighbors
class Solution {
public:
Node* cloneGraph(Node* node)
{
// BFS
queue <Node*> q;
// add starting vec
q.push(node);
Node *curr;
int value;
vector <int> visited;
unordered_map <int, Node*> umap;
while(!q.empty())
{
curr = q.front();
q.pop();
visited.push_back(curr->val);
//cout << curr->val << "-";
// create new node
Node *newNode = new Node(curr->val);
// add new node val and addr to umap
umap[value] = newNode;
// clone neighbour list
vector <Node*> nlist;
//vector <Node*> list = curr->neighbours; // make copy of given list
for(Node* node: curr->neighbours)
{
value = node->val;
//cout << value << " ";
// search in map first, if exists take addr else make and insert into
// list
if(umap.find(value) == umap.end())
{
umap[value] = new Node(value);
}
nlist.push_back(umap[value]);
if(find(visited.begin(), visited.end(), value) == visited.end())
{
q.push(node);
}
}
cout << endl;
newNode->neighbours = nlist; // copy nlist to nodes list part
}
// starting of new node = umap[1];
return umap[1];
}
};
int main()
{
Node ob1;
Node ob2;
ob1.val = 1;
ob2.val = 2;
vector <Node*> nlist;
nlist.push_back(&ob1);
ob2.neighbours = nlist;
nlist.pop_back();
nlist.push_back(&ob2);
ob1.neighbours = nlist;
Solution obj;
Node *sv = obj.cloneGraph(&ob1);
cout << sv->val << "-";
for(Node *node : sv->neighbours)
{
cout << node->val << endl;
}
cout << &ob1 << " " << sv << endl;
}
When I comment out the last line my out put is
1-2
When I leave it as it is then the output is
1-0x7ffee561e4e0 0x7fb1ba402840
Also I just tried to comment out the cout << endl; which is towards the end of the while loop and also commented out the last statement of my program which gives an output
1-
What is the reason for such strange behaviour?
umap[value] = newNode; exhibits undefined behavior, by way of accessing an uninitialized variable value.
In practice, value contains some garbage that happens to sit on the stack. Changes to seemingly unrelated parts of the program affect the contents of the stack, and thereby the initial value of value, and ultimately the way the aforementioned undefined behavior manifests itself.
Related
I'm working on a class project and this piece of code won't let me delete an instance of a class without throwing a breakpoint error.
The class is Node, I'm trying to build a singly linked list for data structures and algorithms. I'll include the whole program, it isn't long, but the code in question that's causing the problem is in deleteMin(), the delete u.
#include <iostream>
using namespace std;
// we create a Node class.
class Node { // inside this class we hold two pieces of information.
int x; // the integer x, which is our data.
Node* next; // and the address of the next node.
public:
Node(int x0) : x(x0), next(NULL) { } // Here I've set up a constructor that sets x equal to the argument
// and defaults the next address to NULL.
bool add(int newValue); // Here follows our three required functions.
int deleteMin();
int size();
void printSSL(); // I also added a printSSL() function so that we can test and see what's going on.
};
//Originally I wanted to put these inside of the Node class, but then you'd end up with a unique head and tail for every Node.
//So instead I've left them outside. If you wanted to create multiple SSList's then you'd want to create an object out of these as well.
Node* head; // The first value in the our SLList.
Node* tail; // The last value in our SLList.
int n; // The number of elements in the list.
// I should mention here that head and tail are set equal to the first value in the SLList in the Main() function below.
// Here follows the actual implementation.
// I chose to try and simplify things by focusing on the add() function.
// If the add function organizes the information, and puts it into the SLList in order,
//then deleteMin() only has to pull the first value.
bool Node::add(int newValue) { // add() is a member function of Node and it takes in the newValue were adding to the list.
Node* u = new Node(newValue); // First thing we do is create a new Node using the argument value x. We pass this into a pointer, u.
if (newValue <= head->x) { // Next, we check to see if the new value is less than the head.
u->next = head; // if it is, then our job is done and we just make this new, smaller value, the new head.
head = u; // we do this by making the initial head equal to the next address in the new Node u.
n++; // Once we have the address saved, we make u into the new head and increment n.
return true; // There's no iteration in this case, so this if statement would be O(1).
}//O(1)
else { // If the new value is greater than the head, then we have to store it further back in the SLList.
Node* y = head; // This was the hardest part of the whole thing... I solved it by creating two Node pointers,
Node* z = head; // Both Node pointers are set equal to head, but this is mostly just to ensure that they aren't empty.
while ((newValue > y->x) && (y != tail)) { // Then I set a while loop that looks at whether the new value is less than the value in the head.
z = y; // The while loop continues, moving through the list of values, setting y equal to the next value,
y = y->next; // and using z to keep track of the former value.
} // The loop exits when we either a) hit the end of the SLList, y == tail, or when the new value is
if (y == tail) { // smaller than the next value, newValue < y->x. When the loop exits we have to deal with these two
y->next = u; // scenarios separately. If we reached the end of our list, then adding the new value is as simple as
tail = u; // setting y->next equal to u, then we make u into the new tail.
} // if we didn't reach the end of the list, then we have to set u inbetween z and y. This was really
else { // the only reason I needed z. I needed to be able to update the address of the previous Node, and
z->next = u; // I also needed to have the address of the next Node, this way I could slip u inbetween the two.
u->next = y; // Worst case scenario, this function runs through the entire SLList and then adds the value at the end.
} // I could have shaved off some time by asking if(newValue > tail->x) then run the z->next=u; and u->next=y; after
n++; // after that, but that throws an error becauset ail is a NULL pointer, which is bull#*#!
return true; // because I'm not dealing the tail->next, all I care about is tail->x which isn't NULL.
}//O(n) // But because this is an SSList and not a DLList I have no way of going to the n-2 element.
}//O(max(1, n)) // When that's done, we just increment n and leave the function.
// Considering that the worst case scenario, when x > tail->x, takes us through the whole SLList.
// I'm going to say that this add function is O(n).
int Node::deleteMin() { // The deleteMin() function starts by checking whether or not the
int x = head->x;
Node* u = head;
head = head->next;
delete u; // I have to figure out what the hells going on right here, why can't I delete this?
return x;
}
int Node::size() {
cout << n + 1 << endl;
return n + 1;
}
void Node::printSSL() {
Node* u = head;
cout << "Head:";
for (int i = 0; i <= n; i++) {
cout << i << ":(" << u->x << ", " << u->next << ") ";
u = u->next;
}
cout << " Tail" << endl;
}
int main()
{
Node one(1);
head = &one;
tail = &one;
one.printSSL();
one.deleteMin();
}
You declared an object of the type Node
Node one(1);
You may not apply the operator delete to a pointer to the object because the object was not allocated dynamically. It has automatic storage duration.
Pay attention to that it is a bad idea when functions depend on global variables. For example you will be unable to define two lists in your program.
What you need is to define a class named something like List the following way
class List
{
private:
Node *head = nullptr, *tail = nullptr;
public:
// special member functions and some other member functions;
void clear();
~List() { clear(); }
};
and to allocate nodes dynamically that will be inserted in the list.
The destructor and the function clear will delete all the allocated nodes in the list.
class Node also should be defined as a nested class of the class List.
For example the function clear can be defined the following way
#include <functional>
//...
void List::clear()
{
while ( head )
{
delete std::exchange( head, head->next );
}
tail = nullptr;
}
#include <iostream>
using namespace std;
class SLList { // SLList is the object that holds the entire list.
public: // The Node class lives inside the SLList class.
struct Node {
int data;
Node* next;
Node(int x0) : data(x0), next(NULL) {}
Node() : data(NULL), next(NULL) {}
};
Node* head;
Node* tail;
int n;
SLList() : n(0) {
Node* initial = new Node();
head = initial;
tail = initial;
cout << "You've created a new SSList" << endl;
}
bool add(int newValue);
int deleteMin();
int size();
void printSSL();
};
bool SLList::add(int newValue) { //
if (n == 0) {
head->data = newValue;
n++;
return true;
}
else {
Node* u = new Node(newValue);
if (newValue <= head->data) { //
u->next = head; //
head = u; //
n++; //
return true; //
}//O(1)
else { //
Node* y = head; //
Node* z = head; //
while ((newValue > y->data) && (y != tail)) { //
z = y; //
y = y->next; //
} //
if (y == tail && newValue > y->data) {
y->next = u; //
tail = u; //
} //
else { //
z->next = u; //
u->next = y; //
} //
n++; //
return true;
}
}//O(n) //
}//O(max(1, n)) //
int SLList::deleteMin() {
int x = head->data;
Node* u = head;
head = head->next;
delete u;
n--;
return x;
}//O(1)
int SLList::size() {
cout << n + 1 << endl;
return n + 1;
}//O(1)
void SLList::printSSL() {
Node* u = head;
cout << n << " Nodes|" << "Head:";
for (int i = 0; i < n; i++) {
cout << i << ":(" << u->data << ", " << u->next << ") ";
u = u->next;
}
cout << " Tail" << endl;
}//O(n)
int main() {
SLList* one = new SLList;
one->printSSL();
one->add(30);
one->printSSL();
one->add(20);
one->printSSL();
for (int i = 0; i < 7; i++) {
int x = rand() % 50;
one->add(x);
one->printSSL();
}
for (int i = 0; i < 9; i++) {
one->deleteMin();
one->printSSL();
}
}
This question already has answers here:
Undefined, unspecified and implementation-defined behavior
(9 answers)
Closed 1 year ago.
The node Class is as follow:
class node
{
public:
int data; //the datum
node *next; //a pointer pointing to node data type
};
The PrintList Function is as follow:
void PrintList(node *n)
{ while (n != NULL)
{
cout << n->data << endl;
n = n->next;
}
}
If I try running it I get all three values (1,2,3) but I get an additional number as well which I'm unable to figure out what it represents, Can someone throw light on the same?
int main()
{
node first, second, third;
node *head = &first;
node *tail = &third;
first.data = 1;
first.next = &second;
second.data = 2;
second.next = &third;
third.data = 3;
PrintList(head);
}
I Know it can be fixed with
third.next = NULL;
But I am just curious what does this number represents in output, If I omit the above line
1
2
3
1963060099
As described in the comment by prapin, third.next is not initialized.
C++ has a zero-overhead rule.
Automatically initializing a variable would violate this rule as the value might be initialized (a second time) later on or never even be used.
The value of third.next is just the data that happened to live in the same memory location as third.next does now.
For this reason, it's recommended to always initialize your variables yourself.
It is better to initialize variables & it is better to use nullptr. Like that (See 1-3):
#include <iostream>
class node
{
public:
int data = 0; // 1
node* next = nullptr; // 2
};
void PrintList(node* n)
{
while (n != nullptr) // 3
{
std::cout << n->data << std::endl;
n = n->next;
}
}
int main()
{
node first, second, third;
node* head = &first;
node* tail = &third;
first.data = 1;
first.next = &second;
second.data = 2;
second.next = &third;
third.data = 3;
// third.next points to where?
PrintList(head);
}
Additional note:
I would prefer to use the STL container std::list:
#include <list>
#include <iostream>
std::list<int> int_list;
void PrintList()
{
for (auto i : int_list)
std::cout << i << std::endl;
}
int main()
{
int_list.push_back(1);
int_list.push_back(2);
int_list.push_back(3);
PrintList();
}
Or in case of list of node objects:
#include <list>
#include <iostream>
class node
{
public:
node(int data) : m_data{ data } {};
int m_data = 0;
// and maybe extra data-members
};
std::list<node> node_list;
void PrintList()
{
for (auto i : node_list)
std::cout << i.m_data << std::endl;
}
int main()
{
node_list.push_back(node(1));
node_list.push_back(node(2));
node_list.push_back(node(3));
PrintList();
}
I don't understand what's wrong with my code here:
//Inserting n nodes, then print their values
#include <iostream>
#include <string>
using namespace std;
//Defining a node and it's head pointer
struct node
{
int data;
node *next;
};
node *head=NULL;
node *link;
node *tmp;
int main()
{
int n;
cin>>n;
while (n>0)
{
//Insert n nodes into the list
link=new node;
if (head==NULL)
{
head=link;
}
cin>>link->data;
link=link->next;
n--;
}
link=NULL;
//print data present in those n nodes
tmp=head;
while (tmp!=NULL)
{
cout<<tmp->data;
tmp=tmp->next;
}
return 0;
}
The first part of the code defines a node.
The second part (beginning of main function) is code for creating a linked list with n nodes in it. So I insert n nodes.
Finally, I output them using the pointer tmp. However, instead of getting the data values, I just get an infinite loop. What is going on here?
Thanks
#include <iostream>
#include <string>
using namespace std;
//Defining a node and it's head pointer
struct node
{
int data;
node *next=NULL;
};
node *head=NULL;
node *link;
node *tmp;
int main()
{
int n,limit;
cin>>n;
limit=n;
while (n>0)
{
tmp=link;
link=new node;
link->next=NULL;
cin>>link->data;
if (head==NULL)
{
head=link;
}
if(n!=limit) //check whether tmp is null initially tmp will be null for first element
{
tmp->next=link;
}
n--;
}
//print data present in those n nodes
tmp=head;
while (tmp!=NULL)
{
cout<<tmp->data<<"\n";
tmp=tmp->next;
}
return 0;
}
This code does what you want using std::list. I don't see why you should go on create your own crude and unsafe linked list. (I don't even see why you should use a linked list; in this particular a std::vector would be a more reasonable choice).
#include <iostream>
#include <list>
int main() {
std::list<int> linked_list;
// fill the list
std::cout << "Please enter the number of elements in the list: " << std::endl;
int n {};
std::cin>>n;
while (n-- > 0) {
std::cout << "Please enter the data for this item: " << std::endl;
int data {};
std::cin>>data;
linked_list.push_back(data);
}
// print out data the list
for (auto& data : linked_list)
std::cout << "printing data entry: " << data << std::endl;
return 0;
}
First of all it is c++. I recommend to use std::list:
#include <iostream>
#include <list> // list
int main()
{
int n;
std::cin >> n;
std::list<int> l;
while ( l.size() < n )
{
int data;
std::cin >> data;
l.push_back( data );
}
for ( int data : l )
std::cout << data << std::endl;
return 0;
}
But if you want to di it yourself you have to remember the tail of your list and add each new node as successor of the tail node. Further you should delete the list at the end of your program:
#include <iostream>
struct node
{
int data;
node *next;
};
node *head = NULL;
int main()
{
int n;
std::cin >> n;
node *tail = NULL;
while ( n>0 )
{
node *link = new node;
link->next = NULL; // successor of new node is NULL
std::cin >> link->data;
if ( head==NULL)
head = link; // if list is empty new node is head of list
else
tail->next = link; // successor of tail is new node
tail = link;
n--;
}
node *tmp = head;
while ( tmp != NULL) // delete the list from head to tail
{
std::cout << tmp->data << std::endl;
tmp = tmp->next;
}
while ( head != NULL)
{
tmp = head->next;
delete head;
head = tmp;
}
return 0;
}
I'm trying to wrap my head around how to write an algorithm to sort a linked list, but I'm having a hard time coming up with something that will work. What we need to do have a linked list that contains a name in a string, and an int for hours. After displaying the list and the sum of the hours, we then have to sort the list in ascending order by the hours in a queue. I have the list and all it's functioned stored in a class object, as you will see. I cleared the whole function of what I had in hopes of coming up with a fresh idea but nothing is coming to mind. I initially was going to create a second linked list that had the sorted list, but then I began to wonder if it was possible to sort it within the same list. Here is my code as of posting.
#include <iostream>
#include <ctime>
using namespace std;
// OrderedLL class
template<class T>
class OrderedLL
{
private:
struct NODE
{
string sName;
int sHours;
NODE *next;
};
NODE *list;
NODE *rear;
public:
// Constructor
OrderedLL () { list = rear = NULL;}
// Insert item x -------------------------------------
void Insert(string x, int y)
{
NODE *r;
// Create a new node
r = new(NODE); r->sName = x; r->sHours = y;
r->next = NULL;
// Inserts the item into the list
r->next = list;
list = r;
}
// Display the linked list --------------------------
void display()
{ NODE *p = list;
while( p != NULL)
{ cout << p->sName << "/" << p->sHours << "-->"; p = p->next;}
cout << "NULL\n";
}
// Delete x from the linked list --------------------
void DeleteNode(T x)
{
NODE *p = list, *r = list;
while( p->info != x) {r=p; p=p->next; }
if( p == list)
{ // delete the first node
list = p->next; delete(p);
}
else
{ r->next = p->next; delete(p);}
}
// Sort by hours ------------------------------------
void sortHours()
{
NODE *p, *q;
}
// Display the total hours --------------------------
friend T totHours(OrderedLL LL)
{
NODE *p;
int total = 0;
p = LL.list;
while(p != NULL)
{
total += p->sHours;
p = p->next;
}
cout << "Total spending time = " << total << endl;
}
}; // end of OrderedLL class
int main(void)
{
// Declare variables
time_t a;
OrderedLL<int> unsortedLL;
OrderedLL<int> sortedLL;
int inHours;
string inName;
// Displays the current time and date
time(&a);
cout << "Today is " << ctime(&a) << endl;
// Asks the user to enter a name and hours 5 times, inserting each entry
// into the queue
for(int i = 0; i < 5; i++)
{
cout << "Enter name and Time: ";
cin >> inName >> inHours;
unsortedLL.Insert(inName, inHours);
}
// Displays the unsorted list
cout << "\nWaiting List-->";
unsortedLL.display();
totHours(unsortedLL);
// Calls for the function to sort the list into a queue by hours
unsortedLL.sortHours();
unsortedLL.display();
return 0;
} // End of "main"
As always thanks to anyone who can help
Try sorting the linked-list like you are sorting an integer array. Instead of swapping the nodes, swap the contents inside the nodes.
If you don't care about efficiency you can use any sorting algorithm. What's different between linked lists and arrays is that the swap operation, to swap the position of two elements while sorting, will be a lot slower because it has to run through the links of the list.
An O(n²) bubble sort is easy to implement with a linked list since it only swaps an element with its neighbor.
If you care about efficiency you can look into implementing the merge sort algorithm even if it's a little more complicated.
You should insert like this
void Insert(string x, int y)
{
NODE *r;NODE *temp;
// Create a new node
r = new NODE; r->sName = x; r->sHours = y;r->next = NULL;
if(list==null)//check if list is empty
{
list=r;//insert the node r in the list
}
else
{
temp=list;
while(temp->next!=null)temp=temp->next;//reach to the end of the list
temp->next=r;//insert it at the end of the list
}
}
No need of rear pointer..just check if list->next is null,if yes you are at the end
and your sorthour function should be
void sortHours()
{
for(NODE* n=list;n->next!=null;n=n->next)//get each of the node in list 1 by 1 except the last one i.e. n
{
for(NODE* n1=n->next;n1!=null;n1=n1->next)//compare the list n node with all the nodes that follow it i.e.n1
{
if(n->sHours > n1->sHours)//if one of the node is the less than n
{
//swap n and n1
node temp=*n;
n->age=n1->age;
n->name=n1->name;
n1->age=temp.age;
n1->name=temp.name;
}
}
}
}
This is a FIFO program using linked list . The program does not give the desired output but generates a long loop which stops after sometime and there is a message that the program has stopped working. What is the problem ?
#include <iostream>
using namespace std;
struct node {
int data;
struct node* previous; // This pointer keeps track of the address of the previous node
};
struct queue {
node* first;
node* last;
};
node* dataNode_P_A;
bool loop = true;
struct node* enterData();
struct node* enter_N_Data();
void displayQueue();
int main() {
struct node* dataNode= enterData();
while( loop ) {
cout << "Want to enqueue ? Press y/n : ";
char ans;
cin >> ans;
if( ans == 'y' ) {
struct node* dataNode_N = enter_N_Data();
} else {
break;
}
}
displayQueue();
}
struct node* enterData() {
cout << "Enter the number : ";
dataNode_P_A = new node; // Now dataNode points to a chunk allocated to node
cin >> dataNode_P_A->data;
dataNode_P_A->previous = NULL; // this is set to NULL because no one follows till now
queue* q = new queue;
q->first = dataNode_P_A; // this pointer points to the first element
return dataNode_P_A;
}
struct node* enter_N_Data() {
cout << endl << "Enter the number : ";
node* dataNode = new node;
cin >> dataNode->data;
dataNode->previous = dataNode_P_A;
queue* q = new queue;
q->last = dataNode; // this pointer points to the last element
return dataNode;
}
void displayQueue() {
while( dataNode_P_A != NULL ) {
cout << dataNode_P_A->data << endl;
dataNode_P_A++;
}
}
You are constructing queues and then abandoning them.
You fail to update dataNode_P_A, so that you are not constructing a list so much as a tassel.
You invoke dataNode_P_A++ when you clearly don't know what it means.
You have written a long, complicated piece of code without testing it along the way.
You should start over, and go step by step.
Where to begin... First off the queue data structure isn't particularly used for anything. But that's not the root of your problem. That lies here:
void displayQueue() {
while( dataNode_P_A != NULL ) {
cout << dataNode_P_A->data << endl;
dataNode_P_A++;
}
}
When iterating through a linked list, you move to the next element by navigating to ->previous:
void displayQueue() {
while( dataNode_P_A != NULL ) {
cout << dataNode_P_A->data << endl;
dataNode_P_A = dataNode_P_A->previous;
}
}
Having said that, you're doing some other things that are questionable - like modifying your global list (dataNode_P_A). That's not a problem in your example, but it can be a problem if you ever want to do anything to the list other than display it.
Here's another version of displayQueue that doesn't have that problem:
void displayQueue() {
node *entry = dataNode_P_A;
while( entry != NULL ) {
cout << entry->data << endl;
entry = entry->previous;
}
}
You should edit your enter_N_Data() function like:
node* temp; // global as others in your program
struct node* enter_N_Data() {
cout << endl << "Enter the number : ";
node* dataNode = new node;
cin >> dataNode->data;
temp = new node;
temp = dataNode_P_A;
dataNode_P_A = dataNode; // update dataNode_P_A
dataNode->previous = temp;
queue* q = new queue;
q->last = dataNode; // this pointer points to the last element
return dataNode;
}
and keep everything same while following the suggestions by # Larry Osterman and # Beta.