Data Structure Examples
Data Structure Examples
#include <iostream>
using namespace std;
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
}
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<<a.pop()<<" is Popped\n";
cout<<a.pop()<<" is Popped\n";
cout<<a.pop()<<" is Popped\n";
return 0;
}
QUEUE:-
#include <iostream>
using namespace std;
class queue
{
private:
int t[MAX];
int al; // Addition End
int dl; // Deletion End
public:
queue()
{
dl=-1;
al=-1;
}
void del()
{
int tmp;
if(dl==-1)
{
cout<<"Queue is Empty";
}
else
{
for(int j=0;j<=al;j++)
{
if((j+1)<=al)
{
tmp=t[j+1];
t[j]=tmp;
}
else
{
al--;
if(al==-1)
dl=-1;
else
dl=0;
}
}
}
}
void add(int item)
{
if(dl==-1 && al==-1)
{
dl++;
al++;
}
else
{
al++;
if(al==MAX)
{
cout<<"Queue is Full\n";
al--;
return;
}
}
t[al]=item;
void display()
{
if(dl!=-1)
{
for(int iter=0 ; iter<=al ; iter++)
cout<<t[iter]<<" ";
}
else
cout<<"EMPTY";
}
};
int main()
{
queue a;
int data[5]={32,23,45,99,24};
for(int iter = 0 ; iter < 5 ; iter++)
{
a.add(data[iter]);
cout<<"Addition Number : "<<(iter+1)<<" : ";
a.display();
cout<<endl;
}
cout<<endl;
cout<<"Queue after adding Elements: ";
a.display();
cout<<endl<<endl;
for(iter=0 ; iter < 5 ; iter++)
{
a.del();
cout<<"Deletion Number : "<<(iter+1)<<" : ";
a.display();
cout<<endl;
}
return 0;
}
*********************************************************************
LINKEDLIST:-
#include <iostream>
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;i<c;i++)
{
q = q->link;
if( q == NULL )
{
cout<<"\nThere are less than "<<c<<" elements.";
return;
}
}
t = new node;
t->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;
q = q->link;
}
cout<<"\nElement "<<num<<" not Found.";
}
void linklist::display()
{
node *q;
cout<<endl;
for( q = p ; q != NULL ; q = q->link )
cout<<endl<<q->data;
int linklist::count()
{
node *q;
int c=0;
for( q=p ; q != NULL ; q = q->link )
c++;
return c;
}
linklist::~linklist()
{
node *q;
if( p == NULL )
return;
while( p != NULL )
{
q = p->link;
delete p;
p = q;
}
}
int main()
{
linklist ll;
cout<<"No. of elements = "<<ll.count();
ll.append(12);
ll.append(13);
ll.append(23);
ll.append(43);
ll.append(44);
ll.append(50);
ll.add_as_first(2);
ll.add_as_first(1);
ll.addafter(3,333);
ll.addafter(6,666);
ll.display();
cout<<"\nNo. of elements = "<<ll.count();
ll.del(333);
ll.del(12);
ll.del(98);
cout<<"\nNo. of elements = "<<ll.count();
return 0;
}
return *p;
}
int main(void)
{
LLIST *n = NULL;
return 0;
}
#include <iostream>
using namespace std;
struct node
{
int data;
node *link;
};
class lstack
{
private:
node* top;
public:
lstack()
{
top=NULL;
}
void push(int n)
{
node *tmp;
tmp=new node;
if(tmp==NULL)
cout<<"\nSTACK FULL";
tmp->data=n;
tmp->link=top;
top=tmp;
}
int pop()
{
if(top==NULL)
{
cout<<"\nSTACK EMPTY";
return NULL;
}
node *tmp;
int n;
tmp=top;
n=tmp->data;
top=top->link;
delete tmp;
return n;
}
~lstack()
{
if(top==NULL)
return;
node *tmp;
while(top!=NULL)
{
tmp=top;
top=top->link;
delete tmp;
}
}
};
int main()
{
lstack s;
s.push(11);
s.push(101);
s.push(99);
s.push(78);
cout<<"Item Popped = "<<s.pop()<<endl;
cout<<"Item Popped = "<<s.pop()<<endl;
cout<<"Item Popped = "<<s.pop()<<endl;
return 0;
}
using namespace std;
struct node
{
int data;
node *link;
};
class lqueue
{
private:
node *front,*rear;
public:
lqueue()
{
front=NULL;
rear=NULL;
}
void add(int n)
{
node *tmp;
tmp=new node;
if(tmp==NULL)
cout<<"\nQUEUE FULL";
tmp->data=n;
tmp->link=NULL;
if(front==NULL)
{
rear=front=tmp;
return;
}
rear->link=tmp;
rear=rear->link;
}
int del()
{
if(front==NULL)
{
cout<<"\nQUEUE EMPTY";
return NULL;
}
node *tmp;
int n;
n=front->data;
tmp=front;
front=front->link;
delete tmp;
return n;
}
~lqueue()
{
if(front==NULL)
return;
node *tmp;
while(front!=NULL)
{
tmp=front;
front=front->link;
delete tmp;
}
}
};
int main()
{
lqueue q;
q.add(11);
q.add(22);
q.add(33);
q.add(44);
q.add(55);
cout<<"\nItem Deleted = "<<q.del();
cout<<"\nItem Deleted = "<<q.del();
cout<<"\nItem Deleted = "<<q.del();
return 0;
}
BINARY SEARCH :-
#include <iostream>
using namespace std;
#define YES 1
#define NO 0
class tree
{
private:
struct leaf
{
int data;
leaf *l;
leaf *r;
};
struct leaf *p;
public:
tree();
~tree();
void destruct(leaf *q);
tree(tree& a);
void findparent(int n,int &found,leaf* &parent);
void findfordel(int n,int &found,leaf *&parent,leaf* &x);
void add(int n);
void transverse();
void in(leaf *q);
void pre(leaf *q);
void post(leaf *q);
void del(int n);
};
tree::tree()
{
p=NULL;
}
tree::~tree()
{
destruct(p);
}
void tree::destruct(leaf *q)
{
if(q!=NULL)
{
destruct(q->l);
del(q->data);
destruct(q->r);
}
}
void tree::findparent(int n,int &found,leaf *&parent)
{
leaf *q;
found=NO;
parent=NULL;
if(p==NULL)
return;
q=p;
while(q!=NULL)
{
if(q->data==n)
{
found=YES;
return;
}
if(q->data>n)
{
parent=q;
q=q->l;
}
else
{
parent=q;
q=q->r;
}
}
}
void tree::add(int n)
{
int found;
leaf *t,*parent;
findparent(n,found,parent);
if(found==YES)
cout<<"\nSuch a Node Exists";
else
{
t=new leaf;
t->data=n;
t->l=NULL;
t->r=NULL;
if(parent==NULL)
p=t;
else
parent->data > n ? parent->l=t : parent->r=t;
}
}
void tree::transverse()
{
int c;
cout<<"\n1.InOrder\n2.Preorder\n3.Postorder\nChoice: ";
cin>>c;
switch©
{
case 1:
in(p);
break;
case 2:
pre(p);
break;
case 3:
post(p);
break;
}
}
void tree::in(leaf *q)
{
if(q!=NULL)
{
in(q->l);
cout<<"\t"<<q->data<<endl;
in(q->r);
}
}
void tree::pre(leaf *q)
{
if(q!=NULL)
{
cout<<"\t"<<q->data<<endl;
pre(q->l);
pre(q->r);
}
}
void tree::post(leaf *q)
{
if(q!=NULL)
{
post(q->l);
post(q->r);
cout<<"\t"<<q->data<<endl;
}
}
void tree::findfordel(int n,int &found,leaf *&parent,leaf *&x)
{
leaf *q;
found=0;
parent=NULL;
if(p==NULL)
return;
q=p;
while(q!=NULL)
{
if(q->data==n)
{
found=1;
x=q;
return;
}
if(q->data>n)
{
parent=q;
q=q->l;
}
else
{
parent=q;
q=q->r;
}
}
}
void tree::del(int num)
{
leaf *parent,*x,*xsucc;
int found;
while(xsucc->l != NULL)
{
parent=xsucc;
xsucc=xsucc->l;
}
x->data=xsucc->data;
x=xsucc;
}
delete x;
return;
}
delete x;
return;
}
delete x;
return;
}
}
int main()
{
tree t;
int data[]={32,16,34,1,87,13,7,18,14,19,23,24,41,5,53};
for(int iter=0 ; iter < 15 ; i++)
t.add(data[iter]);
t.transverse();
t.del(16);
t.transverse();
t.del(41);
t.transverse();
return 0;
}
Post fix :-
#include<iostream>
#include<conio.h>
class Stack
private:
char s[100];
int depth;
public:
Stack()
depth=0;
void push(char x)
s[depth++]=x;
}
char pop()
if(depth<0)
depth=0;
if(depth>=1)
return s[--depth];
else
return 0;
bool IsEmpty()
return (depth==0);
char head()
if(depth>0)
return s[depth-1];
else
return 0;
};
void main()
Stack stack;
char *infix;
char expression[50];
cin>>expression;
infix=expression;
while(*infix!=0)
switch(*infix)
case '+':
if(stack.head() != 0)
while(!stack.IsEmpty())
cout<<stack.pop();
stack.push('+');
break;
case '-':
if(stack.head() != 0)
while(!stack.IsEmpty())
cout<<stack.pop();
stack.push('-');
break;
case '*':
if(stack.head()=='*'||
stack.head()=='/')
while(!stack.IsEmpty())
cout<<stack.pop();
stack.push('*');
break;
case '/':
if(stack.head()=='*'||
stack.head()=='/')
while(!stack.IsEmpty())
cout<<stack.pop();
stack.push('/');
break;
default:
cout<<(*infix);
*(infix++);
while(!stack.IsEmpty())
cout<<stack.pop();
getch();
return;
__________________