Advertisement
coffeebeforecode

Untitled

Jun 10th, 2021
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.03 KB | None | 0 0
  1. /*
  2. a) Create an unordered linked list to enroll the students who wish to participate for a technical event
  3. organised by the university taking details like Name, regno, age, Branch of study, phone number.
  4. Search for those students whose age not more than 20 and their branch of study
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #define MAX 50
  10.  
  11. struct student{
  12.     char name[MAX];
  13.     int regNo;
  14.     int age;
  15.     char branch[MAX];
  16.     char ph[MAX];
  17.     struct student* next;
  18. };
  19.  
  20. struct student* front;
  21. struct student* rear;
  22.  
  23. void push()
  24. {
  25.     struct student* new_node = (struct student*) malloc(sizeof(struct student));
  26.    
  27.     printf("\nInput name: ");
  28.     scanf("%s", new_node->name);
  29.     printf("\nInput reg. Number: ");
  30.     scanf("%d", &new_node->regNo);
  31.     printf("\nInput age: ");
  32.     scanf("%d", &new_node->age);
  33.     printf("\nInput branch: ");
  34.     scanf("%s", new_node->branch);
  35.     printf("\nInput phone number: ");
  36.     scanf("%s", new_node->ph);
  37.    
  38.     if (front == NULL){
  39.         front = rear = new_node;
  40.     }
  41.     else{
  42.         /* 3. Make next of new node as head */
  43.         rear->next = new_node;
  44.    
  45.         /* 4. move the head to point to the new node */
  46.         rear  = new_node;
  47.     }
  48.  
  49. }
  50.  
  51. void search20(){
  52.     if (front == NULL){
  53.         printf("\nNo students in list");
  54.         return;
  55.     }
  56.  
  57.     struct student* temp = front;
  58.     printf("\nStudents whose age is not more than 20 and their branch:\n");
  59.     while (temp != NULL){
  60.         if (temp->age <= 20){
  61.             printf("--------------------------------");
  62.             printf("\nName: %s\nAge: %d\nBranch: %s\n", temp->name, temp->age, temp->branch);
  63.             printf("--------------------------------");
  64.         }
  65.         temp = temp->next;
  66.     }
  67. }
  68.  
  69. void delete24(){
  70.     if (front == NULL){
  71.         printf("\nNo students in list");
  72.         return;
  73.     }
  74.  
  75.     struct student* temp = front;
  76.     printf("\nDeleting Students whose age is greater than 24\n");
  77.     while (temp != NULL){
  78.         if (temp->age > 24){
  79.             if(temp == front){
  80.                 if (front->next == NULL){
  81.                     front = rear = NULL;
  82.                     free(temp);
  83.                     return;
  84.                 }
  85.                 front = front->next;
  86.                 free(temp);
  87.                 temp = front;
  88.             }
  89.             else{
  90.                 struct student* temp2 = temp;
  91.                 temp = temp->next;
  92.                 free(temp2);
  93.             }
  94.         }
  95.         else{
  96.             temp = temp->next;
  97.         }
  98.     }
  99. }
  100.  
  101. void display(){
  102.     if (front == NULL){
  103.         printf("\nNo students remaining");
  104.         return;
  105.     }
  106.  
  107.     struct student* temp = front;
  108.  
  109.     printf("\nStudent details:\n");
  110.     printf("--------------------------------");
  111.     while (temp != NULL){
  112.    
  113.         printf("\nName: %s", temp->name);
  114.         printf("\nReg. number: %d", temp->regNo);
  115.         printf("\nAge: %d", temp->age);
  116.         printf("\nBranch: %s", temp->branch);
  117.         printf("\nPhone Number: %s\n", temp->ph);
  118.         printf("--------------------------------");
  119.    
  120.         temp = temp->next;
  121.     }
  122.  
  123. }
  124.  
  125. int main(){
  126.    
  127.     int ch;
  128.     while(1){
  129.         printf("\n\tMENU\n");
  130.         printf("\n1. Insert element into list");
  131.         printf("\n2. Search for those students whose age not more than 20 and their branch of study");
  132.         printf("\n3. Deletion those student whose age is more than 24 and display the content of the linked list");
  133.         printf("\n4. Display current list");
  134.         printf("\n5: Exit\n>>");
  135.  
  136.         scanf("%d", &ch);
  137.  
  138.         switch (ch)
  139.         {
  140.         case 1:
  141.             push();
  142.             break;
  143.         case 2:
  144.             search20();
  145.             break;
  146.         case 3:
  147.             delete24();
  148.             display();
  149.             break;
  150.         case 4:
  151.             display();
  152.             break;
  153.         case 5:
  154.             exit(0)  ;
  155.         default:
  156.             printf("\nWrong Choice!");
  157.             break;
  158.         }
  159.  
  160.     }
  161.  
  162.     return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement