top of page
Click here to go to the home page of AskTheCode.

Take marks as input in Linked List & Display the highest & smallest marks

Team ATC

Linked List | Data Structure Post

 

#include<stdio.h>

#include<stdlib.h>


struct stud

{

int marks;

struct stud *next;

};


typedef struct stud Node;


void insertAtTheBegin(Node **start_ref, int data);


void bubbleSort(Node *start);

void swap(Node *a,Node *b);

void printList(Node *start);


int main()

{

char ans='y';

void disp( Node *ptr);

Node *tmp;

Node *n1;


Node *start=NULL;

do

{

n1=(Node *)malloc(sizeof(Node));

printf("Enter marks for the student: ");

fflush(stdin);

scanf ("%d",&n1->marks);


n1->next=NULL;


if(start==NULL)

start=n1;

else

{for(tmp=start;tmp->next!=NULL;tmp=tmp->next)

;

tmp->next=n1;


}


ans=' ';

printf("Want to continue..(y/n) or (Y/N)?\n");


fflush(stdin);

scanf("%c",&ans);

fflush(stdin);


}while(ans=='y'||ans=='Y');


printList(start);

bubbleSort(start);

printf("\n After sorting marks list in decending order:\n");

printList(start);

printf("\n\nThe highest marks is %d.",start->marks);

printf("\n\nThe lowest marks is %d.",n1->marks);

return 0;

}


void insertAtTheBegin(Node **start_ref, int data)

{

Node *ptr1 = (Node*)malloc(sizeof(Node));

ptr1->marks = data;

ptr1->next = *start_ref;

*start_ref = ptr1;

}



void printList(Node *start)

{

Node *temp = start;

printf("\n");

while (temp!=NULL)

{

printf("%d ", temp->marks);

temp = temp->next;

}

}



void bubbleSort(Node *start)

{

int swapped, i;

Node *ptr1;

Node *lptr = NULL;


if (start == NULL)

return;


do

{

swapped = 0;

ptr1 = start;


while (ptr1->next != lptr)

{

if (ptr1->marks < ptr1->next->marks)

{

swap(ptr1, ptr1->next);

swapped = 1;

}

ptr1 = ptr1->next;

}

lptr = ptr1;

}

while (swapped);

printf("\n");

}


void swap(Node *a, Node *b)

{

int temp = a->marks;

a->marks = b->marks;

b->marks = temp;

}


Recent Posts

See All

Comments


bottom of page