Code:
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
typedef struct lista lista;
typedef struct item item;
struct item
{
char item_name[30];
item *next;
};
struct lista
{
int liczba;
item *data;
char napis[10];
lista *next;
};
lista* find(lista *l,char name[61])
{
lista *current = l;
int i;
for(i=1;current!=NULL;i++)
{
if(strcmp(current->napis,name) == 0)
{
printf("[%d] %d %s\n",i ,current->liczba,current->napis);
return current;
}
current=current->next;
}
return (NULL);
}
lista *dodajprzedmiot(lista *temp, char *item_named)
{
if(temp->data == NULL)
{
temp = (lista*)malloc(sizeof(lista));
strcpy(temp->data->item_name, item_named);
temp->data->next = NULL;
}
else
{
lista *pom;
pom = temp;
while(pom->data->next) pom->data=pom->data->next;
pom->next=(lista*)malloc(sizeof(lista));
strcpy(pom->data->next->item_name, item_named);
pom->data->next->next=NULL;
}
return (NULL);
}
lista* kasuj(lista *list)
{
lista *pom=NULL;
while(list)
{
pom=list->next;
free(list);
list=pom;
}
return NULL;
}
void dodaj(lista **l, int n, char tab[100])
{
if((*l)==NULL)
{
*l = (lista*)malloc(sizeof(lista));
(*l)->liczba=n;
strcpy((*l)->napis, tab);
(*l)->next = NULL;
}
else
{
lista *pom;
pom=(*l);
while(pom->next) pom=pom->next;
pom->next=(lista*)malloc(sizeof(lista));
pom->next->liczba=n;
strcpy(pom->next->napis, tab);
pom->next->next=NULL;
}
}
void dodajmiejsce(lista **l, int n, char tab[100])
{
if((*l)==NULL)
{
*l = (lista*)malloc(sizeof(lista));
(*l)->liczba=n;
strcpy((*l)->napis, tab);
(*l)->next =NULL;
return;
}
if( n<(*l)->liczba)
{
lista *pom=NULL;
lista *pom2=NULL;
pom=(lista*)malloc(sizeof(lista));
pom->liczba=n;
strcpy(pom->napis, tab);
pom2=pom;
pom->next=(*l);
(*l)=pom2;
return;
}
else
{
lista *pom;
lista *pom2;
pom=(*l);
while((pom->next!= NULL) && (n>pom->next->liczba)) pom=pom->next;
pom2=pom->next;
pom->next=(lista*)malloc(sizeof(lista));
pom->next->liczba=n;
strcpy(pom->next->napis, tab);
pom->next->next=pom2;
}
}
void wypiszliste(lista *lis)
{
for( ; lis; lis=lis->next)
{
printf("%d %s \n", lis->liczba, lis->napis);
}
}
lista* usun(lista *l, int us)
{
if(l==NULL)
return l;
if(l->liczba == us)
{
lista *pom=l->next;
free(l);
return pom;
}
else
{
lista *zwrot=l;
lista *pom=NULL;
while(( l->next->liczba != us) && (l->next->next!=NULL)) l=l->next;
if(l->next->liczba==us)
{
pom=l->next;
l->next=l->next->next;
free(pom);
}
return zwrot;
}
}
int licz(lista* p)
{
int l=0;
for(;p;p=p->next)
l++;
return l;
}
int main()
{
int wybur, l;
char ss[100];
srand(time(0));
lista *pocz=NULL;
dodaj(&pocz, 1, "jeden");
dodaj(&pocz, 2, "dwa");
dodaj(&pocz, 3, "trzy");
dodaj(&pocz, 4, "cztery");
dodaj(&pocz, 5, "piec");
dodaj(&pocz, 6, "szesc");
dodaj(&pocz, 7, "siedem");
dodaj(&pocz, 8, "osiem");
dodaj(&pocz, 9, "dziewiec");
while(1)
{
wypiszliste(pocz);
printf("\n\nElementow: %d, \nOPCJE:\n1 dodaj \n 2 usun\n 3 usun wszystkie\n", licz(pocz));
scanf("%d", &wybur);
if (wybur==1)
{
printf(" podaj liczbe\n");
scanf("%d", &l);
printf(" podaj opis\n");
scanf("%s", ss);
dodajmiejsce(&pocz, l, ss);
}
if(wybur==2)
{
printf(" podaj liczbe do usuniecia\n");
scanf("%d", &l);
pocz=usun(pocz, l);
}
if(wybur==3)
{
pocz=kasuj(pocz);
}
if(wybur==4)
{
char namec[30];
printf("podaj imie");
scanf("%s",namec);
find(pocz,namec);
}
if(wybur==5)
{
char nameo[30];
char namec[30];
lista *temporal = NULL;
printf("name of client to add");
scanf("%s",namec);
printf("name of item");
scanf("%s",nameo);
temporal = find(pocz,namec);
dodajprzedmiot(temporal,nameo);
}
}
return 0;
}