Critical error detected c0000374
#pragma once
typedef struct node
{
int value;
node* next;
node* before;
} node;
void print_nodes(node* list) {
node *current = (node*)malloc(sizeof(node));
//current->value = 0;
current->next = list;
while (current->next != nullptr) {
printf("%i\n", current->next->value); <-THROW an Exception in the Fist loop
current->next = current->next->next;
}
free(current);
}
void add_node(node* list) {
}
inline void new_nodes(node* list, size_t anzahl) {
list[0].before = NULL;
for (int i = 0; i <= anzahl; i++) {
list[i].value = i + 1;
list[i].next = &list[i + 1];
list[i + 1].next = &list[i - 1];
}
list[anzahl].next = NULL;
}
The printf statement Throws an Exception ... but only sometimes.
My cpp calls the function new_nodes with the size_t = 10 so it cant be tooooo big.
Additional Info one time even the heap "broke".
Thanks for your Help.
this:
void print_nodes(node* list)
{
node *current = (node*)malloc(sizeof(node));
//current->value = 0;
current->next = list;
while (current->next != nullptr)
{
printf("%i\n", current->next->value); <-THROW an Exception in the Fist loop
current->next = current->next->next;
}
free(current);
}
Needs to be heavily modified: Suggest:
modified (after clarification by the OP) to use a 'headless' linked list
void print_nodes(node* list)
{
node * current = list;
while (current)
{
printf("%i\n", current->value);
current = current->next;
}
}
Related
Following the tutorial, I wrote down this function to find the key in a linked list. However, it doesn't seem to work for me, curious about the reason. Here's part of the find function:
node_t *find_node(node_t *head, int number_to_find)
{
node_t *tmp = head;
while (tmp != NULL)
{
if(tmp->value == number_to_find)
{
return tmp;
tmp = tmp->next;
}
}
return NULL;
}
I embedded this function into my program. It successfully compiled but as it runs, the terminal did not show anything.
#define MAX_LIST 25
typedef struct node
{
int value;
struct node *next;
}node_t;
node_t *create_new_node(int value)
{
node_t *new = malloc(sizeof(node_t));
new->value = value;
new->next = NULL;
return new;
}
node_t *add_to_list(node_t *head, node_t *next_node)
{
next_node->next = head;
return next_node;
}
/*
TO FIND VALUE IN THE LINKED LIST
*/
node_t *find_link_list(node_t *head, int number_to_find)
{
node_t *tmp = head;
while (tmp != NULL)
{
if(tmp->value == number_to_find)
{
return tmp;
tmp = tmp->next;
}
}
return NULL;
}
bool print_linked_list(node_t *head)
{
if(head == NULL)
{
return false;
}
else
{
node_t *tmp = head;
while(tmp != NULL)
{
printf("%d ", tmp->value);
tmp = tmp->next;
}
return true;
}
}
int main()
{
node_t *head = NULL;
node_t *tmp;
for(int i = 0; i < MAX_LIST; i++)
{
tmp = create_new_node(i);
head = add_to_list(head, tmp);
}
tmp = find_link_list(head, 9);
printf("%d \n", tmp->value);
print_linked_list(head);
}
Anyone has a clue why this happen? It would be much appreciated : )
tmp = tmp->next;
should be outside of the if statement. Currently, you're saying, get to the next node in the list only if the value is equal to the target value. So instead you can have
while (tmp != NULL)
{
if(tmp->value == number_to_find)
{
return tmp;
}
tmp = tmp->next;
}
I need to segregate even and odd nodes in a Linked List in C.
Example:
Original list: 3,1,8,2,5,6
After change: 8,2,6,3,1,5
I have a problem when the head list data is an odd number, it is cutting out the even numbers.
Example:
Original list: 3,1,8,2,5,6
After change: 3,1,5
typedef struct Node {
int data; // store information
struct Node * next; //referance to the next node
}Node;
Node * create(Node * L, int value)
{
Node *current = L ,* new_node = (Node*)malloc(sizeof(Node)); // create and allocate a node
new_node->data = value;
new_node->next = NULL;
if (L == NULL)
{
L = new_node;
current = new_node;
}
else
{
while (current->next != NULL)
current = current->next;
current->next = new_node;
current = new_node;
}
return L;
}
Node * Change_Even_Odd(Node * L)
{
Node *oddhead = NULL, *evenhead = NULL, *lastodd = NULL, *lasteven = NULL, *current = L;
while (current != NULL)
{
// if current is even node
if (current->data % 2 == 0)
{
if (evenhead == NULL)
{
evenhead = current;
lasteven = current;
}
else
{
lasteven->next = current; // to connect the node to the list in the end
lasteven = current; // final list
}
}
else
{
// if current is odd node
if (oddhead == NULL)
{
oddhead = current;
lastodd = current;
}
else
{
lastodd->next = current;
lastodd = current;
}
}
current = current->next;
}
if (evenhead != NULL) // to put the even node in the head list
L = evenhead;
if (lasteven != NULL) // link the odd nodes to the even nodes
lasteven->next = oddhead;
if (lastodd != NULL) //end of list
lastodd->next = NULL;
return L;
}
void Print_List(Node * head)
{
while (head != NULL)
{
printf("%4d", head->data);
head = head->next;
}
}
int main()
{
Node * list = NULL; //empty linked list
srand(time(NULL));
int i, size = rand() % 10 + 1;
for (i = 0; i < size; i++)
list = create(list, rand() % 10 + 1);
printf("%d\n",size);
Print_List(list);
printf("\n");
Change_Even_Odd(list);
Print_List(list);
printf("\n");
}
Change_Even_Odd(list); should be list = Change_Even_Odd(list); (I already pointed out.)
– BLUEPIXY
Why does the last append call not work? I have to add some garbage here because it is complaining that my post is mostly code, I hope it is enough details by now.
typedef struct node {
int val;
struct node * next;
} node_t;
void append_node(node_t * head, int val) {
node_t * current = head;
while(current->next != NULL) {
current = current->next;
}
current->next = malloc(sizeof(node_t));
if(current->next == NULL)
printf("err");
current = current->next;
current->val = val;
current->next = NULL; //malloc(sizeof(node_t));
}
void print_list(node_t * head) {
node_t * current = head;
while(current->next != NULL) {
printf("%d ", current->val);
current = current->next;
}
printf("\n");
}
int main() {
node_t * list = malloc(sizeof(node_t));
list->val = 1;
list->next = NULL;
append_node(list,12);
append_node(list,14);
append_node(list,17);
print_list(list);
return 0;
}
Output:
1 12 14
The problem is in your print function. You don't print the last element.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int val;
struct node * next;
} node_t;
void append_node(node_t * head, int val) {
node_t * current = head;
while(current->next != NULL) {
current = current->next;
}
current->next = malloc(sizeof(node_t));
if(current->next == NULL)
printf("err");
current = current->next;
current->val = val;
current->next = NULL; //malloc(sizeof(node_t));
}
void print_list(node_t * head) {
node_t * current = head;
while(current!= NULL) {
printf("%d ", current->val);
current = current->next;
}
printf("\n");
}
int main() {
node_t * list = malloc(sizeof(node_t));
list->val = 1;
list->next = NULL;
append_node(list,12);
append_node(list,14);
append_node(list,17);
print_list(list);
return 0;
}
I am trying to code very basic sorting method for linked lists. I am getting unhandled exception. What is the mistake i am making? Here is my code:-
struct LinkedNode// structure for linked list
{
int data;
struct LinkedNode *next;
}*start = NULL;
following function creates a linked list
void CreateLinkedList()
{
LinkedNode *newNode, *current;
printf("enter 5 numbers to create linked list\n");
for(int i=0; i<5; i++)
{
newNode = (struct LinkedNode *)malloc(sizeof(LinkedNode));
scanf("%d", &newNode->data);
newNode->next = NULL;
if(start == NULL)
{
start = newNode;
current = newNode;
}
else
{
current->next = newNode;
current = newNode;
}
}
}
following function is used for sorting the linked list nodes
void SortLinkedList()
{
struct LinkedNode *node=NULL, *temp = NULL;
int tempvar;//temp variable to store node data
node = start;
temp = node->next;//temp node to hold node data and next link
while(node != NULL && node->next != NULL)
{
for(int j=0; j<5; j++)//value 5 because I am taking only 5 nodes
{
if(node->data > temp->data)//swap node data
{
tempvar = node->data;
node->data = temp->data;
temp->data = tempvar;
}
temp = temp->next;
}
node = node->next;
}
}
Try This code
void SortLinkedList()
{
struct LinkedNode *node=NULL, *temp = NULL;
int tempvar;//temp variable to store node data
node = start;
//temp = node;//temp node to hold node data and next link
while(node != NULL)
{
temp=node;
while (temp->next !=NULL)//travel till the second last element
{
if(temp->data > temp->next->data)// compare the data of the nodes
{
tempvar = temp->data;
temp->data = temp->next->data;// swap the data
temp->next->data = tempvar;
}
temp = temp->next; // move to the next element
}
node = node->next; // move to the next node
}
}
1 - outer while loop is use for the total number of pass that will require to sort the linked list..
2- In second while loop we are actually comparing the data of the nodes that we want to sort
Instead of implementing your own sort, you can just use qsort. Of course, qsort requires an array, but that is easy enough to create.
In this case, you know that the list has 5 members. But, if you didn't know, you could just count them.
int size_of_list = 0;
struct LinkedNode *p;
for (p = start; p != NULL; p = p->next) ++size_of_list;
if (size_of_list == 0) {
// Nothing to do
return;
}
Now you can create your array;
struct LinkedNode *arr[size_of_list + 1], **arrp = arr;
for (p = start; p != NULL; p = p->next) *arrp++ = p;
*arrp = NULL;
Then, use qsort on arr. There are lots of examples, but the trick is writing an appropriate comparison function.
int cmp_LinkedNode(const void *a, const void *b) {
const struct LinkedNode * const *aa = a;
const struct LinkedNode * const *bb = b;
return ((*aa)->data > (*bb)->data) - ((*aa)->data < (*bb)->data);
}
//...
qsort(arr, size_of_list, sizeof(*arr), cmp_LinkedNode);
And then, rewire the list into the sorted order.
for (int i = 0; i < size_of_list; ++i) arr[i]->next = arr[i+1];
start = arr[0];
yeah sorting a linked list using nodes/links is a pretty hard job. Spend hours doing it myself but since i have done it why not help others..
What you need to do is simply find the minimum value in your list. Swap it with the head node and the recur for head->next.
The code for sort is only of 3 to 4 lines if you have FindMin() and Swap() functions made..
here is the complete code for sort(),swap()and findmin().
void sort(node **start)
{
if (((*start)->next == NULL) || (*start == NULL))
{
return;
}
node *min = findmin(*start);
swap(*start, min, start);
sort(&((*start)->next));
}
void swap(node *p1, node *p2, node **start)
{
node *p1pre = NULL;
node *p1curr = *start;
while (p1curr!=p1)
{
p1pre = p1curr;
p1curr = p1curr->next;
}
node *p2pre = NULL;
node *p2curr = *start;
while (p2curr != p2)
{
p2pre = p2curr;
p2curr = p2curr->next;
}
if (p1pre != NULL)
{
p1pre->next = p2curr;
}
else
{
*start = p2curr;
}
if (p2pre != NULL)
{
p2pre->next = p1curr;
}
else
{
*start = p1curr;
}
node *temp = p2curr->next;
p2curr->next = p1curr->next;
p1curr->next = temp;
}
node* findmin(node *start)
{
int flag = 0;
if (start == NULL)
{
cout << "list is empty" << endl;
}
else
{
node *curr = start->next;
node *min = start;
while (curr->next != NULL)
{
if (min->value > curr->value)
{
min = curr;
flag++;
}
curr = curr->next;
}
if ((curr->next == NULL) && (min->value > curr->value))
{
min = curr;
flag++;
}
if (flag > 0)
{
return min;
}
}
}
void *node_search(node_t *root, void *key) {
node_t** curr = &root;
int outcome;
static int comparison = 0;
while (*curr){
outcome = strcmp(key, (*curr)->name);
comparison++;
printf("%d", outcome);
if(outcome<0) {
curr = &(*curr)->left;
} else {
if(outcome == 0){
printf("%s---> ", key);
printf("%d number of comparisions\n", comparison);
comparison=0;
return (*curr)->movie;
}
curr = &(*curr)->right;
}
}
printf("%s---> ", key);
printf("%d number of comparisions but NOT FOUND\n", comparison);
comparison = 0;
return (*curr);
}
when the number of data is small it finds what i need to find pefectly
but when the same data set but larger in size gets used it prints out not found
why is this??
here's my insertion to tree
node_t *insert_node(node_t *root, node_t *new)
{
node_t** curr = &root;
while (*curr)
{
if (strcmp(new->name, (*curr)->name) < 0) {
curr = &(*curr)->left;
} else {
curr = &(*curr)->right;
}
}
*curr = new;
return root;
}
In insert_node, you should be passing in root as a node_t ** instead of a node_t *. Otherwise, if you have an empty tree the root node won't get created. Also, be sure to initialize root to NULL in your main.
Since you'll be passing in the address of your root pointer, insert_node doesn't need to return anything.
void insert_node(node_t **root, node_t *new)
{
node_t** curr = root;
while (*curr)
{
if (strcmp(new->name, (*curr)->name) < 0) {
curr = &(*curr)->left;
} else {
curr = &(*curr)->right;
}
}
*curr = new;
}
int main()
{
node_t *root = NULL;
...
insert_node(&root, node1);
insert_node(&root, node2);
...
}