Código
#include <stdio.h> #include <stdlib.h> struct listNode { 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(); while (choice != 3) { switch (choice) { case 1: insert(&startPtr, item); printList(startPtr); break; case 2: if (!isEmpty(startPtr)) { if (delete(&startPtr, item)) { printList(startPtr); } else } else break; default: instructions(); break; } } return 0; } void instructions(void) { " 1 to insert an element into the list.\n" " 2 to delete an element from the list.\n" " 3 to end.\n"); } void insert(LISTNODEPTR *sPtr, char value) { LISTNODEPTR newPtr, previewPtr, currentPtr; if (newPtr != NULL) { newPtr->data = value; newPtr->nextPtr = NULL; previousPtr = NULL; currentPtr = *sPtr; while (currentPtr != NULL && value > currentPtr->data) { previousPtr = currentPtr; currentPtr = currentPtr->nextPtr; } if (previousPtr == NULL) { newPtr->nextPtr = *sPtr; //¿Para que es esta linea?, si sPtr se usa por primera ves con NULL, entonces aqui newPTr.nextPtr valdra NULL? *sPtr = newPtr; // Como es esta linea no entiendo } else { previousPtr->nextPtr = newPtr; // Para que es esto? newPtr->nextPtr = currentPtr; // y Esto para que se hace? una explicacion detallada por favor } } else } char delete(LISTNODEPTR *sPtr, char value) { LISTNODEPTR previousPtr, currentPtr, tempPtr; if(value == (*sPtr)->data) { // Porque se usan parentesis en *aPtr ? , no es valido sPtr.data? o sPtr->data? tempPtr = *sPtr *sPtr = (*sPtr)->nextPtr; //Parentesis para que? return value; } else { previousPtr = *sPtr; currentPtr : (*sPtr)->nextPtr; //Parentesis para que? while (currentPtr != NULL && currentPtr->data != value) { previousPtr = currentPtr; currentPtr = currentPtr->nextPtr; } if (currentPtr != NULL) { tempPtr = currentPtr; previousPtr->nextPtr = currentPtr->nextPtr; return value; } } return '\0'; } int isEmpty(LISTNODEPTR sPtr) { return sPtr == NULL; } void printList(LISTNODEPTR sPtr) { if (currentPtr == NULL) else { while (currentPtr != NULL) { printf("%c --> ", currentPtr->data); currentPtr = currentPtr->nextPtr; } printf("NULL\n\n"); } }
Agradecido por su valiosa explicacion
Un saludo