linked list c programming error by inserting new element












-2















I am trying to insert an element but it get the error "Process finished with exit code 11"



struct node {
int key;
struct node *next;
};
struct node* init(){
struct node *head =NULL;
return head;
}
void create(struct node * head,int num) {
struct node * tmp = head;
struct node * prev = NULL;
struct node* new = malloc(sizeof(struct node));
new->key = num;
prev = tmp;
tmp = tmp->next;
while(tmp!= NULL && tmp->key < num){
prev = tmp;
tmp = tmp->next;
}
new->next = tmp;
prev->next = new;
if (tmp== NULL)
head=tmp;
}
int main() {
int num;
struct node* head;
head=init()
printf("Enter data:");
scanf("%d",&num);
create(head,num);
}


i am trying to insert an element into a linked list and the element should be sorted and entered at the same time.can someone tell me that the error is ? i cannot seem to find out the error.










share|improve this question




















  • 2





    Did you try to run your code under debugger? On what line an error appears?

    – Miamy
    Nov 19 '18 at 18:01











  • A node is no list. A list is no node.

    – Swordfish
    Nov 19 '18 at 18:08













  • tmp = tmp->next; i am having the error here

    – George Jose
    Nov 19 '18 at 18:14













  • how can i correct it?

    – George Jose
    Nov 19 '18 at 18:15











  • @GeorgeJose Sorry, but since there is no list in your code, there is no easy fix.

    – Swordfish
    Nov 19 '18 at 19:20
















-2















I am trying to insert an element but it get the error "Process finished with exit code 11"



struct node {
int key;
struct node *next;
};
struct node* init(){
struct node *head =NULL;
return head;
}
void create(struct node * head,int num) {
struct node * tmp = head;
struct node * prev = NULL;
struct node* new = malloc(sizeof(struct node));
new->key = num;
prev = tmp;
tmp = tmp->next;
while(tmp!= NULL && tmp->key < num){
prev = tmp;
tmp = tmp->next;
}
new->next = tmp;
prev->next = new;
if (tmp== NULL)
head=tmp;
}
int main() {
int num;
struct node* head;
head=init()
printf("Enter data:");
scanf("%d",&num);
create(head,num);
}


i am trying to insert an element into a linked list and the element should be sorted and entered at the same time.can someone tell me that the error is ? i cannot seem to find out the error.










share|improve this question




















  • 2





    Did you try to run your code under debugger? On what line an error appears?

    – Miamy
    Nov 19 '18 at 18:01











  • A node is no list. A list is no node.

    – Swordfish
    Nov 19 '18 at 18:08













  • tmp = tmp->next; i am having the error here

    – George Jose
    Nov 19 '18 at 18:14













  • how can i correct it?

    – George Jose
    Nov 19 '18 at 18:15











  • @GeorgeJose Sorry, but since there is no list in your code, there is no easy fix.

    – Swordfish
    Nov 19 '18 at 19:20














-2












-2








-2








I am trying to insert an element but it get the error "Process finished with exit code 11"



struct node {
int key;
struct node *next;
};
struct node* init(){
struct node *head =NULL;
return head;
}
void create(struct node * head,int num) {
struct node * tmp = head;
struct node * prev = NULL;
struct node* new = malloc(sizeof(struct node));
new->key = num;
prev = tmp;
tmp = tmp->next;
while(tmp!= NULL && tmp->key < num){
prev = tmp;
tmp = tmp->next;
}
new->next = tmp;
prev->next = new;
if (tmp== NULL)
head=tmp;
}
int main() {
int num;
struct node* head;
head=init()
printf("Enter data:");
scanf("%d",&num);
create(head,num);
}


i am trying to insert an element into a linked list and the element should be sorted and entered at the same time.can someone tell me that the error is ? i cannot seem to find out the error.










share|improve this question
















I am trying to insert an element but it get the error "Process finished with exit code 11"



struct node {
int key;
struct node *next;
};
struct node* init(){
struct node *head =NULL;
return head;
}
void create(struct node * head,int num) {
struct node * tmp = head;
struct node * prev = NULL;
struct node* new = malloc(sizeof(struct node));
new->key = num;
prev = tmp;
tmp = tmp->next;
while(tmp!= NULL && tmp->key < num){
prev = tmp;
tmp = tmp->next;
}
new->next = tmp;
prev->next = new;
if (tmp== NULL)
head=tmp;
}
int main() {
int num;
struct node* head;
head=init()
printf("Enter data:");
scanf("%d",&num);
create(head,num);
}


i am trying to insert an element into a linked list and the element should be sorted and entered at the same time.can someone tell me that the error is ? i cannot seem to find out the error.







c linked-list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 17:52









Broman

6,301112341




6,301112341










asked Nov 19 '18 at 17:48









George JoseGeorge Jose

62




62








  • 2





    Did you try to run your code under debugger? On what line an error appears?

    – Miamy
    Nov 19 '18 at 18:01











  • A node is no list. A list is no node.

    – Swordfish
    Nov 19 '18 at 18:08













  • tmp = tmp->next; i am having the error here

    – George Jose
    Nov 19 '18 at 18:14













  • how can i correct it?

    – George Jose
    Nov 19 '18 at 18:15











  • @GeorgeJose Sorry, but since there is no list in your code, there is no easy fix.

    – Swordfish
    Nov 19 '18 at 19:20














  • 2





    Did you try to run your code under debugger? On what line an error appears?

    – Miamy
    Nov 19 '18 at 18:01











  • A node is no list. A list is no node.

    – Swordfish
    Nov 19 '18 at 18:08













  • tmp = tmp->next; i am having the error here

    – George Jose
    Nov 19 '18 at 18:14













  • how can i correct it?

    – George Jose
    Nov 19 '18 at 18:15











  • @GeorgeJose Sorry, but since there is no list in your code, there is no easy fix.

    – Swordfish
    Nov 19 '18 at 19:20








2




2





Did you try to run your code under debugger? On what line an error appears?

– Miamy
Nov 19 '18 at 18:01





Did you try to run your code under debugger? On what line an error appears?

– Miamy
Nov 19 '18 at 18:01













A node is no list. A list is no node.

– Swordfish
Nov 19 '18 at 18:08







A node is no list. A list is no node.

– Swordfish
Nov 19 '18 at 18:08















tmp = tmp->next; i am having the error here

– George Jose
Nov 19 '18 at 18:14







tmp = tmp->next; i am having the error here

– George Jose
Nov 19 '18 at 18:14















how can i correct it?

– George Jose
Nov 19 '18 at 18:15





how can i correct it?

– George Jose
Nov 19 '18 at 18:15













@GeorgeJose Sorry, but since there is no list in your code, there is no easy fix.

– Swordfish
Nov 19 '18 at 19:20





@GeorgeJose Sorry, but since there is no list in your code, there is no easy fix.

– Swordfish
Nov 19 '18 at 19:20












1 Answer
1






active

oldest

votes


















-1














It's not clear what your function create()




void create(struct node * head, int num) {
struct node * tmp = head;
struct node * prev = NULL;
struct node* new = malloc(sizeof(struct node));
new->key = num;
prev = tmp;
tmp = tmp->next;
while (tmp != NULL && tmp->key < num) {
prev = tmp;
tmp = tmp->next;
}
new->next = tmp;
prev->next = new;
if (tmp == NULL)
head = tmp;
}



is supposed to do. You effectively pass it a NULL pointer and return void, so everything it does is meaningless to the outside world.



Thetm starting point for every no bs linked list implementation:



#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>

typedef struct node_tag {
int value;
struct node_tag *next;
} node_t;

// write functions to encapsulate the data and provide a stable interface:

node_t* node_create_value(int value)
{
node_t *new_node = calloc(1, sizeof *new_node);
if(new_node) new_node->value = value;
return new_node;
}

node_t* node_advance(node_t const *node) { return node->next; }


typedef struct list_tag { // a list usually consists of
node_t *head; // a pointer to the first and
node_t *tail; // a pointer to the last element
// size_t size; // one might want to add that.
} list_t;

list_t list_create(void)
{
list_t list = { NULL, NULL };
return list;
}

// make code based on these functions "speak" for itself:

node_t* list_begin(list_t const *list) { return list->head; }
node_t* list_end (list_t const *list) { return list->tail; }
bool list_is_empty(list_t const *list) { return !list_begin(list); }

// common operations for lists:

node_t* list_push_front(list_t *list, int value)
{
node_t *new_node = node_create_value(value);
if (!new_node)
return NULL;

new_node->next = list->head;
return list->head = new_node;
}

node_t* list_push_back(list_t *list, int value)
{
// push_back on an empty list is push_front:
if (list_is_empty(list))
return list->tail = list_push_front(list, value);

node_t *new_node = node_create_value(value);
if (!new_node)
return NULL;

list->tail->next = new_node;
return list->tail = new_node;
}

node_t* list_insert_after(list_t *list, node_t *node, int value)
{
if (list_end(list) == node)
return list_push_back(list, value);

node_t *new_node = node_create_value(value);
if (!new_node)
return NULL;

new_node->next = node->next;
return node->next = new_node;
}

node_t* list_insert_sorted(list_t *list, int value)
{
// first handle the special cases that don't require iterating the whole list:

if (list_is_empty(list) || value < list_begin(list)->value)
return list_push_front(list, value);

if (value > list_end(list)->value)
return list_push_back(list, value);

// the general (worst) case:

for (node_t *current_node = list_begin(list); node_advance(current_node); current_node = node_advance(current_node))
if (value < node_advance(current_node)->value)
return list_insert_after(list, current_node, value);

return NULL; // should never happen
}

void list_print(list_t const *list)
{
for (node_t *current_node = list_begin(list); current_node; current_node = node_advance(current_node))
printf("%dn", current_node->value);
}

void list_free(list_t *list)
{
for(node_t *current_node = list_begin(list), *next_node; current_node; current_node = next_node) {
next_node = current_node->next;
free(current_node);
}
}

// user code should not be required to know anything about the inner workings
// of our list:

int main(void)
{
list_t list = list_create();
for (int i = 1; i < 10; i += 2) {
if (!list_push_back(&list, i)) {
list_free(&list);
fputs("Not enough memory :(nn", stderr);
return EXIT_FAILURE;
}
}

list_print(&list);
putchar('n');

for (int i = 0; i < 11; i += 2) {
if (!list_insert_sorted(&list, i)) {
list_free(&list);
fputs("Not enough memory :(nn", stderr);
return EXIT_FAILURE;
}
}

list_print(&list);
list_free(&list);
}


Output:



1
3
5
7
9

0
1
2
3
4
5
6
7
8
9
10





share|improve this answer


























  • This doesn't answer the question in any way, shape, or form.

    – n.m.
    Nov 19 '18 at 19:35











  • @n.m. If that is your opinion downvote away. Plan-B: make suggestions on how to improve it. The code in the question is fubar.

    – Swordfish
    Nov 19 '18 at 19:40








  • 1





    The question seems to be about an error that code posted by OP has. An improvement plan: remove the code from the answer, explain what the error is, how to fix it, and how to avoid it in the future. Do not present an ideal from-the-book answer to the exercise. It is rather unlikely to be helpful to someone who is struggling with basic notions of the language.

    – n.m.
    Nov 19 '18 at 19:53













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380109%2flinked-list-c-programming-error-by-inserting-new-element%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














It's not clear what your function create()




void create(struct node * head, int num) {
struct node * tmp = head;
struct node * prev = NULL;
struct node* new = malloc(sizeof(struct node));
new->key = num;
prev = tmp;
tmp = tmp->next;
while (tmp != NULL && tmp->key < num) {
prev = tmp;
tmp = tmp->next;
}
new->next = tmp;
prev->next = new;
if (tmp == NULL)
head = tmp;
}



is supposed to do. You effectively pass it a NULL pointer and return void, so everything it does is meaningless to the outside world.



Thetm starting point for every no bs linked list implementation:



#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>

typedef struct node_tag {
int value;
struct node_tag *next;
} node_t;

// write functions to encapsulate the data and provide a stable interface:

node_t* node_create_value(int value)
{
node_t *new_node = calloc(1, sizeof *new_node);
if(new_node) new_node->value = value;
return new_node;
}

node_t* node_advance(node_t const *node) { return node->next; }


typedef struct list_tag { // a list usually consists of
node_t *head; // a pointer to the first and
node_t *tail; // a pointer to the last element
// size_t size; // one might want to add that.
} list_t;

list_t list_create(void)
{
list_t list = { NULL, NULL };
return list;
}

// make code based on these functions "speak" for itself:

node_t* list_begin(list_t const *list) { return list->head; }
node_t* list_end (list_t const *list) { return list->tail; }
bool list_is_empty(list_t const *list) { return !list_begin(list); }

// common operations for lists:

node_t* list_push_front(list_t *list, int value)
{
node_t *new_node = node_create_value(value);
if (!new_node)
return NULL;

new_node->next = list->head;
return list->head = new_node;
}

node_t* list_push_back(list_t *list, int value)
{
// push_back on an empty list is push_front:
if (list_is_empty(list))
return list->tail = list_push_front(list, value);

node_t *new_node = node_create_value(value);
if (!new_node)
return NULL;

list->tail->next = new_node;
return list->tail = new_node;
}

node_t* list_insert_after(list_t *list, node_t *node, int value)
{
if (list_end(list) == node)
return list_push_back(list, value);

node_t *new_node = node_create_value(value);
if (!new_node)
return NULL;

new_node->next = node->next;
return node->next = new_node;
}

node_t* list_insert_sorted(list_t *list, int value)
{
// first handle the special cases that don't require iterating the whole list:

if (list_is_empty(list) || value < list_begin(list)->value)
return list_push_front(list, value);

if (value > list_end(list)->value)
return list_push_back(list, value);

// the general (worst) case:

for (node_t *current_node = list_begin(list); node_advance(current_node); current_node = node_advance(current_node))
if (value < node_advance(current_node)->value)
return list_insert_after(list, current_node, value);

return NULL; // should never happen
}

void list_print(list_t const *list)
{
for (node_t *current_node = list_begin(list); current_node; current_node = node_advance(current_node))
printf("%dn", current_node->value);
}

void list_free(list_t *list)
{
for(node_t *current_node = list_begin(list), *next_node; current_node; current_node = next_node) {
next_node = current_node->next;
free(current_node);
}
}

// user code should not be required to know anything about the inner workings
// of our list:

int main(void)
{
list_t list = list_create();
for (int i = 1; i < 10; i += 2) {
if (!list_push_back(&list, i)) {
list_free(&list);
fputs("Not enough memory :(nn", stderr);
return EXIT_FAILURE;
}
}

list_print(&list);
putchar('n');

for (int i = 0; i < 11; i += 2) {
if (!list_insert_sorted(&list, i)) {
list_free(&list);
fputs("Not enough memory :(nn", stderr);
return EXIT_FAILURE;
}
}

list_print(&list);
list_free(&list);
}


Output:



1
3
5
7
9

0
1
2
3
4
5
6
7
8
9
10





share|improve this answer


























  • This doesn't answer the question in any way, shape, or form.

    – n.m.
    Nov 19 '18 at 19:35











  • @n.m. If that is your opinion downvote away. Plan-B: make suggestions on how to improve it. The code in the question is fubar.

    – Swordfish
    Nov 19 '18 at 19:40








  • 1





    The question seems to be about an error that code posted by OP has. An improvement plan: remove the code from the answer, explain what the error is, how to fix it, and how to avoid it in the future. Do not present an ideal from-the-book answer to the exercise. It is rather unlikely to be helpful to someone who is struggling with basic notions of the language.

    – n.m.
    Nov 19 '18 at 19:53


















-1














It's not clear what your function create()




void create(struct node * head, int num) {
struct node * tmp = head;
struct node * prev = NULL;
struct node* new = malloc(sizeof(struct node));
new->key = num;
prev = tmp;
tmp = tmp->next;
while (tmp != NULL && tmp->key < num) {
prev = tmp;
tmp = tmp->next;
}
new->next = tmp;
prev->next = new;
if (tmp == NULL)
head = tmp;
}



is supposed to do. You effectively pass it a NULL pointer and return void, so everything it does is meaningless to the outside world.



Thetm starting point for every no bs linked list implementation:



#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>

typedef struct node_tag {
int value;
struct node_tag *next;
} node_t;

// write functions to encapsulate the data and provide a stable interface:

node_t* node_create_value(int value)
{
node_t *new_node = calloc(1, sizeof *new_node);
if(new_node) new_node->value = value;
return new_node;
}

node_t* node_advance(node_t const *node) { return node->next; }


typedef struct list_tag { // a list usually consists of
node_t *head; // a pointer to the first and
node_t *tail; // a pointer to the last element
// size_t size; // one might want to add that.
} list_t;

list_t list_create(void)
{
list_t list = { NULL, NULL };
return list;
}

// make code based on these functions "speak" for itself:

node_t* list_begin(list_t const *list) { return list->head; }
node_t* list_end (list_t const *list) { return list->tail; }
bool list_is_empty(list_t const *list) { return !list_begin(list); }

// common operations for lists:

node_t* list_push_front(list_t *list, int value)
{
node_t *new_node = node_create_value(value);
if (!new_node)
return NULL;

new_node->next = list->head;
return list->head = new_node;
}

node_t* list_push_back(list_t *list, int value)
{
// push_back on an empty list is push_front:
if (list_is_empty(list))
return list->tail = list_push_front(list, value);

node_t *new_node = node_create_value(value);
if (!new_node)
return NULL;

list->tail->next = new_node;
return list->tail = new_node;
}

node_t* list_insert_after(list_t *list, node_t *node, int value)
{
if (list_end(list) == node)
return list_push_back(list, value);

node_t *new_node = node_create_value(value);
if (!new_node)
return NULL;

new_node->next = node->next;
return node->next = new_node;
}

node_t* list_insert_sorted(list_t *list, int value)
{
// first handle the special cases that don't require iterating the whole list:

if (list_is_empty(list) || value < list_begin(list)->value)
return list_push_front(list, value);

if (value > list_end(list)->value)
return list_push_back(list, value);

// the general (worst) case:

for (node_t *current_node = list_begin(list); node_advance(current_node); current_node = node_advance(current_node))
if (value < node_advance(current_node)->value)
return list_insert_after(list, current_node, value);

return NULL; // should never happen
}

void list_print(list_t const *list)
{
for (node_t *current_node = list_begin(list); current_node; current_node = node_advance(current_node))
printf("%dn", current_node->value);
}

void list_free(list_t *list)
{
for(node_t *current_node = list_begin(list), *next_node; current_node; current_node = next_node) {
next_node = current_node->next;
free(current_node);
}
}

// user code should not be required to know anything about the inner workings
// of our list:

int main(void)
{
list_t list = list_create();
for (int i = 1; i < 10; i += 2) {
if (!list_push_back(&list, i)) {
list_free(&list);
fputs("Not enough memory :(nn", stderr);
return EXIT_FAILURE;
}
}

list_print(&list);
putchar('n');

for (int i = 0; i < 11; i += 2) {
if (!list_insert_sorted(&list, i)) {
list_free(&list);
fputs("Not enough memory :(nn", stderr);
return EXIT_FAILURE;
}
}

list_print(&list);
list_free(&list);
}


Output:



1
3
5
7
9

0
1
2
3
4
5
6
7
8
9
10





share|improve this answer


























  • This doesn't answer the question in any way, shape, or form.

    – n.m.
    Nov 19 '18 at 19:35











  • @n.m. If that is your opinion downvote away. Plan-B: make suggestions on how to improve it. The code in the question is fubar.

    – Swordfish
    Nov 19 '18 at 19:40








  • 1





    The question seems to be about an error that code posted by OP has. An improvement plan: remove the code from the answer, explain what the error is, how to fix it, and how to avoid it in the future. Do not present an ideal from-the-book answer to the exercise. It is rather unlikely to be helpful to someone who is struggling with basic notions of the language.

    – n.m.
    Nov 19 '18 at 19:53
















-1












-1








-1







It's not clear what your function create()




void create(struct node * head, int num) {
struct node * tmp = head;
struct node * prev = NULL;
struct node* new = malloc(sizeof(struct node));
new->key = num;
prev = tmp;
tmp = tmp->next;
while (tmp != NULL && tmp->key < num) {
prev = tmp;
tmp = tmp->next;
}
new->next = tmp;
prev->next = new;
if (tmp == NULL)
head = tmp;
}



is supposed to do. You effectively pass it a NULL pointer and return void, so everything it does is meaningless to the outside world.



Thetm starting point for every no bs linked list implementation:



#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>

typedef struct node_tag {
int value;
struct node_tag *next;
} node_t;

// write functions to encapsulate the data and provide a stable interface:

node_t* node_create_value(int value)
{
node_t *new_node = calloc(1, sizeof *new_node);
if(new_node) new_node->value = value;
return new_node;
}

node_t* node_advance(node_t const *node) { return node->next; }


typedef struct list_tag { // a list usually consists of
node_t *head; // a pointer to the first and
node_t *tail; // a pointer to the last element
// size_t size; // one might want to add that.
} list_t;

list_t list_create(void)
{
list_t list = { NULL, NULL };
return list;
}

// make code based on these functions "speak" for itself:

node_t* list_begin(list_t const *list) { return list->head; }
node_t* list_end (list_t const *list) { return list->tail; }
bool list_is_empty(list_t const *list) { return !list_begin(list); }

// common operations for lists:

node_t* list_push_front(list_t *list, int value)
{
node_t *new_node = node_create_value(value);
if (!new_node)
return NULL;

new_node->next = list->head;
return list->head = new_node;
}

node_t* list_push_back(list_t *list, int value)
{
// push_back on an empty list is push_front:
if (list_is_empty(list))
return list->tail = list_push_front(list, value);

node_t *new_node = node_create_value(value);
if (!new_node)
return NULL;

list->tail->next = new_node;
return list->tail = new_node;
}

node_t* list_insert_after(list_t *list, node_t *node, int value)
{
if (list_end(list) == node)
return list_push_back(list, value);

node_t *new_node = node_create_value(value);
if (!new_node)
return NULL;

new_node->next = node->next;
return node->next = new_node;
}

node_t* list_insert_sorted(list_t *list, int value)
{
// first handle the special cases that don't require iterating the whole list:

if (list_is_empty(list) || value < list_begin(list)->value)
return list_push_front(list, value);

if (value > list_end(list)->value)
return list_push_back(list, value);

// the general (worst) case:

for (node_t *current_node = list_begin(list); node_advance(current_node); current_node = node_advance(current_node))
if (value < node_advance(current_node)->value)
return list_insert_after(list, current_node, value);

return NULL; // should never happen
}

void list_print(list_t const *list)
{
for (node_t *current_node = list_begin(list); current_node; current_node = node_advance(current_node))
printf("%dn", current_node->value);
}

void list_free(list_t *list)
{
for(node_t *current_node = list_begin(list), *next_node; current_node; current_node = next_node) {
next_node = current_node->next;
free(current_node);
}
}

// user code should not be required to know anything about the inner workings
// of our list:

int main(void)
{
list_t list = list_create();
for (int i = 1; i < 10; i += 2) {
if (!list_push_back(&list, i)) {
list_free(&list);
fputs("Not enough memory :(nn", stderr);
return EXIT_FAILURE;
}
}

list_print(&list);
putchar('n');

for (int i = 0; i < 11; i += 2) {
if (!list_insert_sorted(&list, i)) {
list_free(&list);
fputs("Not enough memory :(nn", stderr);
return EXIT_FAILURE;
}
}

list_print(&list);
list_free(&list);
}


Output:



1
3
5
7
9

0
1
2
3
4
5
6
7
8
9
10





share|improve this answer















It's not clear what your function create()




void create(struct node * head, int num) {
struct node * tmp = head;
struct node * prev = NULL;
struct node* new = malloc(sizeof(struct node));
new->key = num;
prev = tmp;
tmp = tmp->next;
while (tmp != NULL && tmp->key < num) {
prev = tmp;
tmp = tmp->next;
}
new->next = tmp;
prev->next = new;
if (tmp == NULL)
head = tmp;
}



is supposed to do. You effectively pass it a NULL pointer and return void, so everything it does is meaningless to the outside world.



Thetm starting point for every no bs linked list implementation:



#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>

typedef struct node_tag {
int value;
struct node_tag *next;
} node_t;

// write functions to encapsulate the data and provide a stable interface:

node_t* node_create_value(int value)
{
node_t *new_node = calloc(1, sizeof *new_node);
if(new_node) new_node->value = value;
return new_node;
}

node_t* node_advance(node_t const *node) { return node->next; }


typedef struct list_tag { // a list usually consists of
node_t *head; // a pointer to the first and
node_t *tail; // a pointer to the last element
// size_t size; // one might want to add that.
} list_t;

list_t list_create(void)
{
list_t list = { NULL, NULL };
return list;
}

// make code based on these functions "speak" for itself:

node_t* list_begin(list_t const *list) { return list->head; }
node_t* list_end (list_t const *list) { return list->tail; }
bool list_is_empty(list_t const *list) { return !list_begin(list); }

// common operations for lists:

node_t* list_push_front(list_t *list, int value)
{
node_t *new_node = node_create_value(value);
if (!new_node)
return NULL;

new_node->next = list->head;
return list->head = new_node;
}

node_t* list_push_back(list_t *list, int value)
{
// push_back on an empty list is push_front:
if (list_is_empty(list))
return list->tail = list_push_front(list, value);

node_t *new_node = node_create_value(value);
if (!new_node)
return NULL;

list->tail->next = new_node;
return list->tail = new_node;
}

node_t* list_insert_after(list_t *list, node_t *node, int value)
{
if (list_end(list) == node)
return list_push_back(list, value);

node_t *new_node = node_create_value(value);
if (!new_node)
return NULL;

new_node->next = node->next;
return node->next = new_node;
}

node_t* list_insert_sorted(list_t *list, int value)
{
// first handle the special cases that don't require iterating the whole list:

if (list_is_empty(list) || value < list_begin(list)->value)
return list_push_front(list, value);

if (value > list_end(list)->value)
return list_push_back(list, value);

// the general (worst) case:

for (node_t *current_node = list_begin(list); node_advance(current_node); current_node = node_advance(current_node))
if (value < node_advance(current_node)->value)
return list_insert_after(list, current_node, value);

return NULL; // should never happen
}

void list_print(list_t const *list)
{
for (node_t *current_node = list_begin(list); current_node; current_node = node_advance(current_node))
printf("%dn", current_node->value);
}

void list_free(list_t *list)
{
for(node_t *current_node = list_begin(list), *next_node; current_node; current_node = next_node) {
next_node = current_node->next;
free(current_node);
}
}

// user code should not be required to know anything about the inner workings
// of our list:

int main(void)
{
list_t list = list_create();
for (int i = 1; i < 10; i += 2) {
if (!list_push_back(&list, i)) {
list_free(&list);
fputs("Not enough memory :(nn", stderr);
return EXIT_FAILURE;
}
}

list_print(&list);
putchar('n');

for (int i = 0; i < 11; i += 2) {
if (!list_insert_sorted(&list, i)) {
list_free(&list);
fputs("Not enough memory :(nn", stderr);
return EXIT_FAILURE;
}
}

list_print(&list);
list_free(&list);
}


Output:



1
3
5
7
9

0
1
2
3
4
5
6
7
8
9
10






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 20:10

























answered Nov 19 '18 at 18:20









SwordfishSwordfish

9,38811436




9,38811436













  • This doesn't answer the question in any way, shape, or form.

    – n.m.
    Nov 19 '18 at 19:35











  • @n.m. If that is your opinion downvote away. Plan-B: make suggestions on how to improve it. The code in the question is fubar.

    – Swordfish
    Nov 19 '18 at 19:40








  • 1





    The question seems to be about an error that code posted by OP has. An improvement plan: remove the code from the answer, explain what the error is, how to fix it, and how to avoid it in the future. Do not present an ideal from-the-book answer to the exercise. It is rather unlikely to be helpful to someone who is struggling with basic notions of the language.

    – n.m.
    Nov 19 '18 at 19:53





















  • This doesn't answer the question in any way, shape, or form.

    – n.m.
    Nov 19 '18 at 19:35











  • @n.m. If that is your opinion downvote away. Plan-B: make suggestions on how to improve it. The code in the question is fubar.

    – Swordfish
    Nov 19 '18 at 19:40








  • 1





    The question seems to be about an error that code posted by OP has. An improvement plan: remove the code from the answer, explain what the error is, how to fix it, and how to avoid it in the future. Do not present an ideal from-the-book answer to the exercise. It is rather unlikely to be helpful to someone who is struggling with basic notions of the language.

    – n.m.
    Nov 19 '18 at 19:53



















This doesn't answer the question in any way, shape, or form.

– n.m.
Nov 19 '18 at 19:35





This doesn't answer the question in any way, shape, or form.

– n.m.
Nov 19 '18 at 19:35













@n.m. If that is your opinion downvote away. Plan-B: make suggestions on how to improve it. The code in the question is fubar.

– Swordfish
Nov 19 '18 at 19:40







@n.m. If that is your opinion downvote away. Plan-B: make suggestions on how to improve it. The code in the question is fubar.

– Swordfish
Nov 19 '18 at 19:40






1




1





The question seems to be about an error that code posted by OP has. An improvement plan: remove the code from the answer, explain what the error is, how to fix it, and how to avoid it in the future. Do not present an ideal from-the-book answer to the exercise. It is rather unlikely to be helpful to someone who is struggling with basic notions of the language.

– n.m.
Nov 19 '18 at 19:53







The question seems to be about an error that code posted by OP has. An improvement plan: remove the code from the answer, explain what the error is, how to fix it, and how to avoid it in the future. Do not present an ideal from-the-book answer to the exercise. It is rather unlikely to be helpful to someone who is struggling with basic notions of the language.

– n.m.
Nov 19 '18 at 19:53




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380109%2flinked-list-c-programming-error-by-inserting-new-element%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?