GeeksforGeeks » Linked List specific questions
programs of data structures
(2 posts)-
1.program to implement two stacks of integers using a single array. The (Single) array will have two stack pointers (SP). The first stack pointer will manage all even numbers. This SP will initially be at the beginning of the array. The second stack pointer will manage all odd numbers and shall initially point to the end of the array. Your program should use the appropriate stack pointer depending on whether the number is odd or even to push into the stack. For popping ask the user whether the number to be popped is odd or even and then pop using the correct stack pointer.
Ensure that the two stack pointers do not cross over. But the challenge is to see that the array is utilised optimally. Do not fix half the array for even and half for odd numbers. -
{ int max=80,a[80];
for(int i=0;i<80;i++)
{ cout<< "eneter the no\n";
cin>>b;
if(b%2==0)
{ a [i]=b;}
else
{ a[max-1]=n; max--;}}
for(int i=0;i<max;i++)
{ if(a[i]%2==0)
{ stack1.push(a[i]);}
if(a[i]%2!=0)
{ stack2.push(a[i]);}
}
Reply
You must log in to post.