|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Apr 2002
Città: Vigevano(PV)
Messaggi: 2124
|
[C]Stack
Codice:
/* ### 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) } Why??
__________________
Gnu/Linux User ![]() |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Apr 2002
Città: Vigevano(PV)
Messaggi: 2124
|
ap
__________________
Gnu/Linux User ![]() |
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
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... |
![]() |
![]() |
![]() |
#4 | |
Senior Member
Iscritto dal: Apr 2002
Città: Vigevano(PV)
Messaggi: 2124
|
Quote:
figura di m ![]()
__________________
Gnu/Linux User ![]() |
|
![]() |
![]() |
![]() |
#5 |
Senior Member
Iscritto dal: Jul 2002
Città: Roma
Messaggi: 806
|
Credo che non venga mai messo nulla sullo stack, in quanto nella Push() è presente il controllo
Codice:
if (st->stop == 0) return; |
![]() |
![]() |
![]() |
#6 |
Senior Member
Iscritto dal: Jul 2002
Città: Roma
Messaggi: 806
|
opss
battuto sul secondo! ![]() |
![]() |
![]() |
![]() |
#7 |
Senior Member
Iscritto dal: Apr 2002
Città: Vigevano(PV)
Messaggi: 2124
|
non mi stampa nulla lo stesso
![]() Che interdetto che sono Codice:
/* ### 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) }
__________________
Gnu/Linux User ![]() Ultima modifica di Luc@s : 08-06-2004 alle 09:01. |
![]() |
![]() |
![]() |
#8 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
for(i=0; i <= SIZE; ++i)
![]() |
![]() |
![]() |
![]() |
#9 | |
Senior Member
Iscritto dal: Apr 2002
Città: Vigevano(PV)
Messaggi: 2124
|
Quote:
é vero era sempre pari a SIZE i ![]()
__________________
Gnu/Linux User ![]() |
|
![]() |
![]() |
![]() |
#10 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
tmp->sp = 0;
tmp->stack[tmp->sp++] = val; Non importa comunque inizializzare lo stack con un elemento... |
![]() |
![]() |
![]() |
#11 | |
Senior Member
Iscritto dal: Apr 2002
Città: Vigevano(PV)
Messaggi: 2124
|
Quote:
E un abitudine che ho preso per evitare casino ![]()
__________________
Gnu/Linux User ![]() |
|
![]() |
![]() |
![]() |
#12 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
E' sbagliata anche la anche la pop:
st->stack[--st->top]; |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 13:22.