Kod:
Kod: Zaznacz cały
#include <stdio.h>
#include <stdlib.h>
struct Dane {
float x;
};
struct Element{
struct Dane dane;
struct Element *nast;
};
typedef struct Element ELEMENT;
typedef ELEMENT* ADRES;
void wypiszListe(ADRES lista){
if(lista==NULL){
printf("Lista jest pusta.
");
return -1;
}
printf("Nasza lista:");
while(lista!=NULL){
printf("%.2f", lista -> dane.x);
lista=lista -> nast;
}
printf("
");
}
void dodajDoListy(ADRES *lista, float liczba){
ADRES elem;
elem =(ADRES)malloc(sizeof( ELEMENT ) );
elem -> dane.x = liczba;
elem -> nast = NULL;
if( (*lista) == NULL )
(*lista) = elem;
else{
ADRES pom = (*lista);
while( pom -> nast != NULL ){
pom = pom -> nast;
pom -> nast = elem;
}
}
}
void czyscListe( ADRES *lista ){
ADRES pom = *lista;
while( pom != NULL){
pom = pom -> nast;
free( *lista );
(*lista) = pom;
}
(*lista) = NULL;
}
void usunZListy (ADRES* lista, float liczba){
ADRES pom, nast, pop;
if ((*lista) == NULL){
printf("Lista jest pusta, nie ma co usuwac
");
return;
}
pom = (*lista);
while( pom != NULL){
if( pom -> dane.x = liczba )
break;
pom = pom -> nast;
}
if( pom == NULL ){
printf("Nie ma elementu %d na liscie
", liczba);
}
else{
nast = pom -> nast;
if( pom == (*lista) )
(*lista) = nast;
else{
pop = (*lista);
while( pop -> nast != pom )
pop = pop -> nast;
pop -> nast = nast;
}
free( pom );
}
}
void EXIT(){
printf("KONIEC PROGRAMU");
}
int main()
{
int wybor;
do{
printf("----------------------------
");
printf(" MENU GLOWNE
");
printf("----------------------------
");
printf("1. DodajDoListy.
");
printf("2. WypiszListe
");
printf("3. UsunZlisty
");
printf("4. CzyscListe
");
printf("5. EXIT
");
printf("Wybor:");
scanf("%d", &wybor);
ADRES lista = NULL;
//for(i=0;i<N;i++)
switch(wybor){
case 1: dodajDoListy( &lista, scanf("%d") ); break;
case 2: wypiszListe(lista); break;
case 3: usunZListy( &lista, scanf("%d") ); break;
case 4: czyscListe( &lista ); break;
case 5: EXIT; break;
}
}
while(wybor!=5);
return 0;
}