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: "); 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("\nIngrese la opcion: ");
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:
}
}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: "); n.sig=NULL;
return n;
};
int main()
{
int op;
do
{
printf("******* COLA DOBLE ********"); printf("\nQue accion desea realizar? Ingrese la opcion: \n"); 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"); switch(op)
{
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
return 0;
default:
}
}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
)); 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){
return;
}
do
{
lista=lista->next;
}while(lista!=NULL);
}
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){
printf("******* LISTA CIRCULAR ********\n"); printf("\nQue accion desea realizar?\n\n"); printf ("8. Buscar anterior\n"); printf ("9. Buscar siguiente\n"); printf("Ingrese la opcion: ");
switch(op){
case 1:
x=crearNodo();
head=insertar(head,x);
break;
case 2:
x=borrar(head, nombre);
if(x!=NULL){
printf("Se elimino: %s\n", nombre
); //head= x;
}
else
printf("No se encuentra: %s\n", nombre
); break;
case 3:
if(buscar(head, nombre)==NULL)
printf("No se encuentra: %s\n", nombre
); else
printf("Se encuentro: %s\n", nombre
); break;
case 4:
listar(head);
break;
case 5:
return 0;
default:
}
}
}
Mod: Los códigos deben ir en etiquetas GeSHi, no escribas el título en mayúsculas