Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] Stack C!

Test problem!!! realloc stack

  • This topic is locked This topic is locked
4 replies to this topic

#1 surke83

surke83

    CC Lurker

  • New Member
  • Pip
  • 5 posts
  • Learning:C

Posted 15 June 2012 - 10:10 AM

Consider the implementation of the "dynamic" stack by using a field variable length!Assume that the stack constains integer data type!!!
I need to write functions for placing elements on the stack and removal on the stack!!!(Pop and Push).
If the stack no spaces for the new elements,stack should be dynamically increased by 10 new spaces!!
When removing elements from stack is necessary to check how many empty spaces there are on the stack,if there are more than 10 spaces,reduce the size of the stack for 10!!!
The functions should return 1 if the operation succeeded,otherwise a 0!!
My program does not work!!!

typedef struct{
	   int allocated,topStack;
	   elementtype* element;
}STACK;

void MAKE_NULL(STACK *S)
{
S->allocated=S->topStack=0;
S->element=NULL;
}
void Push( elementtype x,STACK *S)
{
if (S->topStack==S->allocated)
{
S->element=(elementtype*)realloc( S->element,sizeof(elementtype)*( S->allocated+10));
if (S->element==NULL)
   error("PUSH:not enough memory for reallocation!");
S->allocated+=10;
}
S->element[S->topStack]=x;
S->topStack++;
}
void Pop(STACK *S)
{
if(S->topStack==0)
  error("Stack is empty!")
S->topStack--;
if(S->topStack+10< S->allocated)
{
S->element=(elementtype*)realloc( S->element,sizeof(elementtype)*( S->allocated-10));
if (S->element==NULL)
   error("POP:not enough memory for reallocation!");
S->allocated-=10;
}
}

  • 0

#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 15 June 2012 - 10:13 AM

MAKE_NULL should call free() on element before setting it to NULL. What error are you getting?
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#3 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 15 June 2012 - 10:41 AM

The only problem I see is in your Pop function. I think the Pop method should be like following.
void Pop(STACK *S)
{
   if(S->topStack==0) {
      error("Stack is empty!")
      return; /* I think we should return from here. */
   }
   S->topStack--;
   /* I think when there are at least ten room are empty, let us re-create the memory structure by reducing the empty10 rooms*/
   if(S->topStack+10 <= S->allocated) {
      S->element=(elementtype*)realloc( S->element,sizeof(elementtype)*( S->allocated-10));
      if (S->element==NULL) {
         error("POP:not enough memory for reallocation!");
         MAKE_NULL(S);
         return;
      }
      S->allocated-=10;
   }
}

I also think if realloc fails, you should just handle it in a way that the data-structure does not get access further without re-initialize -- I mean you should call MAKE_NULL in that case. So your Push method should be as follows...
void Push( elementtype x,STACK *S)
{
    if (S->topStack==S->allocated) {
        S->element=(elementtype*)realloc( S->element,sizeof(elementtype)*( S->allocated+10));
        if (S->element==NULL) {
           error("PUSH:not enough memory for reallocation!");
           MAKE_NULL(S);
           return;
        }
        S->allocated+=10;
    }
    S->element[S->topStack]=x;
    S->topStack++;
}

  • 0

#4 surke83

surke83

    CC Lurker

  • New Member
  • Pip
  • 5 posts
  • Learning:C

Posted 15 June 2012 - 12:57 PM

Thanks for the assistance!!!Program works only without these calls MAKE_NULL(S); and return;
  • 0

#5 Roger

Roger

    Skadoosh!

  • Administrator
  • 1222 posts
  • Programming Language:C, PHP
  • Learning:Others

Posted 16 June 2012 - 08:21 AM

This topic has been marked as SOLVED. If you have a similar question or topic, go back to the subforum and start a new topic to continue discussions.
  • 0

New around here? Click here to register and start participating in under a minute?

Or do a quick search and you may find the answer you're looking for.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download