answersLogoWhite

0


Best Answer

/*implementing two stacks in 1 array*/ #include #include #include int size,top1=-1,top2,a[100]; void push(); void pop(); void peep(); void main() { int i,ch; char c='y'; clrscr(); printf("\Enter size:"); scanf("%d",&size); top2=size; while(c=='y') { clrscr(); printf("\n ###### MENU #####\n"); printf("\n1: push"); printf("\n2: pop"); printf("\n3: peep"); printf("\n4: exit") ; printf("\n Enter choice:"); fflush(stdin); scanf("%d",&ch); switch(ch) { case 1:push(); break; case 2:pop(); break; case 3:peep(); break; case 4:exit(0); default:printf("\n wrong choice"); } printf("\n Another operation(y/n):"); fflush(stdin); scanf("%c",&c); } exit(0); getch(); } void push() { int item,ch; if(top1+1==top2) { printf("stack is full "); printf("\n cant push "); } else { printf("\nEnter item to be inserted:"); scanf("%d",&item); printf("\n enter choice(1 for stack1 else stack2):"); scanf("%d",&ch); if(ch==1) { top1++; a[top1]=item; } else { top2--; a[top2]=item; } } } void pop() { int ch; printf("\n Enter from which stack u want to pop(1 for stack1/else stack2):"); scanf("%d",&ch); if(ch==1) { if(top1==-1) printf("\n cant delete from stack 1 its empty"); else printf("\n item deleted is:%d" ,a[top1--]); } else { if(top2==size) printf("\n cant delete from s2its empty"); else printf("\n item deleted is: %d" ,a[top2++]); } } void peep() { int i; printf("\n stck 1:" ); for(i=0;i=top2;i--) printf(" %d",a[i]); }

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

Simple. Make each element in the array a stack.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Two stacks in one array
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Explain the Different types of array?

one dementional array and two dementional array


What are the parts of the thylakoid membranes arranged in stacks called?

One stack is a granum. Two or more stacks are called grana.


How does two dimensional array differ from single dimensional array?

A one dimensional array is a scalar value repeated one or more times.A two dimensional array is an array of one dimensional arrays.A three dimensional array is an array of two dimensional arrays, and so forth.The one dimensional array is like a list of things, where the two dimensional array is like an array of things. (Think one row of a spreadsheet versus the whole spreadsheet.)[addendum]Every level of array depth is also a level of pointer depth. For example: A 3 dimensional int array is an int***. So a one dimensional int array is an int*, and a two dimensional int array is an int**. This is only important if you are doing pointer work, but it can become very important.


A sentence for array?

Array your infantry in two lines, one behind the other.


How do you implement stack without array?

Stacks are often implemented using the same node structure as a linked list.


How do you merge two array without using function?

Take another array big enough to hold both array copy content of these two array into new one. You merged two array and haven't used a single function.!


What are the kinds of arrays?

1. One dimension array 2. Two dimension array 3. Multi dimentional array


What does a 2 dimensional array do?

A two dimensional array is a one-dimensional array of one-dimensional arrays. That is, just as we can have an array of integers, we can also have an array of integer arrays. This idea can be extended such that we can have an array of two-dimensional arrays (a three-dimensional array), and so on. We typically use a two-dimensional array to represent a table of rows and columns, where each row is a one-dimensional array.


What is a multi array?

A two-dimensional array is the simplest multi-dimensional array and is implemented as a one-dimensional array where every element is itself a one-dimensional array. We can imagine a two-dimensional array as being a table of rows and columns where every row is an array in its own right. A three-dimensional array is simply a one-dimensional array of two-dimensional arrays, which can be imagined as being an array of tables. Extending the concept, a four-dimensional array is a table of tables. Multi-dimensional arrays may be jagged. That is, a two-dimensional array may have rows of unequal length. Unlike regular arrays, jagged arrays cannot be allocated in contiguous memory. Instead, we use the outer array (the first dimension) to store pointers to the inner arrays. An array of strings (character arrays) is an example of a two-dimensional jagged array.


How will you initialize a 2-d array?

All arrays are one-dimensional. A two-dimensional array is simply a one-dimensional array of one-dimensional arrays: int a[2][3]; This is an array of 2 elements where each element is itself an array of 3 integers. In other words it is an array of 6 integers. The two dimensions simply allow us to split the array into two sub-arrays of 3 elements each.


How many types of arrays in c?

An array is simply a contiguous block of memory containing two or more elements. There are two types of array: a static array which is allocated on the stack at compile time; and a dynamic array which is allocated on the heap at runtime. Both can be one-dimensional or multi-dimensional. A one-dimensional array can be likened to a row (or column) of chessboard squares, with as many squares as required to store all the elements. A multi-dimensional array is any array with two or more dimensions. A two-dimensional array can be likened to the whole chessboard, where any square can be identified by its row and column index. However the dimensions needn't be equal. A two-dimensional array can also be imagined as a one-dimensional array where every element is simply another one-dimensional array. Three-dimensional arrays can be likened to a cube, or as a one-dimensional array of two-dimensional arrays. A four-dimensional array can be linked to a one-dimensional array of three-dimensional arrays, and so on. Although every one-dimensional array must be allocated in contiguous memory, multi-dimensional arrays can be dynamically allocated so that each dimension is itself a separately allocated one-dimensional array of pointers to the next dimension, making it possible to allocate extremely large arrays over a series of smaller allocations rather than as a single contiguous block.


What is single dimensional array in c plus plus?

A one dimensional array is an array of objects that goes in one "direction". Any array with only one [] is a one dimensional array. For example: int numbers[6]; is a one dimensional array. int numbers[6][3]; is a two dimensional array.Graphical terms:One dimensional array[4]:14 - 75 - 8164 - 234Two dimensional array[2][3]:47 - 178108 - 8517 - 128It didn't come out quite how I wanted it...