1

I am doing the insertion at the beginning but my code inserts it in the end. I am thinking about that but I think my logic is right. I think that first I make a pointer p and initialized it as head then increment it till it is not equal to head and then make a new link. Is it a right approach?

Here it is,

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

typedef struct node{

    int data;
    struct  node *next;
        
}sll;

void traversal(sll* head){

    sll* p;
    p=head;
       
    do
    {
        printf("%d\n",p->data);
        p=p->next;
    }while (p!=head);
    
}

sll* insertionstart(sll* head,int data){

        sll* ptr=(sll*)malloc(sizeof(sll));
        ptr->data=data;
       
        sll* p=head;
         do
        {
           p= p->next;
    }   while (p->next!=head);
        
         p->next=ptr;
        ptr->next=head;
        
        return head;
        
        
}

int main(){
    sll* ptr;
    sll* head;
    sll* second;
    sll* third;
    sll* fourth;
    sll* fifth;

    

    head = (sll*) malloc(sizeof(sll));
    second = (sll*) malloc(sizeof(sll));
    third = (sll*) malloc(sizeof(sll));
    fourth = (sll*) malloc(sizeof(sll));
    fifth = (sll*) malloc(sizeof(sll));


    head -> data=56;
    head ->next=second;

    
    second -> data=3;
    second->next=third;

    
    third -> data=18;
    third ->next=fourth;

    
    fourth -> data=90;
    fourth ->next=fifth;

    fifth -> data=76;
    fifth ->next=head;

    traversal(head);
    printf("*******************\n");
    head=insertionstart(head,42);
    traversal(head);

    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • sir bez i think in circular ll first p goes to the last element p->next =ptr so that i am incrementing it. – The Gentelmen 24 Oct 03 '20 at 06:03
  • To insert at the beginning you must have some code that changes the `head` pointer to point to the new node. At the moment your function always just returns the old `head`. – kaylum Oct 03 '20 at 06:05
  • sir here the p was incremented and the p=head so that the head was also incremented – The Gentelmen 24 Oct 03 '20 at 06:11
  • `p` is **a copy** of `head`. Changing `p` has no effect on `head`. If you don't believe that simply run your program in a debugger and inspect the pointer values as the program runs. Also, if it did change `head` then `p->next!=head` would make no sense as `head` would be constantly changing. – kaylum Oct 03 '20 at 06:16
  • yes sir you are right now if i remove the return head it is print at the beginning. – The Gentelmen 24 Oct 03 '20 at 06:18
  • Just a note about circular lists, if you need to preserve the order (rather than chaining the first node do the list ends up reversed), it's almost easier (and much more efficient for insertions) to use a doubly-linked circular list. The you have O(1) insertions at the end using the `head->prev` pointer. – David C. Rankin Oct 03 '20 at 06:32

1 Answers1

0

If you want the new element to be the new head, then simply make this change in insertionstart:

return head;  -->  return ptr;
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63