User:Prasadshekhar29/sandbox

linked list' using namespace std; template class node { public:t data; node * next; node { data=0; next=NULL; } }; template class linkedlist { node*first; public:linkedlist { first=NULL; } void insertion(t); /*void printlist; void remove; void successor(t); void predecessor(t);*/ }; template void linkedlist::insertion (t data) { node*ptr,*lptr; if(first==NULL) { ptr=new node; first=ptr; ptr->data=data; ptr->next=NULL; } else { ptr=first; while(ptr!=NULL) { lptr=ptr; ptr=ptr->next; } ptr=new node; ptr->data=data; ptr->next=NULL; lptr->next=ptr; } cout<<"inserted"<>ch; switch(ch) { case 1:cout<<"enter the element"; cin>>data; list.insertion(data); break; /*case 2:cout<<"enter the element" cin>>data; list.find(data); break; case 3:list.printlist; break; case 4:cout<<"enter the index\n"; cin>>data; list.remove(data); break; case 5:cout<<"enter the element"; cin>>data; break; case 6:cout<<"enter an element"; cin>>data; list.successor(data); break;*/ case 7:exit(0); break; } } } stack using linked list using namespace std; template class node { public: t data; node *next; node { data=0; next=NULL; } }; class stack { node *first; public: stack { first=NULL; } void push(t); t pop; void display; }; template void stack::push(t data) { node*ptr=first; if(ptr==NULL) { ptr=new node; ptr->data=data; ptr->next=NULL; first=ptr; } else { ptr=new node; ptr->data=data; ptr->next=first; first=ptr; } } template t stack::pop { if(first==NULL) { return 1; } else { t data=first->data; first=first->next; return data; } } template void stack::display { node*ptr=first; while(ptr!= NULL) { cout<data; ptr=ptr->next; } } int main { stack st; int n,ch; while(1) { cout<<"1.push \n 2.pop \n 3.exit \n 4.display \n"; cout<<"enter your choice"; cin>>ch; switch(ch) { case 1:cout<<"enter element"; cin>>n; st.push(n); break; case 2:cout<<"poped value is"<>p; int m=p.length; for(i=0;i<m;i++) { if(isalnum(p[i])) { a1.push(p[i]-48); } else { y=a1.pop; x=a1.pop; switch(p[i]) { case '+' :z=x+y; a1.push(z); break; case '-' :z=x-y; a1.push(z); break; case '*' :z=x*y; a1.push(z); break; case '/' :z=x/y; a1.push(z); break; default: break; } } } int q=a1.pop; cout<<q; }
 * 1) include
 * 2) include
 * 1) include
 * 2) include<stdlib.h>
 * 1) template
 * 1) include
 * 2) include

queue using linked list include include using namespace std; template class node { public: t data; node *next; node { data=0; next=NULL; } }; template class queue { node<t>*first; public: queue { first=NULL; } void enqueue(t); t dequeue; void display; int isempty { return first=NULL; } }; template void queue<t>::enqueue(t data) { node<t>*ptr,*nptr,*lptr; nptr=new node<t>; nptr->data=data; nptr->next=NULL; if(first==NULL) { first=nptr; } else { ptr=first; while(ptr!=NULL) { lptr=ptr; ptr=ptr->next; } lptr->next=nptr; } } template t queue<t>::dequeue { node<t>*temp; if(first==NULL) { cout<<"underflow"; } else { temp=first; t data=first->data; first=first->next; delete temp; return data; } } template void queue<t>::display { node<t>*ptr=first; if(ptr=='\0') { cout<<"list is empty"; } else { while(ptr!=NULL) { cout<<ptr->data<<"\n"; ptr=ptr->next; } } } int main { int n,ch; queue s; while(1) { cout<<"enter your choice"; cout<<"\n 1=insert \n 2= delete \n 3=display \n 4=exit\n"; cin>>ch; switch(ch) { case 1:cout<<"enter a element"; cin>>n; s.enqueue(n); break; case 2:cout<<"\n deleted value="<<s.dequeue; break; case 3:s.display; break; case 4: exit(0); break; } } }

infix to post fix

include include using namespace std; template class stack1 { private:int max,top,c,i; t s[20]; public:stack1 { max=5; top=-1; } void push(t n) { if(top==max) { cout<<"stack is full"; } else { top++; s[top]=n; } } void pop { t n; n=s[top]; top--; if(n=='(') { cout<<""; }else cout<<n; } void display { int i; for(i=0;i<=top;i++) { if(s[i]=='('||checktop=='(') { cout<<""; } else { cout<<s[i]; } } } char checktop { return s[top]; } int priority(char x) { int r; switch(x) { case'+':r=1; break; case'-':r=1; break; case'*':r=2; break; case'/':r=2; break; } return(r); } int isempty { if(top==-1) { return 1; } else { return 0; } } }; int main { stack1 a1; string p; char n,i; cout<<"enter the expression:"; cin>>p; int m=p.length; for(i=0;i<=m;i++) { if(isalnum(p[i])) { cout<<p[i]; } else if(p[i]=='(') { a1.push(p[i]); } else if(p[i]==')') { do { a1.pop; } while(a1.checktop=='('); } else { char ab=a1.checktop; if(ab!='\0') { int p1=a1.priority(ab); int p2=a1.priority(p[i]); if(p2<=p1) { a1.pop; } } a1.push(p[i]); } } a1.display; }