0

Implement an array of stacks where stacks are defined :

typedef struct StackNode {

   int data;

   StackNode* next;

} StackNode;

Each array element points to a stack, each stack is initialized as an empty stack. When you start adding elements it will start adding them to the stack in Stacks[0]; if you say -2 in stdin and then 4 for example, the next entries will go to Stacks[4];

For example:

5 10 -2 3 9 7 89 -1
will result in :
Stacks[0] -> 10 -> 5
Stacks[1]
Stacks[2]
Stacks[3] -> 89 -> 7 -> 9

-1 will stop the code from running.

I am having problem with implementing an array of stacks so any help would be appreciated :)

4
  • It is not homework, but it is part of the code i am writing and i am trying to finish it as soon as possible. I think I understand everything but the idea of implementing the array. So if this way just a stack to push and pop elements it would've been easy. Thanks for the reply :)
    – c2009l123
    Nov 12, 2009 at 9:15
  • 1
    Can you post the code that is actually operating on the structure? Or are you having problems even getting started? I think this probably needs the homework tag, as jinguy suggested.
    – user50049
    Nov 12, 2009 at 9:16
  • even if it is not a homework?
    – c2009l123
    Nov 12, 2009 at 9:20
  • @c2009l123: Sorry, I had posted that comment as you were writing your reply indicating that to Rom, so I didn't see it until after my comment was posted.
    – user50049
    Nov 12, 2009 at 9:37

6 Answers 6

2
int t = 0, index = 0;
while(t != -1)
{
  scanf("%d", &t);
  if(t == -2)
  {
    scanf("%d", &t);
    index = t;
    continue;
  }
  if(t >= 0)
    push(stacks[index], t);
}
2
  • and push function would just be like push(StackNode *tp, int t) right? i mean not StackNode *tp[int i] or something like that?
    – c2009l123
    Nov 12, 2009 at 9:30
  • If stacks is an array of pointers to StackNode objects, yeah, your signature for push function is correct.
    – Amarghosh
    Nov 12, 2009 at 9:49
2

In line 3 of

typedef struct StackNode {
   int data;
   StackNode* next;          /* line 3 */
} StackNode;

the type StackNode does not yet exist. The type StackNode only begins to exist once the type struct StackNode is fully parsed.

But the type struct StackNode already exists at that point. It is still incomplete, but you can declare pointers to it.

typedef struct StackNode {
   int data;
   struct StackNode* next;          /* line 3 */
} StackNode;
1
  • This is not what i am facing a problem with though. For example you can : StackNode *Stacks[10]; to create an array of stacks of size 10. However i am having problem to do operations like pushing for example to a specific number in the array.
    – c2009l123
    Nov 12, 2009 at 9:18
1

You may try something like the following (tested):

//includes
#define SIZE 10


typedef struct StackNode{
 int data ;
 struct StackNode* next ;
}StackNode ;



StackNode* new_stackNode(int num)
{
 StackNode* ptr = (StackNode*)malloc(sizeof(StackNode)) ;
 ptr->data = num ;
 ptr->next = 0 ;
 return ptr ;
}



int main()
{
 StackNode *arr[SIZE] = {0};
 int st_index = 0 ;
 int num = 0 ;
 while(num != -1)
 {
  scanf("%d",&num);
  if( num == -2 )
  st_index++ ;
  else
  {
   StackNode* ptr = new_stackNode(num) ;
   ptr->next = arr[st_index] ;
   arr[st_index] = ptr ;
  }
 }
}
0

If you go to the bottom of this Wikipedia article, you'll see an example of what I think you are trying to implement. What you have is a linked list. Additionally, here are some handy doubly linked list helpers which might serve as example.

If you post the actual usage of your code as-is, you might receive a better (more specific) answer.

0

You must first get an array of StackNodes:

StackNode ** stacks = malloc(sizeof(StackNode*) * 5); /* allocate 5 stack nodes */
stacks[0] = malloc(sizeof(StackNode)); /* allocate first stack */
...

Now you have a function push(StackNode *, int value). To push on a certain stack, call it with push(stacks[2], value) (to push on the third stack, for example).

1
  • do i need to do that one by one? what if i have 100 stacks, would i just loop over them? That seems kind of inefficient
    – c2009l123
    Nov 12, 2009 at 9:25
0

This doesn't look like a stack, it looks like a linked list. Of course you can implement stack-like semantics using a linked list, but it's generally not what is meant by "a stack".

To push a new number, you would need to:

  • Allocate a new StackNode.
  • Initialize it with the new number.
  • Link it to the previous.

You could encapsulate this into a function:

void stack_push(StackNode *root, int value)
{
    /* Code omitted */
}

Then you'd just need to provide the proper stack in order to control onto which of your many stacks to push the new number:

stack_push(&Stacks[2], 4711);
1
  • I have done that, but how to push the number to a specific stack that is linked to a specific number in the array. For example i want to push number (3) to Stacks[5] or pop a number from Stacks[2]
    – c2009l123
    Nov 12, 2009 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.