Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct podatak {
- char naziv[100];
- char adresa[100];
- char telefon[20];
- } TPodatak;
- typedef struct cvor {
- TPodatak info;
- struct cvor *next;
- } TCvor;
- int unosElementa(TCvor **phead, TPodatak *el, int pos) {
- TCvor *temp;
- int brojac=1;
- int i;
- temp=*phead;
- while(temp!=NULL) {
- brojac++;
- temp=temp->next;
- }
- if(pos<1 || pos>brojac+1)
- return 0;
- else {
- TCvor *novi;
- novi = (TCvor *)malloc(sizeof(TCvor));
- if(novi==NULL)
- return 0;
- i=0;
- while((*el).naziv[i]!=NULL) {
- (novi->info).naziv[i]=(*el).naziv[i];
- i++;
- }
- (novi->info).naziv[i]=NULL;
- i=0;
- while((*el).adresa[i]!=NULL) {
- (novi->info).adresa[i]=(*el).adresa[i];
- i++;
- }
- (novi->info).adresa[i]=NULL;
- i=0;
- while((*el).telefon[i]!=NULL) {
- (novi->info).telefon[i]=(*el).telefon[i];
- i++;
- }
- (novi->info).telefon[i]=NULL;
- if(pos==1) {
- novi->next=*phead;
- *phead=novi;
- }
- else {
- temp=*phead;
- for(i=1; i<pos-1; i++)
- temp=temp->next;
- novi->next=temp->next;
- temp->next=novi;
- }
- return 1;
- }
- }
- int brisiElement(TCvor **phead, int pos) {
- TCvor *temp;
- TCvor *del;
- int brojac=1;
- int i;
- if(*phead==NULL)
- return 0;
- temp=*phead;
- while(temp!=NULL) {
- brojac++;
- temp=temp->next;
- }
- if(pos<1 || pos>brojac)
- return 0;
- else {
- if(pos==1) {
- del=*phead;
- (*phead)=(*phead)->next;
- free(del);
- }
- else {
- temp=*phead;
- for(i=1; i<pos-1; i++)
- temp=temp->next;
- del=temp->next;
- temp->next=del->next;
- free(del);
- }
- return 1;
- }
- }
- int ispisiListu(TCvor *head) {
- TCvor *temp = head;
- if(head==NULL)
- return 0;
- while(temp!=NULL) {
- printf("\n********************************\n");
- printf("%s",(temp->info).naziv);
- printf("\n");
- printf("%s",(temp->info).adresa);
- printf("\n");
- printf("%s",(temp->info).telefon);
- printf("\n********************************\n");
- temp=temp->next;
- }
- return 1;
- }
- void obrisiListu(TCvor **phead) {
- while((*phead)!=NULL)
- brisiElement(phead,1);
- printf("\nLista je obrisana!");
- }
- char meni() {
- char x;
- printf("\n\n\n\n--------------------------------------------");
- printf("\n1. Unos elementa u listu");
- printf("\n2. Brisanje elementa iz liste");
- printf("\n3. Ispis liste");
- printf("\n4. Obrisi listu");
- printf("\n5. Kraj rada");
- printf("\n\nVas izbor je -> ");
- do {
- scanf("%c",&x);
- fflush(stdin);
- } while(x<'1' || x>'5');
- return x;
- }
- int main() {
- TCvor *head=NULL;
- char odg;
- int pos;
- TPodatak el;
- printf("Dobro dosli u program za rad sa jednostrukospregnutom listom celih brojeva.");
- do {
- odg=meni();
- switch(odg) {
- case '1' : printf("\nUnesite naziv -> ");
- gets(el.naziv);
- printf("\nUnesite adresu - > ");
- gets(el.adresa);
- printf("\nUnesite telefon -> ");
- gets(el.telefon);
- printf("\nUnesite poziciju novog elementa u listi -> ");
- scanf("%d",&pos);
- if(unosElementa(&head,&el,pos))
- printf("\nElement je unet u listu!");
- else
- printf("\nElement nije unet u listu!");
- break;
- case '2' : printf("\nUnesite poziciju elementa za brisanje u listi -> ");
- scanf("%d",&pos);
- if(brisiElement(&head, pos))
- printf("\nElement je izbrisan!");
- else
- printf("\nElement nije izbrisan!");
- break;
- case '3' : if(ispisiListu(head)==0)
- printf("\nLista je prazna!");
- break;
- case '4' : obrisiListu(&head);
- }
- } while(odg!='5');
- obrisiListu(&head);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement