insertion function
I have a homework problem where I have to:Write an insertion function that inserts an element at the first position in the list following an element storing a particular DATA item. I also have to use this function. I don't understand what the problem is asking. Can someone explain this problem to me in terms that are easier to understand?
#include "list.h"
LINK lookup(DATA c, LINK head)
{
if (head == NULL)
return N开发者_如何学GoULL;
else if ( c == head -> d)
return head;
else
return (lookup(c, head -> next));
}
The question is asking you to write a function that finds a specified item in the list, and then inserts the new item in the list after this position.
So given the list
List = {Item1, Item2, Item3, Item4}
Your function List.Insert(Item2, Item5)
(for example) should result in
List = {Item1, Item2, Item5, Item3, Item4}
You have a linked list. The list is composed of LINK
structs (to be more precise, it appears the LINK
type is typedef'd to be a pointer to the actual node structs. We don't have the definition in front of us, but presumably each LINK
struct holds a DATA
struct as well as the usual linking pointer(s).
Your task is to take the head-of-the-list LINK
and a DATA
struct and search through the list until you find the first LINK
that has a DATA
identical to the one you're given. Then, insert the new element into the list just before that LINK
.
You are asked to find DATA c
in the linked list (the function lookup( )
will do that for you), then insert a DATA
item between c
and the DATA
item following c
. If c
is the last DATA
item in the list, then you are to add your new DATA
item as the final DATA
item. If the list is empty, i.e. if lookup( )
returns NULL
, then your new DATA
item should be stored as the one and only DATA
item in the list.
Why not post the relevant part of list.h
so we can understand what the linked list looks like?
It would also be a good idea to post the entire lookup( )
function.
精彩评论