Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <malloc.h>
  5.  
  6. // structure definition
  7. struct node {
  8.     char name[50];
  9.     int age;
  10.     int student_no;
  11.     char nationality[50];
  12.     struct node *next;
  13. };
  14. typedef struct node *NODE_T;
  15.  
  16. // function declarations
  17. void enter_new_data();
  18. void remove_existing_data();
  19. void display_all_data();
  20. void remove_existing_data();
  21. void show_options();
  22. NODE_T getnode();
  23. void freenode(NODE_T p);
  24. void free_buffer();
  25. void continue_f(char message[100]);
  26.  
  27. // global variables
  28. char ch;
  29.  
  30. // create linked-list's main holder variable
  31. NODE_T students,conductor;
  32.  
  33. int main(){
  34.    
  35.     // iniatilize students linked-list by NULL
  36.     students = NULL;
  37.    
  38.     // show the options that user has
  39.     show_options();
  40.    
  41.     return 1;
  42. }
  43.  
  44. // this function will list options that user can apply
  45. void show_options(){
  46.    
  47.     system("cls"); // clear screen
  48.    
  49.     int opt,opt_bool=0;
  50.    
  51.     printf("%s\n\n%s\n%s\n%s\n%s\n\n","The Options You Have","1. Add New Student's Record","2. Delete An Existing Student's Record","3. Display The List Of Students","4. Exit");
  52.    
  53.    
  54.     while(opt_bool != 1){
  55.         printf("Operation:  ");
  56.         scanf("%d",&opt);
  57.        
  58.         if(opt == 1 || opt == 2 || opt == 3 || opt == 4){
  59.             opt_bool = 1;
  60.         }
  61.            
  62.     }
  63.    
  64.     // check the operation and go to the operation
  65.     if(opt == 1){ // adding record
  66.         enter_new_data();
  67.     } else if(opt == 2){ // removing record
  68.         remove_existing_data();
  69.     } else if(opt == 3){ // displaying records
  70.         display_all_data();
  71.     } else if(opt == 4){ // exit the program
  72.         exit(0);
  73.     }
  74. }
  75.  
  76. // enter new student data into linked-list
  77. void enter_new_data(){
  78.    
  79.     system("cls"); // clear screen
  80.    
  81.     // get a new node
  82.     NODE_T p = getnode();
  83.    
  84.     printf("You are entering a new student's record\n");
  85.    
  86.     // take student's name
  87.     printf("Student's Name: ");
  88.     scanf("%s",p->name);
  89.     free_buffer();
  90.    
  91.     // take student's age
  92.     printf("Student's Age: ");
  93.     scanf("%d",&p->age);
  94.     free_buffer();
  95.    
  96.     // take student's number
  97.     printf("Student's Number: ");
  98.     scanf("%d",&p->student_no);
  99.     free_buffer();
  100.    
  101.     // take student's nationality
  102.     printf("Student's Nationality: ");
  103.     scanf("%s",p->nationality);
  104.     free_buffer();
  105.    
  106.     // set p->next next value of last node of linked-list, which is equal to 0
  107.     p->next = 0;
  108.    
  109.     conductor = students; // assign linked-list to the conductor to traverse
  110.    
  111.     // if there is no any node yet, add node p as first node
  112.     if(conductor == NULL) {
  113.         students = p; // since this will be first node of the linked-list we are assigning it to the students
  114.     } else {
  115.        
  116.         // reach the last node
  117.         while (conductor->next != 0)
  118.         {
  119.             conductor = conductor->next;
  120.         }
  121.        
  122.         conductor->next = p; // append the node p to the linked list
  123.     conductor = conductor->next;
  124.     }
  125.    
  126.    
  127.     continue_f("Adding new record is done."); // ask press any key to continue
  128.    
  129.     show_options(); // show options
  130.  
  131. }
  132.  
  133. // to display all data of linked list
  134. void display_all_data(){
  135.    
  136.     system("cls"); // clear screen
  137.    
  138.     printf("The Student Records\n\n");
  139.    
  140.     printf("%20s%10s%20s%20s","STUDENT'S NAME","AGE","STUDENT'S NUMBER","NATIONALITY"); // captions
  141.    
  142.     conductor = students; // assign linked-list to the conductor to traverse
  143.    
  144.     if (conductor != NULL ) { /* Makes sure there is a place to start */
  145.         // traverse untill last node
  146.         do {
  147.            
  148.             printf("\n%20s%10d%20d%20s",conductor->name,conductor->age,conductor->student_no,conductor->nationality); // record
  149.            
  150.             conductor = conductor->next;
  151.         } while (conductor != NULL);
  152.     } else {
  153.         printf("\n\n There is not any record yet."); // print that we don't have ant record yet
  154.     }
  155.    
  156.     continue_f("Listing records is done."); // ask press any key to continue
  157.    
  158.     show_options(); // show options
  159. }
  160.  
  161. // to display all data of linked list
  162. void remove_existing_data(){
  163.    
  164.     system("cls"); // clear screen
  165.    
  166.     printf("The Student Deletion\n\n");
  167.    
  168.     conductor = students; // assign linked-list to the conductor to traverse
  169.    
  170.     if (conductor != NULL ) { /* Makes sure there is a place to start */
  171.        
  172.         int std_num;
  173.         NODE_T p,prev = conductor;
  174.        
  175.         printf("Type student number of student who you want to delete from the list.\nStudent number: ");
  176.         scanf("%d",&std_num); // assign entered number into std_num variable
  177.         free_buffer();
  178.        
  179.         // check if first node is one we are looking for
  180.         if(conductor->student_no == std_num) {
  181.            
  182.             students = conductor->next; // assign current node into prev struct pointer in order to reach previous node
  183.            
  184.         } else {
  185.        
  186.             // traverse untill last node
  187.             while (conductor != NULL) {
  188.            
  189.                 // if the current node is student we are looking for break the loop
  190.                 if(conductor->student_no == std_num){
  191.                    
  192.                     break;
  193.                 }
  194.                
  195.                 prev = conductor; // assign current node into prev struct pointer in order to reach previous node
  196.            
  197.                 conductor = conductor->next; // jump to the next node
  198.             }
  199.            
  200.             // if the std_num match any record
  201.             if(conductor != NULL)
  202.                 prev->next = conductor->next; // change the previous node's next pointer to next pointer value of the node that we are goşng to remove
  203.        
  204.         }
  205.        
  206.         // if record is found
  207.         if(conductor != NULL) {
  208.            
  209.             printf("\n1 record is deleted.\n\n");
  210.             printf(" Name: %s\n Age: %d\n Student Numer: %d\n Nationality: %s",conductor->name,conductor->age,conductor->student_no,conductor->nationality); // print the record
  211.            
  212.             freenode(conductor); // free the node removed from the linked-list
  213.         } else {
  214.             // if there is not record matc with entered student number
  215.             printf("\n Student not found.");
  216.         }
  217.     } else {
  218.         printf(" There is not any record to delete yet."); // print that we don't have ant record yet
  219.     }
  220.    
  221.     continue_f("Deleting operation is done."); // ask press any key to continue
  222.    
  223.     show_options(); // show options
  224. }
  225.  
  226. // create new node
  227. NODE_T getnode(void){
  228.     NODE_T p;
  229.     p = (struct node *) malloc(sizeof(struct node));
  230.     return p;
  231. }
  232.  
  233. // set free a node
  234. void freenode(NODE_T p){
  235.     free(p);
  236. }
  237.  
  238. // clear the buffer if there are any extra data in it
  239. void free_buffer(){
  240.     while (getchar() != '\n') { }  
  241. }
  242.  
  243. void continue_f(char message[100]){
  244.     printf("\n\n%s\nPress any key to continue...",message);
  245.     getch(); // wait for pushing any key from user
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement