1,298 views
1 1 vote

The following C function takes a single-linked list of integers as a parameter and rearranges the elements of the list. The function is called with the list containing the integers 1, 2, 3, 4, 5, 6, 7 in the given order. What will be the contents of the list after function completes execution?

struct node {
    int value;
    struct node *next;
};

void rearrange(struct node *list) {
    struct node *p, *q;
    int temp;
   1: if (!list || !list -> next) return;
    p = list; q = list -> next;
    while(q) {
        temp = p -> value; p->value =  q -> value;
        q->value = temp; p = q ->next;
        q = p? p ->next : 0;
    }   
}

What is meant by line 1?

1 Answer

3 3 votes
@hem chandraJoshi It means that if  the list is empty or the list has only 1 element then return.

!list is another way of saying list==NULL, both will return 1 if list is NULL. (negation of NULL is non-NULL, which in C is described as some non-zero value, and in most compilers is taken as 1, so !NULL is 1.)

!list->next is another way of saying !(list->next), as -> has higher priority. This condition checks that the next pointer of first node is NULL.
Position:
Show:

Related questions

0 0 votes
0 0 answers
959
959 views
Lakshman Bhaiya asked Oct 26, 2018
959 views
Consider the following function:Find(Element Type X,List L) { Position Prev_Pos,XPos; Prev_Pos=Find Previous(X,L); if(Prev_Pos ->Next!=NULL) /* found */ ...
0 0 votes
1 1 answer
2.5k
2.5k views
gagan55 asked Jul 13, 2023
2,452 views
How to Visualize this code ?#include<stdio.h #include<stdlib.h struct node{ int data; struct node *next; }; void addFirst(struct node head,int val){ struct node *newNode...
0 0 votes
2 2 answers
1.2k
1.2k views
srestha asked Apr 29, 2019
1,190 views
Can somebody write the code or algorithm, how merge sort works efficiently in linked list? Is Heap sort most inefficient in Linked List Sorting? Elaborate plz
0 0 votes
1 answers 1 answer
4.4k
4.4k views
Arnab Bhadra asked Jun 28, 2017
4,398 views
Insertion of a node into a doubly linked list requires how many changes to various Next and Previous Pointer?A. No ChangeB. 1 Next , 1 PreviousC. 2 Next , 2 PreviousD. 3 ...