Advertisement
Lauda

Imenik

Jan 6th, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct podatak {
  5.     char naziv[100];
  6.     char adresa[100];
  7.     char telefon[20];
  8.     } TPodatak;
  9.  
  10. typedef struct cvor {
  11.     TPodatak info;
  12.     struct cvor *next;
  13.     } TCvor;
  14.  
  15. int unosElementa(TCvor **phead, TPodatak *el, int pos) {
  16.     TCvor *temp;
  17.     int brojac=1;
  18.     int i;
  19.  
  20.     temp=*phead;
  21.     while(temp!=NULL) {
  22.         brojac++;
  23.         temp=temp->next;
  24.     }
  25.  
  26.     if(pos<1 || pos>brojac+1)
  27.         return 0;
  28.     else {
  29.         TCvor *novi;
  30.         novi = (TCvor *)malloc(sizeof(TCvor));
  31.         if(novi==NULL)
  32.             return 0;
  33.  
  34.         i=0;
  35.         while((*el).naziv[i]!=NULL) {
  36.             (novi->info).naziv[i]=(*el).naziv[i];
  37.             i++;
  38.         }
  39.         (novi->info).naziv[i]=NULL;
  40.  
  41.         i=0;
  42.         while((*el).adresa[i]!=NULL) {
  43.             (novi->info).adresa[i]=(*el).adresa[i];
  44.             i++;
  45.         }
  46.         (novi->info).adresa[i]=NULL;
  47.  
  48.         i=0;
  49.         while((*el).telefon[i]!=NULL) {
  50.             (novi->info).telefon[i]=(*el).telefon[i];
  51.             i++;
  52.         }
  53.         (novi->info).telefon[i]=NULL;
  54.  
  55.         if(pos==1) {
  56.             novi->next=*phead;
  57.             *phead=novi;
  58.         }
  59.         else {
  60.             temp=*phead;
  61.             for(i=1; i<pos-1; i++)
  62.                 temp=temp->next;
  63.             novi->next=temp->next;
  64.             temp->next=novi;
  65.         }
  66.         return 1;
  67.     }
  68. }
  69.  
  70. int brisiElement(TCvor **phead, int pos) {
  71.     TCvor *temp;
  72.     TCvor *del;
  73.     int brojac=1;
  74.     int i;
  75.  
  76.     if(*phead==NULL)
  77.         return 0;
  78.  
  79.     temp=*phead;
  80.     while(temp!=NULL) {
  81.         brojac++;
  82.         temp=temp->next;
  83.     }
  84.  
  85.     if(pos<1 || pos>brojac)
  86.         return 0;
  87.     else {
  88.          if(pos==1) {
  89.              del=*phead;
  90.             (*phead)=(*phead)->next;
  91.             free(del);
  92.         }
  93.         else {
  94.             temp=*phead;
  95.             for(i=1; i<pos-1; i++)
  96.                 temp=temp->next;
  97.             del=temp->next;
  98.             temp->next=del->next;
  99.             free(del);
  100.         }
  101.         return 1;
  102.     }
  103. }
  104.  
  105. int ispisiListu(TCvor *head) {
  106.     TCvor *temp = head;
  107.     if(head==NULL)
  108.         return 0;
  109.     while(temp!=NULL) {
  110.         printf("\n********************************\n");
  111.         printf("%s",(temp->info).naziv);
  112.         printf("\n");
  113.         printf("%s",(temp->info).adresa);
  114.         printf("\n");
  115.         printf("%s",(temp->info).telefon);
  116.         printf("\n********************************\n");
  117.         temp=temp->next;
  118.     }
  119.     return 1;
  120. }
  121.  
  122. void obrisiListu(TCvor **phead) {
  123.     while((*phead)!=NULL)
  124.         brisiElement(phead,1);
  125.     printf("\nLista je obrisana!");
  126. }
  127.  
  128. char meni() {
  129.     char x;
  130.     printf("\n\n\n\n--------------------------------------------");
  131.     printf("\n1. Unos elementa u listu");
  132.     printf("\n2. Brisanje elementa iz liste");
  133.     printf("\n3. Ispis liste");
  134.     printf("\n4. Obrisi listu");
  135.     printf("\n5. Kraj rada");
  136.     printf("\n\nVas izbor je -> ");
  137.     do {
  138.         scanf("%c",&x);
  139.         fflush(stdin);
  140.     } while(x<'1' || x>'5');
  141.     return x;
  142. }
  143.  
  144. int main() {
  145.     TCvor *head=NULL;
  146.     char odg;
  147.     int pos;
  148.     TPodatak el;
  149.     printf("Dobro dosli u program za rad sa jednostrukospregnutom listom celih brojeva.");
  150.     do {
  151.         odg=meni();
  152.         switch(odg) {
  153.             case '1' : printf("\nUnesite naziv -> ");
  154.                      gets(el.naziv);
  155.                      printf("\nUnesite adresu - > ");
  156.                      gets(el.adresa);
  157.                      printf("\nUnesite telefon -> ");
  158.                      gets(el.telefon);
  159.                      printf("\nUnesite poziciju novog elementa u listi -> ");
  160.                      scanf("%d",&pos);
  161.                      if(unosElementa(&head,&el,pos))
  162.                         printf("\nElement je unet u listu!");
  163.                      else
  164.                         printf("\nElement nije unet u listu!");
  165.                      break;
  166.  
  167.             case '2' : printf("\nUnesite poziciju elementa za brisanje u listi -> ");
  168.                      scanf("%d",&pos);
  169.                      if(brisiElement(&head, pos))
  170.                         printf("\nElement je izbrisan!");
  171.                      else
  172.                         printf("\nElement nije izbrisan!");
  173.                      break;
  174.  
  175.             case '3' : if(ispisiListu(head)==0)
  176.                         printf("\nLista je prazna!");
  177.                      break;
  178.             case '4' : obrisiListu(&head);
  179.         }
  180.     } while(odg!='5');
  181.     obrisiListu(&head);
  182.     return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement