Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
//Programing in C language
Use an array of size 10 to implement an odd number stack and an even number
stack in the same array.
The odd numbers will be inserted from the left side of the array and even
numbers will be inserted from the right side of the array.

Write following functions and call in main :
1) Insert_odd 2) insert_even 3) isfull_odd 4) isfull_even 5) isempty_odd
6)isempty_even 7)iscollide_even_odd

What I have tried:

I have tried Implementation of stack.
#include  int stack[5]; int top = -1; int isempty() { if(top == -1)
return 1; else return 0; }
int isfull() { if(top == 4) {printf("Stack is full \n"); return 1; }
else return 0; } int pop() { int data; if(!isempty()) { data = stack(top); top = top - 1;
return data; } else { printf("Could not retrieve data, Stack is empty.\n"); } } void push(int data) { if(!isfull()) { top = top + 1; stack[top] = data; }
else { printf("Could not insert data, Stack is full.\n"); } } int main() { int data;
// push items on to the stack // push(1); push(2); push(3); push(4); push(5); push(6);
// print stack data // while(!isempty()) { data = pop(); printf("%d\n",data); }
printf("Stack full: %s\n", isfull()?"true":"false"); printf("Stack empty: %s\n", isempty()?"true":"false"); return 0;
}
Posted
Updated 1-Nov-21 23:55pm
Comments
Patrice T 2-Nov-21 5:47am    
Try to format your code using lines breaks where necessary.

Well your code apparently does NOT fulfill the requirements. It is a staring point though. You have to duplicate some variables (e.g. top: you need something like top_even and top_odd) and function (for instance you do need two insert functions). Moreover you have to check for odd/even collision.
 
Share this answer
 
You can't use copy and paste code from the internet for a stack and expect it to meet your assignment: you need to use an array in a different way.
You need two indexes: top_left and top_right together with the functions your teacher describes:
Insert_odd
insert_even
isfull_odd 
isfull_even
isempty_odd
isempty_even 
iscollide_even_odd
Because your array size if defined for you - 10 elements - your top_left and top_right indexes should start from 0 and 9 respectively.
The isempty_xxx functions are simple: they check if the appropriate index is at it's starting value.
iscollide_even_odd is also simple is the right index smaller than the left index?
The push and pop operations are very similar to those you found, but with the "right" ones going "downwards" instead of up.

Give it a try: this isn't complicated - if you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Quote:
Use an array of size 10 to implement an odd number stack and an even number stack in the same array.

You are requested to implement 2 stacks in 1 array of 10.
This means that your program must be able to store any mix of odd/even numbers as long as they are no more than 10 numbers.

What is your question ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900