#include<iostream.h> #include<conio.h> #include<iomanip.h> #include<stdlib.h> class NODE { public: int key; NODE * next; NODE(int n); }; NODE * root; void push(int n) { NODE *p; p=new NODE(n); p->next=root; root=p; } void pop(int & item) { if(root == NULL) { cout<<"Error!!"; exit(1); } else { item=root->key; root=root->next; } } void show() { NODE * q; q = root; int i=0; while(q!=NULL) { cout<<q->key<<" , "; q=q->next; if(++i > 10) break; } cout<<endl; } int main() { cout<<"Program stack.cpp starts..."<<endl; int i, dataindex, item; int Data[10] = {12,14,76,48,10,33,42,98,23,57}; int Operation[10] = {1,1,1,0,1,0,0,1,0,1}; root = NULL; dataindex = 0; for(i=0;i<10;i++) { if(Operation[i]==1) { push(Data[dataindex]); cout<<"Pushed..."<<Data[dataindex]<<" Stack-> "; dataindex++; show(); } else { pop(item); cout<<"Popped"<<item<<"Stack-> "; show(); } } getch(); return 0; } NODE::NODE(int n) { key = n; next = NULL; }
Tag Archives: cpp
C++ program to perform operations on a text file
#include<fstream.h> #include<conio.h> #include<stdlib.h> class text_file { char ch; char fname[11]; char fname1[11]; ifstream ifile; ofstream ofile; public: void write() { cout<<"\nEnter file name(to be created):"; cin>>ch; ofile.open(fname); if(!ofile) { cout<<"\n\n ERROR!!"; exit(1); } cout<<"\nEnter data.....\n(press ESc. to exit editor)"; while(ch!=(char)27) { ofile.put(ch); } ofile.close(); } void read() { cout<<"\nEnter file name(to be read):"; cin>>fname; ifile.open(fname); if(!ifile) { cout<<"\n\n ERROR!!"; exit(1); } cout<<"\nData is displayed now..."; while(ifile) { ifile.get(ch); cout<<ch; } ifile.close(); } void copy() { cout<<"\nEnter file name(source):"; cin>>fname; ifile.open(fname); if(!ifile) { cout<<"\n\n ERROR!!"; exit(1); } cout<<"\nEnter file name(destination):"; cin>>fname1; ofile.open(fname1); if(!ofile) { cout<<"\n\n ERROR!!"; exit(1); } cout<<"\nData is copied"; while(ifile) { ifile.get(ch); ofile.put(ch); } ifile.close(); ofile.close(); } void cut() { cout<<"\nEnter file name(source):"; cin>>fname; ifile.open(fname); if(!ifile) { cout<<"\n\n ERROR!!"; exit(1); } cout<<"\nEnter file name(destination):"; cin>>fname1; ofile.open(fname1); if(!ofile) { cout<<"\n\n ERROR!!"; exit(1); } cout<<"\nData is cut and pasted !!"; while(ifile) { ifile.get(ch); ofile.put(ch); } ofile.close(); ifile.close(); remove("fname"); } }; int main() { int opt; text_file t; cout<<"\n1. Create"; cout<<"\n2. Read"; cout<<"\n3. Copy"; cout<<"\n4. Cut"; cout<<"\nEnter option"; cin>>opt; switch(opt) { case 1: t.create(); break; case 2: t.read(); break; case 3: t.copy(); break; case 4: t.cut(); break; default: cout<<"\nWrong option!!"; } return 0; }