Programming and Data Structures — GATE Previous Year Questions
12Questions
21Total marks
36Minutes
Every verified previous-year GATE question in the Programming and Data Structures section of the W3Colleges bank, in chronological order. Practise them untimed with worked explanations, or take the set as a timed test.
The following C function takes a singly linked list of integers as a parameter and rearranges the elements of the list. The list is created with the integers 1, 2, 3, 4, 5, 6, 7 in that order, and rearrange is called with a pointer to the first node.
struct node {
int value;
struct node *next;
};
void rearrange(struct node *list) {
struct node *p, *q;
int temp;
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 will be the contents of the list after the function completes execution?
The following C function takes a singly linked list as an input argument. It modifies the list by moving the last element to the front of the list and returns the modified list. Some part of the code is left blank.
typedef struct node {
int value;
struct node *next;
} Node;
Node *move_to_front(Node *head) {
Node *p, *q;
if ((head == NULL) || (head->next == NULL))
return head;
q = NULL;
p = head;
while (p->next != NULL) {
q = p;
p = p->next;
}
_______________________________
return head;
}
Choose the correct alternative to replace the blank line.
#include <stdio.h>
int jumble(int x, int y) {
x = 2 * x + y;
return x;
}
int main() {
int x = 2, y = 5;
y = jumble(y, x);
x = jumble(y, x);
printf("%d\n", x);
return 0;
}