i just started learning pointers in c and i am doing exercise questions in programming in c by stephen G. Kochan. one of the question is
- Write a function called insertEntry to insert a new entry into a linked list. Have the procedure take as arguments a pointer to the list entry to be inserted (of type struct entry as defined in this chapter), and a pointer to an element in the list after which the new entry is to be inserted.
i have done this question as it's very straightforward. below is the source code for it
#include<stdio.h>
struct entry
{
int value;
struct entry *next;
};
void insertEntry(struct entry *newPtr,struct entry *afterPtr);
int main(void)
{
struct entry n1,n2,n3;
struct entry *startPtr=&n1;
n1.value=100;
n1.next=&n2;
n2.value=200;
n2.next=&n3;
n3.value=300;
n3.next=(struct entry *)0;
struct entry n2_3;
n2_3.value=250;
insertEntry(&n2_3,&n2);
struct entry *listPtr;
listPtr=startPtr;
while(listPtr!=(struct entry *)0)
{
printf("%i ",listPtr->value);
listPtr=listPtr->next;
}
printf("\n");
return 0;
}
void insertEntry(struct entry *newPtr,struct entry *afterPtr)
{
newPtr->next=afterPtr->next;
afterPtr->next=newPtr;
}
the other question that i am stuck with is
- The function developed in above exercise only inserts an element after an existing element in the list, thereby preventing you from inserting a new entry at the front of the list. How can you use this same function and yet overcome this problem? (Hint: Think about setting up a special structure to point to the beginning of the list.)
i couldn't think of anyway to do the second question. any helpful hints or ideas for moving forward would be really helpful. also, i know this is not a tutoring website, but i am trying to learning these things by myself and no one to contact, i'd really appreciate it if someone helps me move forward.