Code for deleteAtPosition and deleteAtTail function that was given as homework

that was given as homework

@adityakakkar hi aditya can u plss tell me what you want from me code??

i am getting segmentation fault while creating a funtion to delete node at any position of linked list and also the last node of linked list

@adityakakkar oohke so i give u a correct code it will be okay ??

void deleteAtTail(node *&head) { if (head == NULL) { return; } else { int k = 0; node *tailb = head; while (k <= length(head) - 1) { tailb = tailb->next; tailb++; } node *tail = head; while (tail->next != NULL) { tail = tail->next; } delete tail; tailb->next = NULL; } } void deletePosition(node *&head, int p) { if (head == NULL) { return; } else if (p == 0) { deleteHead(head); } else { int jump = 1; node *temp = head; while (jump <= p - 1) { temp = temp->next; jump++; } int k = 0; node *cur = head; while (k <= p) { cur = cur->next; cur++; } temp->next = cur->next; delete cur; } } //this is what i coded

yes that will be fine

@adityakakkar check this
https://ide.codingblocks.com/s/653809 delete at pos thoda sa galat tha toh meine theek krdiya and delete at tail ka easy code krdiya

i have implemented your solution and it is working, i understood it also but i have thought of a little twick which is also working.You are declaring a new node as tobedeleted. But i am confused that i am thinking in the right direction or not. My code is void deleteAtTail(node *&head) { if (head == NULL) { return; } else if (head->next == NULL) { deleteHead(head); return; } else { node *temp = head; while (temp->next->next != NULL) { temp = temp->next; } temp->next = NULL; temp = temp->next; delete temp; } }

is my way correct or it can give me error in any case

@adityakakkar your code is also right but one mistake that after traversing at the end if u direct do temp->next==NULL then u cant able to go further bcz link breaks here so u have to first store that node in any node then go for it
like Node *tobedel=temp->next;
then temp->next=NULL then delete that storing node

i hope you understood @adityakakkar please give feedback

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.