This document was uploaded by user and they confirmed that they have the permission to share
it. If you are author or own the copyright of this book, please report to us by using this DMCA
report form. Report DMCA
Overview
Download & View Data Structure Examples as PDF for free.
STACK:#include using namespace std; #define MAX 10
// MAXIMUM STACK CONTENT
class stack { private: int arr[MAX];
// Contains all the Data
int top;
//Contains location of Topmost Data pushed
onto Stack public: stack()
//Constructor
{ top=-1;
//Sets the Top Location to -1 indicating
an empty stack } void push(int a)
// Push ie. Add Value Function
{ top++;
// increment to by 1
if(top<MAX) { arr[top]=a;
//If Stack is Vacant store
Value in Array } else { cout<<"STACK FULL!!"<<endl; top--; } } int pop()
// Delete Item. Returns the
deleted item { if(top==-1) { cout<<"STACK IS EMPTY!!!"<<endl; return NULL; } else { int data=arr[top];
//Set Topmost
Value in data arr[top]=NULL;
//Set Original
top--;
// Decrement top by
return data;
// Return deleted
Location to NULL 1 item } } };
int main() { stack a; a.push(3); cout<<"3 is Pushed\n"; a.push(10); cout<<"10 is Pushed\n"; a.push(1); cout<<"1 is Pushed\n\n"; cout<
QUEUE:#include using namespace std; #define MAX 5
LINKEDLIST:#include using namespace std; class linklist { private: struct node { int data; node *link; }*p; public: linklist(); void append( int num ); void add_as_first( int num ); void addafter( int c, int num ); void del( int num ); void display(); int count(); ~linklist(); }; linklist::linklist() {
p=NULL; } void linklist::append(int num) { node *q,*t; if( p == NULL ) { p = new node; p->data = num; p->link = NULL; } else { q = p; while( q->link != NULL ) q = q->link; t = new node; t->data = num; t->link = NULL; q->link = t; } } void linklist::add_as_first(int num) { node *q; q = new node; q->data = num; q->link = p; p = q; } void linklist::addafter( int c, int num) { node *q,*t;
int i; for(i=0,q=p;ilink; if( q == NULL ) { cout<<"\nThere are less than "<data = num; t->link = q->link; q->link = t; } void linklist::del( int num ) { node *q,*r; q = p; if( q->data == num ) { p = q->link; delete q; return; } r = q; while( q!=NULL ) { if( q->data == num ) { r->link = q->link; delete q; return; } r = q;