Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
void insert(node *ptr,int data,int key)
{
    node *prev;
    ptr=ptr->next;
    while((ptr->key)<key)
    {
        prev=ptr;
        ptr=ptr->next;
    }
    node *nw;
    nw=(node*)malloc(sizeof(node));
    nw->info=data;
    nw->key=key;
    nw->next=ptr;
    prev->next=nw;
}
Posted
Updated 17-May-13 20:00pm
v2

1 solution

Since you say you are writing a 'c-style' program, I'll presume that 'node' is some kind of pre-defined structure, elsewhere declared, above the 'insert' function you have provided, or is added in an include file, in the first several lines of code, in the document that holds this function.

The lower case 'insert' is generic enough to be flagged as an issue, as a pre-declared-machine-function. The '*ptr' is another issue all together, as the '*' is code for 'pointer to' 'ptr', which is an abbreviation for the word 'pointer'.

Depending on how the English of the Code is Compiled by the machine Co-Processor, results may vary. The machine is very strict.

You Wrote:
C#
void insert(node *ptr,int data,int key)
{
    node *prev;
    ptr=ptr->next;
    while((ptr->key)<key)
    {
        prev=ptr;
        ptr=ptr->next;
    }
    node *nw;
    nw=(node*)malloc(sizeof(node));
    nw->info=data;
    nw->key=key;
    nw->next=ptr;
    prev->next=nw;
}


When inserting new data to replace old data in a more 'main' function body, and modifying its variables and structure containers, your input function members: node, data, key, prev, next; may be already 'key' words within the compiler and 'C' language, are also what you bring into the function body, the information for each of these members, must fully comply both in type and structure, within the code of the called function above and elsewhere within the code in the main code calling this particular insert function. Syntax structure compliance is mandatory.

The above code is commented below:

C#
void insert(node* ptr, int data, int key)
{
    node* ptr_node = main_current_node;
///I don't like bad internally declared function variable naming and are you inserting a previous or a next node?///

    main_current_node = main_next_node;  ///maybe a main_next, main_prev are needed as well///

//How is 'next' declared as a structure within the main 'node' struct, you can't make your incoming data structure 'node' equal to one of its structure members///
 
    while((main_current_node->key) < key)

///I don't like bad variable naming and spaces are good///

    {
   
        ptr_node = main_current_node;/// This is OK, as both are same type.

        main_current_node = main_next_node; ///This will work.
    }

///AT this point main_node_key is GREATER-THAN key(incoming with function call) and past the while loop. and prev is not initialized.///

    node* nw;/// Declared Internal to this function is OK, and we have prev already so why a second one, if you were going to do a return instead of a void////

    nw = (node*)malloc(sizeof(node));

/// This is OK, but why are you allocating memory to a pointer?  Better would be:///
    prev = new node(); /// OR prev = (node*)malloc(sizeof(node));///
    
    nw->info = data;/// Is 'info' typed as an 'int'?

    nw->key = key;/// This is OK if there is a 'key' member within the 'node' structure

    nw->next = ptr->next;/// This is better.  A node within a node is an endless loop.

    prev->next = nw;///This is not OK. it would work an be better as

    main_next_node = nw;/// where main_node is a pointer variable to the 'node' structure


}



This a first rough try at a repair. Any way the code you wrote is a bit ambiguous.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900