Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.17 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 show_options();
  21. NODE_T getnode();
  22. void freenode(NODE_T p);
  23. void free_buffer();
  24. void continue_f(char message[100]);
  25.  
  26. // global variables
  27. char ch;
  28.  
  29. // create linked-list's main holder variable
  30. NODE_T students,conductor;
  31.  
  32. int main(){
  33.    
  34.     // allocate memory for the node link-list holder
  35.     students = NULL;
  36.     //students->next = 0; // initialize next to 0 for first node of linked-list
  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.    
  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.     printf("%s, %d, %d, %s",p->name,p->age,p->student_no,p->nationality);
  110.    
  111.    
  112.     // if there is no any node yet, add node p as first node
  113.     if(students == NULL) {
  114.         students = p;
  115.     } else {
  116.        
  117.         conductor = students; // assign linked-list to the conductor to traverse
  118.        
  119.         // reach the last node
  120.         while (conductor->next != 0)
  121.         {
  122.             conductor = conductor->next;
  123.         }
  124.        
  125.         conductor->next = p; // append the node p to the linked list
  126.     }
  127.    
  128.     freenode(p); // set free node p
  129.    
  130.     continue_f("Adding new record is done."); // ask press any key to continue
  131.    
  132.     show_options(); // show options
  133.  
  134. }
  135.  
  136. // to display all data of linked list
  137. void display_all_data(){
  138.    
  139.     system("cls"); // clear screen
  140.    
  141.     printf("The Student Records\n\n");
  142.    
  143.     printf("%s%7s%18s%15s","STUDENT'S NAME","AGE","STUDENT NUMBER","NATIONALITY"); // captions
  144.    
  145.     freenode(conductor);
  146.     conductor = getnode();
  147.     conductor = students; // assign linked-list to the conductor to traverse
  148.    
  149.     if (conductor != NULL ) { /* Makes sure there is a place to start */
  150.         // traverse untill last node
  151.         while ( conductor->next != 0)
  152.         {
  153.            
  154.             printf("\n%s%7d%18d%15s",conductor->name,conductor->age,conductor->student_no,conductor->nationality); // record
  155.            
  156.             conductor = conductor->next;
  157.         }
  158.     } else {
  159.         printf("\n\n There is not any record yet.");
  160.     }
  161.    
  162.     continue_f("Listing records is done."); // ask press any key to continue
  163.    
  164.     show_options(); // show options
  165. }
  166.  
  167. // create new node
  168. NODE_T getnode(void){
  169.     NODE_T p;
  170.     p = (struct node *) malloc(sizeof(struct node));
  171.     return p;
  172. }
  173.  
  174. // set free a node
  175. void freenode(NODE_T p){
  176.     free(p);
  177. }
  178.  
  179. // clear the buffer if there are any extra data in it
  180. void free_buffer(){
  181.     while (getchar() != '\n') { }  
  182. }
  183.  
  184. void continue_f(char message[100]){
  185.     printf("\n\n%s\nPress any key to continue...",message);
  186.     getch(); // wait for pushing any key from user
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement