Anasayfa

C ve Sistem Programcıları Derneği

  • CSD Hakkında
    • Yönetim Kurulu
    • Referanslar
    • Üyelik
    • Adres
    • Kroki
    • İletişim
  • Kurslar
    • Kurs Başvurusu
    • Eğitmenler
  • Seminerler
    • Seminer İste
    • Seminer Ver
  • Kaynaklar
    • Yararlı Linkler
    • Kaynak Kod Arşivi
    • Makaleler
    • Çalışma Soruları
    • Yararlı Kitaplar
    • Dosya Arşivi
    • Döküman Arşivi
  • Duyurular
  • Haberler
  • İş İlanları
    • İş İlanı Gönder
  • SSS
  • Kurs Başvurusu

menü

  • CSD Hakkında
    • Yönetim Kurulu
    • Referanslar
    • Üyelik
    • Adres
    • Kroki
    • İletişim
  • Kurslar
    • Kurs Başvurusu
    • Eğitmenler
  • Seminerler
    • Seminer İste
    • Seminer Ver
  • Kaynaklar
    • Yararlı Linkler
    • Kaynak Kod Arşivi
    • Makaleler
    • Çalışma Soruları
    • Yararlı Kitaplar
    • Dosya Arşivi
    • Döküman Arşivi
  • Duyurular
  • Haberler
  • İş İlanları
    • İş İlanı Gönder
  • SSS
  • Kurs Başvurusu

e-posta ile bilgilendirme.

Derneğimizdeki etkinliklerden haberdar olun!

Haber mektubu üyeliğiniz

Kimler çevrimiçi

Şu an 0 kullanıcı ve 3 ziyaretçi çevrimiçi.
""
dreamhost
Anasayfa

Bağlı Liste Uygulaması

25.03.2010 - 15:02 tarihinde nerg gönderdi

Aşağıdaki örnekte tekli bağlı liste oluşturılmasına ve kullanılmasına ilişkin fonksiyonlar tanımlanıyor.

Kaynak kod: 
/********** datelist.h **************************/
 
typedef struct tagNode {
   Date date;
   struct tagNode *pnext;
}Node;
 
typedef struct {
   Node *pstart;
   Node *pend;
   size_t size;
}*ListHandle;
 
ListHandle openlist(void);
void closelist(ListHandle);
void push_front(ListHandle handle);
void push_back(ListHandle handle);
void display_list(ListHandle handle);
void pop_front(ListHandle handle);
void pop_back(ListHandle handle);
void remove_date(const Date *);
void clear_list(ListHandle handle);
size_t get_size(ListHandle handle);
/********** datelist.c **************************/
PRIVATE Node *create_node(void);
PRIVATE void free_nodes(Node *p);
/*************************************************************************/
PRIVATE void free_nodes(Node *p)
{
   Node *temp;
   while (p) {
      temp = p;
      p = p->pnext;
      free(temp);
   }
}
/*************************************************************************/
PRIVATE Node *create_node(void)
{
   Node *pd = (Node *)malloc(sizeof(Node));
 
   if (!pd) {
      printf("cannot allocate memory!\n");
      exit(EXIT_FAILURE);
   }
   return pd;
}
/*************************************************************************/
PUBLIC ListHandle openlist(void)
{
   ListHandle pd = (ListHandle)malloc(sizeof(*pd));
 
   if (!pd) {
      printf("cannot allocate memory!\n");
      exit(EXIT_FAILURE);
   }
   pd->pstart = pd->pend = NULL;
   pd->size = 0;
 
   return pd;
}
/*************************************************************************/
PUBLIC void closelist(ListHandle handle)
{
   clear_list(handle);
   free(handle);
}
/*************************************************************************/
PUBLIC void push_front(ListHandle handle)
{
   Node *pnew = create_node();
   set_random(&pnew->date);
 
   handle->size++;
   if (handle->pstart == NULL) {
      handle->pstart = handle->pend = pnew;
      pnew->pnext = NULL;
      return;
   }
   pnew->pnext = handle->pstart;
   handle->pstart = pnew;
}
/*************************************************************************/
PUBLIC void push_back(ListHandle handle)
{
   Node *pnew = create_node();
   set_random(&pnew->date);
   pnew->pnext = NULL;
 
   handle->size++;
   if (handle->pstart == NULL) {
      handle->pstart = handle->pend = pnew;
      return;
   }
   handle->pend->pnext = pnew;
   handle->pend = pnew;
}
/*************************************************************************/
PUBLIC void display_list(ListHandle handle)
{
   Node *cur = handle->pstart;
 
   if (!handle->size) {
      printf("empty list!\n");
      return;
   }
   while (cur) {
      display_date(&cur->date);
      cur = cur->pnext;
   }
}
/*************************************************************************/
PUBLIC void clear_list(ListHandle handle)
{
   free_nodes(handle->pstart);
   handle->pstart = handle->pend = NULL;
}
/*************************************************************************/
PUBLIC size_t get_size(ListHandle handle)
{
   return handle->size;
}
/*************************************************************************/
PUBLIC void pop_front(ListHandle handle)
{
   Node *temp;
 
   if (!handle->size) {
      printf("liste bos!\n");
      return;
   }
   handle->size--;
 
   if (handle->pstart == handle->pend) {
      free(handle->pstart);
      handle->pstart = handle->pend = NULL;
      return;
   }
   temp = handle->pstart;
   handle->pstart = handle->pstart->pnext;
   free(temp);
}
/*************************************************************************/
PUBLIC void pop_back(ListHandle handle)
{
   Node *temp, *cur;
 
   if (!handle->size) {
      printf("liste bos!\n");
      return;
   }
 
   handle->size--;
 
   if (handle->pstart == handle->pend) {
      free(handle->pstart);
      handle->pstart = handle->pend = NULL;
      return;
   }
   temp = handle->pend;
 
   for (cur = handle->pstart; cur->pnext != handle->pend; cur = cur->pnext)
      ;
   cur->pnext = NULL;
   handle->pend = cur;
 
   free(temp);
}
/*************************************************************************/
void display_menu()
{
   printf("[1] PUSH FRONT\n");
   printf("[2] PUSH BACK\n");
   printf("[3] DISPLAY LIST\n");
   printf("[4] POP FRONT\n");
   printf("[5] POP BACK\n");
   printf("[6] EMPTY LIST\n");
   printf("[7] EXIT\n");
 
   printf("seciminiz : ");
}
/*************************************************************************/
int get_option()
{
   int option;
 
   display_menu();
   scanf("%d", &option);
 
   if (option < 1 || option > 7)
      option = 0;
 
   return option;
}
/*************************************************************************/
 
int main()
{
   int option;
   ListHandle handle = openlist();
 
   for (;;) {
      option = get_option();
      switch (option) {
      case 1: push_front(handle); break; 
      case 2: push_back(handle); break; 
      case 3: display_list(handle);break; 
      case 4: pop_front(handle);break; 
      case 5: pop_back(handle);break; 
      case 6: clear_list(handle); break;
      case 7: goto END;
      case 8: printf("invalid entry!\n");
      }
   }
END:
   closelist(handle);
   printf("end of program!\n");
 
   return 0;
}
  • Yazıcı uyumluYazıcı uyumlu
  • Arkadaşa gönderArkadaşa gönder
  • PDFPDF
Etiketler:
  • C
  • bağlı listeler

Adres: 2.Taşocağı Cd. Oğuz Sk. Barbaros Apt. No: 11/4 PK: 34387  Mecidiyeköy / İSTANBUL Tel: (212) 274 63 60  (212) 274 99 89  (212) 275 88 97  (212) 288 35 20 Fax: (212) 275 88 97 E-Posta: csystem@csystem.org

İçerik yayınları
Fervens Drupal theme by Leow Kah Thong. Designed by Design Disease and brought to you by Smashing Magazine.