#include<iostream>
#include<stddef.h>
using namespace std;
typedef struct u_stack_t {
void * () store;
void () current;
size_t capacity;
} u_stack_t;
u_stack_t () u_create_stack();
u_stack_t () u_create_stack_with_capacity(size_t capacity);
void u_delete_stack(u_stack_t*);
void () u_push_stack(u_stack_t*, void*, size_t);
void () u_pop_stack(u_stack_t*, size_t);
u_stack_t () u_create_stack_with_capacity(size_t capacity) {
u_stack_t () stack = (u_stack_t*)malloc(sizeof(stack));
if (stack) {
stack->capacity = capacity + (sizeof(max_align_t) - capacity % sizeof(max_align_t));
stack->store = malloc(capacity);
if (stack->store) {
stack->current = stack->store;
return stack;
}
free(stack);
}
return NULL;
}
u_stack_t *create_stack() {
u_stack_t* stack = (u_stack_t*)malloc(sizeof(stack));
if(stack) {
stack->capacity = sizeof(max_align_t);
stack->store = malloc(stack->capacity);
if (stack->store) {
stack->current = stack->store;
return stack;
}
free(stack);
}
return NULL;
}
void *u_push_stack(u_stack_t *stack, void *store, size_t capacity) {
u_stack_t *newItem = (u_stack_t *)malloc(sizeof *newItem);
if (newItem) {
newItem -> store = store;
newItem -> current = stack -> current;
stack -> current = newItem;
stack -> capacity += 1;
}
}
bool stackIsEmpty(u_stack_t *stack)
{
return stack -> current == NULL ? true : false;
}
/*void *u_pop_stack(u_stack_t *stack, size_t capacity)
{
u_stack_t *oldTop;
max_align_t item;
if( stack -> current == NULL) {
stack -> capacity = 0;
stack -> current = NULL;
stack -> store = NULL;
}
oldTop -> current = stack -> current;
// item = oldTop->store;
stack->current = oldTop->store;
free(oldTop);
oldTop = NULL;
stack -> capacity -= 1;
}
/*void *u_push_stack(u_stack_t *stack, void*store, size_t capacity) {
void *temp = new u_stack_t;
cin >> temp->store;
temp->current = *stack;
*stack = temp;
}*/
void* stackTop(u_stack_t *stack)
{
return (*(stack)).current;
}
int main() {
u_stack_t* stack;
u_stack_t* stacking;
int *tab2;
void *tab3;
int tab[5];
tab2 = &tab[0];
//tab3 = tab2;
//tab3 = &tab[0];
tab[1] = 1;
tab[2] = 2;
tab[3] = 3;
tab[4] = 4;
tab[0] = 5;
// cout << *(tab + 1) << endl;
(*(stack)).current = (tab2 + 4);
(*(stack)).store = tab2;
(*(stack)).capacity = 5;
cout << " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx " << endl;
u_create_stack_with_capacity(7);
cout << "here" << endl;
// cout << u_create_stack_with_capacity(7)->capacity << endl;
u_push_stack(stack, tab, stack->capacity);
cout << " ssssssssssssssssssssssssssssssssssss " << endl;
system("pause");
return 0;
}
Witam, czy mógłby ktoś sprawdzić mi funkcję push? Nie implementowałam wcześniej stosu na wskaźnikach, dlatego proszę o wyrozumiałość. :) btw. w definicji zamiast () są gwiazdki. *
vpiotr