linked lists in c++pooja/csl201/lectures/ppt for linked...class stack{int size; t* a; int top;...

26

Upload: others

Post on 02-Jun-2020

7 views

Category:

Documents


0 download

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
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

#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

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

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

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

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

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

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

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

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

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

#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

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

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

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

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

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

#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

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

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

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

#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

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

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

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

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();

}

}