Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: KAPPA47 en 27 Octubre 2016, 01:00 am



Título: ayuda en programas de estructuras de datos en c
Publicado por: KAPPA47 en 27 Octubre 2016, 01:00 am
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):

Código
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. #define MAX 10
  6.  
  7. struct nodo{
  8.    char op[10];
  9.    struct nodo *pnodo;
  10. };
  11.  
  12. struct nodo *top=NULL;
  13. int numNodos=0;
  14.  
  15. struct nodo *new(char *op)
  16. {
  17.    struct nodo *n;
  18.  
  19.    n=(struct nodo *) malloc(sizeof(struct nodo));
  20.    printf("\nIngresa un nombre: ");
  21.    gets(n->op);
  22.    fflush(stdin);
  23.    strcpy(n->op, op);
  24.    n->pnodo=NULL;
  25.  
  26.    return n;
  27. }
  28.  
  29. struct nodo *pop()
  30. {
  31.    struct nodo *pn;
  32.  
  33.    if(numNodos==0){
  34.        printf("La pila esta vacia.\n");
  35.        return NULL;
  36.    }
  37.    pn=top;
  38.    top=top->pnodo;
  39.  
  40.    return pn;
  41. }
  42.  
  43. struct nodo *push(char *op)
  44. {
  45.    struct nodo *n;
  46.  
  47.    if(numNodos==MAX){          //Pila llena
  48.        printf("La pila esta llena.\n");
  49.        return NULL;
  50.    }
  51.    n=new(op);
  52.    if(numNodos>0)              //Pila tiene nodos
  53.        n->pnodo=top;
  54.    top=&* n->pnodo;
  55.  
  56.    return top;
  57. }
  58.  
  59. int main()
  60. {
  61.    char *op;
  62.    int menu=0;
  63.    struct nodo *n;
  64.  
  65.    do
  66.    {
  67.        printf("******* PILA DINAMICA ********\n");
  68.        printf("\nQue accion desea realizar?\n");
  69.        printf("1. Insertar(Encolar)\n");
  70.        printf("2. Eliminar(Desencolar)\n");
  71.        printf("3. Mostrar\n");
  72.        printf("4. Salir\n");
  73.        printf("\nIngrese la opcion: ");
  74.        scanf("%d", &menu);
  75.        fflush(stdin);
  76.        printf("\n");
  77.  
  78.        switch(menu){
  79.            case 1:
  80.                n=new(char *op);
  81.                op=push(op, n);
  82.                break;
  83.            case 2:
  84.                n=pop(char *oper);
  85.                break;
  86.            case 3:
  87.                break;
  88.            case 4:
  89.                return 0;
  90.                break;
  91.            default:
  92.                printf ("Opcion invalida\n");
  93.        getchar();
  94.        }
  95.    }while(op!=4);
  96. }

PROGRAMA 2 (cola doble)

Código
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #define MAX 10
  5.  
  6. struct colaDoble{
  7.    struct cola *head;
  8.    struct cola *tail;
  9.    int nodos;
  10. };
  11.  
  12. struct cola{
  13.    int cola;
  14.    struct cola *sig;
  15.    struct cola *ant;
  16.    struct cola *act;
  17. };
  18.  
  19. int colaVacia(struct colaDoble *CC)
  20. {
  21.    return CC->nodos==0;          //return cola *head==cola *tail
  22. }
  23.  
  24. int colaLlena(struct colaDoble *CC)
  25. {
  26.    return CC->nodos==MAX;
  27. }
  28.  
  29. int insertarCC(struct colaDoble *CC, struct cola C)
  30. {
  31.    if(colaLlena(CC)==1){
  32.        printf("La cola esta llena.\n");
  33.        return 0;
  34.    }
  35.    if(colaVacia(CC)==1)
  36.        CC->head=&C;
  37.    else
  38.        CC->tail.sig=C;
  39.    CC.tail=&C;
  40.    CC.nodos++;
  41.    return 1;
  42. }
  43.  
  44. struct cola* eliminarCC(struct colaDoble *CC)
  45. {
  46.    short cola *C;
  47.    if(colaVacia==1){
  48.        printf("La cola esta vacia.\n");
  49.        return NULL;
  50.    }=CC->tail;
  51.    if(CC.nodos==1)
  52.        CC->head=CC.tail=NULL;
  53.    else
  54.        CC->head=CC.head.sig;
  55.    CC.nodos--;
  56.    return C;
  57. };
  58.  
  59. struct cola nuevo()
  60. {
  61.    struct cola n;
  62.    printf("Introduce un numero: ");
  63.    scanf("%d", &n dato):
  64.    fflush(stdin);
  65.    n.sig=NULL;
  66.    return n;
  67. };
  68.  
  69. int main()
  70. {
  71.    int op;
  72.    do
  73.    {
  74.        printf("******* COLA DOBLE ********");
  75.        printf("\nQue accion desea realizar? Ingrese la opcion: \n");
  76.        scanf("%d", &op);
  77.        fflush(stdin);
  78.        printf("1.Insertar en inicio(Encolar por head)\n");
  79.        printf("2.Insertar en final(Encolar por tail)\n");
  80.        printf("3.Eliminar en inicio(Desencolar por head)\n");
  81.        printf("4.Eliminar en final(Desencolar por tail)\n");
  82.        printf("5.Listar\n");
  83.        printf("6.Salir\n");
  84.        switch(op)
  85.        {
  86.            case 1:
  87.                break;
  88.            case 2:
  89.                break;
  90.            case 3:
  91.                break;
  92.            case 4:
  93.                break;
  94.            case 5:
  95.                break;
  96.            case 6:
  97.                return 0;
  98.            default:
  99.                printf ("Opcion invalida\n");
  100.        getchar();
  101.        }
  102.    }while(op!=6);
  103.  
  104.    return 0;
  105. }
  106.  
PROGRAMA 3 (codigo de lista circular pasar a lista doblemente ligada circular conforme al menu)

Código
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct nodo{
  5. char nombre[40];
  6. struct nodo *next;
  7. };
  8.  
  9. struct nodo *crearNodo(){
  10. struct nodo *x;
  11.  
  12. x=(struct nodo *) malloc(sizeof(struct nodo));
  13. printf("Nombre: ");
  14. gets(x->nombre);
  15. x->next=NULL;
  16.  
  17. return x;
  18. }
  19.  
  20. struct nodo *buscar(struct nodo *lista, char *k){
  21. if(lista==NULL) return NULL;
  22.  
  23. while(lista){
  24. if(strcmp(lista->nombre, k)==0)
  25. break;
  26. lista=lista->next;
  27. }
  28. return lista;
  29. }
  30.  
  31. struct nodo *borrar(struct nodo *lista, char *k){
  32. struct nodo *ant, *act;
  33.  
  34. if(lista==NULL) return NULL;
  35.  
  36. ant=lista;
  37. act=lista;
  38. while(act!=NULL){
  39. if(strcmp(act->nombre, k)==0){
  40. break;
  41. }
  42. ant=act;
  43. act=act->next;
  44. }
  45. if(act!=NULL){
  46. if(act==lista)
  47. lista=act->next;
  48. ant->next=act->next;
  49. }
  50. return lista;
  51. }
  52.  
  53. void listar(struct nodo *lista){
  54. if(lista==NULL){
  55. printf("Lista vacia\n");
  56. return;
  57. }
  58. do
  59.    {
  60. printf("%s->",lista->nombre);
  61.        lista=lista->next;
  62. }while(lista!=NULL);
  63. printf("\n");
  64. }
  65.  
  66. struct nodo *insertar(struct nodo *head, struct nodo *x){
  67. if(head!=NULL)
  68. x->next=head;
  69. head=x;
  70.  
  71. return head;
  72. }
  73.  
  74. int main()
  75. {
  76. char nombre[40];
  77. int op=0;
  78. struct nodo *x;
  79. struct nodo *head=NULL;
  80.  
  81. while(1){
  82. system("cls");
  83.        printf("******* LISTA CIRCULAR ********\n");
  84.        printf("\nQue accion desea realizar?\n\n");
  85. printf("1. Insertar\n");
  86. printf("2. Eliminar\n");
  87. printf("3. Buscar\n");
  88. printf("4. Listar\n");
  89. printf ("6. Siguiente\n");
  90. printf ("7. Anterior\n");
  91. printf ("8. Buscar anterior\n");
  92. printf ("9. Buscar siguiente\n");
  93. printf ("10. Borar todo\n");
  94. printf("11. Salir\n\n");
  95. printf("Ingrese la opcion: ");
  96. scanf("%d", &op);
  97. fflush(stdin);
  98. printf("\n");
  99.  
  100. switch(op){
  101. case 1:
  102. x=crearNodo();
  103. head=insertar(head,x);
  104. break;
  105. case 2:
  106.                printf("Buscar: ");
  107. gets(nombre);
  108. x=borrar(head, nombre);
  109. if(x!=NULL){
  110. printf("Se elimino: %s\n", nombre);
  111. //head= x;
  112. }
  113. else
  114. printf("No se encuentra: %s\n", nombre);
  115. system("pause");
  116. break;
  117. case 3:
  118.                printf("Buscar: ");
  119. gets(nombre);
  120.  
  121. if(buscar(head, nombre)==NULL)
  122. printf("No se encuentra: %s\n", nombre);
  123. else
  124. printf("Se encuentro: %s\n", nombre);
  125. system("pause");
  126. break;
  127. case 4:
  128. listar(head);
  129. system("pause");
  130. break;
  131. case 5:
  132. return 0;
  133. default:
  134. printf("Opcion invalida\n");
  135. }
  136. }
  137. }
  138.  
  139.  


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


Título: Re: ayuda en programas de estructuras de datos en c
Publicado por: engel lex en 27 Octubre 2016, 01:04 am
cual es tu duda? cual es el error  o problema?


Título: Re: ayuda en programas de estructuras de datos en c
Publicado por: KAPPA47 en 27 Octubre 2016, 02:28 am
tanto en pila como en cola no me queda claro como pasar los struct que sirven para insertar, eliminar, etc en el menu de la funcion main

en la lista se que se maneja next y prev pero no se aplicarlo


Mod: No escribir en mayúsculas, segundo aviso