insertar un elemento

Isaac
11 de Noviembre del 2003
/*
PLANTEAMIENTO: Realizar un programa que inserte un nodo antes y despues que un nodo X.
AUTOR: y Oróstico Jiménez Isaac.
FECHA DE ELABORACIÓN: de bre del 2003.
ULTIMA MODIFICACIÓN: de bre del 2003.
OBSERVACIONES: Septimo programa de Estructura de Datos II.
*/

# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <alloc.h>
# include <iostream.h>

void insertar_inicio(void);
void insertar_final(void);
void insertar_antes(void);
void insertar_despues(void);
void extraer_inicio(void);
void extraer_final(void);
void visualizar(void);

struct alumno
{
char nombre[20], ap [20], am [20], edad [5], sex [10];
struct alumno *sig;
}*INICIO=NULL, *AUX=NULL, *FIN=NULL, *X=NULL;

main()
{
char opc, opcion;
do
{
menu: clrscr();
gotoxy(30, 8); cout <<"1.- INSERTAR";
gotoxy(30,10); cout <<"2.- EXTRAER";
gotoxy(30,12); cout <<"3.- VISUALIZAR";
gotoxy(30,14); cout <<"4.- SALIR";
opc=getch( );
switch (opc)
{
case '1':
do
{
clrscr (); gotoxy(30, 8); cout <<"SUBMENU DE INSERTAR:";
gotoxy(30, 6); cout <<"A.- INSERTAR AL INICIO.";
gotoxy(30, 7); cout <<"B.- INSERTAR AL FINAL.";
gotoxy(30, 8); cout <<"C.- INSERTAR ANTES DE UN NODO X.";
gotoxy(30, 9); cout <<"D.- INSERTAR DESPUES DE UN NODO X.";
gotoxy(30,10); cout <<"E.- REGRESAR AL MENU PRINCIPAL";
gotoxy(30,11); cout<<"ESCOGE UNA OPCION: ";
cin>> opcion;
switch (opcion)
{
case 'A': insertar_inicio( );
break;
case 'B': insertar_final( );
break;
case 'C': insertar_antes( );
break;
case 'D': insertar_despues( );
break;
}
} while (opcion!='E');
break;
case '2':
do
{
clrscr (); gotoxy(30, 8); cout <<"SUBMENU DE EXTRAER:";
gotoxy(30, 8); cout <<"A.- EXTRAER AL INICIO";
gotoxy(30, 9); cout <<"B.- EXTRAER AL FINAL";
gotoxy(30,10); cout <<"C.- REGRESAR AL MENU PRINCIPAL";
gotoxy(30,11); cout<<"ESCOGE UNA OPCION: ";
cin>> opcion;
switch (opcion)
{
case 'A': extraer_inicio( );
break;
case 'B': extraer_final( );
break;
}
} while (opcion!='C');
break;
case '3':
visualizar( );
}
} while (opc!='4');
}

void insertar_inicio (void)
{
AUX=(struct alumno *)malloc(sizeof(struct alumno));
clrscr(); gotoxy (10,9); cout<< "NOMBRE: ";
cin>> AUX->nombre;
gotoxy (10,10); cout<< "APELLIDO PATERNO: ";
cin>> AUX->ap;
gotoxy (10,11); cout<< "APELLIDO MATERNO: ";
cin>> AUX->am;
gotoxy (10,12); cout<< "EDAD: ";
cin>> AUX->edad;
gotoxy (10,13); cout<< "SEXO: ";
cin>> AUX->sex;

AUX->sig=NULL;
if (INICIO==NULL)
{
FIN=INICIO=AUX;
}
else
{
AUX->sig=INICIO;
INICIO=AUX;
}
}

void insertar_final (void)
{
AUX=(struct alumno *)malloc(sizeof(struct alumno));
clrscr(); gotoxy (10,9); cout<< "NOMBRE: ";
cin>> AUX->nombre;
gotoxy (10,10); cout<< "APELLIDO PATERNO: ";
cin>> AUX->ap;
gotoxy (10,11); cout<< "APELLIDO MATERNO: ";
cin>> AUX->am;
gotoxy (10,12); cout<< "EDAD: ";
cin>> AUX->edad;
gotoxy (10,13); cout<< "SEXO: ";
cin>> AUX->sex;

AUX->sig=NULL;
if (FIN==NULL)
{
FIN=INICIO=AUX;
}
else
{
FIN->sig=AUX;
FIN=AUX;
}
}

void insertar_antes (void)
{
char REF [20];
int BAND=0;
clrscr (); gotoxy (10, 8); cout<< "ANTES DE QUE NODO DESEA INSERTAR EL NUEVO ELEMENTO: ";
cin>> REF;
AUX=FIN;
BAND=1;
while ((AUX->sig!=REF)&&(BAND=1))
{
if (AUX->sig!=NULL)
{
FIN=AUX;
AUX=AUX->sig;
}
else
{
BAND=0;
}
}
if (BAND==1)
{
X=(struct alumno *)malloc(sizeof (struct alumno));
gotoxy (10,9); cout<< "NOMBRE: ";
cin>> X->nombre;
gotoxy (10,10); cout<< "APELLIDO PATERNO: ";
cin>> X->ap;
gotoxy (10,11); cout<< "APELLIDO MATERNO: ";
cin>> X->am;
gotoxy (10,12); cout<< "EDAD: ";
cin>> X->edad;
gotoxy (10,13); cout<< "SEXO: ";
cin>> X->sex;
if (INICIO==AUX)
{
X->sig=INICIO;
INICIO=X;
}
else
{
FIN->sig=X;
X->sig=AUX;
}
}
}


void insertar_despues (void)
{
cout<< "NO DISPONIBLE";
}

void extraer_inicio (void)
{
if (INICIO==NULL)return;
AUX=INICIO;
INICIO=INICIO->sig;
free(AUX);
}

void extraer_final (void)
{
if (INICIO->sig==NULL)
{
free(INICIO);
INICIO=NULL;
}
else
{
AUX=INICIO;
while (AUX->sig!=NULL)
{
FIN=AUX;
AUX=AUX->sig;
}
FIN->sig=NULL;
free(AUX);
}
}

void visualizar (void)
{
int fila=10;
if (INICIO==NULL) return;
clrscr();
AUX=INICIO;
while (AUX!=NULL)
{
gotoxy ( 1, 8); cout<< "NOMBRE:";
gotoxy (20, 8); cout<< "APELLIDO PATERNO: ";
gotoxy (40, 8); cout<< "APELLIDO MATERNO: ";
gotoxy (60, 8); cout<< "EDAD: ";
gotoxy (70, 8); cout<< "SEXO: ";
gotoxy ( 1,fila); cout<< AUX->nombre;
gotoxy (20,fila); cout<< AUX->ap;
gotoxy (40,fila); cout<< AUX->am;
gotoxy (60,fila); cout<< AUX->edad;
gotoxy (70,fila); cout<< AUX->sex;
fila++;
AUX=AUX->sig;
}
getch( );
}

necesito la asesoria en como insertar antes y despues de un nodo x, tengo mal esa parte del codigo, espero y alguien pueda asesorarme.