PDA

View Full Version : [C]aiutino liste


D4rkAng3l
24-01-2006, 23:49
Ciao,
mi date una mano a capire questo codice?l'anno scorso avevo capito le liste grazie al libro della McRownhill che festisce le funzioni in modo completamente diverso rispetto al Deitel...questo è un codice della deitel...non sono mai riuscito a capirlo bene e stò rosicando:

Le cose che non capisco sono quei maledetti typedef...lo sò che sono una strunzata ma in questo caso che fanno?

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


/* 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");
}
}

Guts
25-01-2006, 14:25
anche il mio libro usa quel typedef, serve a creare il tipo LISTNODEPTR come puntatore a LISTNODE. nn ha un'utilità particolare ti evita solo qualche asterisco, imho è inutile, ma magari ha qualche funzione che ancora nn ho capito

kk3z
25-01-2006, 14:28
Non c'era bisogno di postare tutto il codice, quel typedef definisce un puntatore alla struttura, è la stessa cosa di
typedef struct listNode *LISTNODEPTR;

Tutto può essere ridotto a
typedef struct listNode { /* self-referential structure */
char data;
struct listNode *nextPtr;
} LISTNODE, *LISTNODEPTR;