//Program on Stack ADT Using Arrays
#include<iostream.h>
#include<conio.h>
#define maxSize 10
#include<stdlib.h>
class stack
{
public:
stack(); // constructor call the display function
void pop(); //used to pop the value as per user demand
void push(); //used for push the value as per user demand
int empty(); //used to check the stack
int full(); //used to check the stack whether it is full as max limit is 80
void display(); //used to display menu for operations
void stackDisplay(); //used to display whole stack items
void operation(); //used to enter the user choice
private:
int top;
int item[maxSize];
};
stack::stack()
{
top = 0;
display();
}
void stack :: display()
{
cout << "STACK MAIN MENU GIVEN BELOW" << endl;
cout << "PRESS 1 FOR PUSH THE ITEM ON STACK" << endl;
cout << "PRESS 2 FOR POP THE ITEM ON STACK" << endl;
cout << "PRESS 3 FOR DISPLAY THE WHOLE STACK" << endl;
cout << "PRESS 4 FOR EXIT THE PROGRAM" << endl;
cout<<"ENTER YOUR CHOICE" << endl;
operation();
}
void stack :: operation()
{
int choice;
cin >> choice;
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
stackDisplay();
break;
case 4:
exit(4);
default:
cout << "PLZ ENTER VALID NUMBER" <<endl;
operation();
}
}
int stack::empty()
{
if( top 0)
{
cout<<"THE STACK IS EMPTY UNDERFLOW" << endl;
cin.get();
}
else
{
char ch;
cout<<"AS THE LAST ELEMENT IN STACK IS "<<item[top] << endl;
top = top-1;
cout<<"WANT TO POP OUT ANOTHER ELEMENT y/n" << endl;
cin>>ch;
if(ch=='y')
pop();
cin.get();
}
cout<<"PRESS ENTER TO GO TO MAIN MENU " << endl;
cin.get();
clrscr();
display();
}
void stack::stackDisplay()
{
cout<<"THE ELEMENTS IN STACK IS"<<endl;
for(int i = 1;i <= top; i++)
{
cout << item[i] << endl;
}
cin.get();
cout<<"PRESS ENTER FOR MAIN MENU " << endl;
cin.get();
clrscr();
display();
}
void main()
{
clrscr();
gotoxy(15,10);
cout<<"WELCOME TO THE PROGRAM OF STACK MADE BY SAURABH " << endl;
gotoxy(15,11);
cout<<"________________________________________________" << endl;
cin.get();
clrscr();
stack obj;
getch();
}
BY. SAURABH (GNDU RC JAL CSE 2nd YEAR)
A stack is a last-in first-out data structure. To create a stack with an array, all insertions (pushes) and extractions (pops) occur at the back of the array.
// declare a stack of integers using an array (a vector is a variable-length array)
std::vector<int> stack;
// push 10 values onto the stack
for (int i=0; i<10; ++i) stack.push_back (i);
// print top of stack and pop until the stack is empty
while (!stack.empty()) {
std::cout<<stack.back()<<std::endl;
stack.pop_back();
}
The output will be the input values (0-9) in reverse order (9-0).
The following program creates a stack ADT with cat and dog objects. The stack is initialised with between 1 and 10 cat and dog elements picked randomly. Thereafter, there's a 1/5 chance a cat or a dog will be added to the stack and a 3/5 chance the stack will be popped. The program ends when the stack is empty.
Note that each ADT is given an unique value to identify it, thus making it easier to see which element is being pushed and popped.
Note also that using an array (or vector) is an inefficient method of implementing a stack. The main problem with arrays is the need to reallocate the entire array in contiguous memory whenever insufficient memory exists to accommodate new elements. Vectors hide the details of this reallocation process, but it is important to keep this in mind. Also, arrays should only be used when you need constant-time random access to any element in the array, which is clearly not a requirement of a stack where only the last element requires constant-time access.
Singly-linked lists are more efficient when implementing stack ADTs as the individual nodes are allocated and deallocated in non-contiguous memory as and when they are required, with constant-time access to the last node. This both saves memory and improves overall performance.
#include<iostream>
#include<vector>
#include<string>
#include<time.h>
// all elements in the array must be derived from the ADT class
struct ADT
{
static int s_num;
int m_num;
std::string m_type;
ADT (std::string type): m_type (type), m_num (++s_num) {}
std::string get_type () const { return (m_type); }
int get_num () const { return (m_num); }
virtual std::string speak () const = 0;
};
int ADT::s_num = 0;
std::ostream& operator<<(std::ostream& os, const ADT& adt)
{
os << adt.get_type() << ' '
<< adt.get_num() << ' '
<< adt.speak();
return (os);
}
std::vector<ADT*> stack;
struct cat : ADT
{
cat () : ADT("cat") {}
std::string speak () const { return ("miaow"); }
};
struct dog : ADT
{
dog () : ADT("dog") {}
std::string speak () const { return ("woof"); }
};
void pop ()
{
if (stack.size())
{
ADT* adt = stack.back ();
std::cout << "Popping " << *adt << std::endl;
stack.pop_back ();
delete (adt);
}
}
void push (ADT& adt)
{
std::cout << "Pushing " << adt << std::endl;
stack.push_back (&adt);
}
void push ()
{
// randomly push a cat or dog onto the stack
int rnd = rand() % 2;
switch (rnd)
{
case (0) : push ( *(new cat())); break;
case (1) : push ( *(new dog())); break;
}
}
int main()
{
srand ((unsigned) time (NULL));
// initialise stack with 1-10 random elements
int rnd = rand () % 10 + 1;
for( int i = 0; i < rnd; ++i )
push ();
do
{
// 2/5 chance of pushing
// 3/5 chance of popping
rnd = rand () % 5;
switch (rnd)
{
case (0) :
case (1) : push(); break;
default: pop();
}
}
while (stack.size());
}
Example output:
Pushing dog 1 woof
Pushing dog 2 woof
Pushing dog 3 woof
Pushing cat 4 miaow
Pushing cat 5 miaow
Pushing dog 6 woof
Pushing dog 7 woof
Pushing cat 8 miaow
Pushing dog 9 woof
Popping dog 9 woof
Pushing dog 10 woof
Pushing cat 11 miaow
Pushing cat 12 miaow
Popping cat 12 miaow
Pushing dog 13 woof
Popping dog 13 woof
Popping cat 11 miaow
Popping dog 10 woof
Pushing dog 14 woof
Pushing cat 15 miaow
Popping cat 15 miaow
Pushing cat 16 miaow
Pushing cat 17 miaow
Pushing cat 18 miaow
Popping cat 18 miaow
Popping cat 17 miaow
Pushing cat 19 miaow
Popping cat 19 miaow
Pushing dog 20 woof
Pushing cat 21 miaow
Popping cat 21 miaow
Popping dog 20 woof
Popping cat 16 miaow
Popping dog 14 woof
Pushing cat 22 miaow
Pushing cat 23 miaow
Pushing cat 24 miaow
Pushing dog 25 woof
Popping dog 25 woof
Popping cat 24 miaow
Popping cat 23 miaow
Popping cat 22 miaow
Pushing cat 26 miaow
Popping cat 26 miaow
Popping cat 8 miaow
Popping dog 7 woof
Popping dog 6 woof
Popping cat 5 miaow
Popping cat 4 miaow
Popping dog 3 woof
Popping dog 2 woof
Pushing dog 27 woof
Pushing cat 28 miaow
Pushing cat 29 miaow
Popping cat 29 miaow
Pushing cat 30 miaow
Popping cat 30 miaow
Popping cat 28 miaow
Popping dog 27 woof
Popping dog 1 woof
import java.util.Scanner;
public class AStack<E> implements STackADT<E>
{
Integer arr[];
int top,size;
AStack(int n)
{
size=n;
top=-1;
arr=new Integer[size];
}
public boolean isEmpty()
{
if(top==-1)
return true;
else
return false;
}
public void pop()
{
if(top>=0)
{
System.out.println("the deleted element is "+arr[top]);
}
else
{
System.out.println("stack is empty");
}
}
public void push(E element)
{
if(top==size-1)
{
System.out.println("stack over flow");
}
else
{
top=top+1;
arr[top]=(Integer)element;
System.out.println("added succesfully");
}
}
@SuppressWarnings("unchecked")
public E top()
{
if(top>=0)
return (E)arr[top];
else
{
return null;
}
}
@SuppressWarnings("unchecked")
public static void main(String args[])
{
System.out.println("Enter size of Array:");
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
AStack as=new AStack(n);
while(true){
System.out.println("1.Check empty\n2.Delete element\n3.Add element\n4.Display Peeek Element\nEnter your choice");
int z=scan.nextInt();
switch(z)
{
case 1:
if(as.isEmpty())
{
System.out.println("No elements in stack");
}
else
{
System.out.println("stack is not empty");
}
break;
case 2:
as.pop();
break;
case 3:
System.out.println("Enter the element");
Scanner sa1=new Scanner(System.in);
Integer n1=sa1.nextInt();
as.push(n1);
break;
case 4:
if(as.top()!=null)
{
System.out.println("The element is:"+as.top());
}
else{System.out.println("stack is empty");}
break;
}
}
}
}
CREATE PACKAGE given bellow
package stack;
public interface STackADT<E>
{
public E top();
public void pop();
public void push(E element);
public boolean isEmpty();
}
Arrays are static structures and are unsuitable for implementing stacks. You could use a dynamic array, but these should only be used when resizing can be kept to a minimum, which is not the case with stacks.
In any case, the STL (standard template library) already provides std::stack so there's really no point in implementing your own stack. The std::stack provides a std::deque as the underlying container, but you can also use any container that supports the back(), push_back() and pop_back() methods, including vectors and lists.
The main problem to overcome is the data type of the container. If you push and pop the same data type then there is no problem, but if you need to store a variety of different data types then you need to derive all those data types from a generic data type. Pushing generic data types onto the stack isn't a major problem, but popping them off means you must provide some way of determining the specific data type. Pure-virtual methods are the obvious choice here, thus the generic data type needs to be an abstract base class which then becomes the parameter for your underlying container class in the stack.
That depends on where you define them. Arrays defined inside functions are declared on the stack (like other variables defined in functions). Arrays defined outside of any function, or using the static keyword inside a function are allocated in the static data area of the program. Other arrays may be allocated using malloc() (or "new" in C++); these are allocated on the heap.
The purpose of using arrays in C is to store multiple values in one variable. Then you can make programs that use arrays like lists, printing values from multiple arrays into one line. It take memory in continues block then we can know memory location easily. We can retrieve data quickly.
huh?
You can write a C++ fib pro using arrays but the problem is the prog becomes very complicated since u need to pass the next adding value in an array.....
You have misread or misunderstood something.
That depends on where you define them. Arrays defined inside functions are declared on the stack (like other variables defined in functions). Arrays defined outside of any function, or using the static keyword inside a function are allocated in the static data area of the program. Other arrays may be allocated using malloc() (or "new" in C++); these are allocated on the heap.
abdulrahman
In order to write a program to convert stack into queue using c language you must be able to identify the proper program. Having a special certification in programing will be beneficial as well to make sure you recognize the proper queues for the programs.
The purpose of using arrays in C is to store multiple values in one variable. Then you can make programs that use arrays like lists, printing values from multiple arrays into one line. It take memory in continues block then we can know memory location easily. We can retrieve data quickly.
huh?
Write computer-programs, I suppose.
7x9 is the multiplication fact that can be found using the arrays 2x9 and 5x9.
By using the library function #define A[] we can define the size of arrays
You can write a C++ fib pro using arrays but the problem is the prog becomes very complicated since u need to pass the next adding value in an array.....
http://www.assignmentsclub.com/
Write in whatever you want so long as it's not someone else's book! You can write in a notebook, a journal, or just a legal pad or stack of blank paper. You can even write using your computer or tablet.
leakage in arrays occur when you declare an array with big size and using only very few bytes.