Professional Documents
Culture Documents
Data Structures Code Implementation
Data Structures Code Implementation
#include<iostream>
#include<conio.h>
int data;
sNode* next;
};
int data;
cNode* next;
};
dNode* prev;
int data;
dNode* next;
};
class list
private:
sNode* sHead;
sNode* sCur;
sNode* st;
dNode* dHead;
dNode* dCur;
dNode* dt;
cNode* cHead;
cNode* cCur;
cNode* ct;
public:
list()
sHead=NULL;
dHead=NULL;
cHead=NULL;
int ch,n;
ty:
system("cls");
cout<<"4.Exit\n";
cin>>ch;
if(ch==1)
{
int c,d;
sm:
system("cls");
cout<<"1.Create\n";
cout<<"2.Traverse\n";
cout<<"3.Delete\n";
cout<<"4.back\n";
cin>>c;
if(c==1)
cout<<"Enter Value:";
cin>>n;
l.sList(n);
goto sm;
else if(c==2)
l.sTraverse();
getch();
goto sm;
else if(c==3)
cin>>d;
l.sDel(d);
goto sm;
}
else if(c==4)
goto ty;
else if(ch==2)
int c,d;
string t;
dm:
system("cls");
cout<<"1.Create\n";
cout<<"2.Traverse\n";
cout<<"3.Delete\n";
cout<<"4.Back\n";
cin>>c;
if(c==1)
cout<<"Enter Value:";
cin>>n;
l.dList(n);
goto dm;
else if(c==2)
cin>>t;
if(t=="f"||t=="F") {l.dTraverse("f");}
getch();
goto dm;
else if(c==3)
cin>>d;
l.dDel(d);
goto dm;
else if(c==4)
goto ty;
else if(ch==3)
int c,d;
cm:
system("cls");
cout<<"1.Create\n";
cout<<"2.Traverse\n";
cout<<"3.Delete\n";
cout<<"4.Back\n";
cin>>c;
if(c==1)
cout<<"Enter Value:";
cin>>n;
l.cList(n);
goto cm;
else if(c==2)
l.cTraverse();
goto cm;
else if(c==3)
cin>>d;
l.cDel(d);
goto cm;
else if(c==4)
goto ty;
else if(ch==4)
exit(0);
}
}
if(sHead==NULL)
sHead=new sNode;
sHead->data=n;
sHead->next=NULL;
else
sCur=sHead;
while(sCur->next!=NULL)
sCur=sCur->next;
st=new sNode;
st->data=n;
st->next=NULL;
sCur->next=st;
sCur=sHead;
while(sCur->next!=NULL)
{
cout<<sCur->data<<"\t";
sCur=sCur->next;
cout<<sCur->data<<"\t"<<endl;
sCur=sHead;
for(int i=1;i<dk;i++)
sCur=sCur->next;
st=sCur->next;
delete sCur;
sCur=sHead;
for(int i=1;i<dk-1;i++)
sCur=sCur->next;
sCur->next=st;
if(cHead==NULL)
{
cHead=new cNode;
cHead->data=n;
cHead->next=cHead;
else
cCur=cHead;
while(cCur->next!=cHead)
cCur=cCur->next;
ct=new cNode;
ct->data=n;
ct->next=cHead;
cCur->next=ct;
cCur=cHead;
while(cCur->next!=cHead)
cout<<cCur->data<<"\t";
cCur=cCur->next;
cout<<cCur->data<<"\t"<<endl;
}
cCur=cHead;
for(int i=1;i<dk;i++)
cCur=cCur->next;
ct=cCur->next;
delete cCur;
cCur=cHead;
for(int i=1;i<dk-1;i++)
cCur=cCur->next;
cCur->next=ct;
if(dHead==NULL)
dHead=new dNode;
dHead->data=n;
dHead->next=NULL;
dHead->prev=NULL;
}
else
dCur=dHead;
while(dCur->next!=NULL)
dCur=dCur->next;
dt=new dNode;
dt->data=n;
dt->next=NULL;
dt->prev=dCur;
dCur->next=dt;
dCur=dHead;
if(c=="f"||c=="F")
dCur=dHead;
while(dCur->next!=NULL)
cout<<dCur->data<<"\t";
dCur=dCur->next;
cout<<dCur->data<<"\t"<<endl;
else if(c=="b"||c=="B")
dCur=dHead;
while(dCur->next!=NULL)
dCur=dCur->next;
while(dCur->prev!=NULL)
cout<<dCur->data<<"\t";
dCur=dCur->prev;
cout<<dCur->data<<"\t"<<endl;
dCur=dHead;
for(int i=1;i<dk;i++)
{
dCur=dCur->next;
dt=dCur->next;
delete dCur;
dCur=dHead;
for(int i=1;i<dk-1;i++)
dCur=dCur->next;
dt->prev=dCur;
dCur->next=dt;
};
main()
list l;
l.menu(l);
#include<iostream>
#include<conio.h>
int stk[size];
int top=-1;
void push(int n)
if(top==-1)
top++;
stk[top]=n;
else if(top<size)
top++;
stk[top]=n;
else
cout<<"stack is full"<<endl;
void pop()
if(top==-1)
cout<<"Emmpty";
else
{
top--;
stk[top];
void disp()
for(int i=top;i>=0;i--)
cout<<stk[i]<<endl;
main()
int n;
for(int i=0;i<7;i++)
cout<<"Enter value::";
cin>>n;
push(n);
disp();
pop();
pop();
cout<<endl<<endl<<endl;
disp();
getch();
}
STACK IMPLEMENTATION USING Linked List
#include<iostream>
struct node
int data;
node* next;
};
node* top=NULL;
class rStack
private:
node* cur;
node* temp;
public:
void push(int n)
temp=new node;
temp->data=n;
if(top==NULL)
temp->next=NULL;
top=temp;
else
{
temp->next=top;
top=temp;
void pop()
if(top==NULL)
cout<<"Stack is empty!\n";
else
cur=top;
top=top->next;
delete(cur);
void traverse()
if(top==NULL)
cout<<"Stack is empty!\n";
else
cur=top;
while(cur->next!=NULL)
cur=cur->next;
cout<<cur->data<<endl;
};
main()
rStack obj;
obj.push(1);
obj.push(2);
obj.traverse();
obj.pop();
obj.pop();
obj.traverse();
#include<conio.h>
#define size 3
int que[size];
int front=-1;
int rare=-1;
void enque(int n)
if(front==-1)
front++;
rare=front;
que[rare]=n;
else if(rare>=size)
cout<<"Full"<<endl;
else
rare++;
que[rare]=n;
void deque()
if(rare==-1)
cout<<"Empty";
front=-1;
rare=-1;
else if(front==rare)
{
front=-1;
rare=-1;
else
front++;
void disp()
for(int i=front;i<=rare;i++)
cout<<que[i]<<endl;
main()
enque(5);
enque(6);
enque(7);
enque(8);
enque(9);
disp();
cout<<endl<<endl;
deque();
deque();
deque();
deque();
deque();
disp();
getch();
#include<conio.h>
struct node
class cQueue
private:
node* cur;
node* temp;
public:
cQueue()
cur=NULL; temp=NULL;
void menu()
cout<<"2.Dequeue\n";
cout<<"3.Traverse\n";
cout<<"4.Front\n";
cout<<"5.Empty\n";
cout<<"6.Exit\n";
cin>>ch;
if(ch==1)
{
int n;
cout<<"Enter value:";
cin>>n;
enQueue(n);
getch();
goto mm;
else if(ch==2)
getch();
goto mm;
else if(ch==3)
{
cout<<"Displaying Queue:";
disp();
goto mm;
else if(ch==4)
cFront();
getch();
goto mm;
else if(ch==5)
isEmpty();
getch();
goto mm;
else if(ch==6)
exit(0);
}
void enQueue(int n)
temp=new node;
temp->data=n;
if(front==NULL)//empty
{
temp->next=temp;
rear=temp;
front=rear;
else
rear->next=temp;
temp->next=front;
rear=temp;
void deQueue()
if(front==NULL)
cout<<"Queue is empty!\n";
if(front==rear)
delete(front);
front=NULL;
rear=NULL;
}
else
cur=front;
front=front->next;
rear->next=front;
delete(cur);
void cFront()
if(front==NULL)
cout<<"Queue is empty!\n";
else
cout<<front->data<<endl;
void isEmpty()
if(front==NULL)
cout<<"Queue is empty!\n";
else
{
void disp()
if(front==NULL)
cout<<"Queue is Empty!\n";
}
else
{
cur=front; while(cur->next!=front)
{
cout<<cur->data<<"\t"; cur=cur-
>next;
}
cout<<cur->data<<"\t"; cout<<"\n";
}
}
};
main()
{
struct bTree
int data;
bTree *left;
bTree *right;
};
bTree *temp;
temp=new bTree;
temp->data=data;
temp->left=NULL;
temp->right=NULL;
return temp;
if(Root==NULL)
Root=creat(n);
}
else if(n<Root->data)
Insert(Root->left,n);
else
Insert(Root->right,n);
if(Root->data==n)
cout<<"Found";
else
if(n<Root->data)
while(Root->left!=NULL)
if(Root->left->data==n)
cout<<"Found\n";
break;
else
Root=Root->left;
}
}
else
while(Root->right!=NULL)
if(Root->right->data==n)
cout<<"Found\n";
break;
else
Root=Root->right;
cout<<Root->data<<" ";
if(Root!=NULL)
preDisp(Root->left);
cout<<Root->data;
preDisp(Root->right);
main()
int n;
bTree *Root;
Root=NULL;
for(int i=1;i<=5;i++)
cin>>n;
Insert(Root,n);
preDisp(Root);
cout<<endl;
binSerch(Root,10);
min(Root);
return 0;
#include<conio.h>
void inorder(int[],int);
main()
{
int no[size];
int i,data;
char c;
for(i=1;i<size;i++)
no[i]=-1;
do
cin>>data;
creatBST(no,data,1);
c=getch();
while(c=='y'||c=='Y');
inorder(no,1);
if(index>=size)
else
if(no[index]==-1)
no[index]=data;
}
else
if (data<no[index])
creatBST(no,data,index*2);
else
creatBST(no,data,(index*2)+1);
if(no[index]!=-1)
inorder(no,index*2);
cout<<no[index]<<endl;
inorder(no,(index*2)+1);
#include <cstdlib>
struct node
int key;
node* left;
node* right;
int hight;
};
int high(node* N)
if(N==NULL)
return 0;
return N->hight;
return (a>b)? a : b;
node *temp;
temp=new node;
malloc (sizeof(node));
temp->key=data;
temp->left=NULL;
temp->right=NULL;
temp->hight=1;
return temp;
node *y = x->right;
x->right=T2;
y->left=x;
x->hight=max(high(x->left),high(x->right))+1;
y->hight=max(high(y->left),high(y->right))+1;
return y;
node *x = y->left;
x->right=y;
y->left=T2;
x->hight=max(high(x->left),high(x->right))+1;
y->hight=max(high(y->left),high(y->right))+1;
return x;
int getbalance(node* N)
if(N==NULL)
return 0;
if(Root==NULL)
return (creat(n));
if(n<Root->key)
Root->left=Insert(Root->left,n);
else if (n>Root->key)
Root->right=Insert(Root->right,n);
else
return Root;
Root->hight=1+ max(high(Root->left),high(Root->right));
return rightR(Root);
return leftR(Root);
if(balance > 1 && n < Root->left->key)
Root->left=leftR(Root->left);
return rightR(Root);
Root->right=rightR(Root->right);
return leftR(Root);
return Root;
if(Root!=NULL)
preDisp(Root->left);
cout<<Root->key<<endl;
preDisp(Root->right);
main()
node *root;
root=NULL;
root=Insert(root,10);
root=Insert(root,20);
root=Insert(root,30);
root=Insert(root,40);
root=Insert(root,50);
root=Insert(root,25);
preDisp(root);