problem in taking input after making a linkedlist input function - c++

#include <iostream>
#include <cmath>
using namespace std;
class node {
public:
int data;
node* next;
node(int d) {
data = d;
next = NULL;
}
};
void insertAtTail(node*& head, int d) {
node* temp = head;
node* n = new node(d);
n->next = NULL;
if (head == NULL) {
head = n;
} else {
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = n;
}
}
void buildList(node*& head, int t) {
int data;
cin >> data;
int q = 1;
while (q != t + 1) {
insertAtTail(head, data);
cin >> data;
q++;
}
return;
}
int main() {
node* head = NULL;
int t, q;
cin >> t;
buildList(head, t);
int u;
cin >> u;
cout << u << endl;
print(head);
}
After making an input function for the Linked List (buildList) , whenever I declare another variable (int u) and take input, it always takes a garbage value and does not take the inputted value. Please review it and tell me where am I wrong

Related

Only Printing the First Value of Linked List

I have no idea why display function is not displaying anything other than the first node's data. I've tried switching the While(p!=NULL) to while(p->next!= NULL but when I do that instead of only the first node's data displaying no data is being displayed.
#include <iostream>
using namespace std;
class Node {
public:
int no;
Node* next;
};
Node* createNode(int no1) {
Node* n = new Node();
n->no = no1;
n->next = NULL;
return n;
}
void addValue(int x, Node** head) {
//insert first node into linked list
Node* n = createNode(x),*p = *head;
if (*head == NULL) {
*head = n;
}
//insert second node onwards into linked list
else {
while (p->next!= NULL) {
p->next = n;
p = p->next;
}
}
}
void display(Node *head) {
Node* temp = head;
// temp is equal to head
while (temp->next!=NULL) {
cout << temp->no;
temp = temp->next;
}
}
int main() {
int num; char choice;
Node* head = NULL;
do {
cout << "Enter a number : ";
cin >> num;
addValue(num,&head);
cout << "Enter [Y] to add another number : ";
cin >> choice;
} while (choice == 'Y');
cout << "List of existing record : ";
display(head);
return 0;
}
I've tried changing the contents fo the else while loop in the addRecord function to p = p->next; p->next = n; in that order to no avail.
In the while loop, it should be
while (p->next!= NULL) {
p = p->next;
}
p->next = n;
Traverse until the end of linked list is reached and then, add the new entry.

Segmentation fault in passing pointer to a linked list

Hello fellow programmers, the below code gives segmentation fault. This code aims to insert an element at the end of a linked list. I tried using print statements to debug it. I think the error is in passing the linked list pointer to insert() function. Please tell me how can I correct it. Thanks in advance.
Below is the code:
#include <iostream>
using namespace std;
class node {
public:
int data;
node *next;
node(int data) {
this->data = data;
this->next = NULL;
}
};
class linked_list {
public:
node *head;
linked_list() {
this->head = NULL;
}
};
void insert(node **head, int data);
void print(linked_list *L);
int main() {
int N;
linked_list *A = new linked_list();
cout << "N: ";
cin >> N;
for(int i=0; i<=N-1; i++) {
int t;
cin >> t;
insert(&(A->head), t);
}
print(A);
return 0;
}
void insert(node **head, int data) {
node *temp = new node(data);
if(*head == NULL) {
*head = temp;
return;
} else {
node *t = *head;
while(t->next != NULL) {
t=t->next;
}
t->next = temp;
return;
}
}
void print(linked_list *L) {
node * t = L->head;
while(t!=NULL) {
cout << t->data << " ";
t = t->next;
}
return;
}
main.cpp:42:14: error: using the result of an assignment as a
condition without parentheses [-Werror,-Wparentheses]
if(*head = NULL) {
~~~~~~^~~~~~
main.cpp:42:14: note: place parentheses around the assignment to
silence this warning
if(*head = NULL) {
^
( )
main.cpp:42:14: note: use '==' to turn this assignment into an
equality comparison
if(*head = NULL) {
^
==
1 error generated.
You're using assignment where you intended to do a comparison.

Why is this code for single linked list not working?

Being a beginner I tried this single linked list program to accept and display first to last elements.I can't figure out what is wrong.After you run it the program stops responding after taking in the first element. I am not very familiar with the language and am new to pointer concept. This was an assignment work.
#include <iostream>
using namespace std;
struct node
{
int data;
node* next;
};
class alpha
{
public:
node* head;
node* last;
node* n;
node* p;
int x;
char ch;
void input()
{
cout << "Enter the element..";
cin >> x;
insert(x);
cout << "Do you want to add more?";
cin >> ch;
if (ch == 'y')
{
input();
}
else
{
display();
}
}
void insert(int x1)
{
n = new node;
n->data = x1;
if (head == NULL)
{
head = n;
last = n;
}
else
{
n->next = NULL;
last->next = n;
last = n;
}
}
void display()
{
p = head;
while (p != NULL)
{
cout << p->data;
p = p->next;
}
}
};
int main()
{
alpha o;
o.input();
return 0;
}
the big mistake already pointed out is the absence of an initialization by a constructor. Also I suggest to move some data member to private, and make some of them local.
#include <iostream>
using namespace std;
struct node
{
int data;
node* next;
};
class alpha
{
private:
node* head;
node* last;
public:
alpha() {
head = NULL;
last = NULL;
}
void input()
{
int x;
char ch;
cout << "Enter the element..";
cin >> x;
insert(x);
cout << "Do you want to add more?";
cin >> ch;
if (ch == 'y')
{
input();
}
else
{
display();
}
}
void insert(int x1)
{
node* n = new node;
n->data = x1;
if (head == NULL)
{
head = n;
last = n;
}
else
{
n->next = NULL;
last->next = n;
last = n;
}
}
void display()
{
node* p = head;
while (p != NULL)
{
cout << p->data;
p = p->next;
}
}
};
int main()
{
alpha o;
o.input();
return 0;
}
As someone suggested, please implement a destructor ~alpha(), in order to avoid leaks of node instances.

How to delete node from linked list?

Adding integers to list works fine, but there's something wrong with deleting and printing.
I'm not friendly with debugger yet, but I found out that there is error from node pointer 'first'. Its value is -17891602. I don't know what happened...
#include <iostream>
using namespace std;
class nodeList;
class node {
friend class nodeList;
private:
int data;
node* link;
public:
node() { //constructor
data = 0;
link = NULL;
}
node(int d) { //constructor
data = d;
link = NULL;
}
node(int d, node* l){ //constructor
data = d;
link = l;
}
};
class nodeList {
private:
node* first;
int num = 0;
node* nt;
public:
nodeList() {
first = new node();
}
node* end(node* t){ //return pointer of last element
t = first;
for (int i = 0; i < num; i++){
t = t->link;
}
return t;
}
void add(int a){ //add 'a' at the end of the list
node* x = new node(a);
node* y = this->end(nt);
y->link = x;
num++;
}
void del(int n){ //n : data of element that you want to delete from list
node* temp = first;
node* pretemp = NULL;
node* x;
int i;
for (i = 0; i <= this->num; i++){ //loop to find 'n'
pretemp = temp;
temp = temp->link;
if (n == temp->data){
break;
}
}
temp = first;
for (int j = 0; j<i; j++){ //i : where 'n' is,
temp = temp->link;
}
x = temp->link;
pretemp->link = x;
delete temp;
num--;
}
void printList(){
node* temp = first;
temp = temp->link;
for (int i = 0; i<this->num; i++){
cout << temp->data << endl;
temp = temp->link;
}
}
};
int main(){
nodeList *l = new nodeList();
int a;
int select;
while (1){
cout << "1. ADD 2. DELETE 3. PRINT" << endl;
cin >> select;
if (select == 1){
cout << "Enter an integer: ";
cin >> a;
if (cin.fail()) {
cout << "Wrong input" << endl;
break;
}
l->add(a);
l->printList();
}
if (select == 2){
cout << "Enter the data of the element you want to delete: ";
cin >> a;
if (cin.fail()) {
cout << "Wrong input" << endl;
break;
}
l->del(a);
l->printList();
}
if (select == 3){
l->printList();
}
}
}
Your del function deletes pretemp node (node that was before the one that you need to delete).
Here's possible fix:
//n : data of element that you want to delete from list
void del(int n)
{
//loop to find 'n'
for (node *tmp = first; tmp->link; tmp = tmp->link)
{
if (n == tmp->link->data)
{
node *x = tmp->link;
tmp->link = tmp->link->link;
delete x;
num--;
break;
}
}
}
Also, was your del supposed to delete all nodes with data == n?
These functions are a bit complicated. Here is a simpler idea:
void del(int n){
node* pretemp = first, *temp = first->link;
if(pretemp->data == n) { //handle the deleting of the first node
first = first->link;
delete pretemp;
return;
}
while(temp != NULL && temp->data != n) { //find the node with the data member "n"
pretemp = temp;
temp = temp->link;
}
if(temp != NULL) { //if you found the node, delete it
pretemp->link = temp->link;
delete temp;
}
--num;
}
Your code is a bit over-complicated for what it needs.
Try something more like this instead:
#include <iostream>
#include <limits>
using namespace std;
class nodeList;
class node
{
friend class nodeList;
private:
int data;
node* link;
public:
node(int d = 0) //constructor
: data(d), link(NULL)
{
}
};
class nodeList
{
private:
node* first;
int num;
public:
nodeList()
: first(NULL), num(0)
{
}
~nodeList()
{
node *n = first, *t;
while (n)
{
t = n->link;
delete n;
n = t;
}
}
void add(int data) //add 'data' at the end of the list
{
node* n = new node(data);
if (!first)
first = n;
else
{
node *t = first;
while (t->link)
t = t->link;
t->link = n;
}
++num;
}
void del(int data) //data : data of element that you want to delete from list
{
node* n = first;
node* prev = NULL;
while (n) //loop to find 'data'
{
if (data == n->data)
{
if (prev)
prev->link = n->link;
if (n == first)
first = n->link;
delete n;
--num;
return;
}
prev = n;
n = n->link;
}
}
void printList()
{
for(node* n = first; n != NULL; n = n->link)
cout << n->data << endl;
}
};
int main()
{
nodeList l;
int input;
bool keepGoing = true;
do
{
cout << "1. ADD 2. DELETE 3. PRINT 4. EXIT" << endl;
if (!(cin >> input))
{
cout << "Wrong input" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize_t>::max(), '\n');
}
else
{
switch (input)
{
case 1:
cout << "Enter an integer: ";
if (!(cin >> input))
{
cout << "Wrong input" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize_t>::max(), '\n');
}
else
{
l.add(input);
l.printList();
}
break;
case 2:
cout << "Enter the data of the element you want to delete: ";
if(!(cin >> input))
{
cout << "Wrong input" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize_t>::max(), '\n');
}
else
{
l.del(input);
l.printList();
}
break;
case 3:
l.printList();
break;
case 4:
keepGoing = false;
break;
default:
cout << "Wrong input" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize_t>::max(), '\n');
break;
}
}
}
while (keepGoing);
return 0;
}
If you add an extra node* member to your nodeList class, you can simplify and speed up your add() method:
class nodeList
{
private:
node* first;
node* last;
int num;
public:
nodeList()
: first(NULL), last(NULL), num(0)
{
}
~nodeList()
{
node *n = first, *t;
while (n)
{
t = n->link;
delete n;
n = t;
}
}
void add(int data) //add 'data' at the end of the list
{
node* n = new node(data);
if (!first)
first = n;
if (last)
last->link = n;
last = n;
++num;
}
void del(int data) //data : data of element that you want to delete from list
{
node* n = first;
node* prev = NULL;
while (n) //loop to find 'data'
{
if (data == n->data)
{
if (prev)
prev->link = n->link;
if (n == first)
first = n->link;
if (n == last)
last = prev;
delete n;
--num;
return;
}
prev = n;
n = n->link;
}
}
void printList()
{
for(node* n = first; n != NULL; n = n->link)
cout << n->data << endl;
}
};
And if you can change your list to be a double-linked list instead of a single-linked list, you can simplify your del() method as well:
#include <iostream>
#include <limits>
using namespace std;
class nodeList;
class node
{
friend class nodeList;
private:
int data;
node* prev;
node* next;
public:
node(int d = 0) //constructor
: data(d), prev(NULL), next(NULL)
{
}
};
class nodeList
{
private:
node* first;
node* last;
int num;
public:
nodeList()
: first(NULL), last(NULL), num(0)
{
}
~nodeList()
{
node *n = first, *t;
while (n)
{
t = n->next;
delete n;
n = t;
}
}
void add(int data) //add 'data' at the end of the list
{
node* n = new node(data);
if (!first)
first = n;
if (last)
last->next = n;
n->prev = last;
last = n;
++num;
}
void del(int data) //data : data of element that you want to delete from list
{
for(node* n = first; n != NULL; n = n->next) //loop to find 'data'
{
if (data == n->data)
{
if (n->prev)
n->prev->next = n->next;
if (n->next)
n->next->prev = n->prev;
if (n == first)
first = n->next;
if (n == last)
last = n->prev;
delete n;
--num;
return;
}
}
}
void printList()
{
for(node* n = first; n != NULL; n = n->next)
cout << n->data << endl;
}
};

After exiting from function, nodes aren't saved

My program should create a linked list and show it. My problem is when the addelemnt_end function ends, it doesn't update head and last.
I tried with debug and when my function is done, the info and next part from head and last are "unable to read memory".
struct node{
int info;
node *next;
};
node *head, *last;
void addelement_end(node *head, node *last, int element)
{if (head == NULL)
{ node *temp = new node;
temp->info = element;
temp->next = NULL;
last = temp;
head = temp;
}
else {node*temp = new node;
last->next = temp;
temp->info = element;
temp->next = NULL;
last = temp;
}
}
void show(node* head, node *last)
{
if (head==NULL)
cout << "Empty list";
else
while (head != NULL)
{
cout << head->info << " ";
head = head->next;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int x, n, i;
cout << "how many numbers";
cin >> n;
head = last = NULL;
for (i =1; i <= n; i++)
{
cin >> x;
addelement_end(head, last, x);
}
show(head, last);
return 0;
}
It's a very common error. Here is a similar illustration of the problem:
int change_a(int a) {
a = 42;
}
int main() {
int a = 10;
change_a(a);
printf("%d\n", a);
return 0;
}
This will print 10 because in the function change_a you are only modifying a copy of the value contained in the variable a.
The correct solution is passing a pointer (or using a reference since you are using C++).
int change_a(int *a) {
*a = 42;
}
int main() {
int a = 10;
change_a(&a);
printf("%d\n", a);
return 0;
}
But maybe you're going to tell me: "I'm already using a pointer!". Yes, but a pointer is just a variable. If you want to change where the pointer points, you need to pass a pointer to that pointer.
So, try this:
void addelement_end(node **head, node **last, int element)
{
if (*head == NULL)
{ node *temp = new node;
temp->info = element;
temp->next = NULL;
*last = temp;
*head = temp;
}
else {
node *temp = new node;
(*last)->next = temp;
temp->info = element;
temp->next = NULL;
*last = temp;
}
}