Visualizzazione dei risultati da 1 a 3 su 3

Discussione: [C] Liste e typedef

  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2004
    Messaggi
    643

    [C] problemino liste

    Io ho un tipo di dato chiaato listNode che contiene un dato di tipo char e il puntatore al prossimo nodo della lista di tipo struct listNode

    typedef struct listNode LISTNODE; dovrebbe semplicemente rinominarmi il tipo struct listNode in LISTNODE tutto maiuscole per accorciare il nome ed evitare di dover scrivere struct listNode tutte le volte?

    typedef LISTNODE *LISTNODEPTR; questo che cavolo fà invece?

    Grazie
    Andrea

    codice:
    /* Operating and maintaining a list */
    #include <stdio.h>
    #include <stdlib.h>
    
    struct listNode {  /* self-referential structure */
       char data;
       struct listNode *nextPtr;
    };
    
    typedef struct listNode LISTNODE;
    typedef LISTNODE *LISTNODEPTR;
    
    void insert(LISTNODEPTR *, char);
    char Delete(LISTNODEPTR *, char);
    int isEmpty(LISTNODEPTR);
    void printList(LISTNODEPTR);
    void instructions(void);
    
    main()
    {
       LISTNODEPTR startPtr = NULL;
       int choice;
       char item;
    
       instructions();  /* display the menu */
       printf("? ");
       scanf("%d", &choice);
    
       while (choice != 3) {
    
          switch (choice) {
             case 1:
                printf("Enter a character: ");
                scanf("\n%c", &item);
                insert(&startPtr, item);
                printList(startPtr);
                break;
             case 2:
                if (!isEmpty(startPtr)) {
                   printf("Enter character to be deleted: ");
                   scanf("\n%c", &item);
    
                   if (Delete(&startPtr, item)) {
                      printf("%c deleted.\n", item);
                      printList(startPtr);
                   }
                   else
                      printf("%c not found.\n\n", item);
                }
                else
                   printf("List is empty.\n\n");
    
                break;
             default:
                printf("Invalid choice.\n\n");
                instructions();
                break;
          }
    
          printf("? ");
          scanf("%d", &choice);
       }
    
       printf("End of run.\n");
       return 0;
    }
    
    /* Print the instructions */
    void instructions(void)
    {
       printf("Enter your choice:\n"
              "   1 to insert an element into the list.\n"
              "   2 to delete an element from the list.\n"
              "   3 to end.\n");
    }
    
    /* Insert a new value into the list in sorted order */
    void insert(LISTNODEPTR *sPtr, char value)
    {
       LISTNODEPTR newPtr, previousPtr, currentPtr;
      
       newPtr = (LISTNODEPTR)malloc(sizeof(LISTNODE)); /* ho aggiunto un cast */
        
       if (newPtr != NULL) {    /* is space available */
          newPtr->data = value;
          newPtr->nextPtr = NULL;
    
          previousPtr = NULL;
          currentPtr = *sPtr;
    
          while (currentPtr != NULL && value > currentPtr->data) {
             previousPtr = currentPtr;          /* walk to ...   */
             currentPtr = currentPtr->nextPtr;  /* ... next node */
          }
    
          if (previousPtr == NULL) {
             newPtr->nextPtr = *sPtr;
             *sPtr = newPtr;
          }
          else {
             previousPtr->nextPtr = newPtr;
             newPtr->nextPtr = currentPtr;
          }
       }
       else
          printf("%c not inserted. No memory available.\n", value);
    }
    
    /* Delete a list element */
    char Delete(LISTNODEPTR *sPtr, char value)
    {
       LISTNODEPTR previousPtr, currentPtr, tempPtr;
    
       if (value == (*sPtr)->data) {
          tempPtr = *sPtr;
          *sPtr = (*sPtr)->nextPtr;  
          free(tempPtr);             
          return value;
       }
       else {
          previousPtr = *sPtr;
          currentPtr = (*sPtr)->nextPtr;
    
          while (currentPtr != NULL && currentPtr->data != value) {
             previousPtr = currentPtr;          /* walk to ...   */
             currentPtr = currentPtr->nextPtr;  /* ... next node */
          }
    
          if (currentPtr != NULL) {
             tempPtr = currentPtr;
             previousPtr->nextPtr = currentPtr->nextPtr;
             free(tempPtr);
             return value;
          }                                                        
       }
    
       return '\0';
    }
    
    /* Return 1 if the list is empty, 0 otherwise */
    int isEmpty(LISTNODEPTR sPtr)
    {
       return sPtr == NULL;
    }
    
    /* Print the list */
    void printList(LISTNODEPTR currentPtr)
    {
       if (currentPtr == NULL)
          printf("List is empty.\n\n");
       else {
          printf("The list is:\n");
    
          while (currentPtr != NULL) {
             printf("%c --> ", currentPtr->data);
             currentPtr = currentPtr->nextPtr;
          }
    
          printf("NULL\n\n");
       }
    }

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,466
    E' molto semplice ...

    La typedef definisce un nuovo tipo di dato utilizzato nel programma.
    Nel primo caso, il nuovo tipo

    LISTNODE

    corrispondera' a struct listNode.

    Nel secondo caso

    LISTNODEPTR

    corrispondera' a un puntatore a LISTNODE, ovvero, per quanto detto prima, a un puntatore a struct listNode. Quindi, invece di scrivere

    struct listNode *pnode;

    potrai scrivere

    LISTNODEPTR pnode;

  3. #3
    Utente di HTML.it
    Registrato dal
    Jun 2004
    Messaggi
    643
    grazie ho capito...non sò perhcè ma mi ci intrippo sempre su questa definizione e ho sempre preferito come lo spiega il mcgrow hill..sono stupido?

    cmq ho postato un altro 3d sulle liste?potresti dargòli un'occhaita?me la stò a fa addosso

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.