scusa l'ultimo programma che ho scritto era errato, avevo dimenticato #include<string.h> e che il confronto tra stringhe si fa con strcmp, però comunque il problema rimane.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct listNode{
char *stringa;
struct listNode *nexPtr;
};
typedef struct listNode LISTNODE;
typedef LISTNODE *LISTNODEPTR;
void insert(LISTNODEPTR *, char *);
char deletel(LISTNODEPTR *, char *);
int isEmpty(LISTNODEPTR);
void printList(LISTNODEPTR);
void instructions(void);
main()
{
LISTNODEPTR startPtr = NULL;
int choice;
char item[30];
instructions();
printf("? ");
scanf("%d", &choice);
while(choice != 3){
switch(choice){
case 1:
printf("carattere\n");
scanf("\n%s", item);
insert(&startPtr, item);
printList(startPtr);
break;
case 2:
if(!isEmpty(startPtr)){
printf("carattere cancellare\n");
scanf("\n%s", item);
if(deletel(&startPtr, item)){
printf("%s cancellato\n", item);
printList(startPtr);
}
else
printf("%s nn trovato\n", item);
}
else
printf("la lista è vuota\n");
break;
default:
printf("Scelta nn valida\n");
instructions();
break;
}
printf("? ");
scanf("%d", &choice);
}
printf("Fine esecuzione.\n");
system("PAUSE");
return 0;
}
/* definizione funzioni */
void instructions(void)
{
printf("Inserire:\n"
"1 - per inserire un elemento;\n"
"2 - per cancellare un elemento;\n"
"3 - per terminare.\n\n");
}
void insert(LISTNODEPTR *sPtr, char *value)
{
LISTNODEPTR newPtr, previousPtr, currentPtr;
newPtr =(LISTNODEPTR)malloc(sizeof(LISTNODE));
if(newPtr != NULL){
newPtr->stringa = value;
newPtr->nexPtr = NULL;
previousPtr = NULL;
currentPtr = *sPtr;
while(currentPtr != NULL && strcmp(value, currentPtr->stringa) > 0){
previousPtr = currentPtr;
currentPtr = currentPtr->nexPtr;
}
if(previousPtr == NULL){
newPtr->nexPtr = *sPtr;
*sPtr = newPtr;
}
else{
previousPtr->nexPtr = newPtr;
newPtr->nexPtr = currentPtr;
}
}
else
printf("%s nn inserito. Memoria nn disponibile\n");
}
char deletel(LISTNODEPTR *sPtr, char *value)
{
LISTNODEPTR previousPtr, currentPtr, tempPtr;
if(value == (*sPtr)->stringa){
tempPtr = *sPtr;
*sPtr = (*sPtr)->nexPtr;
free(tempPtr);
return *value;
}
else{
previousPtr = *sPtr;
currentPtr = (*sPtr)->nexPtr;
while(currentPtr != NULL && strcmp(currentPtr->stringa, value) != 0){
previousPtr = currentPtr;
currentPtr = currentPtr->nexPtr;
}
if(currentPtr != NULL){
tempPtr = currentPtr;
previousPtr->nexPtr = currentPtr->nexPtr;
free(tempPtr);
return *value;
}
}
return '\0';
}
int isEmpty(LISTNODEPTR sPtr)
{
return sPtr == NULL;
}
void printList(LISTNODEPTR currentPtr)
{
if(currentPtr == NULL)
printf("la lista e' vuota\n");
else{
printf("la lista e':\n");
while(currentPtr != NULL){
printf("%s --> ", currentPtr->stringa);
currentPtr = currentPtr->nexPtr;
}
printf("NULL\n\n");
}
}