Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Array in spiral order.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using namespace std;
#include<iostream>

int main()
{
int a[20][20],row,col,i,j,top=0,bottom,left=0,right,k=1;

cout<<"\n Enter total number of rows :";
cin>>row;
cout<<"\n Enter total number of column :";
cin>>col;

right=col-1;
bottom=row-1;
for(i=0;i<row;i++)
{
cout<<"Enter "<<col<<" element of row "<<i+1<<" :";
for(j=0;j<col;j++)
{
cin>>a[i][j];
}
}

cout<<"\n Output:";

while(top<=bottom || right<=left)
{
for(i=left;i<=right;i++)
{
cout<<a[top][i]<<",";
}
top++;

for(i=top;i<=bottom;i++)
{
cout<<a[i][right]<<",";
}

right--;

for(i=right;i>=left;i--)
{
cout<<a[bottom][i]<<",";
}

bottom--;

for(i=bottom;i>=top;i--)
{
cout<<a[i][left]<<",";
}

left++;

}
return 0;
}
38 changes: 38 additions & 0 deletions BalancedParentheses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using namespace std;
#include<iostream>
#include<stack>

bool checkParentheses(string str){
stack<char> s;
for(int i=0; i<str.length();i++){
if(str[i]=='(' || str[i]=='{' || str[i]== '['){
s.push(str[i]);
}
else if(str[i]==')' || str[i]=='}' || str[i]== ']'){
if(s.empty())
return false;
else if(str[i]==')' && s.top()!= '(')
return false;
else if(str[i]=='}' && s.top()!= '{')
return false;
else if(str[i]==']' && s.top()!= '[')
return false;
else
s.pop();
}
}
if(s.empty())
return true;
return false;
}
int main()
{
string str;
cin>>str;
if(checkParentheses(str)){
cout<<" Yes the parentheses are balanced";
}else{
cout<<" No, the parentheses are not balanced";
}
return 0;
}
34 changes: 34 additions & 0 deletions EvaluationPostfix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//Evaluation of Postfix expression using stack
using namespace std;
#include<iostream>
#include<stack>
int evaluatePostfix(string str){
stack<int> S;
for(int i=0;i<str.length();i++){
if(isdigit(str[i])){
S.push(str[i]-'0');
}
else{
int op2 = S.top();
S.pop();
int op1 = S.top();
S.pop();
switch( str[i] ){
case '+' : S.push(op1+op2); break;
case '-' : S.push(op1-op2); break;
case '*' : S.push(op1*op2); break;
case '/' : S.push(op1/op2); break;
case '^' : S.push(op1^op2); break;
}
}
}
return S.top();
}
int main()
{
string str;
cout<<"Enter the expression to be evaluated :";
cin>>str;
cout<<"Result after evaluation : "<<evaluatePostfix(str);
return 0;
}
35 changes: 35 additions & 0 deletions EvaluationPrefix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//Evaluation of Prefix expression using stack
using namespace std;
#include<iostream>
#include<stack>
int evaluatePrefix(string str){
stack<int> S;
for(int i=str.length()-1;i>=0;i--){
if(isdigit(str[i])){
S.push(str[i]-'0');
}
else{
int op1 = S.top();
S.pop();
int op2 = S.top();
S.pop();
switch( str[i] ){
case '+' : S.push(op1+op2); break;
case '-' : S.push(op1-op2); break;
case '*' : S.push(op1*op2); break;
case '/' : S.push(op1/op2); break;
case '^' : S.push(op1^op2); break;
}
}
}
return S.top();
}
int main()
{
string str;
cout<<"Enter the expression to be evaluated :";
cin>>str;
cout<<"Result after evaluation : "<<evaluatePrefix(str);
return 0;
}

63 changes: 63 additions & 0 deletions InfixToPostfix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using namespace std;
#include<iostream>
#include<stack>

int prec(char c){
if(c == '^')
return 3;
else if( c== '*' || c == '/')
return 2;
else if( c== '+' || c=='-')
return 1;
else{
return -1;
}
}

string postfix(string str){
string result="";
stack<char> S;
for(int i=0;i<str.length();i++){
if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= '0' && str[i] <= '9')){
result+=str[i];
}

else if(str[i]=='(')
S.push(str[i]);

else if(str[i]==')'){
while(S.top()!='('){
result +=S.top();
S.pop();
}
S.pop();
}
else{
if(S.empty())
S.push(str[i]);
else if(prec(str[i])>prec(S.top())){
S.push(str[i]);
}
else{
while(!S.empty() && (prec(str[i])<=prec(S.top()))){
result+=S.top();
S.pop();
}
S.push(str[i]);
}
}
}
while(!S.empty()){
result+=S.top();
S.pop();
}
return result;
}

int main(){
string str;
cout<<"enter the infix expression: ";
cin>>str;
cout<<"the postfix expression of the given infix expression is :"<< postfix(str);
return 0;
}
69 changes: 69 additions & 0 deletions ReverseALinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// reverse a linked list using stack

#include<iostream>
#include<stack>
using namespace std;
struct Node{
int data;
Node *next;
};
Node *head =NULL;

void insertval(){
Node *temp= new Node();
int num;
cout<<"enter the value to be inserted:";
cin>>num;
temp->data= num;
temp->next=head;
head=temp;
}
void display( struct Node *temp){
cout<<temp->data<<" ";
if(temp->next!=NULL){
display(temp->next);
}
}
void reverseList( struct Node *temp)
{
stack<struct Node*> S;
if(head==NULL){ return;}
while(temp!=NULL){
S.push(temp);
temp=temp->next;
}
temp=S.top();
head=temp;
S.pop();
while(!S.empty()){
temp->next = S.top();
S.pop();
temp=temp->next;
}
temp->next=NULL;
cout<<"Reversed list :";
display(head);
}

int main()
{
int ch;
do{
cout<<"\n Enter your choice :";
cout<<"\n 1. Insert a new element ";
cout<<"\n 2. Display ";
cout<<"\n 3. Reverse the list";
cout<<"\n 4. Exit ";
cin>>ch;
if(ch==1){
insertval();
}
else if(ch==2){
display(head);
}
else if(ch==3){
reverseList(head);
}
}while(ch!=4);
return 0;
}