Original Post
I have finally created my own implementation of a data structure :) can anyone tell me a way to improve on this?
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int loop;
typedef struct str
{
struct itm* base;
int count;
}structure;
typedef struct itm
{
struct itm* next;
struct itm* prev;
void* data;
}item;
struct itm* get_item(int n, structure* stack)
{
item* cur = NULL;
item* stp = stack->base;
if(n == 0)
{
return stack->base;
}
for(loop = 0; loop < n; loop++)
{
stp = stp->next;
}
return stp;
}
void add_item(struct itm* itm, structure* stack)
{
struct itm* last = get_item(stack->count, stack);
last->next = itm;
itm->prev = last;
itm->next = NULL;
stack->count++;
}
void init_stack(structure* stack)
{
stack->base = (struct itm *)malloc(sizeof(struct itm));
stack->count = 0;
}
void remove_item(int n, structure* stack)
{
struct itm* rem = get_item(n, stack);
rem->prev->next = rem->next;
if(rem->next != NULL)
rem->next->prev = rem->prev;
free(rem);
stack->count -= 1;
}
item* New_Item(void* data)
{
item* itm = (item*)malloc(sizeof(item));
itm->data = data;
return itm;
}