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
1)
2)
3)
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
Gatecse