0% found this document useful (0 votes)
117 views

Assignment 7

The document describes an assignment involving creating and manipulating linked lists of student data. It involves: 1) Creating a structure to store student name, physics, chemistry, and math marks. 2) Populating a linked list with student data and sorting by marks. 3) Creating another linked list from input integers and deleting all but the last N elements.

Uploaded by

Muskan Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views

Assignment 7

The document describes an assignment involving creating and manipulating linked lists of student data. It involves: 1) Creating a structure to store student name, physics, chemistry, and math marks. 2) Populating a linked list with student data and sorting by marks. 3) Creating another linked list from input integers and deleting all but the last N elements.

Uploaded by

Muskan Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment 7

Structures and Linked Lists


Question 1
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.

Solution:
#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;
}
Question 2
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

Solution:
#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