clanguage07@gmail.com

clanguage07@gmail.com

Saturday, June 16, 2012

Write a function for inserting an item into a linked list .

program:
node *insert(node *head)
{
node *find(node *p, int a);
node *new; /* pointer to new node */
node *n1; /* pointer to node preceding key node */
int key;
int x; /* new item (number) to be inserted */
printf("Value of new item?");
scanf("%d", &x);
printf("Value of key item ? (type -999 if last) ");
scanf("%d", &key);
if(head->number == key) /* new node is first */
{
new = (node *)malloc(size of(node));
new->
number = x;
new->
next = head;
head = new;
}
else /* find key node and insert new node */
{
/* before the key node */
n1 = find(head, key); /* find key node */
if(n1 == NULL)
printf("\n key is not found \n");
else /* insert new node */
{
new = (node *)malloc(sizeof(node));
new->
number = x;
new->
next = n1->
next;
n1->
next = new;
}
}
return(head);
}
node *find(node *lists, int key)
{
if(list->next->number == key) /* key found */
return(list);
else if(list->next->next == NULL) /* end */
return(NULL);
else
find(list->next, key);
}

No comments:

Post a Comment

GET MORE INFORMATION

http://ads.qadservice.com/t?id=c2168e05-8974-4816-872a-91936ff7379d&size=1024x768&drct=true