PDA

View Full Version : [C]Stack


Luc@s
07-06-2004, 19:11
/*
### Copyright (c) 2004 Luca Francesca
### This script is provided under the terms of the GNU General Public License
### (see http://www.gnu.org/licenses/gpl.html for the full license text.)
*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "Common.h"

#define SIZE 100

typedef struct _Stack *Stack;

struct _Stack
{
int stack[SIZE];
int top;
};

Stack Init(int val);
void Push(Stack st, int val);
int Pop(Stack st);
void Delete(Stack st);

int main(int argc, char *argv[])
{
srand(time(NULL));
Stack st = Init(10);
int i = 0;
for(i; i < SIZE; ++i)
{
Push(st, i);
}
for(i; i < SIZE; ++i)
{
printf("%d\n", Pop(st));
}
Delete(st);
ExitFunction();
return 0;
}

Stack Init(int val)
{
Stack tmp;
tmp = malloc(sizeof(struct _Stack));
tmp->top = 0;
tmp->stack[tmp->top] = val;
return tmp;
}

void Push(Stack st, int val)
{
if(st->top == 0)
return;
st->stack[st->top++] = val;
}

int Pop(Stack st)
{
if(st->top == 0)
return 0;
else
return st->stack[st->top--];
}

void Delete(Stack st)
{
SAFE_DELETE(st)
}


Non mi stampa nulla.
Why??

Luc@s
08-06-2004, 07:13
ap

cionci
08-06-2004, 08:49
Devi cambiare la push:

void Push(Stack st, int val)
{
if(st->top == SIZE)
return;
st->stack[st->top++] = val;
}

nella Init non importa pushare un valore...

Luc@s
08-06-2004, 08:50
Originariamente inviato da cionci
DEvi inizializzare tmp->top = 1;

Oppure cambi la push:

void Push(Stack st, int val)
{
if(st->top == SIZE)
return;
st->stack[st->top++] = val;
}


figura di m :oink:

fpucci
08-06-2004, 08:53
Credo che non venga mai messo nulla sullo stack, in quanto nella Push() è presente il controllo

if (st->stop == 0)
return;


ma st->stop è già uguale a 0 in quanto ce lo pone la Init() chiamata precedentemente

fpucci
08-06-2004, 08:53
opss
battuto sul secondo! :D

Luc@s
08-06-2004, 08:55
non mi stampa nulla lo stesso :(
Che interdetto che sono


/*
### Copyright (c) 2004 Luca Francesca
### This script is provided under the terms of the GNU General Public License
### (see http://www.gnu.org/licenses/gpl.html for the full license text.)
*/
#include <stdio.h>
#include <stdlib.h>
#include "Common.h"

#define SIZE 100

typedef struct _Stack *Stack;
typedef int base_type;

struct _Stack
{
base_type stack[SIZE];
int sp;
};

Stack Init(base_type val);
void Push(Stack st, base_type val);
base_type Pop(Stack st);
void Delete(Stack st);

int main(int argc, char *argv[])
{
Stack st = Init(10);
int i = 0;
for(i; i <= SIZE; ++i)
{
Push(st, i);
}
for(i; i <= SIZE; ++i)
{
printf("%d\n", Pop(st));
}
Delete(st);
char exit;
scanf("%c", &exit);
return 0;
}

Stack Init(base_type val)
{
Stack tmp;
tmp = malloc(sizeof(struct _Stack));
tmp->sp = 1;
tmp->stack[tmp->sp] = val;
return tmp;
}

void Push(Stack st, base_type val)
{
if(st->sp == SIZE)
return;
st->stack[st->sp++] = val;
}

base_type Pop(Stack st)
{
if(st->sp == 0)
return 0;
else
return st->stack[st->sp--];
}

void Delete(Stack st)
{
SAFE_DELETE(st)
}

cionci
08-06-2004, 09:02
for(i=0; i <= SIZE; ++i) ;)

Luc@s
08-06-2004, 09:04
Originariamente inviato da cionci
for(i=0; i <= SIZE; ++i) ;)
stamattina dormo proprio.
é vero era sempre pari a SIZE i :oink:

cionci
08-06-2004, 09:04
tmp->sp = 0;
tmp->stack[tmp->sp++] = val;

Non importa comunque inizializzare lo stack con un elemento...

Luc@s
08-06-2004, 09:07
Originariamente inviato da cionci
tmp->sp = 0;
tmp->stack[tmp->sp++] = val;

Non importa comunque inizializzare lo stack con un elemento...

è una mia tendenza a inizalizzare tutto.
E un abitudine che ho preso per evitare casino:)

cionci
08-06-2004, 09:09
E' sbagliata anche la anche la pop:

st->stack[--st->top];