I am making a simple linked list with an insert function that has 4 conditions:
add in the middle of the list
add to the head if the list is not empty
add to an empty list
add to the end of a list
I have made an addNode function that takes care of the last two conditions and im trying to implement it inside of the insertNode but it keeps crashing? is there any specific reason or am I just missing something simple?
addNode:
void linkList::addNode(int dataAdd) // this only adds a node to the end
{
nodePtr n = new node; // what this does is make the node pointer 'n' point to this new node
n->next = NULL;
n->data = dataAdd;
if (head != NULL)
{
follow = head;
while (follow->next != NULL)
{
follow = follow->next;
}
follow->next = n;
}
else
{
head = n;
}
}
insertNode:
void linkList::insertNode(int dataInsert)
{
nodePtr n = new node;
n->next = NULL;
n->data = dataInsert;
follow = head;
while (follow != NULL)
{
trail = follow;
follow = follow->next;
if (head->data < dataInsert){
n->next = head; //n's next will become the current head (n is now the first item in the linkList)
head = n; //n the head structure will now be n (n is the new head)
}
else if (follow->data < dataInsert)
{
trail->next = n;
n->next = follow;
break;
}
else
{
addNode(dataInsert);
}
}
}
I want to call addNode in the else statement in insertNode
Related
I am stuck in a question where I have to make a function to check if the given linked list is palindrome.
Here is my approach for the problem, this is failing in the test case (1->1->2->1) the question is to check if the given linked list is palindromic or not.
ListNode* reverse(ListNode* head)
{
ListNode* c = head;
ListNode* p = NULL;
ListNode* n;
while (c != NULL) {
n = c->next;
c->next = p;
p = c;
c = n;
}
head = p;
return head;
}
class Solution {
public:
bool isPalindrome(ListNode* head)
{
if (head == NULL || head->next == NULL)
return true;
ListNode* temp = NULL;
temp = reverse(head);
int flag = 0;
while (temp != NULL && head != NULL) {
if (temp->val != head->val) {
flag = 1;
}
head = head->next;
temp = temp->next;
}
if (flag == 0)
return true;
else
return false;
}
};
The reverse(ListNode* head) reverses the same linked list to 1->2->1->1.
The head is pointing to the last node 1, whereas the temp points to the first 1.
In code, we were trying to compare the two linked list with starting node temp and head and since those are not equal the code gives a wrong answer.
What I am trying to do is check if node after value is specific number and if is, then delete previoues node.
Something like:
1,2,3,4,5,4
if next node number is 4 then delete this node.
1,2,3,4,5,4 -> 1,2,4,4
node* temp = head;
while (head != NULL) {
if (head->next->number == 4) {
delete temp;
}
head = head->next;
}
Struggling at this moment as compiler crashes.
You delete the head before you advance it:
node *temp = NULL;
while (head->next != NULL) {
if (head->next->number == 4) {
temp = head;
}
if ( temp == NULL ){
head = head->next;
}
else{
head = head->next->next;
delete temp;
temp= NULL:
}
}
I wrote the following code to insert an integer to a linked list, but in a sorting approach.
I commented where my problem is and explained it below:
void LLL::insertSorted(int r) {
node * temp = NULL;
node * current = NULL;
if (head == NULL) {
head = new node;
head->data = r;
head->next = NULL;
} else {
temp = new node;
temp->data = r;
current = head;
while (temp->data > current->data && current != NULL) {
current = current->next;
}
temp->next = current;
/*
* Assume that I have head points to this list: { 3 -> 5 -> 8 -> NULL }
* And I want to insert {6} (temp) to the list just after 5; then what
* I've done so far on my previous code I made temp = {6 -> 8 -> NULL}.
* NOW!! How can correctly insert temp to ((head)) just after {5}??!
*/
}
}
void LLL::insertSorted(int r) {
node * cur = head;
node * prev = NULL;
while((cur != NULL) && (r > cur->data)){//find the location to insert
prev = cur;
cur = cur->next;
}
node *new_node = new node;
new_node->data = r;
new_node->next = cur;
if(prev == NULL){//new first one
head = new_node;
}else{
prev->next = new_node;
}
}
You need to remember which node you are inserting after, and have that node link to the new one.
For example, in your loop to find the node that comes after temp, current, have another variable, say prev, and do pre = current just before current = current->next.
you need a new node when insert
I think just make a new node after the previous one you want insert, like 5 in your comment
This is the final result: (it works!)
It handles the following cases:
case 1: head is empty.
case 2: the new element is the smallest in the list.
case 3: the new element is somewhere in the middle of the list.
case 4: the new element is the largest.
void LLL::insertSorted(int r) {
node * temp = NULL;
node * current = NULL;
node * previous = NULL;
if (head == NULL) {
head = new node;
head->data = r;
head->next = NULL;
} else {
temp = new node;
temp->data = r;
temp->next = NULL;
current = head;
if (temp->data < current->data) {
temp->next = head;
head = temp;
} else {
while (current != NULL && temp->data >= current->data) {
previous = current;
current = current->next;
}
temp->next = current;
previous->next = temp;
}
}
}
So I've got this insert function for a doubly linked list that is working for the most part just up until I try to insert a new node at a given index. I'm having trouble with linking it correctly to the nodes before and after it, if anyone could see why, I keep getting errors when I try to assign one of the points I'll point out in the code:
void insert(int index, ItemType& item)
{
int pos = 0;
Node* current = new Node;
Node* n = new Node;
n->info = item;
if (index >= 0 && index <= size)
{
if (size == 0)
{
head = n;
tail = n;
n->next = NULL;
n->prev = NULL;
size++;
return;
}
else if (index == 0)
{
n->prev = NULL;
n->next = head;
head->prev = n;
head = n;
size++;
return;
}
else if (index == size)
{
n->next = NULL;
n->prev = tail;
tail->next = n;
tail = n;
size++;
return;
}
else if (index < size/2)
{
current = head;
while(pos != index)
{
current = current->next;
pos++;
}
}
else if (index > size/2)
{
int endpos = size - 1;
current = tail;
while(endpos != index)
{
current = current->prev;
endpos--;
}
}
n->next = current;
current->prev->next = n; // HERE is where the code breaks, don't know why.
n->prev = current->prev;
current->prev = n;
size++;
}
}
So the code breaks at the current->prev->next = n statement stating there is an access violation writing location. So I'm not sure if that is coded right or if I've messed up in pointing assignments in earlier code. If anyone knows why its doing that and can point me in the right direction that would be awesome. Thanks.
From my observation,
Your code fails when index = size/2.
When there are two elements(size == 2) and when you try to insert at position 1, then current->prev->next = n; is meaningless
Do one of these changes else if (index <= size/2) or else if (index >= size/2)
If current is the first node in the list, then current->prev will be NULL, so current->prev->next will cause problems. You should check if current is the first item in the list before this line.
Also, your code leaks memory because you are allocating a new Node for current and you do not delete it. Since you are using current to move through the list rather than to create a new node, you should declare it as just
Node* current;
rather than
Node* current = new Node;
I'm very new to C++ so it's a bit hard to understand the syntax sometimes. Anyways, I'm supposed to implement a function that adds and sorts the data given into a linked list. For example if I pass 2 into the list [1,4,5] then I should get [1,2,4,5]
Here is what I have written so far, and no it does not work, I keep getting "blah blah not declared in this scope"
void addSorted(Data * ){
temp = 0;
if (head == NULL) {
head = new LinkNode(newData);
}
else {
LinkNode * current = head;
while (current->next != NULL) {
current = current->next;
}
if(current->data > current->next->data){
temp = current->data;
current->data = current->next->data;
current->next->data = temp;
}
current->next = new LinkNode(newData);
}
}
Someone please help me, I'm using the struct LinkNode I think which is already given, in addition to a bunch of other functions like add, insert, remove, get, and size
I'm not looking to just get the answer, I need to know why what I have isn't working.
Hope that you have really put your effort in it..I am posting the whole function for inserting data into list in sorted order which I had written long time back
void addData(node * head, int data){
if(head == NULL) { // First node
node * temp = new node;
temp -> data = data;
temp -> next = NULL;
head = temp;
root = head;
}else{
node * prev = NULL;
while(head -> next != NULL){ // Sorted Addition Logic is HERE
if(data >= head -> data){
prev = head;
head = head -> next;
continue;
}else{
node * temp = new node;
temp -> data = data;
temp -> next = head;
if(prev != NULL)
prev -> next = temp;
else
head = temp;
break;
}
}
if(head -> next == NULL){
node * temp = new node;
temp -> data = data;
head -> next = temp;
temp -> next = NULL;
}
}
}
Seeing your code, it seems that your logic itself is wrong...you are always ending on the last node (while loop) and in the next if statment you are trying to compare the data of the next node which is not present