(This is a Homework Question)I am having trouble implementing a name sort for a linked list. I have the weight(int) sort working now i just want to sort names in ascending order.
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
int c = 0;
struct node
{
char name[20];
int weight;
node *next;
node *prev;
};
node*head = NULL;
node *tail = NULL;
node *start_ptr = NULL;
Here I create the node and insert into the list in sorted position. I want to do the same for names. A name is assigned a weight(For example a persons weight). It has to be in sorted position so when the print function is called it prints the list in sorted order.
void create(int x,char name)
{
node*p = NULL;
node*r = NULL;
node*np = NULL;
np = new node;
np->weight = x;
np->next = NULL;
np->prev = NULL;
if (c == 0)
{
tail = np;
head = np;
p = head;
p->next = NULL;
p->prev = NULL;
c++;
}
else
{
p = head;
r = p;
if (np->weight < p->weight)
{
np->next = p;
p->prev = np;
np->prev = NULL;
head = np;
p = head;
do
{
p = p->next;
} while (p->next != NULL);
tail = p;
}
else if (np->weight > p->weight)
{
while (p != NULL && np->weight > p->weight)
{
r = p;
p = p->next;
if (p == NULL)
{
r->next = np;
np->prev = r;
np->next = NULL;
tail = np;
break;
}
else if (np->weight < p->weight)
{
r->next = np;
np->prev = r;
np->next = p;
p->prev = np;
if (p->next != NULL)
{
do
{
p = p->next;
} while (p->next != NULL);
}
tail = p;
break;
}
}
}
}
}
void traverse_head()
{
node *t = head;
while (t != NULL)
{
cout << t->weight << "\t";
t = t->next;
}
cout << endl;
}
void print_node()
{
node *temp;
temp = start_ptr;
if (temp == NULL) cout << "Empty List!" << endl;
while (temp != NULL)
{
if (temp == NULL) cout << "Empty List!" << endl;
cout << "Names & weights sorted(ascending) by name. :\n";
cout << "Name : " << temp->name << endl;
cout << "Weight : " << temp->weight << endl;
cout << "Names & weights sorted(ascending) by weight. : \n";
cout << endl;
temp = temp->next;
}
}
int main()
{
int i = 0, n, x;
char names[20];
cout << "Enter the number of people: \n";
cin >> n;
A loop that reads in the weight and the name of that person
while (i < n)
{
cout << "\nEnter Weights: \n";
cin >> x;
cout<<"Enter a Name"<<endl;
cin>>names;
create(x,*names);
i++;
}
cout << "Output: \n";
traverse_head();
system("pause");
return 0;
}
Related
When I run my program, everything works as expected until the destructor of the LList class. On the line that says delete current;, I get the following error:
BlankConsoleLab.exe has triggered a breakpoint.
I have been trying to solve this issue for a long time. I would appreciate it if perhaps someone could try to point out what I am doing wrong here.
Below is my code for the program. Thank you
Linked List Class
class LList
{
private:
Node* head;
Node* tail;
int size = 0;
public:
LList()
{
head = nullptr;
tail = nullptr;
size = 0;
}
LList(Node* h, Node* t)
{
this->head = h;
this->tail = t;
}
~LList()
{
Node* current = head;
while (current != nullptr) {
Node* next = current->m_next;
delete current;
current = next;
}
}
void print()
{
//temporary node pointer to traverse through the linked list
Node* temp = head;
cout << endl << "<MY LINKED LIST>\n";
while (temp != nullptr)
{
cout << temp->m_data.firstName << " ";
cout << temp->m_data.lastName << " ";
cout << temp->m_data.hrWage << " ";
cout << temp->m_data.hrWork << " " << endl;
temp = temp->m_next;
}
cout << endl;
}
void removeFirst()
{
//case 1: linked list is empty (never enters loop)
//case 2: linked list is not empty
if (head != nullptr)
{
Node* temp = head;
head = head->m_next;
delete temp;
//decrease size tracker of the linked list
size--;
}
}
void removeLast()
{
//case 1: linked list is empty (never enters loop)
//case 2: linked list has one node
if (head->m_next == nullptr)
{
removeFirst();
}
//case 3: linked list has more than one node
else if (head != nullptr)
{
Node* cur = head;
Node* prev = nullptr;
while (cur->m_next != nullptr)
{
prev = cur;
cur = cur->m_next;
}
tail = prev;
tail->m_next = nullptr;
delete cur;
//decrease size tracker of the linked list
size--;
}
}
//void removeAt(int pos)
//{
// //Case 1: input is invalid (less than 1 or greater than size)
// if (pos < 1 && pos > size)
// {
// return;
// }
// //Case 2: input is position 1
// else if (pos == 1)
// {
// removeFirst();
// }
// //Case 3: input is the last position (input equals size)
// else if (pos == size)
// {
// removeLast();
// }
// //Case 4: input is valid, and not 1 or last position (greater than 1 and less than size)
// else if (head != nullptr)
// {
// Node* cur = head;
// Node* prev = nullptr;
// for (int i = 1; i < pos; i++)
// {
// prev = cur;
// cur = cur->m_next;
// }
// prev->m_next = cur->m_next;
// delete cur;
// size--;
// }
//}
Node* swap(Node* lh, Node* rh)
{
Node* temp = rh->m_next;
rh->m_next = lh;
lh->m_next = temp;
return rh;
}
void readBin()
{
ifstream file;
file.open("C:\\Users\\there\\source\\repos\\cst126-lab9-JEmersonLawrance\\BlankConsoleLab\\Employee Data.bin", ios::binary);
if (file)
{
Node* cur = head;
Node* prev = nullptr;
for (int i = 0; i < 4; i++)
{
file.read((char*)&cur->m_data.firstName, sizeof(cur->m_data.firstName));
file.read((char*)&cur->m_data.lastName, sizeof(cur->m_data.lastName));
file.read((char*)&cur->m_data.hrWage, sizeof(cur->m_data.hrWage));
file.read((char*)&cur->m_data.hrWork, sizeof(cur->m_data.hrWork));
prev = cur;
cur = cur->m_next;
}
return;
}
else
{
cout << "File could not be opened..\n" << endl;
}
file.close();
}
void writeBin()
{
ofstream file;
file.open("Employee Data Output.bin", ios::binary);
if (file)
{
Node* cur = head;
Node* prev = nullptr;
while (cur != nullptr)
{
file.write((char*)&cur->m_data.firstName, sizeof(cur->m_data.firstName));
file.write((char*)&cur->m_data.lastName, sizeof(cur->m_data.lastName));
file.write((char*)&cur->m_data.hrWage, sizeof(cur->m_data.hrWage));
file.write((char*)&cur->m_data.hrWork, sizeof(cur->m_data.hrWork));
prev = cur;
cur = cur->m_next;
}
}
else
{
cout << "File could not be opened..\n" << endl;
}
file.close();
}
};
Node Class
class Node
{
public:
Employee m_data;
Node* m_next;
Node()
{
m_data.firstName = "";
m_data.lastName = "";
m_data.hrWage = 0;
m_data.hrWork = 0;
m_next = nullptr;
}
Node(Node* next)
{
m_data.firstName = "";
m_data.lastName = "";
m_data.hrWage = 0;
m_data.hrWork = 0;
m_next = next;
}
Node(const Node& copy)
{
m_data.firstName = copy.m_data.firstName;
m_data.lastName = copy.m_data.lastName;
m_data.hrWage = copy.m_data.hrWage;
m_data.hrWork = copy.m_data.hrWork;
}
Node operator = (const Node& copy)
{
m_data.firstName = copy.m_data.firstName;
m_data.lastName = copy.m_data.lastName;
m_data.hrWage = copy.m_data.hrWage;
m_data.hrWork = copy.m_data.hrWork;
return *this;
}
~Node()
{
}
};
Employee Class
struct Employee
{
public:
string firstName;
string lastName;
int hrWage;
int hrWork;
Employee()
{
firstName = "";
lastName = "";
hrWage = 0;
hrWork = 0;
}
Employee(string first, string last, int wage, int work)
{
firstName = first;
lastName = last;
hrWage = wage;
hrWork = work;
}
~Employee()
{
}
void getInput()
{
for (int i = 0; i < 4; i++)
{
cout << "EMPLOYEE # " << i+1 << ":\n" << endl;
cout << "First name: ";
cin >> this[i].firstName;
cout << "Last name: ";
cin >> this[i].lastName;
cout << "Hourly Wage: ";
cin >> this[i].hrWage;
cout << "Hours Worked: ";
cin >> this[i].hrWork;
}
}
void writeBin()
{
ofstream file;
file.open("C:\\Users\\there\\source\\repos\\cst126-lab9-JEmersonLawrance\\BlankConsoleLab\\Employee Data.bin", ios::binary);
if (file)
{
for (int i = 0; i < 4; i++)
{
file.write((char*)&this[i].firstName, sizeof(this[i].firstName));
file.write((char*)&this[i].lastName, sizeof(this[i].lastName));
file.write((char*)&this[i].hrWage, sizeof(this[i]).hrWage);
file.write((char*)&this[i].hrWork, sizeof(this[i]).hrWork);
}
}
else
{
cout << "File could not be opened..\n" << endl;
}
file.close();
}
};
Main Function
int main()
{
cout << "In this program, LIST will read the user information"
<< " in from a binary file, and output it into a different binary"
<< " file.\n" << endl;
Employee data[4];
data->getInput();
data->writeBin();
//creating linked list
Node fourth;
Node third(&fourth);
Node second(&third);
Node first(&second);
LList LIST(&first, &fourth);
LIST.readBin();
LIST.writeBin();
LIST.print();
return 0;
}
In main(), you are constructing your LIST object with pointers to local Node objects that are created in automatic memory within main()'s call frame. When main() exits, all of its local variables are destroyed automatically. But, when the LIST object is destroyed, its destructor tries to call delete on those Node objects which were not created in dynamic memory with new. Thus, your code exhibits undefined behavior.
I am working on reverse linked list on Windows 10 Pro (64 bit) with Visual Studio Community 2019. I would like to know how to resolve the error I get as below. I get below error in the while loop in the member function reverse() in the class List
(*Please assume the list is already prepared like contiguous integer e.g. 0,1,2,3,4,5)
Could you please anyone give me some advice? Thank you in advance.
Unhandled exception thrown: read access violation.
current was 0xDDDDDDDD.
#include <iostream>
#include <string.h>
#include <vector>
using std::cout;
using std::endl;
template <class Data>
class Node
{
public:
Node* next;
Data data;
};
template <class Data>
class List
{
private:
Node<Data>* head;
Node<Data>* tail;
int count;
public:
//constructor
List()
{
count = 0;
head = nullptr;
tail = nullptr;
}
int size()
{
return count;
}
void msg_empty_list()
{
cout << "The list is not modified since it is empty." << endl;
}
int push_back(Data data)
{
Node<Data>* node = new Node<Data>;
node->data = data;
node->next = nullptr;
if (head == nullptr)
{
head = node;
tail = node;
}
else if (head != nullptr)
{
tail->next = node;
tail = tail->next;
}
count++;
return count;
}
int push_front(Data data)
{
Node<Data>* node = new Node<Data>;
node->data = data;
node->next = nullptr;
if (head == nullptr)
head = node;
else if (head != nullptr)
{
node->next = head;
head = node;
}
count++;
return count;
}
int pop_front(void)
{
if (head == nullptr)
return -1;
else if (head != nullptr)
{
Node<Data>* temp;
temp = head;
head = head->next;
delete temp;
count--;
return count;
}
}
int pop_back(void)
{
if (head == nullptr)
return -1;
else if (head != nullptr)
{
Node<Data>* temp = head;
while (temp->next != tail)
temp = temp->next;
delete tail;
tail = temp;
count--;
return count;
}
}
int remove_at(int index)
{
if (head == nullptr)
return -1;
else if (head != nullptr)
{
cout << "Specified index = " << index << endl;
if (index == 0)
{
pop_front();
}
else if (index == -1)
{
pop_back();
}
Node<Data>* temp = head;
Node<Data>* rmv;
int countIndex = 0;
while (countIndex < index - 1)
{
temp = temp->next;
countIndex++;
}
rmv = temp->next;
temp->next = temp->next->next;
delete rmv;
count--;
return count;
}
}
void reverse()
{
Node<Data>* temp = nullptr;
Node<Data>* prev = nullptr;
Node<Data>* current = head;
while (current != nullptr)
{
temp = current->next; // where I get error "Unhandled exception thrown: read access violation. **current** was 0xDDDDDDDD."
current->next = prev;
prev = current;
current = temp;
}
head = prev;
}
void print()
{
Node<Data>* temp = head;
if (head == nullptr)
return;
for (int i = 0; i < count - 1; i++)
{
cout << temp->data << ", ";
temp = temp->next;
}
cout << temp->data;
}
~List()
{
Node<Data>* temp = head;
while (temp->next != nullptr)
{
temp = temp->next;
delete head;
head = temp;
}
}
};
int main()
{
Node<int> x;
Node<bool> y;
Node<char> n;
List<int> list;
//insert items into list
for (int i = 0; i < 10; i++)
{
list.push_back(i);
}
cout << "Original list[size=" << list.size() << "]: ";
//print the list
list.print();
cout << endl;
// push a node to the beginning of the list
cout << endl << "==> Push a node to the head" << endl;
list.push_front(-1);
cout << "Modified list[size=" << list.size() << "]: ";
list.print();
cout << endl;
// pop the head of the list
cout << endl << "==> Pop a node from the head" << endl;
if (list.size() == 0)
list.msg_empty_list();
else
{
list.pop_front();
cout << "Modified list[size=" << list.size() << "]: ";
list.print();
cout << endl;
}
/*
// pop the tail of the list
cout << endl << "==> Pop a node from the tail" << endl;
if (list.size() == 0)
list.msg_empty_list();
else
{
list.pop_back();
cout << "Modified list[size=" << list.size() << "]: ";
list.print();
cout << endl;
}
*/
// delete the node at the specified index
cout << endl << "==> Delete the node at the specified index" << endl;
if (list.size() == 0)
list.msg_empty_list();
else
{
list.remove_at(5);
cout << "Modified list[size=" << list.size() << "]: ";
list.print();
cout << endl;
}
// reverse the list
cout << endl << "==> Reverse the list" << endl;
if (list.size() == 0)
list.msg_empty_list();
else
{
list.reverse();
cout << "Modified list[size=" << list.size() << "]: ";
list.print();
cout << endl;
}
return 0;
}
I can spot a few mistakes in your code, but let's concentrate on the pop_back() function:
int pop_back(void)
{
if (head == nullptr)
return -1;
else if (head != nullptr) // 1)
{
Node<Data>* temp = head;
while (temp->next != tail) // 2)
temp = temp->next;
delete tail;
tail = temp; // 3)
count--;
return count;
}
}
Please don't do this: if (a == nullptr) {...} else if (a != nullptr) {...} is redundant. Just leave the second if away. It lets a reader believe there could be a third case.
It may be luck that this usually works, but some of the other methods don't properly update the tail pointer, so this might never be true. Validate your pop_front method when there's only one element in the list. Other functions might have similar problems.
Here's the actual problem you're observing. You're not setting the next pointer of the new tail element to null, instead it points to the now deleted tail. Insert tail->next = nullptr after this line.
When I call the methods to print the data stored in the nodes of my doubly linked list, nothing prints except for empty strings and 0's
#include <iostream>
#include <string>
using namespace std;
struct node {
int weight;
string name;
node *nextname, *nextweight;
};
node *namehead = NULL, *weighthead = NULL;
bool isEmpty()
{
if (namehead == NULL && weighthead == NULL)
return true;
else
return false;
}
void addperson(int w, string n)
{
node* newNode = new node;
node *prev, *curr = newNode;
if (isEmpty()) {
namehead = newNode;
weighthead = newNode;
}
else {
curr = prev = namehead;
if (curr->name > n) {
namehead = newNode;
newNode->nextname = curr;
}
else {
do {
if (curr->name <= n) {
prev = curr;
curr = curr->nextname;
}
else
break;
} while (curr != NULL);
prev->nextname = newNode;
newNode->nextname = curr;
}
curr = prev = weighthead;
if (curr->weight > w) {
weighthead = newNode;
newNode->nextweight = curr;
}
else {
do {
if (curr->weight <= w) {
prev = curr;
curr = curr->nextweight;
}
else
break;
} while (curr != NULL);
prev->nextweight = newNode;
newNode->nextweight = curr;
}
}
}
void printname()
{
node* curr = namehead;
do {
cout << curr->name << " - " << curr->weight << endl;
curr = curr->nextname;
} while (curr != NULL);
cout << endl;
}
void printweight()
{
node* curr = weighthead;
do {
cout << curr->name << " - " << curr->weight << endl;
curr = curr->nextweight;
} while (curr != NULL);
cout << endl;
}
int main()
{
int w = 0;
string n;
for (int i = 0; i < 15; i++) {
cout << "Enter weight: ";
cin >> w;
if (w == -1)
break;
cout << "Enter name: ";
cin >> n;
addperson(w, n);
}
printname();
printweight();
return 0;
}
Expected output (By name):
John - 220
Steven - 190
Tyler - 150
Expected output (By weight):
Tyler - 150
Steven - 190
John - 220
CURRENT OUTPUT(Both ways):
" " - 0
" " - 0
" " - 0
EDIT
By taking the suggestions in the comments about actually assigning the values w (weight) and n (name) to the temporary node in the add method, the problem has been fixed. Thank you for the help.
curr->weight=w;
curr->name=n;
Assign passed values into weight and name members into placeholder node in the add method:
curr->weight=w;
curr->name=n;
We are suppose to enter a string, and then find where the string is in the linked list and remove that node
when i insert to the front of the list, so i enter data values a, b, c , d, when i print it it comes up as d,c,b,a. Now i insert to the rear of it, entering f and g, and the list now looks, d,c,b,a,f,g. I want to remove f but it just use the remove function it does not and still output the same list
using namespace std;
struct node {
string data;
node* next;
};
node* addFront(node* s);
node* addRear(node* s);
void remove(node* head, string abc);
void print(node* head);
int main() {
node* head = NULL;
cout << "Enter 5 data strings\n";
cout << "This will be inserted from the back\n";
for (int i = 0; i < 5; i++) {
head = addFront(head);
}
print(head);
cout << "Enter 3 strings and this will be inserted from the back of the orignal string\n";
for (int i = 0; i < 3; i++) {
head = addRear(head);
}
print(head);
cout << "Removing the head node\n";
string n;
cout << "Enter a string to remove\n";
cin >> n;
remove(head, n);
print(head);
}
node* addFront(node* s)
{
node* person = new node;
cin >> person->data;
person->next = s;
s = person;
return s;
}
node *addRear(node*s ) {
node* person = new node;
cin >> person->data;
person->next = NULL;
if (s == NULL) {
return person;
}
else {
node* last = s;
while (last->next != NULL) {
last = last->next;
}
last->next = person;
}
return s;
}
void remove(node* head, string a) {
node* previous = NULL;
node* current = head;
if (current == NULL) {
cout << "Value cannot be found\n";
return;
}
else {
while (previous != NULL) {
if (current->data == a) {
previous->next = current->next;
delete current;
break;
}
current = current->next;
}
}
}
void print(node * head)
{
node* temp = head;
while (temp != NULL) // don't access ->next
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
In remove function, previous is most certainly NULL when you hit that while loop.
Perhaps consider a do-while loop instead (with better handling of previous).
You may be better off handling the first node in a different manner since the holder of its previous is essentially the root pointer.
The following code calculates the sum of the elements of the unidirectional list items greater than 3 and smaller than 8 and the result of the sum is changed the beginning of the list.
#include <iostream>
using namespace std;
struct List
{
int num;
List* nItem;
};
int Input()
{
int number;
cout << "Enter the number: "; cin >> number;
return number;
}
void MakeList(List **head, int n)
{
if (n > 0) {
*head = new List;
(*head)->num = Input();
(*head)->nItem = NULL;
MakeList(&(*head)->nItem, n - 1);
}
}
void Print(List* head)
{
if (head != NULL) {
cout << head->num << " ";
Print(head->nItem);
}
}
List* Add_start(List* head, int index, int elm)
{
List* p = new List;
p->num = elm;
p->nItem = NULL;
if (head == NULL) {
head = p;
}
else {
List* current = head;
for (int i = 0; (i < index - 1) && (current->nItem != NULL); i++)
{
current = current->nItem;
}
if (index == 0)
{
p->nItem = head;
head = p;
}
else {
if (current->nItem != NULL) {
p->nItem = current->nItem;
}
current->nItem = p;
}
}
return head;
}
int Sum(List* head)
{
int sum = 0;
List* p = head;
while(p) {
if ((p->num > 3) && (p->num < 8))
sum += p->num;
p = p->nItem;
}
return sum;
}
void DeleteList(List* head)
{
if (head != NULL) {
DeleteList(head->nItem);
delete head;
}
}
int main()
{
int n = 10;
List* head = NULL;
cout << "Enter 10 number to the list\n" << endl;
MakeList(&head, n);
int sum = Sum(head);
head = Add_start(head, 0, sum);
cout << "\nList: ";
Print(head);
cout << endl;
DeleteList(head);
system("pause");
return 0;
}
How can I do the same operation with a bidirectional list?
Notes:
A bidirectional (or double linked) list, also has a member pointing to the previous node: this is the whole difference between the 2 list types (as a consequence the first element - or the one at the left of the list, will have this member pointing to NULL). So, when such a node is created/inserted into a list, this new member should be set as well (I commented in the code places where this happens), for the new node and for the one following it (if any).
I modified the way of how a list is created - MakeList replaced by _MakeList2 + MakeList2; the underscore(_) in _MakeList2 specifies that it's somehow private (convention borrowed from Python) - it's not very nice, but I thought it's easier this way
I don't have Visual Studio on this computer, so I used gcc. It complained about system function so I had to add #include <stdlib.h>
I renamed some of the identifiers (List -> Node, Add_start -> AddNode, nItem -> nNode) either because the new names make more sense, or their names are consistent
I tried to keep the changes to a minimum (so the solution is as close as possible to your original post)
I enhanced (by adding an additional argument: toRight (default value: 1)) the Print func, so it can iterate the list both ways - I am iterating right to left (for testing purposes) before deleting the list
I corrected some (minor) coding style issues
Here's the modified code:
#include <iostream>
#include <stdlib.h>
using namespace std;
struct Node {
int num;
Node *pNode, *nNode; // Add a new pointer to the previous node.
};
int Input() {
int number;
cout << "Enter the number: "; cin >> number;
return number;
}
Node *_MakeList2(int n, Node *last=NULL) {
if (n > 0) {
Node *node = new Node;
node->num = Input();
node->pNode = last;
node->nNode = _MakeList2(n - 1, node);
return node;
}
return NULL;
}
Node *MakeList2(int n) {
return _MakeList2(n);
}
void Print(Node *head, int toRight=1) {
if (head != NULL) {
cout << head->num << " ";
if (toRight)
Print(head->nNode, 1);
else
Print(head->pNode, 0);
}
}
Node* AddNode(Node *head, int index, int elm) {
Node *p = new Node;
p->num = elm;
p->pNode = NULL; // Make the link between this node and the previous one.
p->nNode = NULL;
if (head == NULL) {
head = p;
} else {
Node *current = head;
for (int i = 0; (i < index - 1) && (current->nNode != NULL); i++) {
current = current->nNode;
}
if (index == 0) {
p->nNode = head;
head->pNode = p; // Make link between next node's previous node and the current one.
head = p;
} else {
if (current->nNode != NULL) {
p->nNode = current->nNode;
}
p->pNode = current; // Make the link between this node and the previous one.
current->nNode = p;
}
}
return head;
}
int Sum(Node* head) {
int sum = 0;
Node *p = head;
while(p) {
if ((p->num > 3) && (p->num < 8))
sum += p->num;
p = p->nNode;
}
return sum;
}
void DeleteList(Node *head) {
if (head != NULL) {
DeleteList(head->nNode);
delete head;
}
}
int main() {
int n = 10;
Node *head = NULL, *tail = NULL;
cout << "Enter " << n << " number(s) to the list" << endl << endl;
head = MakeList2(n);
int sum = Sum(head);
head = AddNode(head, 0, sum);
cout << endl << "List: ";
Print(head);
cout << endl;
tail = head;
if (tail) {
while (tail->nNode != NULL)
tail = tail->nNode;
cout << endl << "List reversed: ";
Print(tail, 0);
cout << endl;
}
DeleteList(head);
system("pause");
return 0;
}