Inserción y eliminación de datos en listas
Estructuras de datos
Inserción y eliminación de datos en listas
Degante López Rodrigo
Código fuente
#include<stdio.h>#include<stdlib.h>
struct listNode {
char data;
struct listNode *nextPtr;
};
typedef struct listNode LISTNODE;
typedef LISTNODE *LISTNODEPTR;
void insert(LISTNODEPTR *,char);
chardelete(LISTNODEPTR *,char);
int isEmpty(LISTNODEPTR);
void printList(LISTNODEPTR);
void instructions(void);
/*-------------------------------*/
main()
{
LISTNODEPTR startPtr = NULL;
int choice;
charitem;
clrscr();
instructions();
printf("? ");
scanf("%d", &choice);
while (choice !=3) {
switch(choice) {
case 1:
printf("Ingrese un caracter: ");
scanf("\n%c",&item);
insert(&startPtr, item);
printList(startPtr);
break;
case 2:
if(!isEmpty(startPtr)) {
printf("inserte el caracter a borrar");
scanf("\n%c", &item);if(delete(&startPtr, item)) {
printf("%c Eliminado.\n", item);
printList(startPtr);
}
else
printf("%c no encontrado.\n\n", item);
}
elseprintf("Lista vacia.\n\n");
break;
default:
printf("Opcion no valida. \n\n");
instructions();
break;
}
printf("? ");
scanf("%d", &choice);
}
printf("Fin.\n");
return0;
}
/*-------------------------------*/
void instructions(void)
{
printf("Ingrese su opcion:\n1.Insertar elemento\n2.Eliminar elemento\n3.Salir\n");
}
void insert(LISTNODEPTR *sPtr, charvalue)
{
LISTNODEPTR newPtr, previousPtr, currentPtr;
newPtr = 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){...
Regístrate para leer el documento completo.