3

After almost 3 years, I've started relearning C.

I've created a Linked list, and would like to extend this to creating a sorted linked list. Here is my code:

typedef struct node{
int data;
struct node *ptr;
}node;

node* insert(node* head, int num){
node *temp,*prev,*next;
temp = (node*)malloc(sizeof(node));
temp->data = num;
temp->ptr = '\0';
if(head=='\0'){
    head=temp;
}else{
    next = head;
    prev = next;
    while(next->data<=num){
        prev = next;
        next = next->ptr;
    }
    if(next==NULL){
        prev->ptr = temp;
    }else{
        temp->ptr = prev->ptr;
        prev-> ptr = temp;
    }

}
return head;
}

void main(){
int num;
node *head, *p;
head = '\0';
do{
    printf("Enter a number");
    scanf("%d",&num);
    if(num!=0)
        head = insert(head,num);
}while(num!=0);
p = head;
printf("\nThe numbers are:\n");
while(p!='\0'){
    printf("%d ",p->data);
    p = p->ptr;
}
}

This is my idea. I traverse the list till I find a number >= the input. I store the previous node in prev and next node contains the current value. If next is null, then the list is ended and the number is the highest among the list so it is to be inserted in the last place, if the number is some where in the middle then, prev node's address part is stored in temp nodes address part now temp node pointer holds address of next node.

Edit: Problem with my code is if I enter 1,2 I get error message as a.exe has stopped working. I am using MinGW for compiling. I am breaking the loop when user inputs 0.

NewUser
  • 3,729
  • 10
  • 57
  • 79
  • 2
    `'\0'` is not same as NULL. http://stackoverflow.com/questions/1296843/what-is-the-difference-between-null-0-and-0 – mohit Apr 16 '13 at 17:58
  • @mohit, `'\0'` will work exactly the same as `NULL`. It's not really semantically meaningful, but it should be fine. – Carl Norum Apr 16 '13 at 17:59

1 Answers1

7

You have to change the line

while(next->data<=num)

to

while(next!='\0' && next->data<=num)

When you insert the second element next will be '\0' at the second iteration and trying to get the field data with next->data will lead to a segmentation fault.

With the changed condition in the while if next!='\0' will be false (hence next=='\0') the while is aborted and because of &&'s shortcircuiting next->data is not computed.


EDIT

You have more problems in your code.

If you look at the input 2 1 0 then the output with a correctly working program should be 1 2 but it's 2 1 instead. Teh problem is that in your insert function you do not consider the case where you insert the current smallest element that will be the new head.

Another problem is that you do not free the malloced memory at the end, which leads to memory leaks.

I changed your code to behave correctly:

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

typedef struct node{
    int data;
    struct node *ptr;
} node;

node* insert(node* head, int num) {
    node *temp, *prev, *next;
    temp = (node*)malloc(sizeof(node));
    temp->data = num;
    temp->ptr = NULL;
    if(!head){
        head=temp;
    } else{
        prev = NULL;
        next = head;
        while(next && next->data<=num){
            prev = next;
            next = next->ptr;
        }
        if(!next){
            prev->ptr = temp;
        } else{
            if(prev) {
                temp->ptr = prev->ptr;
                prev-> ptr = temp;
            } else {
                temp->ptr = head;
                head = temp;
            }            
        }   
    }
    return head;
}

void free_list(node *head) {
    node *prev = head;
    node *cur = head;
    while(cur) {
        prev = cur;
        cur = prev->ptr;
        free(prev);
    }       
}

int main(){
    int num;
    node *head, *p;
    head = NULL;
    do {
        printf("Enter a number");
        scanf("%d",&num);
        if(num) {
            head = insert(head, num);
        }
    } while(num);
    p = head;
    printf("\nThe numbers are:\n");
    while(p) {
        printf("%d ", p->data);
        p = p->ptr;
    }
    free_list(head);
    return 0;
}

See https://ideone.com/wT5iQ8 for my code on a testinput together with the correct output.

halex
  • 16,253
  • 5
  • 58
  • 67
  • 1
    Why compare a pointer to a character? Anyway, just `while (next && next->data <= num)` would suffice. – Fred Larson Apr 16 '13 at 18:38
  • 1
    @FredLarson You are right that OP should use `NULL` instead of `'\0'` but I wanted to change as little as possible in his code :). And it works with `'\0'` as well. – halex Apr 16 '13 at 19:01
  • @Dibya I edited my question and added my working code. I hope there are no more bugs because I quickly tested it. If you have questions don't hesitate to ask them :) – halex Apr 17 '13 at 13:11
  • @halex: I got it when first number is greater than second one it was neglected. Thank you very much.. – NewUser Apr 17 '13 at 14:39