none
none

Reputation: 373

More linked lists in C

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.

Upvotes: 12

Views: 603

Answers (5)

milevyo
milevyo

Reputation: 2184

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;

}

Upvotes: 0

GrahamS
GrahamS

Reputation: 10350

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
+---+    +----+    +----+    +----+    +---+

Upvotes: 3

user650876
user650876

Reputation:

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.

Upvotes: 1

Fred Foo
Fred Foo

Reputation: 363707

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.)

Upvotes: 2

Bernd Elkemann
Bernd Elkemann

Reputation: 23550

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.

Upvotes: 3

Related Questions