Sie sind auf Seite 1von 3

Q1.

For a singly linked list, write C code to define the data structure and implement three functions 1) insertAtEnd,
2) deleteAtStart and 3) deleteAtEnd. [5 +5+5+5 = 20M]
Assumption data is of type int; other answers are also acceptable. Skipping of typedef is fine for this question. But
then “struct node” or similar should be used instead of Node.

typedef struct node { void insertAtEnd(Node ** head_ref, int x) {


int data; Node *tmp=(Node *) malloc(sizeof(Node));
struct node * next; tmp->data = x;
} Node; tmp->next = NULL;
if(*head_ref == NULL) {
*head_ref = tmp;
} else {
void deleteAtEnd(Node ** head_ref) {
Node *lastNode = *head_ref;
if(*head_ref ==NULL) return; // empty list
while(lastNode->next != NULL)
if((*head_ref )->next == NULL) { // only 1 Node
lastNode = lastNode -> next;
Node *tmp= *head_ref;
lastNode->next = tmp;
*head_ref = NULL;
}
free (tmp);
}
} else {
Node *prevNode=*head_ref;
Node *lastNode=(*head_ref)-> next; void deleteAtStart(Node ** head_ref) {
while(lastNode->next != NULL) { if(*head_ref ==NULL) return; // empty list
prevNode = lastNode ; Node *tmp= *head_ref;
lastNode = lastNode -> next; *head_ref = (*head_ref )->next;
} free(tmp);
prevNode ->next = NULL; }
free(lastNode);
}
}

Q2
A. pBus->seats[i] = onWindow; 2M
B. pBus->seats[i] = onAisle; 2M
C. i++ 1M
D. empty 1M
E. pBus->seats[other].status == female && isMale 2M can be swapped with next or differ a bit
F. pBus->seats[other].status == male && !isMale 2M can be swapped with F or differ a bit
G. command[i] == 'M'? 2M
H. sP, pBus, isMale 2M
I. freeBlockedSeats(pBus); 2M
J. false; 1M
K. *refPtr 1M
L. 400: 350; 1M
M. (*refPtr)++ 1M
N. & bus 1M
O. &bus, str, & bookingRefCount 2M
P. &bus 1M

Solution for Part B 1/3


Q3. Define the data structure to store nodes in a linked list, with
the details as given in adjoining figure. Note that the final code

int sID

string

Node *
will use the data type in following manner: float

float
total

group

grade
char

name
char

next
Node * nPtr = malloc(sizeof (Node)); nPtr->name=malloc(64); marks in
5 subjects
nPtr->marks[4]=66.5; [6M]

typedef struct _node {


int sID;
float total;
char group;
char * name;
char grade;
struct _node * next;
float marks[5];
} Node;

Q4 A. Write C code for a structure that stores student-ID (max. of 15 characters) and marks in four subjects.
B. Write a function that takes an array of 20 such records and prints the ID and marks of students such that each
line contains details of a single student with ID padded to 16 characters and space separated marks with one digit
after decimal (except if marks are 100). Couple of examples below
2 0 1 7 A 6 P S 1 9 9 9 P 1 0 0 9 9 . 9 4 4 . 5 9 8 . 0 \n
2 0 1 8 B 5 A 6 T S 2 2 2 2 H 8 0 . 5 7 2 . 3 6 9 . 7 5 7 . 7 \n
C. Write a function that sorts the array of 20 such records in ascending order of student ID. [4 + 8 + 8=20M]

A C
typedef struct _studentRec { // typedef optional // can pass address of array as StudentRecord *
char sID[16]; // passing of length as 20 as second arg – optional
float marks[4]; // other sort approaches are also fine
} StudentRecord; // swap function can be defined
void sortRecords(StudentRecord recArray[20]) {
B for(int i=0; i < 20; i++) {
// can pass address of array as StudentRecord * for(int j =i+1; j < 20; j++) {
// optional length (20) as second argument if(strcmp(recArray[i].sID, recArray[j].sID) >0) {
void printRecords(StudentRecord recArray[20]) { StudentRecord temp = recArray[i];
for(int i=0; i < 20; i++) { recArray[i] = recArray[j];
printf("%-16s", recArray[i].sID); recArray[j] = temp;
for(int j =0; j < 4; j++) { }
if(recArray[i].marks[j] >99.999 && }
recArray[i].marks[j] <100.001) }
printf("100"); }
else
printf("%2.1f", recArray[i].marks[j]);
if(j<3) printf(" ");
}
printf("\n");
}
}

Solution for Part B 2/3


Q5. Write complete code to print an inverted pyramid. 5 3 4 #
The character to be used and the height of pyramid 333333333 #######
should be input to the program. For your reference, two 3333333 #####
such outputs are given in attached figures for inputs of 33333 ###
height=4/character=‘#’ and height=5/character=‘3’. 333 #
3

// minor variations of this answer are also fine


#include <stdio.h>
int main() {
int i, j, n; char ch;
scanf("%d %c", &n, & ch);
for(i=0; i < n; i++){
for(j=0; j < i; j++)
printf(" "); // initial blaks
for(j=0; j < 2*(n-i)-1; j++)
printf("%c", ch);
printf("\n");
}
return 0;
}

Solution for Part B 3/3

Das könnte Ihnen auch gefallen