12

Before I begin I want to make it clear that I don't want the answer to my HOMEWORK problem, I would just like if someone could actually explain what exactly my instructor is asking for in this assignment (preferably a dumbed down version) and maybe a helpful push in the right direction. I'm having a lot of trouble with this topic and whenever I ask the instructor I find that he confuses me more than anything else.

So, here is the assignment:

1.Add a new function insertN(struct list *x, int num, int pos, int n) that will insert n copies of the integer num at position pos, if that is possible (if pos is too big, take appropriate action). The main thing I'm confused by here is what he means by the position pos.

Here's the code I am working with-which was written by my teacher and I have to modify it.

#include<stdio.h>
#include<stdlib.h>

struct list {
    int data;
    struct list * next;
        };

struct list *slist;

/*adds a node at the end of the linked list*/
void insert(struct list *x,int num){
  /*if the list is empty*/
  if(x==NULL){
    /*create first node*/
    slist=malloc(sizeof(struct list));
    slist->data=num; 
    slist->next=NULL;
    }
  else{
    /*go to the last node*/
    while(x->next!=NULL) x=x->next;
    /*add node at the end*/
      x->next=malloc(sizeof(struct list));
      x->next->data=num;
      x->next->next=NULL;

  }
}


void display(struct list *x){
  /*traverse the entire linked list*/
  while(x!=NULL){
    printf("%d->",x->data);
    x=x->next;
  }
  printf("NULL");
}

void reverse(struct list *x){
  struct list *prev,*rev,*temp;

  prev=x;
  rev=NULL;

  while(prev!=NULL){
    temp=rev;
    rev=prev;
    prev=prev->next;
    rev->next=temp;
  }
  slist=rev;
}

void search(struct list *x,int a){
struct list *runner;
int found=0;
  for(runner=x;runner!=NULL;runner=runner->next){
  if(runner->data==a){
    printf("data found"); 
    found=1;
break;
  }
  }
if(found==0) printf("data not found");

}

main(){
  int number,a;

  slist=NULL;/*empty linked list*/

  printf("Enter the element for data part:");
  scanf("%d",&number);
  insert(slist,10);
  insert(slist,number);

  insert(slist,20);

  display(slist);
  printf("\n");

  reverse(slist);

  display(slist);
  printf("\nEnter the element for searching:");
  scanf("%d",&a);
  search(slist,a);
  printf("\n");
  getchar();
  getchar();
}

Again, I don't expect an answer to the problem, just an explanation and a push in the right direction.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
none
  • 263
  • 6
  • 13
  • 4
    If your list contains `1, 5, 3, 2`, position 0 contains the 1, position 1 contains the 5 and so on. Insert 3 0's at position 2 would give a list `1, 5, 0, 0, 0, 3, 2` – Erik Mar 09 '11 at 12:45
  • 3
    Also, +1 for wanting to do your own homework. – Erik Mar 09 '11 at 12:50

5 Answers5

3

By saying "at position 5" he means that he wants you to iterate through ("go through") the list 5-steps, then insert there.

If you have a reference to a list like so:

struct list * current;

A single step can be done like this:

current = current -> next;

Now what you have to do is do that until you are at the right position, then insert there.

Bernd Elkemann
  • 23,242
  • 4
  • 37
  • 66
  • if i used a for loop and counter to iterate to position pos would that work? – none Mar 09 '11 at 20:41
  • absolutely. as a "trick" you could also reuse that counter to count the number of obj's to insert. – Bernd Elkemann Mar 09 '11 at 20:46
  • so far i have this: void insertN(struct list *x, int num, int pos, int n){ while(x->next!=NULL) x=x->next; for(x=0; xnext=malloc(sizeof(struct list)); x->next->data=num; x->next->next=NULL;} } but its giving me error messages saying "list* differs in levels of indirection from int" – none Mar 09 '11 at 21:05
  • 1
    the first parameter should be `struct list*` maybe the star got lost because you posted code in the comment, but maybe that already solves the error. – Bernd Elkemann Mar 09 '11 at 21:32
  • 1
    also be careful when dereferencing multiple levels `->` is evaluated right to left for some stupid reason so you will have to do `(x->next)->data` if you want to do something like that. – Bernd Elkemann Mar 09 '11 at 21:33
3

So let's say slist has 3 elements in it like this:

+---+    +----+    +---+
| 7 | -> | 48 | -> | 9 | -> NULL
+---+    +----+    +---+

Which you created by doing something like:

slist=NULL;
insert(slist, 7);
insert(slist, 48);
insert(slist, 9);

Your job is to implement a function called insertN(struct list *x, int num, int pos, int n) which inserts one or more repeated elements into the list at the given position.

When I use that function I might say:

insertN(slist, 69, 1, 2);

which means "insert 2 elements containing 69 into slist at position 1".
So after the call slist should look like this:

+---+    +----+    +----+    +----+    +---+
| 7 | -> | 69 | -> | 69 | -> | 48 | -> | 9 | -> NULL
+---+    +----+    +----+    +----+    +---+
GrahamS
  • 9,980
  • 9
  • 49
  • 63
  • So they would be inserted to the list AFTER the postion 1 which contains 48)? I think I understand this now thank you! – none Mar 09 '11 at 14:27
  • @Laura Boyle: oops sorry.. my mistake. I've corrected the diagram to show that the inserted items starting at position 1. – GrahamS Mar 09 '11 at 15:11
2

A position i can be found by following i next pointers. So 0 is the start of the list, since you get there without following a next pointer. If the list contains n elements, then position n refers to the end.

You'll have to modify the loop following the go to the last node comment. You'll also have to handle a few corner cases, 0 being one of them. (Hint: it's very similar to the case where the list is empty.)

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
1

Guys already an answer has been accepted but what must be done when pos is large.As it would be computationally very expensive. I think it would be preferably better to keep two pointers one to the begining of the list and one at the end.

But since it is singly list we can't travers it backwards.So guys what we must do for the case when pos is large Which is an important aspect of the problem which eveyone has missed.

  • 1
    I think that the statement *"if pos is too big, take appropriate action"* in the question means the code should handle the case where `pos` is larger than the actual list (e.g. trying to insert at position 10 when the list only has 2 elements in it). I don't think they are expecting optimal code for dealing with huge lists at this level of homework (besides traversing the whole list wouldn't really be *"computationally very expensive"*, just a bit sub-optimal). – GrahamS Mar 09 '11 at 15:16
  • You have to keep a record of how long the linked list is to know whether `pos` is at or beyond the end of the list. – Jonathan Leffler Nov 09 '14 at 23:20
0

some thing like this (this insert only one node,extend it to insert n items):

 function insertN(struct list *x, int num, int pos, int n){
    int k=0;
    struct list *nd=x,*tmp;
    while(nd){
        if(k==pos){           // position reached ?
            tmp=nd->next;
            nd->next= new_node(num); 
            nd->next->next=tmp;
            return 1;         // succedd node inserted
        }
        nd=nd->next;          //next node
        k++;                 // next position
    }
    // nd == null means pos is too large
    return 0; // failed to insert node at position pos;

}
milevyo
  • 2,165
  • 1
  • 13
  • 18