Seguimos buscando a Arshak. Ayudanos compartiendo!
Encuesta no oficial de docentes
Resultados de la encuesta no oficial de docentes
Probaste el SIGA Helper?

Donar $100 Donar $200 Donar $500 Donar mensualmente


Enviar respuesta 
 
Calificación:
  • 0 votos - 0 Media
  • 1
  • 2
  • 3
  • 4
  • 5
Buscar en el tema
[Aporte] Lista doblemente enlazada
Autor Mensaje
anonimail Sin conexión
Campeon del cubo Rubik

****

Ing. Electrónica
Facultad Regional Buenos Aires

Mensajes: 111
Agradecimientos dados: 47
Agradecimientos: 98 en 22 posts
Registro en: Feb 2012
Mensaje: #1
[Aporte] Lista doblemente enlazada Dudas y recomendaciones Informática I (Electrónica)
Es un programa que hice hace tiempo y evoluciono con los meses, quizás alguno le sirva como referencia.



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <pthread.h>

#define PORT 3550
#define MAXIMO 100
#define BACKLOG 2

typedef struct t_personas
{
char nombre[25];
int edad;
int ID;

struct t_personas *sig;
struct t_personas *ant;
}PERSONA;

typedef struct t_info
{
PERSONA *inicio;
PERSONA *final;

int personas;
}INFO;

int cantidad=0;

void Agregar (INFO *);
void Modificar (INFO *);
void Borrar (INFO *);
void Mostrar (INFO *);
void Abrir (INFO *);
void Abrir_binario (INFO *);
void Abrir_socket (INFO *);
void Guardar (INFO *);
void Guardar_binario (INFO *);
void Guardar_socket (INFO *);
void Limpiar (INFO *);
void Ordenar (INFO *);
void BorrarTodo (INFO *);

void EspaciosID (int);
void EspaciosNombre (int);

int main()
{
system ("clear");

int x=0;
int opcion=-1;
INFO Lista;

Lista.inicio = NULL;
Lista.final = NULL;
Lista.personas = 0;

while (opcion!=0)
{
printf ("LISTA\n\n1-Agregar persona\n2-Modificar persona\n3-Borrar persona\n4-Mostrar personas\n5-Abrir/Recibir Archivo\n6-Guardar/Enviar Archivo\n7-Ordenar alfabeticamente\n8-Limpiar Nombres repetidos\n9-Borrar todo\n0-Salir\n\nOpcion: ");
scanf ("%d", &opcion);
system ("clear");
switch (opcion)
{
case 1:
printf ("AGREGAR PERSONA\n\n");
Agregar (&Lista);
break;
case 2:
printf ("MODIFICAR PERSONA\n\n");
Modificar (&Lista);
break;
case 3:
printf ("BORRAR PERSONA\n\n");
Borrar (&Lista);
break;
case 4:
printf ("PERSONAS\n\n");
Mostrar (&Lista);
break;
case 5:
while (x!=4)
{
printf ("ABRIR ARCHIVO\n\n");
printf ("1-Abrir Archivo\n2-Abrir binario\n3-Recibir Archivo\n4-Salir\n\nOpcion: ");
scanf ("%d", &x);
switch (x)
{
case 1:
system ("clear");
printf ("Abrir Archivo\n\n");
Abrir (&Lista);
break;
case 2:
system ("clear");
printf ("Abrir binario\n\n");
Abrir_binario (&Lista);
break;
case 3:
system ("clear");
printf ("Recibir Archivo\n\n");
Abrir_socket (&Lista);
break;
case 4:
system ("clear");
break;
default:
system ("clear");
printf ("ERROR: Opcion inexistente\n\n");
}
}
x=0;
break;
case 6:
while (x!=4)
{
printf ("GUARDAR ARCHIVO\n\n");
printf ("1-Guardar Archivo\n2-Guardar en binario\n3-Enviar Archivo\n4-Salir\n\nOpcion: ");
scanf ("%d", &x);
switch (x)
{
case 1:
system ("clear");
printf ("Guardar Archivo\n\n");
Guardar (&Lista);
break;
case 2:
system ("clear");
printf ("Guardar en binario\n\n");
Guardar_binario (&Lista);
break;
case 3:
system ("clear");
printf ("Enviar Archivo\n\n");
Guardar_socket (&Lista);
break;
case 4:
system ("clear");
break;
default:
system ("clear");
printf ("ERROR: Opcion inexistente\n\n");
}
}
x=0;
break;
case 7:
printf ("ORDENAR PERSONAS");
Ordenar(&Lista);
break;
case 8:
printf ("LIMPIAR PERSONAS");
Limpiar (&Lista);
break;
case 9:
while (x != 2)
{
printf ("¿Desea borrar la lista completa?\n\n1-Si\n2-No\n\nOpcion: ");
scanf ("%d", &x);
switch (x)
{
case 1:
system ("clear");
BorrarTodo (&Lista);
printf ("MENSAJE: Lista borrada\n\n");
x=2;
break;
case 2:
system ("clear");
break;
default:
printf ("ERROR: Opcion inexistente\n\n");
}
}
x=0;
break;
case 0:
system ("clear");
printf ("PROGRAMA FINALIZADO\n\n");
break;
default:
system ("clear");
printf ("ERROR: Opcion inexistente\n\n");
}
}
}

void Agregar (INFO *Lista)
{
PERSONA *aux;
PERSONA *nuevo;

nuevo = malloc (sizeof(PERSONA));

if (nuevo != NULL)
{
Lista->personas++;
cantidad++;
nuevo->ID = cantidad;

printf ("Nombre: ");
scanf ("%s", nuevo->nombre);
printf ("Edad: ");
scanf ("%d", &nuevo->edad);

if (Lista->inicio == NULL && Lista->final == NULL)
{
Lista->inicio = nuevo;
nuevo->sig = NULL;

Lista->final = nuevo;
nuevo->ant = NULL;
}
else
{
aux = Lista->final;

aux->sig = nuevo;
nuevo->ant = aux;

Lista->final = nuevo;
nuevo->sig = NULL;
}
}
else
{
printf ("ERROR: Memoria insuficiente\n\n");
}
system ("clear");
}

void Modificar (INFO *Lista)
{
Mostrar (Lista);

PERSONA *aux;
int x=0;
int flag=0;

aux = Lista->inicio;

printf ("Ingrese ID: ");
scanf ("%d", &x);

while (aux != NULL)
{
system ("clear");
if (x == aux->ID)
{
printf ("RESULTADO\n\n");
printf ("ID: %d\n", aux->ID);

printf ("Nombre: %s\n", aux->nombre);
printf ("Edad: %d\n\n", aux->edad);

printf ("Nombre: ");
scanf ("%s", aux->nombre);
printf ("Edad: ");
scanf ("%d", &aux->edad);
flag=1;
}
aux = aux->sig;
}

system ("clear");

if (flag == 0)
{
printf ("MENSAJE: Persona no encontrada\n\n");
}
}

void Borrar (INFO *Lista)
{
Mostrar (Lista);

PERSONA *aux;
int x=0;
int flag=0;
int opcion=0;

aux = Lista->inicio;

printf ("Ingrese ID: ");
scanf ("%d", &x);

while (aux != NULL)
{
system ("clear");
if (x == aux->ID)
{
printf ("RESULTADO\n\n");
printf ("ID: %d\n", aux->ID);
printf ("Nombre: %s\n", aux->nombre);
printf ("Edad: %d\n\n", aux->edad);

while (opcion != 2 && opcion != 1)
{
printf ("¿Desea borrarlo definitivamente?\n\n1-Si\n2-No\n\n");
printf ("Opcion: ");
scanf ("%d", &opcion);

switch (opcion)
{
case 1:
if (aux->ant == NULL && aux->sig == NULL)
{
Lista->inicio = NULL;
Lista->final = NULL;
}
if (aux->ant != NULL && aux->sig != NULL)
{
aux->ant->sig = aux->sig;
aux->sig->ant = aux->ant;
}
if (aux->ant == NULL && aux->sig != NULL)
{
Lista->inicio = aux->sig;
aux->sig->ant = NULL;
}
if (aux->ant != NULL && aux->sig == NULL)
{
Lista->final = aux->ant;
aux->ant->sig = NULL;
}
free (aux);
Lista->personas--;
break;
default:
printf ("ERROR: Opcion inexistente");
}
}
flag = 1;
}
aux = aux->sig;
}

system ("clear");

if (flag == 0)
{
printf ("MENSAJE: Persona no encontrada\n\n");
}
}

void Mostrar (INFO *Lista)
{
char caracteres[50];

if (Lista->inicio != NULL)
{
PERSONA *aux;

aux = Lista->inicio;

printf ("ID Nombre Edad");
printf ("\n");
while (aux != NULL)
{
printf ("\n");
printf ("%d", aux->ID);
sprintf (caracteres, "%d", aux->ID);
EspaciosID (strlen(caracteres));

printf ("%s", aux->nombre);
strcpy (caracteres, aux->nombre);
EspaciosNombre (strlen(caracteres));

printf ("%d", aux->edad);

aux = aux->sig;
}
printf ("\n\nPersonas: %d", Lista->personas);
}
else
{
printf ("MENSAJE: no hay personas");
}
printf ("\n\n");
}

void Abrir (INFO *Lista)
{
char nombre[25];
char texto[80];
int contador=0;

PERSONA *nuevo;
FILE *file;

BorrarTodo(Lista);

nuevo = malloc (sizeof(PERSONA));
Lista->personas = 0;

printf ("Nombre del archivo: ");
scanf ("%s", nombre);

file = fopen (nombre, "r");

Lista->inicio = nuevo;
while ( fgets(texto ,sizeof(texto), file) != NULL)
{
nuevo->ID = atoi (strtok (texto, ","));
strcpy (nuevo->nombre, strtok (NULL,","));
nuevo->edad = atoi (strtok (NULL, "\n"));

nuevo->sig = malloc (sizeof(PERSONA));
nuevo->sig->ant = nuevo;
nuevo = nuevo->sig;

Lista->personas++;
}
Lista->final = nuevo->ant;
nuevo->ant->sig = NULL;
free (nuevo);

cantidad = Lista->personas;

fclose (file);

printf ("\nARCHIVO ABIERTO\n\n");
}

void Abrir_binario(INFO *Lista)
{
char nombre[20];
PERSONA *nuevo;
FILE* file;

printf ("Nombre del archivo: ");
scanf ("%s", nombre);

file = fopen(nombre, "rb");
nuevo = malloc(sizeof(PERSONA));

Lista->inicio = nuevo;
while (fread (nuevo->nombre, sizeof(nuevo->nombre), 1, file) != 0)
{
fread (&nuevo->edad, sizeof(nuevo->edad), 1, file);
fread (&nuevo->ID, sizeof(nuevo->ID), 1, file);

nuevo->sig = malloc (sizeof(PERSONA));
nuevo->sig->ant = nuevo;
nuevo = nuevo->sig;

Lista->personas++;
}
Lista->final = nuevo->ant;
nuevo->ant->sig = NULL;
free (nuevo);

cantidad = Lista->personas;
fclose (file);

printf ("\nARCHIVO ABIERTO\n\n");
}

void Abrir_socket (INFO *Lista)
{
PERSONA *nuevo;

int x=0;
int cant=0;
int fd;
char ip[25];
char buf[100];
int buffer=0;

struct sockaddr_in server;
struct hostent *he;

printf ("Ingrese IP: ");
scanf ("%s", ip);

he = gethostbyname(ip);

server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(server.sin_zero), 8);

fd = socket (AF_INET, SOCK_STREAM, 0);
printf ("Esperando archivo...\n");
if (connect (fd, (struct sockaddr *)&server, sizeof(struct sockaddr)) != -1)
{
BorrarTodo(Lista);

nuevo = malloc (sizeof(PERSONA));
Lista->personas = 0;

Lista->inicio = nuevo;
while (buffer!=1)
{
recv(fd, &buf[x], 1, 0);
if (buf[x]=='\0')
{
buffer=1;
}
if (buf[x]=='\n')
{
buf[x]='\0';
printf ("Buffer: %s\n", buf);

nuevo->ID = atoi(strtok (buf, ","));
strcpy (nuevo->nombre, strtok (NULL,","));
nuevo->edad = atoi (strtok (NULL, "\n"));

nuevo->sig = malloc (sizeof(PERSONA));
nuevo->sig->ant = nuevo;
nuevo = nuevo->sig;

Lista->personas++;

memset(buf, '\0', 100);
x=-1;
}
x++;
}
Lista->final = nuevo->ant;
nuevo->ant->sig = NULL;
free (nuevo);

cantidad = Lista->personas;
close(fd);

printf ("\nARCHIVO RECIBIDO\n\n");

//El buf recibiria toda la informacion en un buf y serian demasiados caracteres.
//Pero lo dejo para que vean como lo solucione arriba.
/*recv(fd,buf,MAXIMO,0);
buffer = buf;

for (x=0; buf[x]!='\0'; x++)
{
if (buf[x]=='\n')
{
cant++;
}
}

for (x=0; x<cant; x++)
{
if (x==0)
{
nuevo->ID = atoi(strtok (buffer, ","));
}
else
{
nuevo->ID = atoi(strtok (NULL, ","));
}
strcpy (nuevo->nombre, strtok (NULL,","));
nuevo->edad = atoi (strtok (NULL, "\n"));

nuevo->sig = malloc (sizeof(PERSONA));
nuevo->sig->ant = nuevo;
nuevo = nuevo->sig;

Lista->personas++;
}
Lista->final = nuevo->ant;
nuevo->ant->sig = NULL;
free (nuevo);

cantidad = Lista->personas;

printf ("\nARCHIVO RECIBIDO\n\n");*/
}
else
{
printf ("ERROR: No existe el servidor con la ip %s o no esta conectado\n\n", ip);
}
close(fd);
}

void Guardar (INFO *Lista)
{
char nombre[20];
FILE *file;

printf ("Nombre del archivo: ");
scanf ("%s", nombre);

file = fopen (nombre, "w");

if (Lista->inicio != NULL)
{
PERSONA *aux;

aux = Lista->inicio;

while (aux != NULL)
{
fprintf (file,"%d,", aux->ID);
fprintf (file,"%s,", aux->nombre);
fprintf (file,"%d\n", aux->edad);
aux = aux->sig;
}
system ("clear");
printf ("Personas guardadas: %d", Lista->personas);
printf ("\nARCHIVO GUARDADO\n\n");
}
else
{
printf ("MENSAJE: no hay personas para guardar");
}
printf ("\n\n");
fclose (file);
}

void Guardar_binario (INFO *Lista)
{
char nombre[20];
FILE *file;
PERSONA *aux;

printf ("Nombre del archivo: ");
scanf ("%s", nombre);

file = fopen (nombre, "wb");

for (aux = Lista->inicio; aux!=NULL; aux=aux->sig)
{
fwrite (aux->nombre, sizeof(aux->nombre), 1, file);
fwrite (&aux->edad, sizeof(aux->edad), 1, file);
fwrite (&aux->ID, sizeof(aux->ID), 1, file);
}
fclose(file);

system ("clear");
printf ("ARCHIVO GUARDADO\n\n");
}

void Guardar_socket (INFO *Lista)
{
int fd, fd2;
int size;
char linea[100];
char nombre[50];

struct sockaddr_in server;
struct sockaddr_in cliente;

printf ("Archivo: ");
scanf ("%s", nombre);

server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = INADDR_ANY;
bzero(&(server.sin_zero),8);

fd = socket(AF_INET, SOCK_STREAM, 0);
bind (fd, (struct sockaddr *)&server, sizeof(struct sockaddr));
listen (fd, BACKLOG);

FILE *file;
file = fopen (nombre, "r");
if (file != NULL)
{
printf ("MENSAJE: Archivo enviado\n\n");
pid_t pid = fork();
if (pid==0)
{
size = sizeof(struct sockaddr);
//printf ("Esperando conexion...\n");
fd2 = accept(fd, (struct sockaddr *)&cliente, &size);
//printf ("Conexion exitosa\n");
//printf ("\nfd2: %d\n", fd2);
while (fgets(linea, sizeof(linea), file) != NULL)
{
send(fd2, linea, strlen(linea), 0);
}
//printf ("\nARCHIVO ENVIADO\n\n");
fclose(file);
close(fd);
close(fd2);
exit(0);
}
}
else
{
system("clear");
printf ("ERROR: No existe el archivo %s\n\n", nombre);
}

close(fd);
}

void Ordenar (INFO *Lista)
{
PERSONA *aux1;
PERSONA *aux2;
PERSONA aux;

aux1 = Lista->inicio;

while (aux1 != NULL)
{
aux2 = aux1->sig;
while (aux2 != NULL)
{
if ((strcmp (aux1->nombre, aux2->nombre)) > 0)
{
strcpy (aux.nombre, aux1->nombre);
strcpy (aux1->nombre, aux2->nombre);
strcpy (aux2->nombre, aux.nombre);

aux.edad = aux1->edad;
aux1->edad = aux2->edad;
aux2->edad = aux.edad;
}
aux2 = aux2->sig;
}
aux1 = aux1->sig;
}
printf ("\n\n");
Mostrar (Lista);
printf ("LISTA ORDENADA\n\n");
}

void Limpiar (INFO *Lista)
{
PERSONA *aux1;
PERSONA *aux2;
PERSONA *aux3;

aux1 = Lista->inicio;

while (aux1 != NULL)
{
aux2 = aux1;
while (aux2->sig != NULL)
{
if (strcmp (aux1->nombre, aux2->sig->nombre) == 0)
{
Lista->personas--;
if (aux2->sig->sig != NULL)
{
aux3 = aux2->sig;
aux2->sig = aux2->sig->sig;
aux2->sig->ant = aux2;
free(aux3);
}
if (aux2->sig->sig == NULL)
{
free(aux2->sig);
aux2->sig = NULL;
Lista->final = aux2;
break;
}
}
aux2 = aux2->sig;
}
aux1 = aux1->sig;
}

printf ("\nLISTA FILTRADA\n\n");
}

void BorrarTodo (INFO *Lista)
{
PERSONA *aux;

aux = Lista->inicio;
while (aux != NULL)
{
free (aux);
aux = aux->sig;
}
Lista->inicio = NULL;
Lista->final = NULL;
Lista->personas = 0;
cantidad = 0;
}

void EspaciosID (int x)
{
int largo = 5;
largo -= x;

int y=0;
while (y<largo)
{
printf (" ");
y++;
}
}

void EspaciosNombre (int x)
{
int largo = 15;
largo -= x;

int y=0;
while (y<largo)
{
printf (" ");
y++;
}
}


10-02-2013 19:23
Encuentra todos sus mensajes Agregar agradecimiento Cita este mensaje en tu respuesta
[-] anonimail recibio 1 Gracias por este post
Feer (10-02-2013)
Buscar en el tema
Enviar respuesta 




Usuario(s) navegando en este tema: 1 invitado(s)