I want to read a text file and store new words in linked list. From this linked list I want to write a dictionary file with new words. I don't know why my code don't run. Can anyone help me?
p/s: when i run debug it found this when store vector element to new_node->word
Error
This is my code
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <cstring>
using namespace std;
typedef struct dictionary
{ string word;
int line;
int page;
struct dictionary* next;
} node;
int main()
{
node* Head = NULL;
ifstream file("filetest.txt");
if(file.fail())
cout << "Loi mo file! "<<endl;
string temp;
int cpage = 1,cline = 1;
while(getline(file,temp))
{
stringstream spliter;
spliter << temp;
vector<string> result;
while(!spliter.eof())
{
string str;
spliter >> str;
result.push_back(str);
}
for(size_t i = 0;i != result.size();i++)
{
if(Find(Head,result[i])==0)
{
Append(&Head,result[i],cline,cpage);
}
}
cline++;
if(cline == 25)
cpage++;
}
file.close();
;
ofstream outfile("test.txt");
node* p = Head;
while(p != NULL)
{
outfile << p->word <<","<<p->page<<"-"<<p->line<<endl;
p=p->next;
}
}
Append( add member to linked list)
void Append(node** First,string &newstr,int newl,int newp)
{
node* new_node = (node*)malloc(sizeof(node));
node* last = *First;
new_node->word=newstr;
new_node->line=newl;
new_node->page=newp;
new_node->next = 0;
if(*First == 0)
{
*First = new_node;
return;
}
while(last->next != 0)
{
last = last->next;
}
last->next = new_node;
return;
}
Find( check if a word is new or not)
int Find(node* head,string &tumoi)
{
node* current = head;
while(current != 0)
{
if(current->word == tumoi)
return 1;
current = current->next;
}
return 0;
}
You should not use malloc with C++ types. It does not properly initialize them.
Your node struct contains a std::string which needs to have its constructor called to be properly initialized.
When you do this
node* new_node = (node*)malloc(sizeof(node));
new_node->word=newstr;
The new_node->word is not initialized and can contain pointers to nowhere.
You should do
node* new_node = new node();
new_node->word=newstr;
instead.
Related
I have a DS assignment where we're supposed to write a program that accepts linked lists, reverses them and prints them out.
now the function itself I think I got right. but the program has to output in a certain way in order to be graded properly which is where I'm struggling.
to demonstrate:
here's the what the sample input and sample output look like
link
and here's what my output looks like
link
and of course here's the code (sorry it's kinda long since I wanted to be completely sure. the focus is on ReversePrinter() )
#include <bits/stdc++.h>
using namespace std;
class SinglyLinkedListNode {
public:
int data;
SinglyLinkedListNode *next;
SinglyLinkedListNode(int node_data) {
this->data = node_data;
this->next = nullptr;
}
};
class SinglyLinkedList {
public:
SinglyLinkedListNode *head;
SinglyLinkedListNode *tail;
SinglyLinkedList() {
this->head = nullptr;
this->tail = nullptr;
}
void insert_node(int node_data) {
SinglyLinkedListNode* node = new SinglyLinkedListNode(node_data);
if (!this->head) {
this->head = node;
} else {
this->tail->next = node;
}
this->tail = node;
}
};
void print_singly_linked_list(SinglyLinkedListNode* node, string sep)
{
while (node) {
cout << node->data;
node = node->next;
if (node) {
cout << sep;
}
}
}
void free_singly_linked_list(SinglyLinkedListNode* node) {
while (node) {
SinglyLinkedListNode* temp = node;
node = node->next;
free(temp);
}
}
// Complete the reversePrint function below.
/*
\* For your reference:
\*
\* SinglyLinkedListNode {
\* int data;
\* SinglyLinkedListNode* next;
\* };
\*
\*/
void reversePrint(SinglyLinkedListNode* head) {
SinglyLinkedListNode* prev = NULL;
while(head != NULL)
{
SinglyLinkedListNode* next_node = head->next;
head->next = prev;
prev = head;
head = next_node;
}
print_singly_linked_list(prev, "\n");
}
int main()
{
int tests;
cin >> tests;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int tests_itr = 0; tests_itr < tests; tests_itr++) {
SinglyLinkedList* llist = new SinglyLinkedList();
int llist_count;
cin >> llist_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int i = 0; i < llist_count; i++) {
int llist_item;
cin >> llist_item;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
llist->insert_node(llist_item);
}
reversePrint(llist->head);
}
return 0;
}
I feel like there's something really simple that I didn't get right but still:)
If you want all output after all input, you should store all of the created lists and then print them.
Also I found one more point: newline is not printed from reversePrint() after the last element, so you have to add one after executing that.
It can be done like this:
int main()
{
int tests;
cin >> tests;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
// allocate an array to store lists
SinglyLinkedList** llists = new SingleyLinkedList*[tests];
for (int tests_itr = 0; tests_itr < tests; tests_itr++) {
SinglyLinkedList* llist = new SinglyLinkedList();
// create list (omit: do just as original code)
// store created list
llists[tests_itr] = llist;
// instead of this
// reversePrint(llist->head);
}
// print created lists
for (int tests_itr = 0; tests_itr < tests; tests_itr++) {
// print a list
reversePrint(llists[tests_itr]->head);
// print a newline
cout << '\n';
}
// free the array
delete[] llists;
return 0;
}
I'm trying to implement insertion sort with link list in C++. But, whenever I'm trying to assign pointer to new node to a link, it gives "segmentation fault (core dumped)". I've checked the line "(*head)->next = newNode;" gives this error.
To run the program compile the program and for input copy the two lines inside comment before the start of insertionSort.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class Node
{
public:
int num;
Node *prev;
Node *next;
Node(int input);
};
Node::Node(int input)
{
num = input;
prev = NULL;
next = NULL;
}
/*
5 2
1 5 3 4 2
*/
void insertionSort(Node **head, int newInput)
{
Node* newNode = new Node(newInput);
if (*head == NULL)
{
*head = newNode;
}
else
{
Node *itr = *head;
if (itr->num >= newInput)
{
newNode->next = itr->next;
itr->prev = newNode;
*head = itr;
}
else
{
Node *itr = (*head)->next;
while (itr != NULL)
{
if (itr->num >= newInput)
{
newNode->prev = itr->prev;
newNode->next = itr;
itr->prev = newNode;
newNode->prev->next = newNode;
newNode = NULL;
}
itr = itr->next;
}
if (newNode != NULL)
{
if (itr == NULL) {
(*head)->next = newNode;
}
else
itr->next = newNode;
}
}
}
}
void printList(Node *head)
{
Node *itr = head;
while (itr != NULL)
{
cout << itr->num << " ";
itr = itr->next;
}
cout << endl;
}
int main()
{
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n, k;
cin >> n >> k;
Node *head = NULL;
int num, i = -1;
while (++i < n)
{
cin >> num;
insertionSort(&head, num);
}
printList(head);
return 0;
}
Try changing
itr->prev = newNode;
to
newNode->prev = newNode;
I'm running your code, and get a write access violation. "newNode->prev was nullptr"
You seemed got your variables mixed up in line 53:
newNode->prev->next = newNode;
should be:
its->prev->next = newNode;
And it has to be executed before its->prev is overwritten. But then the code is still not functioning as you want it. You have put some more effort into it. Within the while-loop, you set newNode to NULL and then reiterate.
And you should really comment your code. You understand your own mistakes better when you describe what you are doing.
By the way, did you notice that you mask Node* itr from line 36 at line 45? you could just reuse the existing object, as you don't use it anymore.
The purpose of my program is to read in data from a file and build a linked list with this data and then deallocate all the nodes used.
the program also needs to print out the address of nodes after they are created and then after that they are deleted
#include <iostream>
#include <string>
#include <fstream>
#include "BigHero.h"
using namespace std;
// Linked List Struct
struct Node{
BigHero data;
Node* Next;
};
// Funtion Prototypes
int countHeros(string,int&);
void createList(BigHero,int,Node*&,Node*&,Node*&);
void printList(Node*,Node*,Node*);
void deallocateList(Node*&,Node*&,Node*&);
int main()
{
// Program Variables
Node* head;
Node* currentPtr;
Node* newNodePtr;
string Filename = "ola5party.dat"; // File string varible
int charNumber = 0; // variable to hold number of Heroes
int i = 0; // Loop control varible
countHeros(Filename,charNumber); // Function call used to count number of Heros
ifstream inFile;
inFile.open(Filename.c_str());
if(!inFile){
cout << "Error in opening file" << endl;
return 0;
}
BigHero Hero;
while(inFile)
{
inFile >> Hero;
createList(Hero,charNumber,head,currentPtr,newNodePtr);
}
printList(head,currentPtr,newNodePtr);
deallocateList(head,currentPtr,newNodePtr);
inFile.close();
return 0;
}
int countHeros(string Filename,int& charNumber)
{
ifstream inFile;
inFile.open(Filename.c_str());
string aLineStr;
while (getline(inFile, aLineStr))
{
if (!aLineStr.empty())
charNumber++;
}
inFile.close();
return charNumber;
}
void createList(BigHero Hero, int charNumber,Node*& head, Node*& currentPtr, Node*& newNodePtr)
{
head = new Node;
head->data =Hero;
currentPtr = head;
newNodePtr = new Node;
cout << "Allocated # " << newNodePtr << endl;
newNodePtr->data = Hero;
currentPtr->Next = newNodePtr;
currentPtr = newNodePtr;
}
void printList(Node* head, Node* currentPtr, Node* newNodePtr)
{
if(head != NULL)
{
currentPtr = head;
while(currentPtr->Next != NULL)
{
cout << currentPtr->data << endl;
currentPtr = currentPtr->Next;
}
}
}
void deallocateList(Node*& head ,Node*& currentPtr,Node*& newNodePtr)
{
if( head != NULL)
{
currentPtr = head;
while( head -> Next != NULL)
{
head = head->Next;
cout << "Deleting # " << head << endl;
delete currentPtr;
currentPtr = head;
}
delete head;
head = NULL;
currentPtr = NULL;
}
}
the program like this runs without errors, but here is the problem it will input all the information required but since i only have one variable hero class it is constantly replacing the information.
i tried to make a class array (example hero[i]) but cant seem to get it right and am not even sure if that is the solution. Everything is fine but i cant get the desired number of class object and i always end up with one class
this is my desired output but i only get one class object
Allocated#0x8722178
Allocated#0x87221d0
Allocated#0x8722210
Allocated#0x8722230
Allocated#0x8722288
Allocated#0x87222c8
Hero:MacWarriorLevel134,(34,16,48)Exp:13425
Hero:LinuxMageLevel149,(24,54,21)Exp:14926
Hero:PCBardLevel122,(18,32,17)Exp:12221
Hero:PythonThiefLevel90,(24,18,61)Exp:9001
Hero:CplusPaladinLevel159,(31,38,29)Exp:15925
Deleting#0x8722178
Deleting#0x87221d0
Deleting#0x8722210
Deleting#0x8722230
Deleting#0x8722288
Deleting#0x87222c8
It seems you have misunderstood the basic idea behind a link listed. You are not supposed to overwrite head again and again when adding element. head shall only be changed when the list is empty.
Try something like this:
struct Node
{
BigHero data;
Node* next;
};
void addNewNode(Node*& head, ....)
{
if (head == nullptr)
{
// List empty so add new node as head
head = new Node;
head->next = nullptr;
return;
}
// Find last element in list (performance can be improved with a tail*)
Node* temp = head;
while (temp->next != nullptr) temp = temp->next;
// Add new element to end of list
temp->next = new Node;
temp->next->next = nullptr
return;
}
int main()
{
Node* head = nullptr;
addNewNode(head, ....);
return 0;
}
For performance it is often good to have a tail-pointer also.
Further you should not define head in main() but make a class/struct for it and put the relevant functions in the class. Like:
struct Node
{
BigHero data;
Node* next;
};
class ListOfNode
{
public:
ListOfNode() : head(nullptr), size(0) {}
~ListOfNode()
{
// Delete all nodes
}
void addNewNode(....)
{
// ....
++size;
}
size_t size() { return size; }
private:
Node* head; // Optional: Add a tail* for better performance
size_t size;
};
int main()
{
ListOfNode list;
list.addNewNode(....);
cout << list.size() << endl;
return 0;
}
It looks like in "SortedInsert", the head is always zero and then the code segfaults anyway... really frustrating. Any idea why the head is always zero even though I set it to something, and then why the code segfaults in general?
Thanks
#include <iostream>
#include <cassert>
#include <string>
#include <stdlib.h>
#include <sstream>
using namespace std;
struct Node {
Node* next = 0;
int data;
~Node(){
if (next != 0){
delete next;
}
}
};
void SortedInsert(Node* head, int value){
if(head == 0){
Node* header = new Node;
header->data = value;
head = header;
return;
}
cout << "TEST" << endl;
Node* temp = head;
while(temp != 0){
if(value > temp->data){
Node* insert = temp->next;
Node* otherTemp = new Node;
otherTemp->data = value;
temp->next= otherTemp;
temp->next->next = insert;
}
temp=temp->next;
}
return;
}
int main() {
srand(32);
Node* sortedList = 0;
for (int i = 0; i < 10; i++){
SortedInsert(sortedList, rand() % 100);
}
Node* temp = sortedList;
for (int i=0; i < 9; i++){
assert(temp->data <= temp->next->data);
temp = temp->next;
}
delete sortedList;
}
SortedInsert has its own copy of the head pointer. When you change head inside the function it doesn't affect the value in main. The solution is to pass head by reference or by passing the address.
void SortedInsert(Node** head, int value) {
//Use *head to refer to the head of the list
}
int main() {
...
Node* sortedList = 0;
SortedInsert(&sortedList, ...);
...
}
Or
void SortedInsert(Node*& head, int value) {
//Use head to refer to the head of the list
}
int main() {
...
Node* sortedList = 0;
SortedInsert(sortedList, ...);
...
}
Try the following
void SortedInsert( Node* &head, int value )
{
if ( head == nullptr || value < head->data )
{
head = new Node { head, value };
}
else
{
Node *current = head;
while ( current->next != nullptr && !( value < current->next->data ) )
{
current = current->next;
}
Node *tmp = new Node { current->next, value };
current->next = tmp;
}
}
As for your funcion implementation then the function deals with a copy of the head. Any changes of the copy do not influence on the argument itself. You should pass the head by reference or return the head from the function.
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.