26 votes 26 votes Let $p$ be a pointer as shown in the figure in a single linked list. What do the following assignment statements achieve? q:= p -> next p -> next:= q -> next q -> next:=(q -> next) -> next (p -> next) -> next:= q DS gate1998 data-structures linked-list normal descriptive + – Kathleen asked Sep 26, 2014 • edited May 8, 2021 by gatecse Kathleen 7.9k views answer comment Share Follow See 1 comment See all 1 1 comment reply Kiyoshi commented May 1, 2021 reply Follow Share just give adresses to each node like 1000, 2000, 3000 then trace it. then it becomes very easy. 1 votes 1 votes Please log in or register to add a comment.
Best answer 38 votes 38 votes Swaps the two nodes next to $p$ in the linked list. Arjun answered Aug 29, 2015 • edited Jan 5, 2018 by kenzou Arjun comment Share Follow See 1 comment See all 1 1 comment reply srestha commented Oct 25, 2017 reply Follow Share 1) 2) 3) 31 votes 31 votes Please log in or register to add a comment.
16 votes 16 votes Assuming p is "i" th node. It swaps "i+1" th & "i+2" th node. How to solve this question :-> Instead of using arrows, try writing address in pointer location , as computer would store, then solving linked list questions like this becomes easy.. Akash Kanase answered Nov 22, 2015 Akash Kanase comment Share Follow See all 0 reply Please log in or register to add a comment.
8 votes 8 votes The following piece of code swap 2nd and 3rd node from list. Abhishek Kumar 38 answered Dec 17, 2018 Abhishek Kumar 38 comment Share Follow See all 0 reply Please log in or register to add a comment.
7 votes 7 votes It is swapping the 2nd and 3rd element of a Linked List.(i.e. if we consider that p points to first node) #include <stdio.h> struct node{ int data; struct node *next; }; struct node *head = NULL; struct node *temp; void insert(int item){ struct node *p = (struct node*)malloc(sizeof(struct node)); p->data = item; if(head == NULL){ head = p; temp = p; } else{ temp->next = p; temp = p; } } void display(struct node *t){ struct node *r = t; while(r){ printf("%d",r->data); r = r->next; } } void modify(struct node *p){ struct node *q; q = p -> next; p -> next= q -> next; q -> next=(q -> next) -> next; (p -> next) -> next= q; } int main(void) { insert(1); insert(2); insert(3); insert(4); insert(5); insert(6); insert(7); insert(8); insert(9); modify(head); display(head); return 0; } If input is 123456789, then output will be 132456789 Aditya answered Sep 23, 2015 Aditya comment Share Follow See all 0 reply Please log in or register to add a comment.