Giter Club home page Giter Club logo

c's People

Contributors

iamlam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

c's Issues

Pila

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

#define MAX_SIZE 100 // Tamaño máximo para la pila y la cola

// Estructura para los nodos de la pila y la cola
struct Node {
int data;
struct Node* next;
};

// Estructura para la pila
struct Stack {
struct Node* top;
};

// Estructura para la cola simple
struct Queue {
struct Node *front, *rear;
};

// Función para inicializar una pila
struct Stack* createStack() {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->top = NULL;
return stack;
}

// Función para inicializar una cola simple
struct Queue* createQueue() {
struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));
queue->front = queue->rear = NULL;
return queue;
}

// Función para crear un nuevo nodo
struct Node* newNode(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->next = NULL;
return node;
}

// Función para empujar un elemento a la pila
void push(struct Stack* stack, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = stack->top;
stack->top = newNode;
printf("%d pushed to stack\n", data);
}

// Función para sacar un elemento de la pila
int pop(struct Stack* stack) {
if (stack->top == NULL) {
printf("Stack underflow\n");
return -1;
}
struct Node* temp = stack->top;
int popped = temp->data;
stack->top = stack->top->next;
free(temp);
return popped;
}

// Función para mostrar la pila
void showStack(struct Stack* stack) {
struct Node* temp = stack->top;
printf("Stack: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

// Función para insertar un elemento en la cola simple
void enqueue(struct Queue* queue, int data) {
struct Node* newNode = newNode(data);
if (queue->rear == NULL) {
queue->front = queue->rear = newNode;
return;
}
queue->rear->next = newNode;
queue->rear = newNode;
}

// Función para eliminar un elemento de la cola simple
int dequeue(struct Queue* queue) {
if (queue->front == NULL) {
printf("Queue is empty\n");
return -1;
}
struct Node* temp = queue->front;
int item = temp->data;
queue->front = queue->front->next;
if (queue->front == NULL)
queue->rear = NULL;
free(temp);
return item;
}

// Función para mostrar la cola simple
void showQueue(struct Queue* queue) {
struct Node* temp = queue->front;
printf("Queue: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.