SOURCE CODE:
#include
#include
void push(int st[],int data,int &top);
void disp(int st[],int &top);
int pop(int st[],int &top);
int flg=0;
int top=-1,tos=-1;
int st[50];
void push(int st[],int data,int &top)
{
if(top==50-1)
flg=0;
else
{
flg=1;
top++;
st[top]=data;
}
}
int pop(int st[],int &top)
{
int pe;
if(top==-1)
{
pe=0;
flg=0;
}
else
{
flg=1;
pe=st[top];
top--;
}
return(pe);
}
void disp(int st[],int &top)
{
int i;
if(top==-1)
{
printf("\nStack is Empty");
}
else
{
for(i=top;i>=0;i--)
printf("\t%d",st[i]);
}
}
void main()
{
int dt,opt;
int q=0;
clrscr();
printf("This Program Is Used to Perform PUSH & POP operations On Stack");
printf("\n\n\tMain Menu.........");
printf("\n\n1.Push");
printf("\n\n2.Pop");
printf("\n\n3.Exit");
do
{
printf("\n\n\tEnter Your Choice 1-3:");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\nEnter the Element to be Push:");
scanf("%d",&dt);
push(st,dt,tos);
if(flg==1)
{
printf("\nAfter Inserting the Element, Stack is:\n\n");
disp(st,tos);
if(tos==50-1)
printf("\nStack is Now Full");
}
else
printf("\nStack Overflow Insertion Not Possible");
break;
case 2:
dt=pop(st,tos);
if(flg==1)
{
printf("\n\tData Deleted From the Stack is:%d\n",dt);
printf("\n\tAfter Deleting the Element from the stack is:\n\n");
disp(st,tos);
}
else
printf("\nStack Empty,Deletio Not Possible:");
break;
case 3:
q=1;
break;
default:printf("\nWrong Choice Enter 1-3 Only");
}
}while(q!=1);
}
OUTPUT
Main Menu.........
1.push
2.pop
3.exit
Enter your choice 1-3:1
Enter the element to be push:4
After inserting the elements,stack is:
4
Enter your choice 1-3:1
Enter the element to be push:7
After inserting the elements,stack is:
7 4
Enter your choice 1-3:1
Enter the element to be push:4
#include
#include
void push(int st[],int data,int &top);
void disp(int st[],int &top);
int pop(int st[],int &top);
int flg=0;
int top=-1,tos=-1;
int st[50];
void push(int st[],int data,int &top)
{
if(top==50-1)
flg=0;
else
{
flg=1;
top++;
st[top]=data;
}
}
int pop(int st[],int &top)
{
int pe;
if(top==-1)
{
pe=0;
flg=0;
}
else
{
flg=1;
pe=st[top];
top--;
}
return(pe);
}
void disp(int st[],int &top)
{
int i;
if(top==-1)
{
printf("\nStack is Empty");
}
else
{
for(i=top;i>=0;i--)
printf("\t%d",st[i]);
}
}
void main()
{
int dt,opt;
int q=0;
clrscr();
printf("This Program Is Used to Perform PUSH & POP operations On Stack");
printf("\n\n\tMain Menu.........");
printf("\n\n1.Push");
printf("\n\n2.Pop");
printf("\n\n3.Exit");
do
{
printf("\n\n\tEnter Your Choice 1-3:");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\nEnter the Element to be Push:");
scanf("%d",&dt);
push(st,dt,tos);
if(flg==1)
{
printf("\nAfter Inserting the Element, Stack is:\n\n");
disp(st,tos);
if(tos==50-1)
printf("\nStack is Now Full");
}
else
printf("\nStack Overflow Insertion Not Possible");
break;
case 2:
dt=pop(st,tos);
if(flg==1)
{
printf("\n\tData Deleted From the Stack is:%d\n",dt);
printf("\n\tAfter Deleting the Element from the stack is:\n\n");
disp(st,tos);
}
else
printf("\nStack Empty,Deletio Not Possible:");
break;
case 3:
q=1;
break;
default:printf("\nWrong Choice Enter 1-3 Only");
}
}while(q!=1);
}
#include<iostream>
#include<list>
class Stack<T> {
public:
T& top (void) { return m_container.front(); }
const T& top (void) const { return m_container.top(); } void pop (void) { m_container.pop_front(); }
void push (T val) { m_container.push_front(val); }
bool empty (void) const { return m_container.empty(); }
private:
std::list<T> m_container {};
};
int main (void) {
Stack<int> s;
std::cout << "Pushing: 42\n";
s.push (42);
std::cout << "Pushing: 67\n";
s.push (67);
while (!s.empty()) {
std::cout << "Extracting: " << s.top() << std::endl;
s.pop();
}
}
Explain The merits of using a deque to implement a stack in data structure
Design an algorithm to show the different operations on a stack?
A stack is implicitly sorted by hierarchical nested order. It does not make sense to sort a stack. Do you mean a list? If so, please ask the question again.
2. Write a program using switch statement that reads a character representing a geometrical figure, then asks the user to enter the required data (ex. Radius for a circle, length and height for a rectangle, etc. ...) . The program should then print the area and circumference.Figures are: circle(c), square(s), rectangle(r), triangle (t).
/* C program to implement stack. Stack is a LIFO data strcuture LIFO - Last in First Out Perform PUSH(insert operation), POP(Delete operation) and Display stack */#include #include #define MAXSIZE 5struct stack /* Structure definition for stack */{int stk[MAXSIZE];int top;};typedef struct stack STACK;STACK s;/* Function declaration/Prototype*/void push (void);int pop(void);void display (void);void main (){int choice;int option = 1;clrscr ();s.top = -1;printf ("STACK OPERATION\n");while (option){printf ("--------------\n");printf (" 1 -> PUSH \n");printf (" 2 -> POP \n");printf (" 3 -> DISPLAY \n");printf (" 4 -> EXIT \n");printf ("--------------\n");printf ("Enter your choice\n");scanf ("%d", &choice);switch (choice){case 1: push();break;case 2: pop();break;case 3: display();break;case 4: return;}fflush (stdin);printf ("Do you want to continue(Type 0 or 1)?\n");scanf ("%d", &option);}}/*Function to add an element to the stack*/void push (){int num;if (s.top -1){printf ("Stack is empty\n");return;}else{printf ("\nThe status of the stack is\n");for (i = s.top; i >= 0; i-){printf ("%d\n", s.stk[i]);}}printf ("\n");}byankit shukla
Stack implementations allow us to easily implement backtracking algorithms.
stack abstract datatype
You would need an array to store the actual stack. Better use an ArrayList or some other structure that you can redimension. You'll also need a variable to point to the "top of stack" - the last element added, which is the first element to be taken away.
int top=-1; int stack[10];
Yes it is possible to implement stack and queue using linked list
This is not possible as this violates the basic definition of stack...That can have only two primitive operations "Push" "POP" If u want to implement what u want u can do that by some implementation of Push pop combination but that is what other data strutures like queue do....
Explain The merits of using a deque to implement a stack in data structure
Design an algorithm to show the different operations on a stack?
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.
chk out the following link showing you how to implement stack. Though it is a c++ implementation it may still help you. http://thetechnofreaks.com/2011/10/26/4-creating-a-stack/#ixzz1bvp8FXdb
Heap is a data-structure, it cannot implement anything. On the other hand, it is true that: 1. Recursive routines might use heap. 2. You can use dynamic memory allocation (heap), to implement a stack; and use the stack to implement recursion.
A stack is implicitly sorted by hierarchical nested order. It does not make sense to sort a stack. Do you mean a list? If so, please ask the question again.