# Insert in Linked List ...

Printable View

• 11-27-2001
yescha
Insert in Linked List ...
Hi;
Here is the function which insert the node according to dictionalry in the Circurlar list, but I couldn't find where is wrong. It is not working.
Anyone has any idea????

void insertnode( struct node *new )
{
struct node *cur, *prev;

while( cur != end )
{
if(strcmp( cur->name, new->name) < 0)
{
prev=cur;
cur = cur->next;
} else
break;
}
if(head == NULL)
{ head = new;
end = new;
end->next = head; }
else {

if(head == end)
{
if(head == end)
{
if(strcmp(cur->name,new->name)<0)
new->next=head;
head->next=new;
end = new;
} else { new->next = head;
head->next=new;
head=new;}

if ( cur == head) {
new->next = head;
head = new; }
else if (cur == end) {
end->next = new;
new->next = head;
}
else {
prev->next = new;
new->next = cur;
}
}
}
• 11-27-2001
quzah
Code:

```void insertnode( struct node *new ) { struct node *cur, *prev; while( cur != end )```
Here you have a problem. Two of them. You shouldn't use the word "new", because while it isn't a keyword in C, it is in C++, and if you ever go to convert this program, you'll get tons of errors. Not really a problem here, but just something to keep in minde.

Second, you use "cur" and "end" without ever giving them values.

Code:

```if(head == end) { if(head == end) { if(strcmp(cur->name,new->name)<0)```
You've just tested "head==end", so why do it twice?

Third: this is way to complex.

Given a circular list, do this:

List *myList;

Assuming "myList" is holding some point in a circular list, do this:
Code:

```insert( List *toInsert ) {     List *cur, *end;     cur = end = myList;     do {         if ( strcmp( cur->data, toInsert->data ) < 0  )         {             cur = cur->next;         }         else             break;     } while ( cur != end  );     /*insert after the end*/     if ( cur == end )     {         toInsert->next = myList;         end->next = toInsert;     }     /*insert after cur*/     else     {         toInsert->next = cur->next;         cur->next = toInsert;     } }```
Your code is WAY to complicated.

Quzah.
• 11-28-2001
goran
yescha, here is a modified version. i assumed that you wish to arrange your linked list in ascending order based on nodename.

typedef struct tag{
char name[50];
...
...
struct tag *next;
}node;

node *newnode()
{
node *np;
np = (node *)malloc(sizeof(node));
if(np)
{
// take whatever you want from input and
// populate node np;
np->next = NULL;
}
else
fprintf( stdout, "Error\n");
return np;
}

node *insertnode(node **head)
{
node *np;
node *curr = *head;
node *prev = NULL;

np=newnode();

if(np)
{
for(; strcmp(curr->name, np->name) < 0; curr=curr->next)
prev=curr;

np->next=curr;
if(prev)
prev->next=np;
else
*head=np;
}
else
fprintf( stdout, " allocation of new node failed.\n");
return np;
}

main()
{
node *head=NULL;
...
...
insertnode(&head);
...
...
}
• 11-28-2001
goran
yup, the indentation really sucks. but i just can't make it proper. no matter how many spaces you put for indentation, they get deleted while submitting the reply.

:(
• 11-28-2001
quzah
Use "code" tags. It'll preserve indentation. If you're familiar with HTML tags, they work the same, but use [ ] instead of < >. Simply do:

[ code ]
...Your code here...
[ /code ]

Without the extra spaces. ie: [likethis] [/likethis]

Quzah.