Results 1 to 3 of 3

Thread: , ++, #

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User hotris's Avatar
    Join Date: Dec:2007
    Location: Sofia
    Posts: 167

    , ++, #

    ++, # ( unsafe ), ?
    .
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef int data;
    typedef int keyType;
    
    struct list {
      keyType key;
      data info;
      struct list *next;
    };
    
    /*        */
    void insertBegin(struct list **L, keyType key, data x)
    { struct list *temp;
      temp = (struct list *) malloc(sizeof(*temp));
      if (NULL == temp) {
        fprintf(stderr, "       !\n");
        return;
      }
    
      temp->next = *L;
      (*L) = temp;
      (*L)->key = key;
      (*L)->info = x;
    }
    
    /*     */
    void insertAfter(struct list **L, keyType key, data x)
    { struct list *temp;
    
      if (NULL == *L) {      /*     =>   */
        insertBegin(L, key, x);
        return;
      }
    
      temp = (struct list *) malloc(sizeof(*temp));
      /*     */
      if (NULL == temp) {
        fprintf(stderr, "     !\n");
        return;
      }
    
      temp->key = key;
      temp->info = x;
      temp->next = (*L)->next;
      (*L)->next = temp;
    }
    
    /*     */
    void insertBefore(struct list **L, keyType key, data x)
    { struct list *temp;
    
      if (NULL == *L) {      /*        */
        insertBegin(L, key, x);
        return;
      }
    
      temp = (struct list *) malloc(sizeof(*temp));
      /*     */
      if (NULL == temp) {
        fprintf(stderr, "     !\n");
        return;
      }
    
      *temp = **L;
      (*L)->next = temp;
      (*L)->key = key;
      (*L)->info = x;
    }
    
    /*     */
    void deleteNode(struct list **L, keyType key)
    { struct list *current = *L;
      struct list *save;
      if ((*L)->key == key) {  /*       */
        current = (*L)->next;
        free(*L);
        (*L) = current;
        return;
      }
    
      /*  ,     */
      while (current->next != NULL && current->next->key != key) {
        current = current->next;
      }
    
      if (NULL == current->next) {
        fprintf(stderr, ":      ! \n");
        return;
      }
      else {
        save = current->next;
        current->next = current->next->next;
        free(save);
      }
    }
    
    /*      */
    void print(struct list *L)
    { while (NULL != L) {
        printf("%d(%d) ", L->key, L->info);
        L = L->next;
      }
      printf("\n");
    }
    
    /*        */
    struct list* search(struct list *L, keyType key)
    { while (L != NULL) {
        if (L->key == key) return L;
        L = L->next;
      }
      return NULL;
    }
    
    int main(void) {
      struct list *L = NULL;
      int i, edata;
      insertBegin(&L, 0, 42);
      for (i = 1; i < 6; i++) {
        edata = rand() % 100;
        printf(" : %d(%d)\n", i, edata);
        insertBefore(&L, i, edata);
      }
    
      for (i = 6; i < 10; i++) {
        edata = rand() % 100;
        printf(" : %d(%d)\n", i, edata);
        insertAfter(&L, i, edata);
      }
    
      print(L);
      deleteNode(&L, 9); print(L);
      deleteNode(&L, 0); print(L);
      deleteNode(&L, 3); print(L);
      deleteNode(&L, 5); print(L);
      deleteNode(&L, 5);
      return 0;
    }

  2. #2
    Teh AimeR AimeR's Avatar
    Join Date: Dec:2006
    Location: //
    Posts: 4,307
    ( , , ...) .

    C#, , , "" ( "") .

    :
    Code:
    struct listitem {
      data info;
      struct listitem *next;
    };
    :
    Code:
    class listitem
    {
    data info;
    listitem next;
    }
    , ... - , 5 "", 6 , .
    Last edited by AimeR; 27th September 2011 at 09:32.
    ASUS X570-P|R7 5800X3D@NH-U14S|2X16G DDR4 3200 Corsair VENGEANCE LPX|Samsung 980 PRO 1TB|Radeon RX 7900 XTX 24G|AOC CQ32G1|Corsair RM750x|CM 693

  3. #3
    Registered User hotris's Avatar
    Join Date: Dec:2007
    Location: Sofia
    Posts: 167
    , . #, , .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |