Lo saque de un libro pero no compila, son listas enlazadas simples
No encuentro el error. Gracias!!
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 deleter(LISTNODEPTR *, char); int isEmpty(LISTNODEPTR); void printList(LISTNODEPTR); void instruccions(void); main() { LISTNODEPTR startPtr = NULL; int choice; char item; instruccions(); 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 (deleter(&startPtr, item)) { printf("%c deleted", item); printList(startPtr); } else { printf("%c not found\n", item); } } else { printf("List is empty\n"); } break; default: //si la opcion es invalida le vuelve a preguntar printf("invalid choice \n"); instruccions(); break; } printf("?"); scanf("%d", &choice); } printf("end of run\n"); return 0; } void instruccions(void) //mostrar opciones { 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"); } void insert(LISTNODEPTR *sPtr, char value) { LISTNODEPTR newPtr, previousPtr, currentPtr; newPtr =(LISTNODEPTR) malloc(sizeof(LISTNODE)); 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; *sPtr = newPtr; } else { previousPtr->nextPtr = newPtr; newPtr->nextPtr = currentPtr; } } else { printf("%c not inserted no memmory avalaible\n", value); } char deleter(LISTNODEPTR *sPtr, char value) { LISTNODEPTR tempPtr, previousPtr, currentPtr; if (value == (*sPtr)->data) { tempPtr = *sPtr; *sPtr = (*sPtr)->nextPtr; free(tempPtr); return value; } else { previousPtr = *sPtr; currentPtr = (*sPtr)->nextPtr; while (currentPtr != NULL&¤tPtr->data != value) { previousPtr = currentPtr; currentPtr = currentPtr->NextPtr; } if (currentPtr != NULL) { tempPtr = currentPtr; previousPtr->nextPtr = currentPtr->nextPtr; free (tempPtr); return value; } } return 0; } int isEmpty(LISTNODEPTR sPtr) { return sPtr == NULL; } void printList(LISTNODEPTR currentPtr) { if (currentPtr == NULL) printf("List is empty"); else { printf("the list is:"); while (currentPtr != NULL) { printf("%c --> ", currentPtr->data); currentPtr = currentPtr->nextPtr; } printf("NULL \n\n"); } }
Mod: modificado el código con etiquetas GeSHi,