Mam dynamiczny stos i nie mam pojęcia czemu on nie działa :( siedziałem nad nim już trochę i nie mogę znaleźć błędu. Proszę o pomoc. z góry bardzo dziękuję :)
Stack.c
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "stack.h"
static int top; /* pierwsze wolne miejsce na stosie */
static int size;
void init(int *s)
{
top=0;
size=0;
s=0;
}
void finalize(int *s)
{
free(s);
}
void clear(int *s)
{
top=0;
}
void push(int *s,int a)
{
if(top>=size)
{
int newsize=(size+1)*2;
int* ns=(int*)realloc(s,newsize*sizeof(int));
if(ns)
s=ns;
else
{
free(s);
abort();
}
fprintf(stderr,"Rozmiar stosu %d -> %d\n",size,newsize);
size=newsize;
}
s[top++]=a;
}
int pop(int *s)
{
assert(top>0);
return s[--top];
}
stack.h
void push(int *s,int a);
int pop(int *s);
void clear(int *s);
void init(int *s);
void finalize(int *s);
teststack.c
#include <stdio.h>
#include "stack.h"
int main()
{
int *s1;
init(s1);
push(s1,1);
push(s1,2);
push(s1,3);
printf("%d %d\n",pop(s1),pop(s1));
printf("%d\n",pop(s1));
finalize(s1);
return 0;
}
Makefile:
teststack: teststack.o stack.o
gcc -Wall $^ -o $@
stack.o: stack.c stack.h
gcc -c -Wall $< -o $@
teststack.o: teststack.c stack.h
gcc -c -Wall $< -o $@
.PHONY: clean
clean:
-rm stack.o teststack.o teststack