Guest User

Untitled

a guest
May 20th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct student{
  6.  
  7. char name[21], surname[31];
  8. int ID,grade,credit;
  9. struct student *next;
  10.  
  11. }STUDENT;
  12.  
  13. int vloz(STUDENT **first, STUDENT **act, STUDENT **prev, STUDENT *pom){
  14.  
  15. int changed=0;
  16.  
  17. if((*first)==NULL){
  18. (*first)=pom;
  19. (*first)->next=NULL;
  20. }
  21. else{
  22. for((*prev)=(*act)=(*first);(*act)!=NULL;(*prev)=(*act), (*act)=(*act)->next){
  23. if(strcmp(pom->surname,(*act)->surname)<0 || strcmp(pom->surname,(*act)->surname)==0){
  24. if((*act)==(*first)){
  25. (*first)=pom;
  26. pom->next=(*act);
  27. changed=1;
  28. break;
  29. }
  30. (*prev)->next=pom;
  31. pom->next=(*act);
  32. changed=1;
  33. break;
  34. }
  35. }
  36. if(changed==0){
  37. (*prev)->next=pom;
  38. pom->next=NULL;
  39. }
  40. }
  41.  
  42. return 0;
  43. }
  44.  
  45. int vypis(STUDENT *first, STUDENT *act, STUDENT *prev){
  46.  
  47. int i=1;
  48.  
  49. if(first==NULL)
  50. printf("Zoznam je prazdny.\n");
  51. else
  52. for(act=first;act!=NULL;act=act->next){
  53. printf("%d.\n",i);
  54. printf("Meno: %s\n",act->name);
  55. printf("Priezvisko: %s\n",act->surname);
  56. printf("Osobne cislo: %d\n",act->ID);
  57. printf("Rocnik: %d\n",act->grade);
  58. printf("Pocet kreditov: %d\n",act->credit);
  59. i++;
  60. }
  61.  
  62. return 0;
  63. }
  64.  
  65. int main(void){
  66.  
  67. char c='A';
  68. STUDENT *first=NULL,*act=NULL,*prev=NULL,*pom=NULL;
  69.  
  70. while(c!='K'){
  71. if(c=='A' || c=='P')
  72. printf("Pridat zaznam: P, Vypisat zoznam: V.\n");
  73. switch(c=getchar()){
  74. case('P'): {
  75. if((pom=(STUDENT*) malloc (sizeof(STUDENT)))==NULL){
  76. printf("Nepodarilo sa alokovat pole.\n");
  77. return 1;
  78. }
  79. scanf("%s",pom->name);
  80. scanf("%s",pom->surname);
  81. scanf("%d",&pom->ID);
  82. scanf("%d",&pom->grade);
  83. scanf("%d",&pom->credit);
  84. vloz(&first,&act,&prev,pom);
  85. } break;
  86. case('V'): vypis(first,act,prev); break;
  87. }
  88. }
  89.  
  90. return 0;
  91. }
Add Comment
Please, Sign In to add comment