This is my first post on StackOverflow, as I am genuinely stuck.
My issue is that everytime I run the following code, at the first call for the function InsertNode(), the return temp node has the correct values for next node and data. However, when the function is called again, for some reasons the head gets reset to data NULL and next pointer recursively pointing to the same address. I'm having a hard time implementing this in OOP, I have done this using plain structs successfully. But with OOP I'm confused as how to declare the Node* Node::InsertNode(Node* head), method in main, as I get an error that InsertNode is undeclared. So as workaround I declared InsertNode outside of the Node class as an independent function. I have a feeling that is what may be causing the issue. would appreciate some help on what is going on or what I should change in my code. Thank you!
hashtable.cpp
#include "Hashtable.hpp"
using namespace std;
Node::Node(){
data = NULL;
Node* nextP = NULL;
};
Node::~Node(){
}
Node* InsertNode(Node* head, int data){
Node* temp = new Node();
if(head->nextP == NULL){
head->data = data;
temp->nextP = head;
head = temp;
} else if(head->nextP!=NULL){
temp->nextP = head;
temp->data = data;
head = temp;
}
return head;
};
void Node::printNode(Node* head){
Node* temp = new Node();
temp = head;
while(temp->nextP != NULL){
printf("%d\n", temp->data);
temp = temp->nextP;
}
}
Hashtable.hpp
#ifndef Hashtable_hpp
#define Hashtable_hpp
#include <stdio.h>
class Node
{
public:
Node* nextP;
Node();
~Node();
void printNode(Node* head);
int data = NULL;
private:
};
Node* InsertNode(Node* head, int data);
#endif /* Hashtable_hpp */
main.cpp
#include <iostream>
#include "stdio.h"
#include <string>
#include "Hashtable.hpp"
using namespace std;
Node head;
//Node* head = new Node();
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
head = *InsertNode (&head, 10);
// head = temp2;
head = *InsertNode (&head, 20);
// head = temp2;
head = *InsertNode (&head, 30);
// head = temp2;
//InsertNode(head, 20);
Node printNode(head);
return 0;
}
So I finally figured out the issue. Since initially I was referencing the class function InsertNode() directly, I was trying avoid the error that I was other getting with the undeclared identifier. So as a work around I moved the function outside of the class declaration, which then caused more problems as you saw above post. Now I realized that when the function exists inside the Class, I have reference it by first de-referencing (my terminology is probably wrong) the function using the following: head->InsertNode(head, data);
I was initially trying different iterations of InsertNode(&head, data) or Node* InsertNode(&head, data) ... etc. Basically trying to brute force my way through the compiler :).
I am attaching the code below, please let me know your comments on what I can improve.
Hashtable.cpp
#include "Hashtable.hpp"
#include <iostream>
using namespace std;
Node::Node(){
data = NULL;
Node* nextP = NULL;
};
Node::~Node(){
}
Node* Node::InsertNode(Node* head, int data){
Node* temp = new Node();
if(head->nextP == NULL){
head->data = data;
temp->nextP = head;
} else if(head->nextP!=NULL){
temp->nextP = head;
temp->data = data;
}
return temp;
};
void Node::printNode(Node* head){
Node* temp = new Node();
temp = head;
while(temp->nextP != NULL){
printf("%d\n", temp->data);
temp = temp->nextP;
}
}
Hashtable.hpp
#ifndef Hashtable_hpp
#define Hashtable_hpp
#include <stdio.h>
#include <iostream>
using namespace std;
class Node
{
int data = NULL;
Node* nextP;
public:
Node();
~Node();
Node* InsertNode(Node* head, int data);
void printNode(Node* head);
private:
};
#endif /* Hashtable_hpp */
main.cpp
#include <iostream>
#include "stdio.h"
#include <string>
#include "Hashtable.hpp"
using namespace std;
Node* head = new Node();
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
Node temp2;
head = head->InsertNode (head, 10);
head = head->InsertNode (head, 20);
head = head->InsertNode (head, 30);
head = head->InsertNode (head, 40);
head = head->InsertNode (head, 50);
head = head->InsertNode (head, 60);
head = head->InsertNode (head, 70);
head->printNode(head);
return 0;
Related
I am a beginner and am working on Linked list. I am trying to make a program which adds elements to the list, updates the list, dislays it and deletes it.I am getting an exception : read access violation. temp was 0xDDDDDDDD.
I think there is some problem with display() function. The debugger also does shows the same.
#include "stdafx.h"
#include "Node.h"
#include<iostream>
using namespace std;
Node::Node() //constructor
{
head = NULL;
}
Node::~Node() //destructor
{
}
void Node::addFirstNode(int n) //adding the first element in the list
{
node *temp = new node;
temp->data = n;
temp->next = NULL;
head = temp;
}
void Node :: addLast(int n) //Adding elements at the end of the list
{
node *last = new node;
last->data = n;
last->next = NULL;
node *temp = new node;
temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = last;
}
void Node::display() //Displaying the list
{
node *temp = head;
while (temp != NULL)
{
cout<<temp->data;
temp = temp->next;
}
}
//the main function:
#include "stdafx.h"
#include "Node.h"
#include<iostream>
using namespace std;
int main()
{
Node a;
a.addFirstNode(101); //Calling function : addFirstNode
a.addLast(102); //Calling function : addLast
a.addLast(103); //Calling function : addLast
a.addLast(104); //Calling function : addLast
a.display(); //Calling function : display
return 0;
}
The Node.h file is as below:
struct node
{
int data;
node *next;
};
class Node
{
private :
node *head;
public:
Node();
~Node();
void addFirstNode(int n);
void addLast(int n);
void display();
};
You should rename Node to better describe what it is, e.g. List.
In Node::addFirst(), replace temp->next = NULL; with temp->next = head; You don't want to throw away your list every time you add a Node to the beginning of it.
In Node::addLast(), replace node *temp = new node; with node *temp = head; You don't want to leak memory every time you add a Node to the end of it.
I have been working all day on not just this but 3 other assignments(for the same class) and this is the last issue I cannot figure out on my own, I am new with templates so not so sure as to how they work 100%.
Without templates this code runs perfectly, but with templates I am receiving a segmentation fault in the prioqueueUNS file at the if(head == NULL) and I cannot figure out why this is happening because I am defaulting head to NULL in the constructor, so any help would be very appreciated
int main
#include "node.h"
#include "prioqueueUNS.cpp"
int main() {
PrioQueueUNS<int> list;
list.insertItem(1);
}
node.h
#ifndef node_h
#define node_h
using namespace std;
template<class Type>
struct node {
Type data;
node<Type> *next;
};
#endif
prioqueueUNS.cpp
#ifndef prioqueueUNS_cpp
#define prioqueueUNS_cpp
#include "node.h
using namespace std;
template<class Type>
class PrioQueueUNS {
private:
node<Type> *head;
node<type> *tail;
int sizee;
int size;
int min;
public:
PrioQueueUNS() {
head = NULL;
tail = NULL;
}
PrioQueueUNS(Type *dataArray, int n) {
head = NULL;
tail = NULL;
}
void insertItem(Type n) {
node<Type> *temp;
temp->data = n;
temp->next = NULL;
if (head == NULL) { //<-- segment faulting when trying to access head
head = temp;
tail = temp;
min = n;
}
}
};
node<Type> *temp;
temp->data = n;
You create a pointer (temp) but it points to nothing, so temp->data tries to access a data field of something that does not exist.
You could fix this by using new but this would require you to destroy the object afterwards.
You never allocate memory for your node. Also declaration of pointer temp is also not correct:-
node<Type> *temp;// temp is a pointer only you have to allocate memory first
temp->data = n;
temp->next = NULL;
Correct it like below:-
For an integer:-
node<int> *temp = new node<int>();
For a flot:-
node<flot> *temp = new node<flot>();
Also correct below declaragtion:-
node<type> *tail; to node<Type> *tail;
I am refreshing my c++ by creating a simple linked list class. What I am having problems is when I try to print the list, there is a zero printing at the beginning of the list. How can I get rid of this? Also, I am having trouble with my second constructor. How would I go about this?`
Here is the code
List.h
#ifndef NODE_H
#define NODE_H
class List{
private:
typedef struct Node{
int data;
struct Node* next;
}* node;
node head;
int listLength;
public:
List();
List(int data, node nextLink);
void printList();
void push(int data);
void Delete(int d);
int listSize(void);
};
my List.cpp
#endif
#include "node.h"
#include <iostream>
using namespace std;
List::List(){
head->data=0;
head->next= NULL;
listLength=0;
}
List::List(int data, node nextLink){
head=NULL;
listLength++;
}
void List::push(int data){
if(head==NULL){
head->data=data;
head->next= NULL;
}
else{
node cursor = head;
while(cursor->next != NULL)
cursor = cursor -> next;
node newNode= new Node;
newNode->data=data;
newNode->next=NULL;
cursor->next= newNode;
}
listLength++;
}
void List::printList(){
node cursor=head;
while(cursor!=NULL){
//if(cursor->data==0){cursor=cursor->next;}
if(cursor->next==NULL){
cout<<cursor->data<<endl;
return;
}
else{
cout<<cursor->data<<" -> ";
cursor=cursor->next;
}
}
cout<<endl;
}
int main(){
List li;
li.push(2);
li.push(3);
li.push(0);
li.push(4);
li.printList();
return 0;
}
You never initialize your head node, so you're writing to unallocated memory in the code below.
if(head==NULL){
head->data=data;
head->next= NULL;
}
It should be:
if(head==NULL){
head = new Node; // added this line
head->data=data;
head->next= NULL;
}
You also probably want the first constructor
List::List(){
head->data=0;
head->next= NULL;
listLength=0;
}
to instead be
List::List(){
head = NULL;
listLength=0;
}
As for the second constructor, I assume you want something like this?
List::List(int data, node nextLink){
head = new Node;
head->data = data;
head->next = nextLink;
listLength = 1;
}
If not, could you better explain what you want?
I would also note that it would be generally considered good programming practice to create a constructor for the Node struct that initializes next to NULL, and then you wouldn't have to set it explicitly every time you create a new Node throughout your code.
I am new on data structure. I am trying to write a linked list for a string and display the list to screen. It crash at Node *p = create("I "); with the warning of access violation writing location. Here is my code, I don't know how to fix it. Please help. Thank you very much.
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
struct Node
{
string data;
Node *prev, *next;
};
Node* create (string value)
{
Node *temp = (Node*)malloc(sizeof(Node));
if (NULL==temp) return NULL;
temp->data=value;
temp->next=NULL;
temp->prev=NULL;
return temp;
}
void addHead (Node* head, string value)
{
Node *temp = new Node;
temp->data=value;
temp->next=head;
head->prev=temp;
head = temp;
temp->prev = NULL;
}
void addTail (Node* head, string value)
{
Node* s = new Node;
Node* temp = new Node;
s=head;
while (s->next!=NULL)
s = s->next;
s->next = temp;
temp->prev = s;
}
void display (Node* head)
{
if (head==NULL) return;
else
{
cout << head->data << " ";
display (head->next);
}
}
int main()
{
Node *p = create("I ");
addTail(p, "want ");
addTail(p, "cookies ");
display(p);
return 0;
}
You need to create a Node using new, not malloc, in your create function. Using malloc, the constructor for Node is not called, and the assignment to data will access an uninitialized string object.
I'm new to linklist, and i'm having a tough time with it. I'm trying to display some values that i've appended to the nodes, But i keep getting linkerror messages. Here is what I have so far.
LinkList.h
-
#ifndef LINKLIST_H
#define LINKLIST_H
class LinkList
{
private:
struct ListNode
{
int value;
ListNode *next;
};
ListNode *head;
public:
LinkList();
void insertNode(int);
void deleteNode(int);
void appendNode(int);
void display() const;
//~LinkList();
};
#endif
Impl.cpp
-
#include <iostream>
#include "LinkList.h"
using namespace std;
void LinkList::appendNode(int num)
{
ListNode * newNode;
ListNode * nodePtr;
newNode = new ListNode;
newNode->value = num;
newNode->next = NULL;
if(!head)
{
head = newNode;
head->value = num;
head->next=NULL;
}
else
{
nodePtr = head;
while(nodePtr->next!=NULL)
nodePtr = nodePtr->next;
newNode = new ListNode;
newNode->value = num;
newNode->next = NULL;
nodePtr->next = newNode;
}
}
void LinkList::display() const
{
ListNode *nodePtr;
nodePtr = head;
while (nodePtr != NULL)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
LinkList::LinkList()
{
head = NULL;
}
main.cpp
-
#include <iostream>
#include "LinkList.h"
using namespace std;
int main()
{
LinkList mine;
mine.appendNode(6);
mine.appendNode(9);
mine.appendNode(11);
mine.display();
return 0;
}
I fixed some of the initial problems but the program just crashes when it runs and i'm not sure why
I'm not sure what the problems is, any help would be greatly appreciated.
You declared a LinkList constructor, and a destructor, but you did not define them:
LinkList::LinkList() : head(NULL)
{
}
LinkList::~LinkList()
{
// delete your memory here...
}