I have been learning about linked lists. I was writing the below code to print a linked list. But it just won't print the list. Can anybody help me troubleshoot?
#include<iostream>
#include "getline.h"
using namespace std;
struct Node
{
string name, dream;
Node *randomptr;
};
Node *Populating(){
cout << "Enter name(press enter to exit): ";
string name = GetLine();
if(name == "") return NULL;
Node *newOne = new Node;
cout << "enter dream: ";
string dream = GetLine();
newOne->randomptr = NULL;
return newOne;
}
void PrintNode(Node *eachnode)
{
cout << eachnode-> name<<endl;
cout << eachnode-> dream << endl;
}
Node* BuilingLinkedList(){
Node *listHead = NULL;
while(true)
{
Node *newOne = Populating();
if(newOne == NULL) break;
newOne->randomptr=listHead;
listHead = newOne;
}
return listHead;
}
void PrintList(Node *list)
{
for(Node *cur = list; cur!= NULL; cur = cur->randomptr)
PrintNode(cur);
}
int main()
{
Node *list = BuilingLinkedList();
PrintList(list);
}
Node *Populating(){
cout << "Enter name(press enter to exit): ";
string name = GetLine();
if(name == "") return NULL;
Node *newOne = new Node;
cout << "enter dream: ";
string dream = GetLine();
newOne->randomptr = NULL;
return newOne;
}
You're not doing anything with name or dream. They're read into local variables but never assigned to the new node.
Related
This question already has an answer here:
How do I properly delete nodes of linked list in C++
(1 answer)
Closed 2 years ago.
I've been asked by a friend to help him with an exercise, basically the idea is like below.
Car number = fr50000 Car owner = AlexNelson Parking time = 3.5 hours.
He was told not to use stuff like string or getline, so it's simple app just to learn the idea of working with linked lists.
So I made this program. When I'm calling the remove() function the first time, it says the list is empty at the beginning (as it should do). But the second and third time, it's saying that the car is removed, but when I call the display() function, the car is still there (it didn't remove from the list)
Can you tell me what's wrong with my code?
#include <iostream>
using namespace std;
int length = 0;//variable of how many items in the list
struct node
{
char carNumber[15];
char carOwner[20];
float parkingTime;
node *link;
};
typedef struct node node;
node *head;//the begining of a list;
bool isempty()
{
if (length == 0)
return true;
else
return false;
}
void insert()
{
if (isempty())
{
head = new node;
cout << "Enter car number: ";
cin >> head->carNumber;
cout << "Enter Car owner: ";
cin >> head->carOwner;
cout << "Enter parking time: ";
cin >> head->parkingTime;
head->link = NULL;
length++;
}
else
{
node *p = head;
node *pnext = new node;
while (true)
{
if (p->link == NULL)
{
p->link = pnext;
break;
}
p = p->link;
}
cout << "Enter car number: ";
cin >> pnext->carNumber;
cout << "Enter Car owner: ";
cin >> pnext->carOwner;
cout << "Enter parking time: ";
cin >> pnext->parkingTime;
pnext->link = NULL;
length++;
}
}
void remove()
{
if (isempty())
{
cout << "List is empty\n";
return;
}
char carnumber[15];
cout << "Enter car number to remove: ";
cin >> carnumber;
node *p;
p = head;
while (p != NULL)
{
if (strcmp(p->carNumber, carnumber) == 0)
{
p = p->link;
cout << "Car removed\n";
return;
}
p = p->link;
}
cout << "Car was not found, check the number\n";
}
void display()
{
if (isempty())
{
cout << "List is empty\n";
return;
}
cout << "Car Number\t\tCar Owner\t\tParking Time\n";
node *p = head;
while (p != NULL)
{
cout << p->carNumber << "\t\t" << p->carOwner << "\t\t" << p->parkingTime << " Hours\n";
p = p->link;
}
}
int main()
{
string number;
display();
insert();
insert();
insert();
display();
remove();
display();
insert();
remove();
display();
}
Your remove() function is not actually removing (or destroying) a node from the list. You need to update the link of the previous node in the list to point to the next node in the list. And you need to update the head too, if removing the 1st node in the list.
Try this instead:
void remove()
{
if (isempty())
{
cout << "List is empty\n";
return;
}
char carnumber[15];
cout << "Enter car number to remove: ";
cin >> carnumber;
node *p = head;
node *prev = NULL;
while (p != NULL)
{
if (strcmp(p->carNumber, carnumber) == 0)
{
if (p == head)
head = p->link;
if (prev)
prev->link = p->link;
delete p;
cout << "Car removed\n";
return;
}
prev = p;
p = p->link;
}
cout << "Car was not found, check the number\n";
}
Alternatively:
void remove()
{
if (isempty())
{
cout << "List is empty\n";
return;
}
char carnumber[15];
cout << "Enter car number to remove: ";
cin >> carnumber;
node **p = &head;
while (*p != NULL)
{
if (strcmp((*p)->carNumber, carnumber) == 0)
{
node *n = *p;
*p = (*p)->link;
delete n;
cout << "Car removed\n";
return;
}
p = &(p->link);
}
cout << "Car was not found, check the number\n";
}
p = p->link; You're modifying the value of the local variable p, not the list. You need to modify the previous node's link field.
In the loop in remove, you are doing p = p->link, when p points to the node you want to remove. But you actually need to update the link field of the node that is pointing to p.
Here's a simple way of doing that:
node **p = &head;
while(*p)
if (strcmp((*p)->carNumber, carnumber) == 0)
break;
else p = &(*p)->link;
if (*p)
{
cout<<"Car removed\n";
delete std::exchange(*p, (*p)->link);
}
else cout << "Car was not found, check the number\n";
If you can't use std::exchange, then you can replace that with:
auto h = *p;
*p = (*p)->link);
delete h;
i am trying to implement a linked list with add, search, remove and print functions. But i keep getting errors that "search, print and file are not declared in this scope" and ISO C++ forbids comparison between pointer and integer [-fpermissive] error. Any help would be appreciated.
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
struct node
{
public:
char name;
node *next;
};
bool isEmpty(node *head);
char welcomeScreen();
void addInitialElement(node *head, node*last, char name);
void add (node *&head, node *&last, char name);
void serach (node *current);
void remove (node *&head, node *&last);
void printFile(node *current);
bool isEmpty(node *head)
{
if(head == NULL)
return true;
else
return false;
}
char welcomeScreen()
{
char options;
cout <<"Address Book \n";
cout <<"Available Commands \n";
cout <<" add <name>. \n";
cout <<" search <name>. \n";
cout <<" remove <name>. \n";
cout <<" print \n";
cout <<" file <filename>. \n";
cout <<" quit \n";
cin >> options;
return options;
}
void addInitialElement(node *head, node*last, char name)
{
node *temp = new node;
temp->name = name;
temp->next = NULL;
head = temp;
last = temp;
}
void add (node *&head, node *&last, char name)
{
if(isEmpty(head))
addInitialElement(head, last,name);
else
{
node *temp = new node;
temp->name = name;
temp->next = NULL;
last->next = temp;
last = temp;
}
}
void serach (node *current)
{
node *temp = new node;
if (current->name == temp)
{
cout << "The List Contains the Following: \n";
while(current != NULL)
{
cout<< current->name <<endl;
current = current->next;
}
}
}
void remove (node *&head, node *&last)
{
if(isEmpty(head))
cout << "ERROR: Not found for Removal. \n";
else if(head == last)
{
delete head;
head == NULL;
last == NULL;
}
else
{
node *temp = head;
head = head->next;
delete temp;
}
}
void printFile(node *current)
{
if(isEmpty(current))
cout << "NO NAME IN LIST. \n";
else
{
cout << "The List Contains the Following: \n";
while(current != NULL)
{
cout<< current->name <<endl;
current = current->next;
}
}
}
int main()
{
node *head = NULL;
node *last = NULL;
char options;
char name;
do{
options = welcomeScreen();
switch(options)
{
case '1': cout <<"please add a name: ";
cin >> name;
add(head, last, name);
break;
case '2' : search(head, last);
cin >> name;
break;
case '3' : remove(head, last);
break;
case '4' : print(head);
break;
case '5' : file(head);
break;
default: cout << "system exit \n";
}
}while(options != '6');
}
You misspelled search. Do a find / replace on "serach".
In case 4, you may mean to call printFile instead of print
case '4' : printFile(head);
In case 5, I don't know what your intention is here, but there is no function called "file". Try searching on that word, file, and you'll see.
I had a circular link list which has add, delete, printlist and search functions etc. All the functions works correctly but when I delete all nodes in the list and than try to add new nodes to the same list, I get project.exe has triggered a breakpoint message.
List.h
#ifndef LIST_H
#define LIST_H
using namespace std;
class list {
public:
struct node {
string data, assignee, date;
node *next;
};
typedef struct node NodePtr;
NodePtr *head, *current, *temp, *dummy;
list();
void add();
void del();
void printList();
void menu();
void exit();
void search();
};
#endif
Source.cpp
#include <cstdlib>
#include <iostream>
#include "List.h"
#include<string>
using namespace std;
list::list() { //constructor
head = NULL;
current = NULL;
temp = NULL;
}
void list::add(){
string addTask, addAssignee, addDate;
NodePtr *newNode = new NodePtr;
//entering the data of the tasks
cout << "Enter the name of the task:\n"<<"<<";
getline(cin, addTask); // compailer doesnt detect the first getline function, ı dont understan why. ı had to put it twice
getline(cin, addTask);
cout << "Enter the assignee name for the task:\n" << "<<";
getline(cin, addAssignee);
cout << "Enter the deadline for the task:\n" << "<<";
getline(cin, addDate);
cout << "\n";
//assignt data to the new node
newNode->data = addTask;
newNode->assignee = addAssignee;
newNode->date = addDate;
newNode->next = NULL;
if (head != NULL){
current = head;
while (current->next != head){ //finding the last node
current = current->next;
}
current->next = newNode;
dummy = newNode;
dummy->next = head; // we make the list, circular
}
else //here will work if the list is empty
{
head = newNode;
dummy = newNode;
head->next=head;
}
}
void list::printList(){
int i = 1;
current = head;
if (head == NULL){
cout << "List is empty\n\n";
}
else
{
do
{
cout << i << "-";
cout << current->data << endl;
cout << current->assignee << endl;
cout << current->date << endl << endl;
current = current->next;
i++;
} while (current != dummy->next);
}
}
void list::del(){ //tüm liste silindikten sonra tekrar işlem(ekleme, listeleme etc.) yapılamıyor hata veriyor
NodePtr *delPtr;
delPtr = NULL;
int ID;
cout << "These tasks exist in the list:\n";
printList();
cout << "Choose the ID of the task you want to remove:\n";
cin >> ID;
if (head == NULL){
cout << "List does not includes any task.\n";
}
else
if (ID == 1){
delPtr = head;
head = head->next;
delete delPtr;
dummy->next = head;
}
else
{
current = head;
for (int k = 1; k <= ID - 1; k++){
temp = current;
current = current->next;
}
if (current == dummy){
dummy = temp;
dummy->next = head;
}
delPtr = current;
current = current->next;
temp->next = current;
delete delPtr;
}
}
void list::menu(){
cout << "Choose an operation\n"
<< "A: Add Task\n"
<< "S: Search for Task\n"
<< "L: List All Tasks\n"
<< "R: Remove Task\n"
<< "E: Exit\n\n"<<"<<";
}
void list::search(){
string searchTask;
NodePtr *search;
search = head;
cout << "To search for a task, enter its task name or assignee name:\n" << "<<";
getline(cin, searchTask);
getline(cin, searchTask);
while (search != dummy){//bu satır problemli gibi gözüküyor
if (search->data == searchTask || search->assignee == searchTask){
cout << search->data << endl
<< search->assignee << endl
<< search->date << endl<<endl;
}
search = search->next;
}
}
void list::exit(){
node *delPtr;
current = head;
if (head != NULL){
do{
temp = current;
delPtr = temp;
current = current->next;
delete delPtr;
} while (current != dummy);
}
}
Main.cpp
#include <cstdlib>
#include <iostream>
#include "List.h"
#include<string>
using namespace std;
int main(){
char operation;
list tasks;
tasks.menu();
cin >> operation;
while (operation!='E' && operation!='e'){
switch (operation)
{
case 'A':case'a':
tasks.add();
break;
case 'S': case's':
tasks.search();
break;
case 'L': case 'l':
cout << "All tasks are listed below:\n";
tasks.printList();
break;
case 'R': case 'r':
tasks.del();
break;
default:
break;
}
tasks.menu();
cin >> operation;
}
tasks.exit();
return 0;
}
I'm currently learning Linked Lists in C++, and I can't write a print function which prints out the elements of the list; I mean I wrote the function but it doesn't work properly.
#include <iostream>
using namespace std;
struct node
{
char name[20];
node* next;
};
node* addNewPerson(node* head)
{
node* person = new node;
cout << "Name: ";
cin >> person->name;
person->next = NULL;
if (head == NULL) //is empty
{
head = person;
}
else
{
person = person->next;
}
return head;
}
void printList(node* head)
{
node* temp = head;
cout << temp->name << endl;
}
int main()
{
node* head = NULL;
node* temp = head;
unsigned short people = 0;
cout << "How many people do you want to invite to the party?" << endl;
cout << "Answer: ";
cin >> people;
cout << endl;
for (unsigned short i = 1; i <= people; i++)
{
addNewPerson(head);
cout << endl;
}
cout << "LIST: " << endl;
cout << endl;
while (temp != NULL)
{
printList(temp);
temp = temp->next;
}
cin.get();
}
I am wondering what I'm doing wrong, please help me!
It is obvious that function addNewPerson is wrong.
node* addNewPerson(node* head)
{
node* person = new node;
cout << "Name: ";
cin >> person->name;
person->next = NULL;
if (head == NULL) //is empty
{
head = person;
}
else
{
person = person->next;
}
return head;
}
You allocated new node person.
node* person = new node;
Set its field next to NULL
person->next = NULL;
Then if head is not equal to NULL you set person to person->next
person = person->next;
As person->next was set to NULL it means that now also person will be equal to NULL.
Moreover the function returns head that can be changed in the function. However you ignore returned value in main
addNewPerson(head);
At least there should be
head = addNewPerson(head);
The valid function addNewPerson could look the following way
node* addNewPerson(node* head)
{
node* person = new node;
cout << "Name: ";
cin >> person->name;
person->next = head;
head = person;
return head;
}
And in main you have to write
for (unsigned short i = 1; i <= people; i++)
{
head = addNewPerson(head);
cout << endl;
}
Function printList does not output the whole list. It outputs only data member name of the first node that is of head.
void printList(node* head)
{
node* temp = head;
cout << temp->name << endl;
}
It should look the following way
void printList(node* head)
{
for ( ; head; head = head->next )
{
cout << head->name << endl;
}
}
And at last main should look as
int main()
{
node* head = NULL;
unsigned short people = 0;
cout << "How many people do you want to invite to the party?" << endl;
cout << "Answer: ";
cin >> people;
cout << endl;
for ( unsigned short i = 0; i < people; i++ )
{
head = addNewPerson(head);
cout << endl;
}
cout << "LIST: " << endl;
cout << endl;
printList( head );
cin.get();
}
In your addNewPerson function person->next never gets set, because head is always NULL.
addNewPerson method
Your addNewPerson method never added a new person to the list it just set the head to the new person.
node* addNewPerson(node* head)
{
node* person = new node;
cout << "Name: ";
cin >> person->name;
person->next = NULL;
if (head->next == NULL) //is empty
{
head->next = person;
}
else
{
person->next = head->next;
head->next = person;
}
return head;
}
printList method
Your method printList should do what it says and print the list, instead of just printing one person at a time.
void printList(node* head)
{
node* tmp = head;
while(tmp->next != NULL) {
tmp = tmp->next;
cout >> tmp->name >> endl;
}
}
main method
Upadted your main method with the new printList method.
int main()
{
node* head = new node;
unsigned short people = 0;
cout << "How many people do you want to invite to the party?" << endl;
cout << "Answer: ";
cin >> people;
cout << endl;
for (unsigned short i = 1; i <= people; i++)
{
addNewPerson(head);
cout << endl;
}
cout << "LIST: " << endl;
cout << endl;
cout << printList(head);
cin.get();
}
Also why are you using unsigned short and char arrays? Do you have a limited amount of memory? Why not just use int and string?
When you are adding a new person, it is not actually put into the linked list, since you have never invoked something like head -> next = person.
The section in your addNewPerson routine should be something like
if (head == NULL) {
head = person;
} else {
node * end = head;
while (end -> next != NULL) // find the end of the list to insert new person
end = end -> next;
end -> next = person;
}
And in the beginning you can't just pass head into addNewPerson, since it is a pointer with null value, the addNewPerson method accepts a pointer by value (value of the memory address it is pointing at) rather than a reference to head. So you need to use
node* addNewPerson(node* &head) { ... }
instead. Finally, when you declare temp, it first receives the value of head as well, so later access to temp would only give you NULL again. You need to change it to
node* &temp = head;
I think the problem is in how the link list is being added to in addNewPerson.
The input to addNewPerson is 'head' so each time, before adding a new node to the linked list, we need to traverse all the way down the linked list till the last node before appending 'person' to the linked list.
struct node
{
char name[20];
node* next;
};
node* addNewPerson(node* head)
{
node* person = new node;
//cout << "Name: ";
cin >> person->name;
person->next = head;
head = person;
return head;
}
void PrintLL(node *head)
{
while (head!=NULL){
cout<< head->name <<endl;
head=head->next;
}
}
int main() {
node* head = NULL;
node* temp = head;
int num_ppl;
cout << "How many people do you want to invite to the party?" << endl;
cout << "Answer: ";
cin >> num_ppl;
for(int arr_i = 0; arr_i < num_ppl; arr_i++){
head = addNewPerson(head);
}
PrintLL(head);
return 0;
}
I have a read function, which reads external files and makes each name a new node, and I'd like to build on the list that I've created, but somehow there's like a gap in the list
example:
I enter in dave, jack, dog, cat.
result:
name: dave
name: jack
name: dog
name: cat
it prints it out, and that's the list right now. but If I close the program, and run it again, the read function reads back those names and creates the linked list again. But if I choose to add a another name "bob". It puts it in the next node over
example:
I enter in bob
result:
name: dave
name: jack
name: dog
name: cat
name: [BLANK LINE]
name: bob
6.cpp
#include "6.h"
int main()
{
petAdoption adopt;
char response;
adopt.read();
do {
adopt.enroll();
adopt.display();
cout << "Another? (y/n): ";
cin >> response;
cin.ignore(100,'\n');
} while (toupper(response) == 'Y');
}
petAdoption::petAdoption()
{
head = NULL;
}
void petAdoption::enroll()
{
char temp[TEMP_SIZE];
cout << "Name: ";
cin.getline(temp, TEMP_SIZE);
pet.name = new char[strlen(temp)+1];
strcpy(pet.name, temp);
newNode = new animal;
newNode->name = pet.name;
newNode->next = NULL;
if (NULL == head)
{
head = newNode;
current = newNode;
}
else
{
current = head;
while (current->next != NULL){
current = current->next;
}
cout << "adding node to the end of the list" << endl;
current->next = newNode;
current = current->next;
}
ofstream write;
write.open("pets.txt", ios::app);
write << current->name << '\n';
write.close();
}
void petAdoption::display()
{
current = head;
while (NULL != current)
{
cout << "Pet's name: " << current->name << endl;
current = current->next;
}
}
void petAdoption::read()
{
ifstream read;
char temp[TEMP_SIZE];
read.open("pets.txt");
if(!read)
{
cout << "/#S/ There are no pets" << endl;
}
else{
while(!read.eof())
{
read.getline(temp, TEMP_SIZE);
pet.name = new char[strlen(temp)+1];
strcpy(pet.name, temp);
newNode = new animal;
newNode->name = pet.name;
newNode->next = NULL;
if (NULL == head)
{
head = newNode;
current = newNode;
}
else
{
current = head;
while (current->next != NULL){
current = current->next;
}
current->next = newNode;
current = current->next;
}
}
}
read.close();
}
6.h
#include <iostream>
#include <cctype>
#include <cstring>
#include <fstream>
using namespace std;
const int TEMP_SIZE = 250;
struct animal
{
char *name;
animal *next;
};
class petAdoption
{
public:
petAdoption();
//~petAdoption();
void enroll();
void read();
void display();
private:
animal *head, *current, *newNode;
animal pet;
};
while(!read.eof())
{
read.getline(temp, TEMP_SIZE);
...
}
is not the way to write a read file loop in C++. eof() only returns true after an attempt to read past the end of the file, so you execute the body of the loop one time too many.
The correct way to write the loop is as follows:
for (;;)
{
read.getline(temp, TEMP_SIZE);
if (read.failed())
break;
...
}
or more idiomatically:
while (read.getline(temp, TEMP_SIZE))
{
...
}
which means exactly the same thing.
Note also that I have used failed() instead of eof() - end-of-file is not the only reason reading could fail.