Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct student{
- char name[21], surname[31];
- int ID,grade,credit;
- struct student *next;
- }STUDENT;
- int vloz(STUDENT **first, STUDENT **act, STUDENT **prev, STUDENT *pom){
- int changed=0;
- if((*first)==NULL){
- (*first)=pom;
- (*first)->next=NULL;
- }
- else{
- for((*prev)=(*act)=(*first);(*act)!=NULL;(*prev)=(*act), (*act)=(*act)->next){
- if(strcmp(pom->surname,(*act)->surname)<0 || strcmp(pom->surname,(*act)->surname)==0){
- if((*act)==(*first)){
- (*first)=pom;
- pom->next=(*act);
- changed=1;
- break;
- }
- (*prev)->next=pom;
- pom->next=(*act);
- changed=1;
- break;
- }
- }
- if(changed==0){
- (*prev)->next=pom;
- pom->next=NULL;
- }
- }
- return 0;
- }
- int vypis(STUDENT *first, STUDENT *act, STUDENT *prev){
- int i=1;
- if(first==NULL)
- printf("Zoznam je prazdny.\n");
- else
- for(act=first;act!=NULL;act=act->next){
- printf("%d.\n",i);
- printf("Meno: %s\n",act->name);
- printf("Priezvisko: %s\n",act->surname);
- printf("Osobne cislo: %d\n",act->ID);
- printf("Rocnik: %d\n",act->grade);
- printf("Pocet kreditov: %d\n",act->credit);
- i++;
- }
- return 0;
- }
- int main(void){
- char c='A';
- STUDENT *first=NULL,*act=NULL,*prev=NULL,*pom=NULL;
- while(c!='K'){
- if(c=='A' || c=='P')
- printf("Pridat zaznam: P, Vypisat zoznam: V.\n");
- switch(c=getchar()){
- case('P'): {
- if((pom=(STUDENT*) malloc (sizeof(STUDENT)))==NULL){
- printf("Nepodarilo sa alokovat pole.\n");
- return 1;
- }
- scanf("%s",pom->name);
- scanf("%s",pom->surname);
- scanf("%d",&pom->ID);
- scanf("%d",&pom->grade);
- scanf("%d",&pom->credit);
- vloz(&first,&act,&prev,pom);
- } break;
- case('V'): vypis(first,act,prev); break;
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment