linked lists in c++pooja/csl201/lectures/ppt for linked...class stack{int size; t* a; int top;...
TRANSCRIPT
![Page 1: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/1.jpg)
![Page 2: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/2.jpg)
#include <iostream>#include<cstdlib>
using namespace std;
![Page 3: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/3.jpg)
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;
};
![Page 4: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/4.jpg)
node::node( ){ //constructorhead = NULL;
}
node::~node( ){ //destructorcout>> “ destructor called’;while( head!= NULL) delete_front() ;
}
![Page 5: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/5.jpg)
void node::insert_front(int value){node *temp = new node;temp->data=value;temp -> next = NULL;
if (head != NULL){temp->next =head;
}head= temp;
}
![Page 6: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/6.jpg)
void node::print( ){node *h = head;cout<<"\n list values: ";while (h!= NULL){
cout << h->data << "\t";h = h->next;
}cout<<"\n";
}
![Page 7: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/7.jpg)
void node::delete_front(){node *h=head;if(head==NULL){
cout<<“ Empty List. \n";return;
}head =head->next;delete(h);
}
![Page 8: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/8.jpg)
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
![Page 9: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/9.jpg)
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;
}
![Page 10: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/10.jpg)
bool node::empty( ) const // is the list empty?{ return head == NULL; }
![Page 11: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/11.jpg)
Stacks to store integers
![Page 12: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/12.jpg)
#include <iostream>#include <cstdlib>
using namespace std;
![Page 13: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/13.jpg)
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() {……………….
};
![Page 14: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/14.jpg)
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;
}}
![Page 15: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/15.jpg)
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;
}}
![Page 16: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/16.jpg)
int main( ) {Stack S(10);
S.push(6);S.push(23);S.push(44);cout << " " << S.pop() ;cout << " " << S.pop();
}
![Page 17: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/17.jpg)
Stacks to hold any type of elements
![Page 18: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/18.jpg)
#include <iostream>#include <cstdlib>#include <string>
using namespace std;
![Page 19: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/19.jpg)
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)…………………
}};
![Page 20: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/20.jpg)
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;
}
![Page 21: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/21.jpg)
Error handling for POPException handling for PUSH
![Page 22: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/22.jpg)
#include <iostream>#include <cstdlib>#include <string> #include <exception>
using namespace std;
![Page 23: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/23.jpg)
class StackFull{private:
string errorMsg;public:
StackFull(string err) {errorMsg=err;
}string getMsg ( ) {
return errorMsg;}
};
![Page 24: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/24.jpg)
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)…………………
}};
![Page 25: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/25.jpg)
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;
}}
![Page 26: Linked lists in C++pooja/csl201/lectures/ppt for Linked...class 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](https://reader036.vdocuments.us/reader036/viewer/2022062918/5ede6eebad6a402d6669c180/html5/thumbnails/26.jpg)
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();
}
}