Open In App

GATE | GATE CS Mock 2018 | Set 2 | Question 42

Like Article
Like
Save
Share
Report

S1 : Anyone of the followings can be used to declare a node for a singly linked list. If we use the first declaration, “struct node * nodePtr;” would be used to declare pointer to a node. If we use the second declaration, “NODEPTR nodePtr;” can be used to declare pointer to a node.

/* First declaration */
struct node {
int data;
struct node * nextPtr;
};

/* Second declaration */
typedef struct node{
int data;
NODEPTR nextPtr;
} * NODEPTR;

S2 : Anyone of the following can be used to declare a node for a singly linked list and “NODEPTR nodePtr;” can be used to declare pointer to a node using any of the following

/* First declaration */
typedef struct node
{
 int data;
 struct node *nextPtr;
}* NODEPTR;

/* Second declaration */
struct node
{
 int data;
 struct node * nextPtr;
};
typedef struct node * NODEPTR;

(A)

Statement S1 is true and statement S2 is false

(B)

Statement S1 is false and statement S2 is true

(C)

Both statements S1 and S2 are true

(D)

Neither statement S1 nor statement S2 is true


Answer: (B)

Explanation:

Both S1 and S2 present different ways to declare a node for a singly linked list and a pointer to that node. In both cases, the node has two fields: an integer data field and a pointer to the next node.

S1 presents two different ways to declare a node and a pointer to that node. The first declaration uses a structure tag to define a node, and a pointer to that node can be declared as a struct node *. The second declaration uses a typedef to define a node, and a pointer to that node can be declared as NODEPTR. But, struct Node does not gets changed to NODEPTR before creating the structure, thus gives a compilation error ” unknown type name ‘NODEPTR’ “.

S2 also presents two different ways to declare a node and a pointer to that node. The first declaration uses a typedef to define a node, and a pointer to that node can be declared as NODEPTR. In this case, NODEPTR is a typedef for the pointer to the node. The second declaration uses a structure tag to define a node, and a pointer to that node can be declared as a struct node *.

Both sets of declarations are valid and can be used to declare a singly linked list and a pointer to that list. The choice between them depends on personal preference and coding style.


Quiz of this Question
Please comment below if you find anything wrong in the above post


Last Updated : 08 Mar, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads