You are on page 1of 11

Assignment 6

Q1. Create a database of students using structures, where in each entry of the
database will have the following fields:

1. a name, which is a string with at most 128 characters


2. their marks in physics which is an int between 0 and 100
3. their marks in chemistry which is an int number between 0 and 100
4. their marks in mathematics which is an int number between 0 and 100

You have to output a list of students in the following order.

1. if a student 'A' has lower marks in physics than a student 'B', then A's data is
listed before B.
2. If A and B have the same physics marks and A has lower chemistry marks than
B, then A is listed before B.
3. If A and B have the same marks in physics and chemistry, and A has lower
marks in mathematics than B, then A is listed before B.
4. If all marks are equal and A's name precedes B's name in the dictionary order,
then A is listed before B.

Input Format :
First line contains the number of students n, where 1<=n<=100.
In following n lines each line contains(space separated) a name and their respective
marks in physics, chemistry, maths, where 0<=marks<=100.

Output Format :
Sorted database of n lines.

Sol. #include<stdio.h>
#include<stdlib.h>
struct student{
char name[20];
int phy,che,math;
};
typedef struct student student;
void print(student *s,int n){
int i;
for(i=0;i<n;i++){
printf("%s\t",s[i].name);
printf("%d\t",s[i].phy);
printf("%d\t",s[i].che);
printf("%d\n",s[i].math);
}
}

int comparator(const void *p, const void *q)


{
float l = ((student *)p)->phy;
float r = ((student *)q)->phy;
if((l-r)!=0) return (l-r);
else{
l = ((student *)p)->che;
r = ((student *)q)->che;
if((l-r)!=0) return(l-r);
else{
l = ((student *)p)->math;
r = ((student *)q)->math;
return (l-r);
} }
}

int main(){
int i,n;
scanf("%d",&n);
student *student_info= (student *)malloc(sizeof(student)*n);
for(i=0;i<n;i++){
scanf("%s",student_info[i].name);
scanf("%d",&student_info[i].phy);
scanf("%d",&student_info[i].che);
scanf("%d",&student_info[i].math);
}
qsort((void *)student_info,n,sizeof(student),comparator);
print(student_info,n);
return 0;
}
Q.2 A graph is abstractly a collection of vertices which are labelled by
non-negative integers, and a collection of edges. A graph called an
undirected graph if we talk of merely the presence of an edge between
vertices i and j, rather than its direction.

For example, the following is a graph:

In this problem, you are given the edges in an undirected graph. An


edge is a pair of non-negative integers (i, j) which indicates that
the vetex i is connected to vetex j by an edge.

Afterwards, you will be given a vertex number n. You have to output


the list of vertices which are connected n by an edge, in the order in
which the edges were input.

Input

You are given the following.

1. The first line contains an integer, E, between 1 and 1000

2. This is followed by E lines, where each containing a pair of


numbers i and j where i and j are both non-negative integers <=
34,000. No edge will be listed more than once.

3. The last line contains a non-negative integer n <= 34,000. n is


assured to be a vertex listed in one of the E lines in part (2).

Output

You have to output the list of nodes to which n has an edge, in the
order in which the edges were input, one line for each vertex.
Sol #include <stdio.h>
#include <stdlib.h>
struct node{
int vertex;
struct node *next;
};
struct list_entry{
struct node *head;
struct node *tail;
};
struct list_entry list_entries[34000];
void init_list_entries()
{
int i;
for ( i=0 ; i<100 ; i++ ){
list_entries[i].head =
list_entries[i].tail =
NULL;
}
}
struct node * make_node ( int data )
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp -> vertex = data;
temp -> next = NULL;
return temp;
}
void insert_at_end(int a, int b)
{
struct node *node1;
struct node *node2;
node1 = make_node (a);
if(list_entries[b].head == NULL){
list_entries[b].head = node1;
list_entries[b].tail = node1;
}else{
list_entries[b].tail->next = node1;
list_entries[b].tail = node1;
}

node2 = make_node(b);
if(list_entries[a].head == NULL){
list_entries[a].head = node2;
list_entries[a].tail = node2;
}else{
list_entries[a].tail->next = node2;
list_entries[a].tail = node2;
}
return;
}
void print_adjacent_vertices_of(int n)
{
struct node *current = list_entries[n].head;

while(current != NULL){
printf("%d\n",current->vertex);
current = current->next;
}
return;
}
int main()
{
int num_edges;
int a;
int b;
int n;
int i=0;
scanf("%d", &num_edges);
for ( i=0; i<num_edges ; i++) {
scanf ( "%d", & a);
scanf ( "%d", & b);
insert_at_end(a,b);
}
scanf("%d",&n);
print_adjacent_vertices_of(n);
}
Q.3 You are given a sequence of integers terminated with a -1. The -1 is
not part of the input sequence.
Next, you are given a positive number N.

You have to create a linked list with the input sequence of integers
as entries. You can use the following structure.

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

Now, you have to delete all but the last N elements from the linked
list, and print the resulting list. (i.e. The resulting list will
consist of only the last N elements from the list.)

If N is longer than the length of the linked list, you must print -1.

While printing, the entries of the list must be separated by a single


space.

Sample Input
------------
3 4 5 6 -1
1

Sample Output
-------------
6
You are given a sequence of integers terminated with a -1. The -1 is
not part of the input sequence.

Next, you are given a positive number N.

You have to create a linked list with the input sequence of integers
as entries. You can use the following structure.

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

Now, you have to delete all but the last N elements from the linked
list, and print the resulting list. (i.e. The resulting list will
consist of only the last N elements from the list.)
If N is longer than the length of the linked list, you must print -1.

While printing, the entries of the list must be separated by a single


space.

Sample Input
------------
3 4 5 6 -1
1

Sample Output
-------------
6

Sol #include <stdio.h>


#include <stdlib.h>
struct node {
int data;
struct node *next;
};
int list_length;
struct node* create_node ( int n )
{
struct node *new_node;
new_node = (struct node *) malloc ( sizeof(struct node) );
new_node->data = n;
new_node->next = NULL;
return new_node;
}
struct node* create_list ( struct node *head )
{
int number;
struct node *current_node, *new_node;
scanf("%d",&number);
if(number != -1){
head = create_node ( number );
current_node = head;
list_length++;
scanf ( "%d", &number );
while ( number != -1 ){
new_node = create_node ( number );
current_node->next = new_node;
list_length++;
current_node = new_node; /* advance to next node */
scanf ( "%d", &number );
}
}
return head;
}
/* Delete the first num nodes from the list */
struct node *delete_first ( int num, struct node *head )
{
int i=1;
struct node *current_node = head;
struct node *next_node;
while ( i <= num ) {
next_node = current_node->next;
free(current_node);
current_node = next_node;
i++;
}
head = current_node;
return head;
}
void print_list ( struct node *head )
{
struct node * current_node;
if ( head == NULL ){
return;
}
current_node = head;
while ( current_node != NULL ) {
printf ( "%d ", current_node->data );
current_node = current_node->next;
}
printf("\n");
return;
}

int main()
{
int number;
int pruned_length=0;
struct node *head;
head = create_list ( head );
scanf("%d", &pruned_length);
if(pruned_length > list_length){
printf ( "-1\n" );
return 0;
}else{
head=delete_first(list_length-pruned_length, head);
print_list(head);
}
return 0;
}

You might also like