Skip to content

Instantly share code, notes, and snippets.

@fallanding
Forked from pfallasro/cola.c
Created November 27, 2022 22:26
Show Gist options
  • Save fallanding/80ce69a4d298f4691003419a0bea35b4 to your computer and use it in GitHub Desktop.
Save fallanding/80ce69a4d298f4691003419a0bea35b4 to your computer and use it in GitHub Desktop.
Pila - C y Cola - C
#include <stdio.h>
#include <stdlib.h>
char* getPtrNom();
int getId();
int isEmpty();
int menu();
void clearBuffer();
void dequeue();
void enqueue();
void isEmptyWrap();
struct persona {
int id;
char *nombre;
struct persona *siguiente;
};
typedef struct persona Nodo;
Nodo *final;
Nodo *inicio;
main() {
final = inicio = 0;
return menu();
}
int menu() {
char c;
do {
printf("\n~~ MENU: cola! ~~");
printf("\nHaga una seleccion:");
printf("\n 1. enqueue()");
printf("\n 2. dequeue()");
printf("\n 3. isEmpty()");
printf("\n q: salir.\n");
c = getchar();
switch(c) {
case '1':
enqueue();
break;
case '2':
dequeue();
break;
case '3':
isEmptyWrap();
break;
default:
break;
}
} while(c != 'q' && c != EOF);
return 1;
}
// metodo wrapper para el menu
void isEmptyWrap() {
clearBuffer();
if (isEmpty()) {
printf("sirilo piscuilo.\n");
} else {
printf("nel pastel.\n");
}
}
int getId() {
int num;
printf("Ingrese el ID: ");
scanf("%d" , &num);
return num;
}
// retorna un nuevo puntero a un arreglo
char* getPtrNom() {
char d,*newAr;
int i = 0;
newAr = (char*) malloc(sizeof(char)*100);
printf("Ingrese el nombre: ");
while((d = getchar()) != EOF && d != '\n') {
newAr[i++] = d;
}
return newAr;
}
// imprime el primer valor ingresado
// el penultimo valor es el nuevo inicio
// el primer valor es borrado de la lista
void dequeue() {
Nodo *actual,
*temporal;
//printf("\tdequeue()\n");
//printf("nfin: %p\nini: %p\n", final, inicio);
clearBuffer();
if (isEmpty()) {
printf("Y metiamos algo en la cola...");
} else {
if (final == inicio) { // si solo hay 1 nodo
//printf("1nodo\t\tnfin: %p\nini: %p\n", final, inicio);
printf("%d | %s\n", final->id, final->nombre);
//free(final); // liberamos la memoria que uso el nodo
//free(inicio); // liberamos la memoria que uso el nodo
final = inicio = 0; // dejamos los punteros en null
} else { // si hay mas de 1 nodo
//printf("1+nodo\t\tnfin: %p\nini: %p\n", final, inicio);
actual = final;
while (actual != inicio) { // recorra la cola y quede en el penultimo
temporal = actual; // temporal guarda la direccion del penultimo
actual = temporal->siguiente;
}
printf("%d | %s\n", inicio->id, inicio->nombre); // imprimimos el ultimo nodo
//free(inicio); // liberamos la memoria usada por el ultimo nodo
inicio = temporal; // el penultimo es ahora el ultimo
}
}
}
// agrega un nodo nuevo al final de la cola
void enqueue() {
Nodo *nodoNuevo,
*temporal;
nodoNuevo = (Nodo*) malloc(sizeof(Nodo));
clearBuffer();
nodoNuevo->nombre = getPtrNom();
nodoNuevo->id = getId();
//printf("\n\tenqueue()\n");
if (isEmpty()) { // si la cola esta vacia
final = nodoNuevo; // el nodo toma el primer
inicio = nodoNuevo; // y el ultimo lugar
} else { // si hay al menos 1 nodo
temporal = final; // almacenamos el ultimo nodo agregado
final = nodoNuevo; // el nodoNuevo toma el primer lugar
final->siguiente = temporal; // apuntando al nodo que estaba en primer lugar
}
//printf("nvo: %p\nfin: %p\nini: %p\n", nodoNuevo, final, inicio);
clearBuffer();
}
int isEmpty() {
if (!final) {
return 1;
} else {
return 0;
}
}
// cuando se manejan menus que tienen breaklines, se ocupa limpiar el buffer
// antes de empezar a leer caracteres
void clearBuffer() {
while(getchar() != '\n')
;
}
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
struct persona{
int id;
char *nombre;
struct persona *siguiente;
};
typedef struct persona Nodo;
Nodo *cabeza = NULL;
Nodo *actual = NULL;
char* getStr();
void clearBuffer();
int getId();
void push();
void pop();
void CrearRaiz();
void ContarNodos();
void ImprimirLista();
int main(){
return menu();
}
void push(){
Nodo *nodo = (struct persona*)malloc(sizeof(struct persona));
if(NULL == nodo){
printf("\n Error en creacion de nodo \n");
}
nodo->id = getId();
clearBuffer();
nodo->nombre=getStr();
nodo->siguiente = cabeza;
cabeza = nodo;
}
void pop(){
if(NULL == cabeza){
printf("La pila esta vacia \n");
}else{
printf("\n -------Elemento Top Removido------- \n");
Nodo *nodo = cabeza;
printf("\n ID: %d ",nodo->id);
printf("Nombre: %s \n", nodo->nombre);
cabeza=nodo->siguiente;
free(nodo);
}
}
void ImprimirLista(void){
if(NULL == cabeza){
printf("La pila esta vacia \n");
}else{
Nodo *nodo = cabeza;
printf("\n -------Inicio de la Pila------- \n");
while(nodo != NULL)
{
printf("\n ID: %d ",nodo->id);
printf("Nombre: %s \n", nodo->nombre);
nodo = nodo->siguiente;
}
printf("\n -------Final de la Pila------- \n");
}
}
void ContarNodos(void){
Nodo *nodo = cabeza;
int i=0;
while(nodo != NULL){
i++;
nodo = nodo->siguiente;
}
printf("\n Cantidad de nodos: %d\n", i);
return;
}
int getId() {
int num;
printf("\n Ingrese el ID: ");
scanf("%d" , &num);
return num;
}
char* getStr() {
char d,*newAr;
int i = 0;
newAr = (char*) malloc(sizeof(char)*100);
printf("\n Ingrese el nombre: ");
while((d = getchar()) != EOF && d != '\n') {
newAr[i++] = d;
}
return newAr;
}
void clearBuffer() {
while(getchar() != '\n')
;
}
int menu() {
char c;
do {
printf("\n~~ MENU: pila! ~~");
printf("\nHaga una seleccion:");
printf("\n 1. push()");
printf("\n 2. pop()");
printf("\n 3. ImprimirLista()");
printf("\n 4. ContarNodos()");
printf("\n q: salir.\n");
c = getchar();
switch(c) {
case '1':
clearBuffer();
push();
break;
case '2':
clearBuffer();
pop();
break;
case '3':
clearBuffer();
ImprimirLista();
break;
case '4':
clearBuffer();
ContarNodos();
break;
default:
break;
}
} while(c != 'q' && c != EOF);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment