Menú

Mostrar Mensajes

Esta sección te permite ver todos los mensajes escritos por este usuario. Ten en cuenta que sólo puedes ver los mensajes escritos en zonas a las que tienes acceso en este momento.

Mostrar Mensajes Menú

Temas - KAPPA47

#1
hola, podrian ayudarme a terminar unos programas de estructuras en c? aqui los codigos:
gracias por tomarse la molestia
PROGRAMA 1 (pila dinamica y debe mostrar la pila completa y la cima de la pila):

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX 10

struct nodo{
   char op[10];
   struct nodo *pnodo;
};

struct nodo *top=NULL;
int numNodos=0;

struct nodo *new(char *op)
{
   struct nodo *n;

   n=(struct nodo *) malloc(sizeof(struct nodo));
   printf("\nIngresa un nombre: ");
   gets(n->op);
   fflush(stdin);
   strcpy(n->op, op);
   n->pnodo=NULL;

   return n;
}

struct nodo *pop()
{
   struct nodo *pn;

   if(numNodos==0){
       printf("La pila esta vacia.\n");
       return NULL;
   }
   pn=top;
   top=top->pnodo;

   return pn;
}

struct nodo *push(char *op)
{
   struct nodo *n;

   if(numNodos==MAX){          //Pila llena
       printf("La pila esta llena.\n");
       return NULL;
   }
   n=new(op);
   if(numNodos>0)              //Pila tiene nodos
       n->pnodo=top;
   top=&* n->pnodo;

   return top;
}

int main()
{
   char *op;
   int menu=0;
   struct nodo *n;

   do
   {
       printf("******* PILA DINAMICA ********\n");
       printf("\nQue accion desea realizar?\n");
       printf("1. Insertar(Encolar)\n");
       printf("2. Eliminar(Desencolar)\n");
       printf("3. Mostrar\n");
       printf("4. Salir\n");
       printf("\nIngrese la opcion: ");
       scanf("%d", &menu);
       fflush(stdin);
       printf("\n");

       switch(menu){
           case 1:
               n=new(char *op);
               op=push(op, n);
               break;
           case 2:
               n=pop(char *oper);
               break;
           case 3:
               break;
           case 4:
               return 0;
               break;
           default:
               printf ("Opcion invalida\n");
       getchar();
       }
   }while(op!=4);
}


PROGRAMA 2 (cola doble)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10

struct colaDoble{
   struct cola *head;
   struct cola *tail;
   int nodos;
};

struct cola{
   int cola;
   struct cola *sig;
   struct cola *ant;
   struct cola *act;
};

int colaVacia(struct colaDoble *CC)
{
   return CC->nodos==0;          //return cola *head==cola *tail
}

int colaLlena(struct colaDoble *CC)
{
   return CC->nodos==MAX;
}

int insertarCC(struct colaDoble *CC, struct cola C)
{
   if(colaLlena(CC)==1){
       printf("La cola esta llena.\n");
       return 0;
   }
   if(colaVacia(CC)==1)
       CC->head=&C;
   else
       CC->tail.sig=C;
   CC.tail=&C;
   CC.nodos++;
   return 1;
}

struct cola* eliminarCC(struct colaDoble *CC)
{
   short cola *C;
   if(colaVacia==1){
       printf("La cola esta vacia.\n");
       return NULL;
   }=CC->tail;
   if(CC.nodos==1)
       CC->head=CC.tail=NULL;
   else
       CC->head=CC.head.sig;
   CC.nodos--;
   return C;
};

struct cola nuevo()
{
   struct cola n;
   printf("Introduce un numero: ");
   scanf("%d", &n dato):
   fflush(stdin);
   n.sig=NULL;
   return n;
};

int main()
{
   int op;
   do
   {
       printf("******* COLA DOBLE ********");
       printf("\nQue accion desea realizar? Ingrese la opcion: \n");
       scanf("%d", &op);
       fflush(stdin);
       printf("1.Insertar en inicio(Encolar por head)\n");
       printf("2.Insertar en final(Encolar por tail)\n");
       printf("3.Eliminar en inicio(Desencolar por head)\n");
       printf("4.Eliminar en final(Desencolar por tail)\n");
       printf("5.Listar\n");
       printf("6.Salir\n");
       switch(op)
       {
           case 1:
               break;
           case 2:
               break;
           case 3:
               break;
           case 4:
               break;
           case 5:
               break;
           case 6:
               return 0;
           default:
               printf ("Opcion invalida\n");
       getchar();
       }
   }while(op!=6);

   return 0;
}

PROGRAMA 3 (codigo de lista circular pasar a lista doblemente ligada circular conforme al menu)

#include<stdio.h>
#include<stdlib.h>

struct nodo{
char nombre[40];
struct nodo *next;
};

struct nodo *crearNodo(){
struct nodo *x;

x=(struct nodo *) malloc(sizeof(struct nodo));
printf("Nombre: ");
gets(x->nombre);
x->next=NULL;

return x;
}

struct nodo *buscar(struct nodo *lista, char *k){
if(lista==NULL) return NULL;

while(lista){
if(strcmp(lista->nombre, k)==0)
break;
lista=lista->next;
}
return lista;
}

struct nodo *borrar(struct nodo *lista, char *k){
struct nodo *ant, *act;

if(lista==NULL) return NULL;

ant=lista;
act=lista;
while(act!=NULL){
if(strcmp(act->nombre, k)==0){
break;
}
ant=act;
act=act->next;
}
if(act!=NULL){
if(act==lista)
lista=act->next;
ant->next=act->next;
}
return lista;
}

void listar(struct nodo *lista){
if(lista==NULL){
printf("Lista vacia\n");
return;
}
do
   {
printf("%s->",lista->nombre);
       lista=lista->next;
}while(lista!=NULL);
printf("\n");
}

struct nodo *insertar(struct nodo *head, struct nodo *x){
if(head!=NULL)
x->next=head;
head=x;

return head;
}

int main()
{
char nombre[40];
int op=0;
struct nodo *x;
struct nodo *head=NULL;

while(1){
system("cls");
       printf("******* LISTA CIRCULAR ********\n");
       printf("\nQue accion desea realizar?\n\n");
printf("1. Insertar\n");
printf("2. Eliminar\n");
printf("3. Buscar\n");
printf("4. Listar\n");
printf ("6. Siguiente\n");
printf ("7. Anterior\n");
printf ("8. Buscar anterior\n");
printf ("9. Buscar siguiente\n");
printf ("10. Borar todo\n");
printf("11. Salir\n\n");
printf("Ingrese la opcion: ");
scanf("%d", &op);
fflush(stdin);
printf("\n");

switch(op){
case 1:
x=crearNodo();
head=insertar(head,x);
break;
case 2:
               printf("Buscar: ");
gets(nombre);
x=borrar(head, nombre);
if(x!=NULL){
printf("Se elimino: %s\n", nombre);
//head= x;
}
else
printf("No se encuentra: %s\n", nombre);
system("pause");
break;
case 3:
               printf("Buscar: ");
gets(nombre);

if(buscar(head, nombre)==NULL)
printf("No se encuentra: %s\n", nombre);
else
printf("Se encuentro: %s\n", nombre);
system("pause");
break;
case 4:
listar(head);
system("pause");
break;
case 5:
return 0;
default:
printf("Opcion invalida\n");
}
}
}




Mod: Los códigos deben ir en etiquetas GeSHi, no escribas el título en mayúsculas