Upload
others
View
7
Download
0
Embed Size (px)
Citation preview
#include <iostream>#include<cstdlib>
using namespace std;
class node{public: // member functions
node(); //constructor~node(); //destructorvoid insert_front(int);void insert_front_ref(int&);void delete_front();void print();
private: // data membersint data;node *next;node *head;
};
node::node( ){ //constructorhead = NULL;
}
node::~node( ){ //destructorcout>> “ destructor called’;while( head!= NULL) delete_front() ;
}
void node::insert_front(int value){node *temp = new node;temp->data=value;temp -> next = NULL;
if (head != NULL){temp->next =head;
}head= temp;
}
void node::print( ){node *h = head;cout<<"\n list values: ";while (h!= NULL){
cout << h->data << "\t";h = h->next;
}cout<<"\n";
}
void node::delete_front(){node *h=head;if(head==NULL){
cout<<“ Empty List. \n";return;
}head =head->next;delete(h);
}
void node::insert_front_ref( int & value){node *temp = new node;temp->data=value;temp -> next = NULL;
if (head != NULL){temp->next =head;
}head= temp;
}
PASS BY REFERENCE
int main(){node mylist;int dd;cin>> dd;mylist.insert_front(dd);
cin>> ss;mylist.insert_front_ref(ss);mylist.print( );mylist.delete_front( );mylist.print( );
mylist.~node( ); //call destructor to free objectsreturn 0;
}
bool node::empty( ) const // is the list empty?{ return head == NULL; }
Stacks to store integers
#include <iostream>#include <cstdlib>
using namespace std;
class Stack{private:
int size, *A;int top;
public: // Stack with max size NStack(int N) {size = N;A = new int[size];top=-1;
}
// void push(int x) {………..
// int pop() {……………….
};
void push(int x) {if (top == size-1)
cout << " STACK FULL : CANNOT PUSH " << endl;else
A[++top]=x; }
int pop() {if (top==-1)
cout << “ EMPTY STACK " << endl;else {int x = A[top--];return x;
}}
void push(int x) {if (top == size-1)
cout << " STACK FULL : CANNOT PUSH " << endl;else
A[++top]=x; }
int pop() {if (top== -1)
cout << “ EMPTY STACK " << endl;else {int x = A[top--];return x;
}}
int main( ) {Stack S(10);
S.push(6);S.push(23);S.push(44);cout << " " << S.pop() ;cout << " " << S.pop();
}
Stacks to hold any type of elements
#include <iostream>#include <cstdlib>#include <string>
using namespace std;
template <typename T> // generic type Tclass Stack{
int size;T* A;int top;
public:Stack(int N) {
size = N;A = new T[size];top= -1;
}void push(T x) {
// if (top == size-1)…………….. }
T pop() {// if (top==-1)…………………
}};
int main() {Stack<int> S(10);S.push(6);cout << " " << S.pop() << endl;cout << " " << S.pop() << endl;
Stack<char> P(3);P.push('c');P.push('t');cout << " " << P.pop() << endl;cout << " " << P.pop() << endl;
}
Error handling for POPException handling for PUSH
#include <iostream>#include <cstdlib>#include <string> #include <exception>
using namespace std;
class StackFull{private:
string errorMsg;public:
StackFull(string err) {errorMsg=err;
}string getMsg ( ) {
return errorMsg;}
};
template <typename T> // generic type Tclass Stack{
int size;T* A;int top;
public:Stack(int N) {
size = N;A = new T[size];top= -1;
}void push(T x) {
// if (top == size-1)…………….. }
T pop() {// if (top==-1)…………………
}};
void push(T x) {if (top == size-1)
throw StackFull(" Stack Full ");else
A[++top]=x; }
T pop() {if (top== -1)
cout << " ERROR : EMPTY STACK " << endl;else {int x = A[top--];return x;
}}
int main() {try {
Stack<int> S(10);S.push(6);cout << " " << S.pop() << endl;Stack<char> P(3);P.push('c');P.push('t');P.push('x'); // stack is full nowP.push('l'); // no space for this
}// catch will be executed only if there is exception
catch(StackFull e) {cout << e.getMsg();
}
}