Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- a) Create an unordered linked list to enroll the students who wish to participate for a technical event
- organised by the university taking details like Name, regno, age, Branch of study, phone number.
- Search for those students whose age not more than 20 and their branch of study
- */
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX 50
- struct student{
- char name[MAX];
- int regNo;
- int age;
- char branch[MAX];
- char ph[MAX];
- struct student* next;
- };
- struct student* front;
- struct student* rear;
- void push()
- {
- struct student* new_node = (struct student*) malloc(sizeof(struct student));
- printf("\nInput name: ");
- scanf("%s", new_node->name);
- printf("\nInput reg. Number: ");
- scanf("%d", &new_node->regNo);
- printf("\nInput age: ");
- scanf("%d", &new_node->age);
- printf("\nInput branch: ");
- scanf("%s", new_node->branch);
- printf("\nInput phone number: ");
- scanf("%s", new_node->ph);
- if (front == NULL){
- front = rear = new_node;
- }
- else{
- /* 3. Make next of new node as head */
- rear->next = new_node;
- /* 4. move the head to point to the new node */
- rear = new_node;
- }
- }
- void search20(){
- if (front == NULL){
- printf("\nNo students in list");
- return;
- }
- struct student* temp = front;
- printf("\nStudents whose age is not more than 20 and their branch:\n");
- while (temp != NULL){
- if (temp->age <= 20){
- printf("--------------------------------");
- printf("\nName: %s\nAge: %d\nBranch: %s\n", temp->name, temp->age, temp->branch);
- printf("--------------------------------");
- }
- temp = temp->next;
- }
- }
- void delete24(){
- if (front == NULL){
- printf("\nNo students in list");
- return;
- }
- struct student* temp = front;
- printf("\nDeleting Students whose age is greater than 24\n");
- while (temp != NULL){
- if (temp->age > 24){
- if(temp == front){
- if (front->next == NULL){
- front = rear = NULL;
- free(temp);
- return;
- }
- front = front->next;
- free(temp);
- temp = front;
- }
- else{
- struct student* temp2 = temp;
- temp = temp->next;
- free(temp2);
- }
- }
- else{
- temp = temp->next;
- }
- }
- }
- void display(){
- if (front == NULL){
- printf("\nNo students remaining");
- return;
- }
- struct student* temp = front;
- printf("\nStudent details:\n");
- printf("--------------------------------");
- while (temp != NULL){
- printf("\nName: %s", temp->name);
- printf("\nReg. number: %d", temp->regNo);
- printf("\nAge: %d", temp->age);
- printf("\nBranch: %s", temp->branch);
- printf("\nPhone Number: %s\n", temp->ph);
- printf("--------------------------------");
- temp = temp->next;
- }
- }
- int main(){
- int ch;
- while(1){
- printf("\n\tMENU\n");
- printf("\n1. Insert element into list");
- printf("\n2. Search for those students whose age not more than 20 and their branch of study");
- printf("\n3. Deletion those student whose age is more than 24 and display the content of the linked list");
- printf("\n4. Display current list");
- printf("\n5: Exit\n>>");
- scanf("%d", &ch);
- switch (ch)
- {
- case 1:
- push();
- break;
- case 2:
- search20();
- break;
- case 3:
- delete24();
- display();
- break;
- case 4:
- display();
- break;
- case 5:
- exit(0) ;
- default:
- printf("\nWrong Choice!");
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement