este es para insertar un dato al principio de la lista ( programandos en Codeblock)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct lista{
struct lista *frenteizquierdo;
int valor;
struct lista *frentederecho;
}*raiz=NULL;
void Insertaprincipio(struct lista **raiz, int dato);
void eliminaX(struct lista **raiz,int x);
void recorre(struct lista *raiz);
int menu(void);
int main(void)
{
char opc;
int dato,x;
do
{
opc=menu();
switch(opc)
{
case '1':
printf("introduce un dato entero: ");
scanf("%d",&dato);
Insertaprincipio(&raiz,dato);
getchar();
break;
case '2':
printf("que numero quieres borrar ");
scanf("%d",&x);
eliminaX(&raiz,x);
getchar();
break;
case '3':
printf("los datos son: ");
recorre(raiz);
getchar();
break;
}
}while(opc!='4');
return 0;
}
int menu(void)
{
int opc;
printf("\n 1.insertar dato ");
printf("\n 2.quita el dato");
printf("\n 3.recorre datos de la lista");
printf("\n 4.salir");
printf("\n\n Selecciona una opción: ");
opc=getchar();
return opc;
}
void Insertaprincipio(struct lista **raiz, int dato)
{
struct lista *nodo,*p;
nodo=(struct lista *)malloc(sizeof(struct lista));
nodo->valor=dato;
if(*raiz==NULL)
{
nodo->frentederecho=NULL;
nodo->frenteizquierdo=NULL;
*raiz=nodo;
}
else
{
p=*raiz;
nodo->frentederecho=p;
p->frenteizquierdo=nodo;
nodo->frenteizquierdo=NULL;
p=nodo;
*raiz=p;
}
}
void recorre(struct lista *raiz)
{
if(raiz==NULL)
printf("lista vacia...");
else
{
if((raiz->frentederecho)!=NULL)
recorre(raiz->frentederecho);
if(raiz!=NULL)
printf("%d, ",raiz->valor);
}
}
void eliminaX(struct lista **raiz,int x)
{
struct lista *p,*q,*z,*r;
int band;
band=0;
if(*raiz==NULL)
printf("la lista esta vacia");
else
{
p=*raiz;
q=p;
z=q;
if(q->frentederecho==NULL)
{
if(q->valor==x)
{
z->frentederecho=NULL;
free(q);
}
else
{
printf("el elemento esta vacio");
}
}
else
{
while ((q->frentederecho!=NULL)&&(band==0))
{
//z=q;
if(q->valor==x)
band=1;
z=q;
q=q->frentederecho;
}
if((q->frentederecho==NULL)&&(band==0))
{
if(q->valor==x)
{
z->frentederecho=NULL;
free(q);
}
else
if(band==1)
{
if(q==z)
{
q=q->frentederecho;
q->frenteizquierdo=NULL;
free(z);
}
else
{
q->frenteizquierdo=z->frenteizquierdo;
r=z->frenteizquierdo;
r->frentederecho=q;
free(z);
}
}
else
{
printf("el elemento no esta en la lista");
}
}
}
}
}