2

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.

yash
  • 1,357
  • 2
  • 23
  • 34
  • 1
    The question calls for an answer in terms of *the same function*, so it's about how you might **use** that function. The function requires an argument pointing to a `struct entry` that logically precedes the one to insert. If you want to be able to use that to insert at the head of the list, then that puts a pretty specific requirement on your overall data structure. – John Bollinger Apr 21 '16 at 13:22
  • i think i got it. i made a few changes to it 1. i made `n3.next=&n1`. pointed the last element to the first element again 2. `if(newnode.next==&n1) { do { printf("%i ",listPtr->value); listPtr=listPtr->next; }while(listPtr!=&newnode); } else { listPtr=&n1; do { printf("%i ",listPtr->value); listPtr=listPtr->next; }while(listPtr!=&n1); }` it works too – yash Apr 21 '16 at 13:35
  • If your original function worked correctly then there was no need to change it, and in fact changing it defeats the apparent purpose of the second question. – John Bollinger Apr 21 '16 at 14:09
  • no i haven't changed the function at all, i just applied a few condition statements while traversing the linkedlist – yash Apr 21 '16 at 14:10
  • I see. You changed your plain linked list into a circular one. I don't think that's the approach Kochan expected, but it is a viable way to solve the problem when you are free to use it. But consider also this: does every `struct entry` in the program have to represent real data? – John Bollinger Apr 21 '16 at 14:19

1 Answers1

1

You could define a type of linked list which have a reference to the header of the list:

struct list {
    struct entry *head;
};

void insert_head(struct list *list, struct entry *node) {
    node->next = list->head;
    list->head = node;
}
fluter
  • 13,238
  • 8
  • 62
  • 100
  • but in the question. it says that **use the same function** ? – yash Apr 21 '16 at 13:21
  • 1
    @buggenerator I know, I'm not give exact answer to the exercise, I just point out a way to approach it. He can adapt the thought to change the original function. – fluter Apr 21 '16 at 13:24