edited by
7,722 views
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
edited by

5 Answers

Best answer
38 votes
38 votes
Swaps the two nodes next to $p$ in the linked list.
edited by
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..
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

Related questions

29 votes
29 votes
11 answers
1
Kathleen asked Sep 25, 2014
14,545 views
A complete $n$-ary tree is one in which every node has $0$ or $n$ sons. If $x$ is the number of internal nodes of a complete $n$-ary tree, the number of leaves in it is g...
21 votes
21 votes
1 answer
3
Kathleen asked Sep 26, 2014
4,132 views
Derive a recurrence relation for the size of the smallest AVL tree with height $h$.What is the size of the smallest AVL tree with height $8$?
20 votes
20 votes
1 answer
4
Kathleen asked Sep 26, 2014
4,631 views
Draw the binary tree with node labels $\text{a, b, c, d, e, f and g}$ for which the inorder and postorder traversals result in the following sequences:Inorder: $\text{a f...