And how long did you spend on this?
Draw it on paper! Use multiple colors to work out insertions and removals.
Your amount of code is inadequate. Maybe spend more time listening in the classroom and taking notes? Work on a team with fellow classmates?
What I'm providing here is really TOO much information for the microscopic amount of work you did!
Unswizzle your structure, especially since you are typedefingi it
typedef struct list
{
int data;
list *next;
} list; list *head,*temp,*node;
Pre-initialize!
Head = NULL;If insertion of a node at front of list
Allocate the node
if Node != NULL then allocation successful!
Node->next = Head <-- What was head pointing at?
Head = Node <-- Head now points to new node
Repeat for each prepended node!
If all new nodes are to be appeneded
Node *pPre;
if (NULL == Head) // First in list
do it like above
else
pTmp = Head
while ( NULL !=pTmp->next) Loop until there is no next
pTmp = pTmp->next
pTmp is NOW last node
pNode->next = NULL
pTmp->next = pNode For node removal, since you are using a single-linked list you need to have your node to be removed to be the one after the node you're referencing.
if node First in list
head = head->next->next;
else <-- ptmp is before the node to remove
pTmp->next = pTmp->next->next;