Linked List Insertion problems - c++

I'm trying to insert an object (bird) that holds a name and an ID number
I'm having problems inserting the object into my linked list
bool List342::Insert(Bird *obj)
{
Node *insNode = new Node;
insNode->data = obj;
if (head == NULL)
{
head = insNode;
return true;
}
//cout << head->data->getID();
if ((insNode->data->getID()) <= (head->data->getID()))
{
insNode->next = head;
head = insNode;
return true;
}
Node *pNode = head;
while ((pNode->next != NULL) && ((pNode->next)->data->getID() <= insNode->data->getID()))
{
pNode = pNode->next;
}
insNode->next = pNode->next;
pNode->next = insNode;
return true;
}
It doesn't seem to insert correctly
I tried putting a cout code to see what numbers are being compared
for example
cout << head->data->getID()
and it seems to output the current id of the current bird
and not the id of the head (should be the lowest ID)
please and thank you!

I do not see where the function would return false. So I think there is no any sense to declare the function as having return type bool because it always returns true.
I have declared the functtion as having return type void though you may declare the function as having return type bool as before but in this case you need to add last statement that will return true.
Maybe it would be better to declare the function as returning a reference to the list itself.
The function could be defined the following way
void List342::Insert( Bird *bird )
{
Node *prev = NULL;
Node *current = head;
while ( current != NULL && !( bird->getID() < current->data->getID() ) )
{
prev = current;
current = current->next;
}
Node *newnode = new Node { bird, current };
if ( prev != NULL ) prev->next = newnode;
else head = newnode;
}
I suppose that Node has the following definition
struct Node
{
Bird *data;
Node *next;
};
You may substitute statement
Node *newnode = new Node { bird, current };
for
Node *newnode = new Node;
newnode->data = bird;
newnode->next = current;

The function is working fine for me.
I hope, the head is change outside of the insert function.
Shall you check the calling portion of the insert or any other places modifying the head value.

Related

Counting occurrence in singly linked list by nodes

I am writing a simple app that gets a list and saves the objects as nodes in a singly linked list and we can add(), remove(), copy(), etc. each node depending on the given data set. each node has a char value which is our data and an int count which counts the occurrence of the related char.
e.g. for a list like
a, a, b, b, c, a
there would be three nodes (since there are three different characters) which are:
[a,3,*next] -> [b,2,*next] -> [c,1,*next] -> nullptr
bool isAvailable() checks if the data is already in the list or not.
Q: When inserting a data there are two options:
The data has not been entered: so we have to create a newNodewith the given data, count=1and *next=NULL.
The data is already entered: so we have to count++ the node that has the same data.
I know if the given data is available or not, but how can I point to the node with same data?
Here's the code:
#include "stdafx.h"
#include<iostream>
using namespace std;
class Snode
{
public:
char data;
int count;
Snode *next;
Snode(char d, int c)
{
data = d;
count = c;
next = NULL;
}
};
class set
{
private:
Snode *head;
public:
set()
{
head = NULL;
tail = NULL;
}
~set();
void insert(char value);
bool isAvailable(char value);
};
set::~set()
{
Snode *t = head;
while (t != NULL)
{
head = head->next;
delete t;
}
}
bool set::isAvailable(char value)
{
Snode *floatingNode = new Snode(char d, int c);
while(floatingNode != NULL)
{
return (value == floatingNode);
floatingNode->next = floatingNode;
}
}
void set::insert(char value)
{
Snode *newNode = new Snode(char d, int c);
data = value;
if (head == NULL)
{
newNode->next = NULL;
head = newNode;
newNode->count++;
}
else
{
if(isAvailable)
{
//IDK what should i do here +_+
}
else
{
tail->next= newNode;
newNode->next = NULL;
tail = newNode;
}
}
}
I know if the given data is available or not, but how can I point to the node with same data?
You'll need to start at the head of the list and iterate along the list by following the next pointers until you find the node with the same data value. Once you've done that, you have your pointer to the node with the same data.
Some other notes for you:
bool set::isAvailable(char value)
{
Snode *floatingNode = new Snode(char d, int c);
while(floatingNode != NULL)
{
return (value == floatingNode);
floatingNode->next = floatingNode;
}
}
Why is this function allocating a new Snode? There's no reason for it to do that, just initialize the floatingNode pointer to point to head instead.
This function always returns after looking at only the first node in the linked list -- which is not the behavior you want. Instead, it should return true only if (value == floatingNode); otherwise it should stay inside the while-loop so that it can go on to look at the subsequent nodes as well. Only after it drops out of the while-loop (because floatingNode finally becomes NULL) should it return false.
If you were to modify isAvailable() slightly so that instead of returning true or false, it returned either floatingPointer or NULL, you'd have your mechanism for finding a pointer to the node with the matching data.
e.g.:
// Should return either a pointer to the Snode with data==value,
// or NULL if no such Snode is present in the list
Snode * set::getNodeWithValueOrNullIfNotFound(char value) const
{
[...]
}
void set::insert(char value)
{
Snode * theNode = getNodeWithValueOrNullIfNotFound(value);
if (theNode != NULL)
{
theNode->count++;
}
else
{
[create a new Snode and insert it]
}
}
You had a lot of problems in your code, lets see what are they:
First of all, Snode doesn't need to be a class, rather you can go with a simple strcut; since we need everything public.(not a mistake, but good practice)
You could simple initialize count = 1 and next = nullptr, so that no need of initializing them throw constructor. The only element that need to be initialized through constructor is Snod's data.
Since c++11 you can use keyword nullptr instead of NULL, which denotes the pointer literal.
Member function bool set::isAvailable(char value) will not work as you think. Here you have unnecessarily created a new Snode and cheacking whether it points to nullptr which doesn't allow you to even enter the loop. BTW what you have written in the loop also wrong. What do you mean by return (value == floatingNode); ? floatingNode is a Snode by type; not a char.
Hear is the correct implementation. Since we don't wanna overwrite the head, will create a Node* pointer and assign head to it. Then iterate through list until you find a match. If not found, we will reach the end of the isAvailable() and return false.
inline bool isAvailable(const char& value)
{
Node *findPos = head;
while(findPos != nullptr)
{
if(findPos -> data == value) return true;
else findPos = findPos->next_node;
}
return false;
}
In void set::insert(char value), your logic is correct, but implementation is wrong. Following is the correct implementation.(Hope the comments will help you to understand.
void insert(const char& value)
{
if(head == nullptr) // first case
{
Node *newNode = new Node(value);
newNode->next_node = head;
head = newNode;
}
else if(isAvailable(value)) // if node available
{
Node *temp = head;
while(temp->data != value) // find the node
temp = temp->next_node;
temp->count += 1; // and count it by 1
}
else // all new nodes
{
Node *temp = head;
while(temp->next_node != nullptr) // to find the null point (end of list)
temp = temp->next_node;
temp = temp->next_node = new Node(value); // create a node and assign there
}
}
Your destructor will not delete all what you created. It will be UB, since your are deleting newly created Snode t ( i.e, Snode *t = head;). The correct implementation is as bellow.(un-comment the debugging msg to understand.)
~set()
{
Node* temp = head;
while( temp != nullptr )
{
Node* next = temp->next_node;
//std::cout << "deleting \t" << temp->data << std::endl;
delete temp;
temp = next;
}
head = nullptr;
}
Last but not least, the naming (set) what you have here and what the code exactly doing are both different. This looks more like a simple linked list with no duplicates. This is however okay, in order to play around with pointers and list.
To make the code or iteration more efficient, you could do something like follows. In the isAvailable(), in case of value match/ if you found a node, you could simply increment its count as well. Then in insert(), you can think of, if node is not available part.
Hope this was helpful. See a DEMO
#include <iostream>
// since you wanna have all of Node in public, declare as struct
struct Node
{
char data;
int count = 1;
Node* next_node = nullptr;
Node(const char& a) // create a constrcor which will initilize data
: data(a) {} // at the time of Node creation
};
class set
{
private:
Node *head; // need only head, if it's a simple list
public:
set() :head(nullptr) {} // constructor set it to nullptr
~set()
{
Node* temp = head;
while( temp != nullptr )
{
Node* next = temp->next_node;
//std::cout << "deleting \t" << temp->data << std::endl;
delete temp;
temp = next;
}
head = nullptr;
}
inline bool isAvailable(const char& value)
{
Node *findPos = head;
while(findPos != nullptr)
{
if(findPos -> data == value) return true;
else findPos = findPos->next_node;
}
return false;
}
void insert(const char& value)
{
if(head == nullptr) // first case
{
Node *newNode = new Node(value);
newNode->next_node = head;
head = newNode;
}
else if(isAvailable(value)) // if node available
{
Node *temp = head;
while(temp->data != value) // find the node
temp = temp->next_node;
temp->count += 1; // and count it by 1
}
else // all new nodes
{
Node *temp = head;
while(temp->next_node != nullptr) // to find the null point (end of list)
temp = temp->next_node;
temp = temp->next_node = new Node(value);
}
}
void print() const // just to print
{
Node *temp = head;
while(temp != nullptr)
{
std::cout << temp->data << " " << temp->count << "\n";
temp = temp->next_node;
}
}
};
int main()
{
::set mySet;
mySet.insert('a');
mySet.insert('a');
mySet.insert('b');
mySet.insert('b');
mySet.insert('c');
mySet.insert('a');
mySet.print();
return 0;
}

Reversing a linked list (by recursion) not working

I was trying to write a code to reverse the linked list but getting wrong output.
Is there something I am missing.
Here is the function
void reverselinklist( struct node **headreverse)
{
struct node *p = *headreverse;
if(p->next == NULL)
{
*headreverse = p;
return;
}
reverselinklist(&(p->next));
p->next->next = p;
p->next = NULL;
}
After display function
Input 409765
Output 4
*headreverse = p is meaningless. You should set *headreverse = p->next each time to move forward, until the last node is reached.
Anyway, I changed your code to make it work:
void reverselinklist(struct node **headreverse)
{
struct node *p = *headreverse;
if(p->next == NULL){
return;
}
*headreverse = p->next;
reverselinklist(headreverse);
p->next->next = p;
p->next = NULL;
}
For single list use two two pointers, to update list as you can not go back.
Here is my code. Hope it will help you to understand concept.
void reverselinklist(struct node** head_ref)
{
struct node* first;
struct node* rest;
first = *head_ref;
rest = first->next;
if (rest == NULL)
return;
reverselinklist(&rest);
first->next->next = first;
first->next = NULL;
*head_ref = rest;
}
If it could me more precise please provide suggestions.
Your headreverse is not being assigned to the new head of the list. Be sure to use 2 arguments for your function, 1) head of the initial list 2) current node(same as your headreverse)
if(p->next == NULL)
{
*head = p; //instead of headreverse use head
return;
}

c++ doubly linked list search and remove

I'm having trouble looking for a node in my list and then deleting it. I've tried so many ways but this is my code so far. I don't know if the problem is in my insert or maybe in my display function? I"m barely learning this.
void removeStudent(int id)
{
node *trash = NULL;
node *current = head;
while ( current!= NULL)
{
if ( current->data.id == id)
{
trash = current;
current->prev->next = current->next;
current->next->prev = current->prev;
delete trash;
}
}
}
this is my insert function
void push(student s)
{
node *tmp = new node;
tmp->data = s;
tmp->next = head;
tmp->prev = NULL;
if (head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
head->prev = tmp;
head = tmp;
}
}
and this is my display function
void display()
{
node *current = head;
while (current!=NULL)
{
cout << current->data.name << endl;
cout << current->data.GPA << endl;
cout << current->data.id << endl;
cout << current->data.university << endl;
current = current->next;
}
}
Try the following. I suppose that your class contains also data member tail.
void removeStudent( int id )
{
node *current = head;
while ( current != NULL && current->data.id != id ) current = current->next;
if ( current != NULL )
{
if ( current->prev != NULL ) current->prev->next = current->next;
else head = current->next;
if ( current->next != NULL ) current->next->prev = current->prev;
else tail = current->prev;
delete current;
}
}
If your class has no data member tail then you have to remove statement
else tail = current->prev;
from the function body.
current = current->next; at the end of the loop?
node *trash = new node;
trash = current;
memory leak, because you are loosing the memory allocate by newin first line and you do not keep a pointer to that memory. You assign the pointer trash the current one.
You should just do that:
node *trash = NULL; // declare a pointer and set it to NULL
HINT:
When dealing with list, in order to test your code use a paper and a pencil, create a small list and run in the paper the cases of modifying (here deleting) a node from the start and the end of the list.
Also, what happens when the list has only one node and what happens when it is empty.

Deleting a node in the linklist

Here is my code to delete all the nodes having the value passed in the argument.
typedef struct nodetype
{
int data;
struct nodetype * next;
} node;
typedef node * list;
void Linklist::deleteNode(list * head, int value)
{
list current = *head;
list previous = *head;
while(current != NULL)
{
if(current->data != value)
{
previous = current;
current = current->next;
}
else if (current->data == value)
{
previous->next = current->next;
delete current;
current = previous->next;
}
}
}
But here if all the elements in the linklist is say 2, then it should delete all the elements in the linklist and finally head should also become NULL so that if I pass this head to count the number of nodes in the list it should say that the list is empty and other similar operations.
According to my current implementation the head is not becoming NULL for the above mentioned case.
Please suggest the modification so that head should become NULL if the linklist has all the nodes with the same value passed in the function argument.
I modified my code as follows and its working file now
void Linklist::deleteNode(list *head, int value)
{
list * current = head;
list * previous = head;
bool flag = false;
while(*current != NULL)
{
if((*current)->data != value)
{
*previous = *current;
*current = (*current)->next;
}
else if ((*current)->data == value)
{
flag = true;
(*previous)->next = (*current)->next;
delete *current;
*current = (*previous)->next;
}
}
if(!flag)
cout<<"Element not found in the linklist\n";
cout<<"Count is "<<Linklist::count(*head)<<endl;
}

Implementing push/pop in a linked list (C++)

I'm trying to incorporate push/pop into a linked list and I can't seem to get it to work. When I run my test function, I set my linked list to zero and I try to push on values but the list keeps getting returned with no values in it. Could anyone possibly tell me what I'm doing wrong?
if (top == NULL){
current = top;
current->next = NULL; //NULL->next : will cause segfault
}
if top is NULL, you set current = top [which is NULL], and then you access current->next, which will cause a segfault, you are trying to access NULL..
EDIT: follow up to comments:
your if statement seems redundant, you should probably only need to set: current->next = head; and head = current; [in addition to the current allocation]
Instead of
if (top == NULL){
current = top;
current->next = NULL;
}
you want
if (top == NULL){
top = current;
current->next = NULL;
}
And of course, after this, you have to make sure that you actually set head to top again.
Now that you've made this change, it should be clear that both cases do the same thing -- so no case distinction is actually necessary. So the function can be simplified to
void push(Data * newPushData){
LinkNode * current = new LinkNode(newPushData);
current->next = head;
head = current;
}
The top variable is local variable for push(...) function. You can use head instead, and I'd rather modify the if statement.
I think that function should look like this:
void push(Data * newPushData){
LinkNode * current = new LinkNode(newPushData);
if (head != NULL){
current->next = head;
head = current;
}
else{
head = current;
current->next = NULL; // if you haven't done it in LinkNode constructor
}
}
can you please specify the attributes of the linked list class ? [ is there slightly chance you are doing something wrong]
Instead of you , I'd do :
void push(Data * newPushData){
if (head == NULL)
head->data = newPushData
tail = head ;
else // regular situation
{
Node * node = new Node() ;
tail->next = node;
node->data = newPushData;
node->next = NULL ;
tail = node ;
}
}
In a linked list you have got to maintain the head pointer point on the head of the list , maintain that the tail pointer is point on the tail of the list ,
You must take care of the 2 cases of enlarging the list.
the best way for learning is to illustrate an insertion on a blank linked list.
Take care
S
void push(Data * newPushData)
{
if( head != NULL )
{
LinkNode current = new LinkNode(newPushData);
current->next = head;
head = current;
}
else
{
head = new LinkNode(newPushData);
}
}
Try this code...
void push(data * newpushdata){
if(head !=null){
linkednode current = new linkednode(newpushdata);
current->next = head;
head = current;
}
else {
head = new linkednode(newpushdata);
}
}
that is my working solution for a Stack containing int elements, but maybe it's better to create a void pushStack using Stack **S instead of Stack *S.
in pop(Stack **S) i created a sentinel, so if the stack is empty -1 is returned.:
typedef struct StackT {
int val;
struct StackT *next;
} Stack;
int isStackEmpty (Stack *S) {
if (S == NULL)
return 1;
else
return 0;
}
int *pop(Stack **S) {
Stack *tmp = *S;
int i = -1;
if (isStackEmpty(tmp) == 0) {
i = tmp->val;
*S = tmp->next;
}
return i;
}
Stack *pushStack (Stack *S, int x) {
Stack *node = (Stack *) malloc (sizeof (Stack));
node->val = x;
node->next = S;
return node;
}
you can call pop and stack easly:
Stack *S = NULL;
int x = somevalue;
int y;
S = pushStack(S, x);
y = pop(&S);