- The Program was developed to Sort Numbers using Three Stacks.
- Initially, all the Numbers are in Stack One, then other Two Stacks used to Sort the Number.
- Max Limit of Number in the array is 100, You may increase it.
- The program read all input numbers from Text File present in C:\ drive
- The Program was developed using VC++.net
- Code is Very simple and understandable, you may optimize it.
#include<fstream> #include<iostream> #include<stdio.h> #include<string>
using namespace std; typedef struct { int top; float arr[100]; }Stack; void createStack(Stack *stack) { stack->top=-1; } void push(Stack *stack,float ele) { if(stack->top==-1) { stack->arr[0]=ele; stack->top=0; } else stack->arr[++(stack->top)]=ele; } float pop(Stack *stack) { if(stack->top==-1) return -1; return stack->arr[(stack->top)--]; } float peek(Stack *stack) { if(stack->top==-1) return -1; return stack->arr[stack->top]; } int isEmpty(Stack *stack) { if(stack->top==-1) return 1; else return 0; } void main() { /////////////Read file///////////////// string *filetext[10]; string line=""; int TotalLines_in_Source_File=0; int j=0; Stack S1,S2,S3; createStack(&S1); createStack(&S2); createStack(&S3); ifstream ifile; ifile.open("c:\\prog.txt", ios::in); int MAXLines=0; float val; if(ifile) { while(!ifile.eof()) { ifile>>val; push(&S1,val); } } int i=0; int len=S1.top; len++; while(i { push(&S2,pop(&S1)); while(!isEmpty(&S1)) { if(peek(&S1)<=peek(&S2)) { push(&S3,pop(&S1)); } else { push(&S3,pop(&S2)); push(&S2,pop(&S1)); } }//while Stack temp; createStack(&temp); temp=S1; S1=S3; S3=temp; i++; } while(!isEmpty(&S2)) cout<<" "< cout<<"\n"; }
Comments
Post a Comment