Visualizzazione dei risultati da 1 a 3 su 3

Discussione: [C]Stack

  1. #1

    [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)
    }
    Non mi stampa nulla.
    Why??
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

  2. #2
    con la Init() poni st->top a zero
    poi chiami una serie di Push che vanno a vuoto perché ritornano appena si vedono st->top == 0
    e la stessa cosa fanno le Pop()

  3. #3
    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 20
    
    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(0);
    	int i = 0;
    	for(i; i <= SIZE; ++i)
    	{
    		Push(st, i);
    	}
    	for(i = 0; 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 = 0;
    	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)
    }
    Risolto
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.