Showing posts with label Stack. Show all posts
Showing posts with label Stack. Show all posts

Thursday, June 11, 2015

DFS tree in C++

#include<iostream.h>
#include<conio.h>
int cost[10][10],i,j,k,n;
int stack[10],top,v,visit[10],visited[10];
void main()
{
clrscr();
int m;
cout <<"Enter no of vertices\Nodes: ";
cin >> n;
cout <<"\nEnter no of Edges: ";
cin >> m;
cout <<"\n\tEnter All EDGES Source & Distination\n";
for(k=1;k<=m;k++)
{
cout<<"\nEnter Source: ";
cin >>i;
cout<<"\nEnter Distination: ";
cin>>j;
cost[i][j]=1;
cout<<"\n-----------------------\n";
}
cout <<"\nEnter initial vertex: ";
cin >>v;
cout <<"\nORDER OF VISITED VERTICES:\n";
cout <<v<<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=n;j>=1;j--)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stack[top]=j;
top++;
}
v=stack[--top];
cout<<v << " ";
k++;
visit[v]=0;
visited[v]=1;
}
getch();
}

Thursday, January 22, 2015

Program to evaluate an expression entered in post-fix form in C++

#include <iostream.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <conio.h>
const int MAX = 50 ;
class postfix
{
int stack[MAX] ;
int top, nn ;
char *s ;
public :
postfix( ) ;
void setexpr ( char *str ) ;
void push ( int item ) ;
int pop( ) ;
void calculate( ) ;
void show( ) ;
} ;
postfix :: postfix( )
{
top = -1 ;
}
void postfix :: setexpr ( char *str )
{
s = str ;
}
void postfix :: push ( int item )
{
if ( top == MAX - 1 )
cout << endl << "Stack is full" ;
else
{
top++ ;
stack[top] = item ;
}
}
int postfix :: pop( )
{
if ( top == -1 )
{
cout << endl << "Stack is empty" ;
return NULL ;
}
int data = stack[top] ;
top-- ;
return data ;
}
void postfix :: calculate( )
{
int n1, n2, n3 ;
while ( *s )
{
if ( *s == ' ' || *s == '\t' )
{
s++ ;
continue ;
}
if ( isdigit ( *s ) )
{
nn = *s - '0' ;
push ( nn ) ;
}
else
{
n1 = pop( ) ;
n2 = pop( ) ;
switch ( *s )
{
case '+' :
n3 = n2 + n1 ;
break ;
case '-' :
n3 = n2 - n1 ;
break ;
case '/' :
n3 = n2 / n1 ;
break ;
case '*' :
n3 = n2 * n1 ;
break ;
case '%' :
n3 = n2 % n1 ;
break ;
case '$' :
n3 = pow ( n2 , n1 ) ;
break ;
default :
cout << "Unknown operator" ;
exit ( 1 ) ;
}

push ( n3 ) ;
}
s++ ;
}
}
void postfix :: show( )
{
nn = pop ( ) ;
cout << "Result is: " << nn ;
}

void main( )
{
clrscr();
char expr[MAX] ;
cout << "\nEnter postfix expression to be evaluated : " ;
cin.getline ( expr, MAX ) ;
postfix q ;
q.setexpr ( expr ) ;
q.calculate( ) ;
q.show( ) ;
getch();
}


Wednesday, October 29, 2014

Decimal to Binary Conversion using Stack in C++

#include<iostream.h>
#include<conio.h>
class stack{
private:
int top,max;
int *stackarr;
public:
stack()
{
cout<<"\nEnter the size of stack: ";
cin>>max;
top=-1;
}
void push(int n)
{
if(top==max)
cout<<"\nStack is full\n";
else
top=top+1;
stackarr[top]=n;
}
int pop()
{
if(top==-1)
cout<<"\nStack is empty\n";
else
top=top-1;
return stackarr[top+1];
}
void decimal(int num)
{
cout<<"\n\nThe decimal to binery convertion of "<<num<<" is Below.\n ";
   for(num;num>0;num=num/2)
  {  push(num%2);       }
while(top!=-1)
  { cout<<pop()<<" ";   }
}
};
void main()
{
clrscr();
int value=0;
stack obj;
cout<<"\nEnter a number in Decimal notaion : ";
cin>>value;
obj.decimal(value);
getch();
}

Tuesday, October 28, 2014

Implementation of Stacks

"Implementation of Stacks in Array through classes in C++"


#include<iostream.h>
#include<conio.h>
class stack{
private:
int top,max;
int *stackarr;
public:
stack()
{
cout<<"\nEnter the size of stack: ";
cin>>max;
top=-1;
int *stackarr=new int[max];
}
void push(int n)
{
if(top==max)
cout<<"\nStack id full\n";
else
top=top+1;
stackarr[top]=n;
}
void pop()
{
if(top==-1)
cout<<"\nStack is empty\n";
else
cout<<"\nThe stack element "<<stackarr[top]<<" is poped\n";
top=top-1;
}
void display()
{

for(int i=0;i<=top;i++)
cout<<"\nvalue is :"<<stackarr[i];
}
};
void main()
{
clrscr();
int a=0,value=0;
stack obj;
while(getch()!=27)
{
cout<<"\n \n\n\t\tSelect an option:\n 1   for Push\n 2   for Pop\n 3   for Display ";
cin>>a;
clrscr();
switch(a)
{
case 1:
{
cout<<"\nEnter value for index: ";
cin>>value;
obj.push(value);
break;
}
case 2:
obj.pop();
break;
case 3:
obj.display();
break;
}
cout<<"\n\n\t   || Press Esc Key to exit || Any Key For next Operation  ||";
}
}

Saturday, September 20, 2014

Data Structure Programs

Read Also:  


"All Data Structure Programs up-till now"

To get code Click on that program heading :) ☺ ☻ 

 ALL Type Of Sorting Click here :: most important :: 

-------------------

`Queues 

----------------

`Single Link-List:

*  How to create single Link-list in C++

*  Insertion at any point in link-list in C++

*  Concatenate or Merge two Link-lists in C++

Deletion At any point in Circular Link List in C++
------------------------------------

`Double Link-list:

=================

Stacks Data Structure:


Decimal to binary conversion using stack in c++
============ 

How to add two matrix size is define by use






Program to print counting 10 to 1 using Recursion

--------------------------------------------------