mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-05 11:40:46 +08:00
Add files via upload
This commit is contained in:
232
Datastructures/Binary Search Tree.cpp
Normal file
232
Datastructures/Binary Search Tree.cpp
Normal file
@@ -0,0 +1,232 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
struct node
|
||||
{
|
||||
int val;
|
||||
node *left;
|
||||
node *right;
|
||||
};
|
||||
|
||||
struct queue
|
||||
{
|
||||
node *t[100];
|
||||
int front;
|
||||
int rear;
|
||||
};
|
||||
|
||||
queue q;
|
||||
|
||||
|
||||
void enqueue(node *n)
|
||||
{
|
||||
q.t[q.rear++]=n;
|
||||
}
|
||||
|
||||
node * dequeue()
|
||||
{
|
||||
return (q.t[q.front++]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Insert(node *n, int x)
|
||||
{
|
||||
if (x<n->val)
|
||||
{
|
||||
if (n->left==NULL)
|
||||
{
|
||||
node *temp=new node;
|
||||
temp->val=x;
|
||||
temp->left=NULL;
|
||||
temp->right=NULL;
|
||||
n->left=temp;
|
||||
}
|
||||
else
|
||||
{
|
||||
Insert(n->left, x);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (n->right==NULL)
|
||||
{
|
||||
node *temp=new node;
|
||||
temp->val=x;
|
||||
temp->left=NULL;
|
||||
temp->right=NULL;
|
||||
n->left=temp;
|
||||
}
|
||||
else
|
||||
{
|
||||
Insert(n->right, x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int findMaxInLeftST(node *n)
|
||||
{
|
||||
while(n->right!=NULL)
|
||||
{
|
||||
n=n->right;
|
||||
}
|
||||
return n->val;
|
||||
}
|
||||
|
||||
void Remove(node *p, node *n, int x)
|
||||
{
|
||||
if (n->val==x)
|
||||
{
|
||||
if (n->right==NULL && n->left==NULL)
|
||||
{
|
||||
if (x<p->val)
|
||||
{
|
||||
p->right=NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
p->left=NULL;
|
||||
}
|
||||
}
|
||||
else if (n->right==NULL)
|
||||
{
|
||||
if (x<p->val)
|
||||
{
|
||||
p->right=n->left;
|
||||
}
|
||||
else
|
||||
{
|
||||
p->left=n->left;
|
||||
}
|
||||
}
|
||||
else if (n->left==NULL)
|
||||
{
|
||||
if (x<p->val)
|
||||
{
|
||||
p->right=n->right;
|
||||
}
|
||||
else
|
||||
{
|
||||
p->left=n->right;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int y=findMaxInLeftST(n->left);
|
||||
n->val=y;
|
||||
Remove(n, n->right, y);
|
||||
}
|
||||
}
|
||||
else if (x<n->val)
|
||||
{
|
||||
Remove(n, n->left, x);
|
||||
}
|
||||
else
|
||||
{
|
||||
Remove(n, n->right, x);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void BFT(node *n)
|
||||
{
|
||||
if(n!=NULL)
|
||||
{
|
||||
cout<<n->val<<" ";
|
||||
enqueue(n->left);
|
||||
enqueue(n->right);
|
||||
BFT(dequeue());
|
||||
}
|
||||
}
|
||||
|
||||
void Pre(node *n)
|
||||
{
|
||||
if (n!=NULL)
|
||||
{
|
||||
cout<<n->val<<" ";
|
||||
Pre(n->left);
|
||||
Pre(n->right);
|
||||
}
|
||||
}
|
||||
|
||||
void In(node *n)
|
||||
{
|
||||
if (n!=NULL)
|
||||
{
|
||||
In(n->left);
|
||||
cout<<n->val<<" ";
|
||||
In(n->right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Post(node *n)
|
||||
{
|
||||
if (n!=NULL)
|
||||
{
|
||||
Post(n->left);
|
||||
Post(n->right);
|
||||
cout<<n->val<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
q.front=0;
|
||||
q.rear=0;
|
||||
int value;
|
||||
int ch;
|
||||
node *root=new node;
|
||||
cout<<"\nEnter the value of root node :";
|
||||
cin>>value;
|
||||
root->val=value;
|
||||
root->left=NULL;
|
||||
root->right=NULL;
|
||||
do
|
||||
{
|
||||
cout<<"\n1. Insert";
|
||||
cout<<"\n2. Delete";
|
||||
cout<<"\n3. Breadth First";
|
||||
cout<<"\n4. Preorder Depth First";
|
||||
cout<<"\n5. Inorder Depth First";
|
||||
cout<<"\n6. Postorder Depth First";
|
||||
|
||||
cout<<"\nEnter Your Choice : ";
|
||||
cin>>ch;
|
||||
int x;
|
||||
switch(ch)
|
||||
{
|
||||
case 1:
|
||||
cout<<"\nEnter the value to be Inserted : ";
|
||||
cin>>x;
|
||||
Insert(root, x);
|
||||
break;
|
||||
case 2:
|
||||
cout<<"\nEnter the value to be Deleted : ";
|
||||
cin>>x;
|
||||
Remove(root, root, x);
|
||||
break;
|
||||
case 3:
|
||||
BFT(root);
|
||||
break;
|
||||
case 4:
|
||||
Pre(root);
|
||||
break;
|
||||
case 5:
|
||||
In(root);
|
||||
break;
|
||||
case 6:
|
||||
Post(root);
|
||||
break;
|
||||
}
|
||||
}
|
||||
while(ch!=0);
|
||||
}
|
||||
125
Datastructures/Doubly Linked List.cpp
Normal file
125
Datastructures/Doubly Linked List.cpp
Normal file
@@ -0,0 +1,125 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
{
|
||||
int val;
|
||||
node *prev;
|
||||
node *next;
|
||||
};
|
||||
|
||||
node *start;
|
||||
|
||||
void insert(int x)
|
||||
{
|
||||
node *t=start;
|
||||
if (start!=NULL)
|
||||
{
|
||||
while(t->next!=NULL)
|
||||
{
|
||||
t=t->next;
|
||||
}
|
||||
node *n= new node;
|
||||
t->next=n;
|
||||
n->prev=t;
|
||||
n->val=x;
|
||||
n->next=NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
node *n= new node;
|
||||
n->val=x;
|
||||
n->prev=NULL;
|
||||
n->next=NULL;
|
||||
start=n;
|
||||
}
|
||||
}
|
||||
|
||||
void remove(int x)
|
||||
{
|
||||
node *t=start;
|
||||
while(t->val!=x)
|
||||
{
|
||||
t=t->next;
|
||||
}
|
||||
t->prev->next=t->next;
|
||||
t->next->prev=t->prev;
|
||||
delete t;
|
||||
}
|
||||
|
||||
void search(int x)
|
||||
{
|
||||
node *t= start;
|
||||
int found =0;
|
||||
while(t!=NULL)
|
||||
{
|
||||
if(t->val==x)
|
||||
{
|
||||
cout<<"\nFound";
|
||||
found=1;
|
||||
break;
|
||||
}
|
||||
t=t->next;
|
||||
}
|
||||
if(found==0)
|
||||
{
|
||||
cout<<"\nNot Found";
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
node *t=start;
|
||||
while(t!=NULL)
|
||||
{
|
||||
cout<<t->val<<"\t";
|
||||
t=t->next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void reverseShow()
|
||||
{
|
||||
node *t=start;
|
||||
while(t->next!=NULL)
|
||||
{
|
||||
t=t->next;
|
||||
}
|
||||
while(t!=NULL)
|
||||
{
|
||||
cout<<t->val<<"\t";
|
||||
t=t->prev;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int choice, x;
|
||||
do
|
||||
{
|
||||
cout<<"\n1. Insert";
|
||||
cout<<"\n2. Delete";
|
||||
cout<<"\n3. Search";
|
||||
cout<<"\n4. Forward print";
|
||||
cout<<"\n5. Reverse print";
|
||||
cout<<"\n\nEnter you choice : ";
|
||||
cin>>choice;
|
||||
switch (choice)
|
||||
{
|
||||
case 1 : cout<<"\nEnter the element to be inserted : ";
|
||||
cin>>x;;
|
||||
insert(x); break;
|
||||
case 2 : cout<<"\nEnter the element to be removed : ";
|
||||
cin>>x;
|
||||
remove(x); break;
|
||||
case 3 : cout<<"\nEnter the element to be searched : ";
|
||||
cin>>x;
|
||||
search(x); break;
|
||||
case 4 : show(); break;
|
||||
case 5 : reverseShow(); break;
|
||||
}
|
||||
}
|
||||
while(choice!=0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
107
Datastructures/Linked List.cpp
Normal file
107
Datastructures/Linked List.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
{
|
||||
int val;
|
||||
node *next;
|
||||
};
|
||||
|
||||
node *start;
|
||||
|
||||
void insert(int x)
|
||||
{
|
||||
node *t=start;
|
||||
if (start!=NULL)
|
||||
{
|
||||
while(t->next!=NULL)
|
||||
{
|
||||
t=t->next;
|
||||
}
|
||||
node *n= new node;
|
||||
t->next=n;
|
||||
n->val=x;
|
||||
n->next=NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
node *n= new node;
|
||||
n->val=x;
|
||||
n->next=NULL;
|
||||
start=n;
|
||||
}
|
||||
}
|
||||
|
||||
void remove(int x)
|
||||
{
|
||||
node *t=start;
|
||||
node *p;
|
||||
while(t->val!=x)
|
||||
{
|
||||
p=t;
|
||||
t=t->next;
|
||||
}
|
||||
p->next=t->next;
|
||||
delete t;
|
||||
}
|
||||
|
||||
void search(int x)
|
||||
{
|
||||
node *t= start;
|
||||
int found =0;
|
||||
while(t!=NULL)
|
||||
{
|
||||
if(t->val==x)
|
||||
{
|
||||
cout<<"\nFound";
|
||||
found=1;
|
||||
break;
|
||||
}
|
||||
t=t->next;
|
||||
}
|
||||
if(found==0)
|
||||
{
|
||||
cout<<"\nNot Found";
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
node *t=start;
|
||||
while(t!=NULL)
|
||||
{
|
||||
cout<<t->val<<"\t";
|
||||
t=t->next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int choice, x;
|
||||
do
|
||||
{
|
||||
cout<<"\n1. Insert";
|
||||
cout<<"\n2. Delete";
|
||||
cout<<"\n3. Search";
|
||||
cout<<"\n4. Print";
|
||||
cout<<"\n\nEnter you choice : ";
|
||||
cin>>choice;
|
||||
switch (choice)
|
||||
{
|
||||
case 1 : cout<<"\nEnter the element to be inserted : ";
|
||||
cin>>x;;
|
||||
insert(x); break;
|
||||
case 2 : cout<<"\nEnter the element to be removed : ";
|
||||
cin>>x;
|
||||
remove(x); break;
|
||||
case 3 : cout<<"\nEnter the element to be searched : ";
|
||||
cin>>x;
|
||||
search(x); break;
|
||||
case 4 : show(); break;
|
||||
}
|
||||
}
|
||||
while(choice!=0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
185
Datastructures/List Array.cpp
Normal file
185
Datastructures/List Array.cpp
Normal file
@@ -0,0 +1,185 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
struct list
|
||||
{
|
||||
int data[50];
|
||||
int top=0;
|
||||
bool isSorted=false;
|
||||
|
||||
int BinarySearch(int *array, int first, int last, int x)
|
||||
{
|
||||
if(last<first)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
int mid=(first+last)/2;
|
||||
if(array[mid]==x)
|
||||
return mid;
|
||||
else if(x<array[mid])
|
||||
return (BinarySearch(array, first, mid-1, x));
|
||||
else if(x>array[mid])
|
||||
return (BinarySearch(array, mid+1, last, x));
|
||||
}
|
||||
|
||||
int LinarSearch(int *array, int x)
|
||||
{
|
||||
for (int i = 0; i < top; i++)
|
||||
{
|
||||
if (array[i]==x)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int Search(int x)
|
||||
{
|
||||
int pos=-1;
|
||||
|
||||
if (isSorted)
|
||||
{
|
||||
pos=BinarySearch(data, 0, top-1, x);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
pos=LinarSearch(data, x);
|
||||
}
|
||||
|
||||
if (pos!=-1)
|
||||
{
|
||||
cout<<"\nElement found at position : "<<pos;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout<<"\nElement not found";
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
void Sort()
|
||||
{
|
||||
int i, j, pos;
|
||||
for(i=0; i<top; i++)
|
||||
{
|
||||
int min=data[i];
|
||||
for(j=i+1; j<top; j++)
|
||||
{
|
||||
if(data[j]<min)
|
||||
{
|
||||
pos=j;
|
||||
min=data[pos];
|
||||
}
|
||||
}
|
||||
|
||||
int temp=data[i];
|
||||
data[i]=data[pos];
|
||||
data[pos]=temp;
|
||||
}
|
||||
isSorted=true;
|
||||
}
|
||||
|
||||
void insert(int x)
|
||||
{
|
||||
if(!isSorted)
|
||||
{
|
||||
|
||||
if(top==49)
|
||||
{
|
||||
cout<<"\nOverflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
data[top]=x;
|
||||
top++;
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
int pos=0;
|
||||
|
||||
for (int i = 0; i < top-1; i++)
|
||||
{
|
||||
if(data[i]<=x && x<=data[i+1])
|
||||
{
|
||||
pos=i+1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (pos==0)
|
||||
{
|
||||
pos=top-1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
for (int i = top; i > pos; i--)
|
||||
{
|
||||
data[i]=data[i-1];
|
||||
}
|
||||
top++;
|
||||
data[pos]=x;
|
||||
}
|
||||
}
|
||||
|
||||
void Remove(int x)
|
||||
{
|
||||
int pos=Search(x);
|
||||
cout<<"\n"<<data[pos]<<" deleted";
|
||||
for (int i = pos; i < top; i++)
|
||||
{
|
||||
data[i]=data[i+1];
|
||||
}
|
||||
top--;
|
||||
}
|
||||
|
||||
void Show()
|
||||
{
|
||||
for (int i = 0; i < top; i++)
|
||||
{
|
||||
cout<<data[i]<<"\t";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
list L;
|
||||
int choice;
|
||||
int x;
|
||||
do
|
||||
{
|
||||
cout<<"\n1.Insert";
|
||||
cout<<"\n2.Delete";
|
||||
cout<<"\n3.Search";
|
||||
cout<<"\n4.Sort";
|
||||
cout<<"\n5.Print";
|
||||
cout<<"\n\nEnter Your Choice : ";
|
||||
cin>>choice;
|
||||
switch (choice)
|
||||
{
|
||||
case 1: cout<<"\nEnter the element to be inserted : ";
|
||||
cin>>x;
|
||||
L.insert(x);
|
||||
break;
|
||||
case 2: cout<<"\nEnter the element to be removed : ";
|
||||
cin>>x;
|
||||
L.Remove(x);
|
||||
break;
|
||||
case 3: cout<<"\nEnter the element to be searched : ";
|
||||
cin>>x;
|
||||
L.Search(x);
|
||||
break;
|
||||
case 4: L.Sort();
|
||||
break;
|
||||
case 5: L.Show();
|
||||
break;
|
||||
}
|
||||
}
|
||||
while(choice!=0);
|
||||
return 0;
|
||||
}
|
||||
78
Datastructures/Queue Using Array.cpp
Normal file
78
Datastructures/Queue Using Array.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int queue[10];
|
||||
int front=0;
|
||||
int rear=0;
|
||||
|
||||
void Enque(int x)
|
||||
{
|
||||
if(rear==10)
|
||||
{
|
||||
cout<<"\nOverflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
queue[rear++]=x;
|
||||
}
|
||||
}
|
||||
|
||||
void Deque()
|
||||
{
|
||||
if (front==rear)
|
||||
{
|
||||
cout<<"\nUnderflow";
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
cout<<"\n"<<queue[front++]<<" deleted";
|
||||
for (int i = front; i < rear; i++)
|
||||
{
|
||||
queue[i-front]=queue[i];
|
||||
}
|
||||
rear=rear-front;
|
||||
front=0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
for (int i = front; i < rear; i++)
|
||||
{
|
||||
cout<<queue[i]<<"\t";
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int ch, x;
|
||||
do
|
||||
{
|
||||
cout<<"\n1. Enque";
|
||||
cout<<"\n2. Deque";
|
||||
cout<<"\n3. Print";
|
||||
cout<<"\nEnter Your Choice : ";
|
||||
cin>>ch;
|
||||
if (ch==1)
|
||||
{
|
||||
cout<<"\nInsert : ";
|
||||
cin>>x;
|
||||
Enque(x);
|
||||
}
|
||||
else if (ch==2)
|
||||
{
|
||||
Deque();
|
||||
}
|
||||
else if (ch==3)
|
||||
{
|
||||
show();
|
||||
}
|
||||
}
|
||||
while(ch!=0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
90
Datastructures/Queue Using Linked List.cpp
Normal file
90
Datastructures/Queue Using Linked List.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
{
|
||||
int val;
|
||||
node *next;
|
||||
};
|
||||
|
||||
|
||||
node *front, *rear;
|
||||
|
||||
|
||||
void Enque(int x)
|
||||
{
|
||||
if (rear==NULL)
|
||||
{
|
||||
node *n= new node;
|
||||
n->val=x;
|
||||
n->next=NULL;
|
||||
rear=n;
|
||||
front=n;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
|
||||
node *n = new node;
|
||||
n->val=x;
|
||||
n->next=NULL;
|
||||
rear->next=n;
|
||||
rear=n;
|
||||
}
|
||||
}
|
||||
|
||||
void Deque()
|
||||
{
|
||||
if (rear==front)
|
||||
{
|
||||
cout<<"\nUnderflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
node *t = front;
|
||||
cout<<"\n"<<t->val<<" deleted";
|
||||
front=front->next;
|
||||
delete t;
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
node *t=front;
|
||||
while(t!=NULL)
|
||||
{
|
||||
cout<<t->val<<"\t";
|
||||
t=t->next;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int ch, x;
|
||||
do
|
||||
{
|
||||
cout<<"\n1. Enque";
|
||||
cout<<"\n2. Deque";
|
||||
cout<<"\n3. Print";
|
||||
cout<<"\nEnter Your Choice : ";
|
||||
cin>>ch;
|
||||
if (ch==1)
|
||||
{
|
||||
cout<<"\nInsert : ";
|
||||
cin>>x;
|
||||
Enque(x);
|
||||
}
|
||||
else if (ch==2)
|
||||
{
|
||||
Deque();
|
||||
}
|
||||
else if (ch==3)
|
||||
{
|
||||
show();
|
||||
}
|
||||
}
|
||||
while(ch!=0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
77
Datastructures/Stack Using Array.cpp
Normal file
77
Datastructures/Stack Using Array.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int stack[10];
|
||||
int top=0;
|
||||
|
||||
void push(int x)
|
||||
{
|
||||
if(top==10)
|
||||
{
|
||||
cout<<"\nOverflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
stack[top++]=x;
|
||||
}
|
||||
}
|
||||
|
||||
void pop()
|
||||
{
|
||||
if (top==0)
|
||||
{
|
||||
cout<<"\nUnderflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout<<"\n"<<stack[--top]<<" deleted";
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
for (int i = 0; i < top; i++)
|
||||
{
|
||||
cout<<stack[i]<<"\n";
|
||||
}
|
||||
}
|
||||
|
||||
void topmost()
|
||||
{
|
||||
cout << "\nTopmost element: "<<stack[top-1];
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int ch, x;
|
||||
do
|
||||
{
|
||||
cout<<"\n1. Push";
|
||||
cout<<"\n2. Pop";
|
||||
cout<<"\n3. Print";
|
||||
cout<<"\n4. Print topmost element:";
|
||||
cout<<"\nEnter Your Choice : ";
|
||||
cin>>ch;
|
||||
if (ch==1)
|
||||
{
|
||||
cout<<"\nInsert : ";
|
||||
cin>>x;
|
||||
push(x);
|
||||
}
|
||||
else if (ch==2)
|
||||
{
|
||||
pop();
|
||||
}
|
||||
else if (ch==3)
|
||||
{
|
||||
show();
|
||||
}
|
||||
else if(ch==4)
|
||||
{
|
||||
topmost();
|
||||
}
|
||||
}
|
||||
while(ch!=0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
75
Datastructures/Stack Using Linked List.cpp
Normal file
75
Datastructures/Stack Using Linked List.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
{
|
||||
int val;
|
||||
node *next;
|
||||
};
|
||||
|
||||
|
||||
node *top;
|
||||
|
||||
void push(int x)
|
||||
{
|
||||
node *n = new node;
|
||||
n->val=x;
|
||||
n->next=top;
|
||||
top=n;
|
||||
}
|
||||
|
||||
void pop()
|
||||
{
|
||||
if (top==NULL)
|
||||
{
|
||||
cout<<"\nUnderflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
node *t = top;
|
||||
cout<<"\n"<<t->val<<" deleted";
|
||||
top=top->next;
|
||||
delete t;
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
node *t=top;
|
||||
while(t!=NULL)
|
||||
{
|
||||
cout<<t->val<<"\n";
|
||||
t=t->next;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int ch, x;
|
||||
do
|
||||
{
|
||||
cout<<"\n1. Push";
|
||||
cout<<"\n2. Pop";
|
||||
cout<<"\n3. Print";
|
||||
cout<<"\nEnter Your Choice : ";
|
||||
cin>>ch;
|
||||
if (ch==1)
|
||||
{
|
||||
cout<<"\nInsert : ";
|
||||
cin>>x;
|
||||
push(x);
|
||||
}
|
||||
else if (ch==2)
|
||||
{
|
||||
pop();
|
||||
}
|
||||
else if (ch==3)
|
||||
{
|
||||
show();
|
||||
}
|
||||
}
|
||||
while(ch!=0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
160
Datastructures/Tree.cpp
Normal file
160
Datastructures/Tree.cpp
Normal file
@@ -0,0 +1,160 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
struct node
|
||||
{
|
||||
int val;
|
||||
node *left;
|
||||
node *right;
|
||||
};
|
||||
|
||||
struct queue
|
||||
{
|
||||
node *t[100];
|
||||
int front;
|
||||
int rear;
|
||||
};
|
||||
|
||||
queue q;
|
||||
|
||||
|
||||
void enqueue(node *n)
|
||||
{
|
||||
q.t[q.rear++]=n;
|
||||
}
|
||||
|
||||
node * dequeue()
|
||||
{
|
||||
return (q.t[q.front++]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void CreateTree(node *curr, node *n, int x, char pos)
|
||||
{
|
||||
if(n!=NULL)
|
||||
{
|
||||
char ch;
|
||||
cout<<"\nLeft or Right of "<<n->val<<" : ";
|
||||
cin>>ch;
|
||||
if(ch=='l')
|
||||
CreateTree(n, n->left, x, ch);
|
||||
else if(ch=='r')
|
||||
CreateTree(n, n->right, x, ch);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
node *t=new node;
|
||||
t->val=x;
|
||||
t->left=NULL;
|
||||
t->right=NULL;
|
||||
if (pos=='l')
|
||||
{
|
||||
curr->left=t;
|
||||
}
|
||||
else if(pos=='r')
|
||||
{
|
||||
curr->right=t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void BFT(node *n)
|
||||
{
|
||||
if(n!=NULL)
|
||||
{
|
||||
cout<<n->val<<" ";
|
||||
enqueue(n->left);
|
||||
enqueue(n->right);
|
||||
BFT(dequeue());
|
||||
}
|
||||
}
|
||||
|
||||
void Pre(node *n)
|
||||
{
|
||||
if (n!=NULL)
|
||||
{
|
||||
cout<<n->val<<" ";
|
||||
Pre(n->left);
|
||||
Pre(n->right);
|
||||
}
|
||||
}
|
||||
|
||||
void In(node *n)
|
||||
{
|
||||
if (n!=NULL)
|
||||
{
|
||||
In(n->left);
|
||||
cout<<n->val<<" ";
|
||||
In(n->right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Post(node *n)
|
||||
{
|
||||
if (n!=NULL)
|
||||
{
|
||||
Post(n->left);
|
||||
Post(n->right);
|
||||
cout<<n->val<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
q.front=0;
|
||||
q.rear=0;
|
||||
int value;
|
||||
int ch;
|
||||
node *root=new node;
|
||||
cout<<"\nEnter the value of root node :";
|
||||
cin>>value;
|
||||
root->val=value;
|
||||
root->left=NULL;
|
||||
root->right=NULL;
|
||||
do
|
||||
{
|
||||
cout<<"\n1. Insert : ";
|
||||
cout<<"\n2. Breadth First";
|
||||
cout<<"\n3. Preorder Depth First";
|
||||
cout<<"\n4. Inorder Depth First";
|
||||
cout<<"\n5. Postorder Depth First";
|
||||
|
||||
cout<<"\nEnter Your Choice : ";
|
||||
cin>>ch;
|
||||
switch(ch)
|
||||
{
|
||||
case 1:
|
||||
int x;
|
||||
char pos;
|
||||
cout<<"\nEnter the value to be Inserted : ";
|
||||
cin>>x;
|
||||
cout<<"\nLeft or Right of Root : ";
|
||||
cin>>pos;
|
||||
if(pos=='l')
|
||||
CreateTree(root, root->left, x, pos);
|
||||
else if(pos=='r')
|
||||
CreateTree(root, root->right, x, pos);
|
||||
break;
|
||||
case 2:
|
||||
BFT(root);
|
||||
break;
|
||||
case 3:
|
||||
Pre(root);
|
||||
break;
|
||||
case 4:
|
||||
In(root);
|
||||
break;
|
||||
case 5:
|
||||
Post(root);
|
||||
break;
|
||||
}
|
||||
}
|
||||
while(ch!=0);
|
||||
}
|
||||
Reference in New Issue
Block a user