We help IT Professionals succeed at work.

too many malloc variables in my c code.

GPicasso
GPicasso asked
on
Hey I have some c code but it has too many references to malloc and I can only have 1, I need some assistance. I am supposed to alter stack.c so that it only uses malloc to create the stack not for anything else.
/*header file*/ 


       typedef int ElementType;
        #ifndef _Stack_h
        #define _Stack_h

        struct Node;
        typedef struct Node *PtrToNode;
        typedef PtrToNode Stack;

        int IsEmpty( Stack S );
        Stack CreateStack( void );
        void DisposeStack( Stack S );
        void MakeEmpty( Stack S );
        void Push( ElementType X, Stack S );
        ElementType Top( Stack S );
        void Pop( Stack S );

        #endif

Open in new window

/*stack.c*/
	#include "stack.h"
        #include "fatal.h"
        #include <stdlib.h>


	
        struct Node
        {
            ElementType Element;
            PtrToNode   Next;
        };

/* START: fig3_40.txt */
        int
        IsEmpty( Stack S )
        {
            return S->Next == NULL;
        }
/* END */

/* START: fig3_41.txt */
        Stack
        CreateStack( void )
        {
            Stack S;

            S = malloc( sizeof( struct Node) );
            if( S == NULL )
                FatalError( "Out of space!!!" );
            S->Next = NULL;
            MakeEmpty( S );
            return S;
        }

        void
        MakeEmpty( Stack S )
        {
            if( S == NULL )
                Error( "Must use CreateStack first" );
            else
                while( !IsEmpty( S ) )
                    Pop( S );
        }
/* END */

        void
        DisposeStack( Stack S )
        {
            MakeEmpty( S );
            free( S );
        }

/* START: fig3_42.txt */
        void
        Push( ElementType X, Stack S )
        {
            PtrToNode TmpCell;

            TmpCell = malloc( sizeof( struct Node ) );
            if( TmpCell == NULL )
                FatalError( "Out of space!!!" );
            else
            {
                TmpCell->Element = X;
                TmpCell->Next = S->Next;
                S->Next = TmpCell;
            }
        }
/* END */

/* START: fig3_43.txt */
        ElementType
        Top( Stack S )
        {
            if( !IsEmpty( S ) )
                return S->Next->Element;
            Error( "Empty stack" );
            return 0;  /* Return value used to avoid warning */
        }
/* END */

/* START: fig3_44.txt */
        void
        Pop( Stack S )
        {
            PtrToNode FirstCell;

            if( IsEmpty( S ) )
                Error( "Empty stack" );
            else
            {
                FirstCell = S->Next;
                S->Next = S->Next->Next;
                free( FirstCell );
            }
        }
/* END */

Open in new window

/*main*/
	#include <stdio.h>
	#include <stdlib.h>
	#include "stack.h"


	void print_stack(Stack printout)
	{
	  while(!IsEmpty(printout))
		{
	  	printf("%d ", Top(printout));
		Pop(printout);
		}
	printf("\n");
	}



	int main()
	{
	  Stack initial = CreateStack();
	  int i = 1;
	  while(i < 10)
		{
		Push(i, initial);
		i++;
		}
	  print_stack(initial);
	}

Open in new window

/*fatal.h*/
#include <stdio.h>
#include <stdlib.h>

#define Error( Str )        FatalError( Str )
#define FatalError( Str )   fprintf( stderr, "%s\n", Str ), exit( 1 )

Open in new window

 Please any help is appreciated.
Comment
Watch Question

This appears to be an academic assignment and I will address it as such.

I suggest you structure your stack as an array -- ArrayStack.  Add a new function StretchStack().  It will make a new stack that is twice the size of the old stack unless the old stack is null.  If the old stack is null, you make a new stack of a size like 2 or 4 (suggest a power of 2).  If the old stack isn't new, you copy the old stack to the new stack and then free the memory allocated to the old stack.

You will need to figure out a way to know when a stack is full and know how big the stack is.  This is easier in C++ because I'd just make some private variables.  Still, it's solvable in C.  

This approach is surprisingly efficient when using an amortized time analysis.  (Operations run in constant time.)

Make sense?

Explore More ContentExplore courses, solutions, and other research materials related to this topic.