Skip to content

Instantly share code, notes, and snippets.

@roktas
Created April 11, 2010 23:46
Show Gist options
  • Save roktas/363143 to your computer and use it in GitHub Desktop.
Save roktas/363143 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct ogrenci {
char *ad;
char *soyad;
int no;
struct ogrenci *sonra;
};
static struct ogrenci *liste; /* öğrenci listesi. ilkle ile başlat, yoket ile sonlandır. */
struct ogrenci *
ogrenci(const char *ad, const char *soyad, int no)
{
struct ogrenci *yeni;
if ((yeni = malloc(sizeof(struct ogrenci))) == NULL) {
fprintf(stderr, "bellek tükendi\n");
exit(EXIT_FAILURE);
}
yeni->ad = strdup(ad);
yeni->soyad = strdup(soyad);
yeni->no = no;
return yeni;
}
/* ogrenci işlevinde yapılanın (bir nevi) tersini yapıyor */
void
icnergo(struct ogrenci *ogr)
{
free(ogr->ad);
free(ogr->soyad);
}
struct ogrenci *
ekle(const char *ad, const char *soyad, int no)
{
struct ogrenci *bu = ogrenci(ad, soyad, no);
bu->sonra = liste;
liste = bu;
return bu;
}
void
ilkle(void)
{
liste = NULL;
}
void
yoket(void)
{
struct ogrenci *ogr = liste, *sonra;
while (ogr) {
sonra = ogr->sonra;
icnergo(ogr);
free(ogr);
ogr = sonra;
}
}
void
gor(struct ogrenci *ogr, void *veri)
{
printf("ad: %s\nsoyad: %s\nno: %d\n\n", ogr->ad, ogr->soyad, ogr->no);
}
void *
esleno(struct ogrenci *ogr, void *veri)
{
/* ikinci argüman karşılaştırılan numarayı taşıyan bir işaretçi */
return (ogr->no == *((int *)veri)) ? (void *)ogr : NULL;
}
void
yinele(void (*yineleyici)(struct ogrenci *, void *), void *veri)
{
struct ogrenci *ogr;
for (ogr = liste; ogr; ogr = ogr->sonra)
(*yineleyici)(ogr, veri);
}
void *
sorgula(void *(*sorgulayici)(struct ogrenci *, void *), void *veri)
{
struct ogrenci *ogr;
void *sonuc;
for (sonuc = NULL, ogr = liste; ogr && !sonuc; sonuc = (*sorgulayici)(ogr, veri), ogr = ogr->sonra)
; /* boş */
return sonuc;
}
void
listele(void)
{
yinele(gor, NULL);
}
void
bulno(int no)
{
struct ogrenci *ogr;
ogr = sorgula(esleno, &no); /* konvansiyon gereği aranan numaranın adresi veriliyor */
if (ogr)
gor(ogr, NULL);
else
fprintf(stderr, "%d numaralı bir kayıt yok!\n", no);
}
int
main(int argc, char *argv[])
{
ilkle();
ekle("recai", "oktas", 856);
ekle("nurettin", "senyer", 4567);
ekle("gokhan", "kayhan", 412);
listele();
bulno(argc > 1 ? atoi(argv[1]) : 856);
yoket();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment