'Linked List function in C
I'm taking an online course and when the instructor executes this code it works, but it doesnt when I try to execute it on DevC++. And I cannot understand why. It's basically supposed to add a value to the linked list based on its order. I'd really appreciate your help
#include <stdio.h>
#include <stdlib.h>
typedef struct n{
int x;
struct n * next;
} node;
void print(node *r){
int count=0;
while(r->next != NULL){
printf("%d-ci eleman= %d \n", count, r->x);
r=r->next;
count++;
}
}
node* siraliek(node *r, int x){
if(r==NULL){ //if the linked list is empty
r=(node*)malloc(sizeof(node));
r->next=NULL;
r->x = x;
return r;
}
node * iter =r;
while(iter->next !=NULL && iter->next->x < x){
iter= iter->next;
}
if(r->x >x){//if the added value is smaller than the previous value in linked list
node*temp = (node*)malloc(sizeof(node));
temp->x = x;
temp->next = r;
return temp;
}
node* temp=(node*)malloc(sizeof(node));
temp->next = iter->next;
iter -> next = temp;
temp ->x = x;
}
int main(){
node*temp = (node*) malloc(sizeof(node));
node * root;
root = (node *)malloc(sizeof(node));
root=NULL;
root =siraliek(root, 200);
root=siraliek(root, 25);
root=siraliek(root, 20);
root=siraliek(root, 300);
node * iter;
iter=root;
print(root);
}
Solution 1:[1]
I think you will be able to find an answer to your problem using this file that I have written. It has a variety of functions that do operations with linked lists. In the main program I have included an example where I insert some numbers and than do the sorting. Please do not hesitate if you have any questions concerning the code!
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <err.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
typedef struct node
{
int value;
struct node *next;
} node;
// Prints a string of characters.
static inline void print(const char* f, ...)
{
va_list args;
va_start(args, f);
vprintf(f, args);
va_end(args);
}
// Prints a string of characters followed by a new line.
static inline void println(const char* f, ...)
{
va_list args;
va_start(args, f);
vprintf(f, args);
va_end(args);
printf("\n");
}
static inline void* mem_alloc(size_t size)
{
void* p = malloc(size);
if (p == NULL)
{
err(EXIT_FAILURE, "Problem with malloc().");
}
return p;
}
// Verify if node is empty
int node_is_empty(node * head)
{
return head == NULL;
}
// Verify if node is not empty
int node_is_not_empty(node * head)
{
return head != NULL;
}
//Builds the sentinell of the node structure
node* node_build_sentinel()
{
// Creates the sentinel.
node* head = mem_alloc(sizeof(node));
head->value = 0;
head->next = NULL;
// Returns the head of the node which is the sentinell.
return head;
}
//Prints the contents of a node
void node_print(node* head)
{
print("{");
while(head->next)
{
head = head->next;
print(" %d", head->value);
}
println(" }");
}
//Frees the allocated node
void node_free(node* head)
{
node* previous;
while (head)
{
previous = head;
head = head->next;
free(previous);
}
}
// Inserts a value right after the head
/*
HEAD -> 1 -> 2 -> ..... -> 8
node_insert_beg(node* HEAD, int 42);
HEAD -> 42 -> 1 -> 2 -> ..... -> 8
*/
void node_insert_beg(node* head, int value)
{
node* tmp = mem_alloc(sizeof(node));
tmp->value = value;
tmp->next = head->next;
head->next = tmp;
}
// Inserts a value right after the head
/*
HEAD -> 1 -> 2 -> ..... -> 8
node_insert_end(node* HEAD, int 42);
HEAD -> 1 -> 2 -> ..... -> 8 -> 42
*/
void node_insert_end(node* head, int value)
{
node* tmp = mem_alloc(sizeof(node));
for (; node_is_not_empty(head->next); head = head->next)
{
//This loop runs to the last node and quits with head being that last node
continue;
}
tmp->value = value;
tmp->next = head->next;
head->next = tmp;
}
//Inserts odd values to the end and even values to the beginning
void node_insert_num_odd_and_even(node* head, int value)
{
//odd
if(value % 2)
{
node_insert_end(head, value);
}
//even
else
{
node_insert_beg(head, value);
}
}
//Extracts the minimum value of the node (in other words deletes it from the node)
void node_extract_min(node* list,node *sup)
{
node *before_min = list;
while (list->next != NULL)
{
if (list->next->value < before_min->next->value)
{
before_min = list;
}
list = list->next;
}
node *output = before_min->next;
before_min->next = before_min->next->next;
output->next = NULL;
while (sup->next!=NULL)
{
sup = sup->next;
}
sup->next = output;
}
//Extracts the maximum value of the node (in other words deletes it from the node)
void node_extract_max(node* list, node* sup)
{
node *before_max = list;
while (list->next != NULL)
{
if (list->next->value > before_max->next->value)
{
before_max = list;
}
list = list->next;
}
node *output = before_max->next;
before_max->next = before_max->next->next;
output->next = NULL;
while (sup->next!=NULL)
{
sup = sup->next;
}
sup->next = output;
}
//Inserts a node into a SORTED linked list
void node_insert_sort(node * head, int value)
{
node* tmp = mem_alloc(sizeof(node));
tmp->value = value;
//Run through the nodes until it is not empty and the value of the next node is smaller than value parameter
for (; node_is_not_empty(head->next) && (head->next->value >= value); head = head->next)
{
continue;
}
tmp->next = head->next;
head->next = tmp;
}
//Sorts any linked list from largest values to the smallest
void node_sort_linked_list(node * head)
{
//Linear sort, find max and swap it with the first val
//If there is only a sentinel
if ( node_is_empty(head->next) )
{
//Do nothing since there is only a sentinel
}
else
{
node * tmp;
tmp = node_build_sentinel();
while (node_is_not_empty(head->next))
{
node_extract_max(head,tmp);
}
head->next = tmp->next;
free(tmp);
}
}
//Deletes the first occurence of value in node
int node_delete_first_occurence(node* head, node* sup, int value)
{
int seen = 0;
node *tmp = head;
while (head->next != NULL)
{
if (head->next->value == value)
{
tmp = head;
seen+=1;
break;
}
head = head->next;
}
if(seen == 0)
{
return seen;
}
node *output = head->next;
tmp->next = tmp->next->next;
output->next = NULL;
while (sup->next!=NULL)
{
sup = sup->next;
}
sup->next = output;
return seen;
}
//Deletes all occurences of value in node
int node_delete_all_occurences(node* head, node* sup, int value)
{
int seen = 0;
node *tmp = head;
while (head->next != NULL)
{
if (head->next->value == value)
{
seen+=1;
tmp = head;
node *output = head->next;
tmp->next = tmp->next->next;
output->next = NULL;
while (sup->next!=NULL)
{
sup = sup->next;
}
sup->next = output;
continue;
}
head = head->next;
}
return seen;
}
//Get a node at index if index invalid return NULL
//DOES NOT DELETE THE NODE
node * node_get_at(node* node, unsigned long index)
{
while (node != NULL && index > 0)
{
node = node->next;
index--;
}
if (node != NULL)
{
node = node->next;
}
return node;
}
//Gets the sum of all the values in the nodes
int* node_sum(node * head)
{
int * sum_elem = malloc(sizeof(int)*2);
int sum = 0;
int elements = 0;
while (head->next != NULL)
{
elements+=1;
sum += head->next->value;
head = head->next;
}
sum_elem[0] = sum;
sum_elem[1] = elements;
return sum_elem;
}
int main()
{
node * nodes = node_build_sentinel();
node_insert_end(nodes,3);
node_insert_end(nodes,1);
node_insert_end(nodes,9);
node_insert_end(nodes,45);
node_insert_end(nodes,-4);
node_sort_linked_list(nodes);
node_print(nodes);
node_free(nodes);
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vladouch |
