diff --git a/Array in spiral order.cpp b/Array in spiral order.cpp new file mode 100644 index 0000000..060243c --- /dev/null +++ b/Array in spiral order.cpp @@ -0,0 +1,57 @@ +using namespace std; +#include + +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>a[i][j]; + } + } + + cout<<"\n Output:"; + + while(top<=bottom || right<=left) + { + for(i=left;i<=right;i++) + { + cout<=left;i--) + { + cout<=top;i--) + { + cout< +#include + +bool checkParentheses(string str){ + stack s; + for(int i=0; i>str; + if(checkParentheses(str)){ + cout<<" Yes the parentheses are balanced"; + }else{ + cout<<" No, the parentheses are not balanced"; + } + return 0; +} diff --git a/EvaluationPostfix.cpp b/EvaluationPostfix.cpp new file mode 100644 index 0000000..6660487 --- /dev/null +++ b/EvaluationPostfix.cpp @@ -0,0 +1,34 @@ +//Evaluation of Postfix expression using stack +using namespace std; +#include +#include +int evaluatePostfix(string str){ + stack S; + for(int i=0;i>str; + cout<<"Result after evaluation : "< +#include +int evaluatePrefix(string str){ + stack 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 : "< +#include + +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 S; + for(int i=0;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; +} diff --git a/ReverseALinkedList.cpp b/ReverseALinkedList.cpp new file mode 100644 index 0000000..463613f --- /dev/null +++ b/ReverseALinkedList.cpp @@ -0,0 +1,69 @@ +// reverse a linked list using stack + +#include +#include +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<data<<" "; + if(temp->next!=NULL){ + display(temp->next); + } +} +void reverseList( struct Node *temp) +{ + stack 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; +}