Carayav
11/22/2016 - 11:04 PM

Makefile

#
# Seccion 1
#
# Esta seccion se puede modificar segun
# los requerimientos de su proyecto

CC=gcc
SRC1=ejemplo.c stack-lde.c
CFLAGS=-Wall -W -std=c99 -g
#LIBS=-ldl

BIN1=ejemplo

#
# Seccion 2 (NO MODIFICAR!!!)
#
OBJ1=$(SRC1:.c=.o)

all:  $(BIN1) $(BIN2) clean

$(BIN1): $(OBJ1)
	$(CC) $(LIBS) $(OBJ1) -o $(@)

%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

clean:
	@rm -f $(BIN1) $(BIN2)
	@rm -f $(OBJ1) $(OBJ2)
	@rm -f *~

docs:
	@doxygen

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

#include "stack-lde.h"


int main() {

	header_t header;

	item_t itemA;
	item_t itemB;
	item_t itemC;
	item_t tmp;

	itemA.dato1 = 10;
	itemA.dato2 = 20;

	itemB.dato1 = 30;
	itemB.dato2 = 40;

	itemC.dato1 = 50;
	itemC.dato2 = 60;

	puts("Inicializa Stack");
	make_stack(&header);

	printf("Cantidad de elementos %d\n\n", size(&header));


	puts("3 items" );

	printf("itemA dato1: %d\tdato2: %d\n",itemA.dato1 , itemA.dato2);
	printf("itemB dato1: %d\tdato2: %d\n",itemB.dato1 , itemB.dato2);
	printf("itemC dato1: %d\tdato2: %d\n\n",itemC.dato1 , itemC.dato2);

	puts("push(&header, itemA)");
	push(&header, itemA);
	puts("push(&header, itemB)");
	push(&header, itemB);
	puts("push(&header, itemC)");
	push(&header, itemC);

	printf("Cantidad de elementos %d\n\n", size(&header));

	tmp = pop(&header);
	printf("pop(&header) itemC = dato1: %d\tdato2: %d\n",tmp.dato1 , tmp.dato2);
	tmp = top(&header);
	printf("top(&header) itemB = dato1: %d\tdato2: %d\n",tmp.dato1 , tmp.dato2);
	puts("swap(&header)  intercambia itemB con itemA");
	swap(&header);
	tmp = top(&header);
	printf("top(&header) itemA = dato1: %d\tdato2: %d\n\n",tmp.dato1 , tmp.dato2);

	while(!isEmpty(&header)){
		printf("Stack No esta vacio quedan %d items\n", size(&header));
		pop(&header);
	}
	if(isEmpty(&header)){
		printf("Stack esta vacio quedan %d items\n", size(&header));
	}
	exit(EXIT_SUCCESS);
}
/** @file lse.c
 *  @brief Biblioteca basica Listas Simplemente Enlazadas
 *
 *  @author Claudio Araya V.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <stdbool.h>
#include "stack-lde.h"

void make_stack(pheader_t pheader){
  pheader->size = 0;
  pheader->stk = NULL;
};

void push(pheader_t pheader, item_t item){
  pframe_t pframe = (pframe_t) malloc(sizeof(frame_t));
  if(NULL == pframe){
    perror("ERROR STACK-LDE:");
    return;
  }
  pframe->item = item;
  pframe->sup  = NULL;
  pframe->inf  = pheader->stk;
  if(pheader->size > 0)
    pheader->stk->sup = pframe;
  pheader->stk = pframe;
  pheader->size++;
  return;
};


item_t pop(pheader_t pheader){
  item_t item = pheader->stk->item;
  pframe_t pframe = pheader->stk;
  if(!isEmpty(pheader)){
    pheader->stk->sup = NULL;
  }
  pheader->stk = pheader->stk->inf;
  free(pframe);
  pheader->size--;
  return item;
};

item_t top(pheader_t pheader){
  return pheader->stk->item;
}

uint16_t size(pheader_t pheader){
  return pheader->size;
};

void swap(pheader_t pheader){
  item_t tmp = pheader->stk->item;
  pheader->stk->item = pheader->stk->inf->item;
  pheader->stk->inf->item = tmp;
};

bool isEmpty(pheader_t pheader){
  return pheader->size ? false : true;
};
/** @file lse.h
 *  @brief Prototipos de la biblioteca de Libreria de Stack. Implementado con LDE.
 *
 *  @author Claudio Araya V.
 */
#include <stdint.h>
#include <stdbool.h>


#ifndef lse_h
#define lse_h


/**
 * Estructura para representar un item.
 */
struct s_item{
  /**
   * @name Seccion Datos
   @{ */
  uint8_t dato1;  /**<Dato 1 de item */
  uint8_t dato2;  /**<Dato 2 de item */
  /*}@*/
};

typedef struct s_item   item_t; /**< Un item */
typedef struct s_item* pitem_t; /**< Puntero a item */

struct s_frame{
  item_t item;
  struct s_frame* sup;
  struct s_frame* inf;
};

typedef struct s_frame   frame_t;
typedef struct s_frame* pframe_t;

struct s_header{
  uint16_t size;
  pframe_t stk;
};

typedef struct s_header   header_t;
typedef struct s_header* pheader_t;

/**
 * [make_stack description]
 * @param pheader [description]
 */
void make_stack(pheader_t pheader);

/**
 * [push description]
 * @param pheader [description]
 * @param item    [description]
 */
void push(pheader_t pheader, item_t item);

/**
 * [pop description]
 * @param  pheader [description]
 * @return         [description]
 */
item_t pop(pheader_t pheader);

/**
 * [top description]
 * @param  pheader [description]
 * @return         [description]
 */
item_t top(pheader_t pheader);

/**
 * [size description]
 * @param  pheader [description]
 * @return         [description]
 */
uint16_t size(pheader_t pheader);

/**
 * [swap description]
 * @param pheader [description]
 */
void swap(pheader_t pheader);

/**
 * [isEmpty description]
 * @param  pheader [description]
 * @return         [description]
 */
bool isEmpty(pheader_t pheader);

#endif