mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-05 11:40:46 +08:00
Merge branch 'master' into master
This commit is contained in:
@@ -3,20 +3,23 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef struct node {
|
||||
typedef struct node
|
||||
{
|
||||
int data;
|
||||
int height;
|
||||
struct node* left;
|
||||
struct node* right;
|
||||
}node;
|
||||
struct node *left;
|
||||
struct node *right;
|
||||
} node;
|
||||
|
||||
int max(int a, int b) {
|
||||
int max(int a, int b)
|
||||
{
|
||||
return a > b ? a : b;
|
||||
}
|
||||
|
||||
// Returns a new Node
|
||||
|
||||
node* createNode(int data) {
|
||||
node *createNode(int data)
|
||||
{
|
||||
node *nn = new node();
|
||||
nn->data = data;
|
||||
nn->height = 0;
|
||||
@@ -27,21 +30,24 @@ node* createNode(int data) {
|
||||
|
||||
// Returns height of tree
|
||||
|
||||
int height(node *root) {
|
||||
if(root==NULL)
|
||||
int height(node *root)
|
||||
{
|
||||
if (root == NULL)
|
||||
return 0;
|
||||
return 1 + max(height(root->left), height(root->right));
|
||||
}
|
||||
|
||||
// Returns difference between height of left and right subtree
|
||||
|
||||
int getBalance(node *root) {
|
||||
int getBalance(node *root)
|
||||
{
|
||||
return height(root->left) - height(root->right);
|
||||
}
|
||||
|
||||
// Returns Node after Right Rotation
|
||||
|
||||
node* rightRotate(node *root) {
|
||||
node *rightRotate(node *root)
|
||||
{
|
||||
node *t = root->left;
|
||||
node *u = t->right;
|
||||
t->right = root;
|
||||
@@ -51,7 +57,8 @@ node* rightRotate(node *root) {
|
||||
|
||||
// Returns Node after Left Rotation
|
||||
|
||||
node* leftRotate(node *root) {
|
||||
node *leftRotate(node *root)
|
||||
{
|
||||
node *t = root->right;
|
||||
node *u = t->left;
|
||||
t->left = root;
|
||||
@@ -61,57 +68,65 @@ node* leftRotate(node *root) {
|
||||
|
||||
// Returns node with minimum value in the tree
|
||||
|
||||
node* minValue(node* root) {
|
||||
if(root->left==NULL)
|
||||
node *minValue(node *root)
|
||||
{
|
||||
if (root->left == NULL)
|
||||
return root;
|
||||
return minValue(root->left);
|
||||
}
|
||||
|
||||
// Balanced Insertion
|
||||
|
||||
node* insert(node* root, int item) {
|
||||
node *insert(node *root, int item)
|
||||
{
|
||||
node *nn = createNode(item);
|
||||
if(root == NULL)
|
||||
if (root == NULL)
|
||||
return nn;
|
||||
if(item<root->data)
|
||||
if (item < root->data)
|
||||
root->left = insert(root->left, item);
|
||||
else
|
||||
root->right = insert(root->right, item);
|
||||
int b = getBalance(root);
|
||||
if(b>1) {
|
||||
if(getBalance(root->left)<0)
|
||||
root->left = leftRotate(root->left); // Left-Right Case
|
||||
return rightRotate(root); // Left-Left Case
|
||||
if (b > 1)
|
||||
{
|
||||
if (getBalance(root->left) < 0)
|
||||
root->left = leftRotate(root->left); // Left-Right Case
|
||||
return rightRotate(root); // Left-Left Case
|
||||
}
|
||||
else if(b<-1) {
|
||||
if(getBalance(root->right)>0)
|
||||
root->right = rightRotate(root->right); // Right-Left Case
|
||||
return leftRotate(root); // Right-Right Case
|
||||
else if (b < -1)
|
||||
{
|
||||
if (getBalance(root->right) > 0)
|
||||
root->right = rightRotate(root->right); // Right-Left Case
|
||||
return leftRotate(root); // Right-Right Case
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
// Balanced Deletion
|
||||
|
||||
node* deleteNode(node *root, int key) {
|
||||
if(root == NULL)
|
||||
node *deleteNode(node *root, int key)
|
||||
{
|
||||
if (root == NULL)
|
||||
return root;
|
||||
if(key < root->data)
|
||||
if (key < root->data)
|
||||
root->left = deleteNode(root->left, key);
|
||||
else if(key > root->data)
|
||||
else if (key > root->data)
|
||||
root->right = deleteNode(root->right, key);
|
||||
|
||||
else {
|
||||
else
|
||||
{
|
||||
// Node to be deleted is leaf node or have only one Child
|
||||
if(!root->right) {
|
||||
node* temp = root->left;
|
||||
delete(root);
|
||||
if (!root->right)
|
||||
{
|
||||
node *temp = root->left;
|
||||
delete (root);
|
||||
root = NULL;
|
||||
return temp;
|
||||
}
|
||||
else if(!root->left) {
|
||||
node* temp = root->right;
|
||||
delete(root);
|
||||
else if (!root->left)
|
||||
{
|
||||
node *temp = root->right;
|
||||
delete (root);
|
||||
root = NULL;
|
||||
return temp;
|
||||
}
|
||||
@@ -124,36 +139,38 @@ node* deleteNode(node *root, int key) {
|
||||
return root;
|
||||
}
|
||||
|
||||
|
||||
// LevelOrder (Breadth First Search)
|
||||
|
||||
void levelOrder(node* root) {
|
||||
queue<node*> q;
|
||||
void levelOrder(node *root)
|
||||
{
|
||||
queue<node *> q;
|
||||
q.push(root);
|
||||
while(!q.empty()) {
|
||||
while (!q.empty())
|
||||
{
|
||||
root = q.front();
|
||||
cout<<root->data<<" ";
|
||||
cout << root->data << " ";
|
||||
q.pop();
|
||||
if(root->left)
|
||||
if (root->left)
|
||||
q.push(root->left);
|
||||
if(root->right)
|
||||
if (root->right)
|
||||
q.push(root->right);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
// Testing AVL Tree
|
||||
node *root = NULL;
|
||||
int i;
|
||||
for(i = 1 ; i <= 7 ; i++)
|
||||
for (i = 1; i <= 7; i++)
|
||||
root = insert(root, i);
|
||||
cout<<"LevelOrder: ";
|
||||
cout << "LevelOrder: ";
|
||||
levelOrder(root);
|
||||
root = deleteNode(root, 1); // Deleting key with value 1
|
||||
cout<<"\nLevelOrder: ";
|
||||
cout << "\nLevelOrder: ";
|
||||
levelOrder(root);
|
||||
root = deleteNode(root, 4); // Deletin key with value 4
|
||||
cout<<"\nLevelOrder: ";
|
||||
root = deleteNode(root, 4); // Deletin key with value 4
|
||||
cout << "\nLevelOrder: ";
|
||||
levelOrder(root);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
struct node
|
||||
{
|
||||
int val;
|
||||
@@ -18,30 +17,27 @@ struct queue
|
||||
|
||||
queue q;
|
||||
|
||||
|
||||
void enqueue(node *n)
|
||||
{
|
||||
q.t[q.rear++]=n;
|
||||
q.t[q.rear++] = n;
|
||||
}
|
||||
|
||||
node * dequeue()
|
||||
node *dequeue()
|
||||
{
|
||||
return (q.t[q.front++]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Insert(node *n, int x)
|
||||
{
|
||||
if (x<n->val)
|
||||
if (x < n->val)
|
||||
{
|
||||
if (n->left==NULL)
|
||||
if (n->left == NULL)
|
||||
{
|
||||
node *temp=new node;
|
||||
temp->val=x;
|
||||
temp->left=NULL;
|
||||
temp->right=NULL;
|
||||
n->left=temp;
|
||||
node *temp = new node;
|
||||
temp->val = x;
|
||||
temp->left = NULL;
|
||||
temp->right = NULL;
|
||||
n->left = temp;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -50,13 +46,13 @@ void Insert(node *n, int x)
|
||||
}
|
||||
else
|
||||
{
|
||||
if (n->right==NULL)
|
||||
if (n->right == NULL)
|
||||
{
|
||||
node *temp=new node;
|
||||
temp->val=x;
|
||||
temp->left=NULL;
|
||||
temp->right=NULL;
|
||||
n->left=temp;
|
||||
node *temp = new node;
|
||||
temp->val = x;
|
||||
temp->left = NULL;
|
||||
temp->right = NULL;
|
||||
n->left = temp;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -65,63 +61,60 @@ void Insert(node *n, int x)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int findMaxInLeftST(node *n)
|
||||
{
|
||||
while(n->right!=NULL)
|
||||
while (n->right != NULL)
|
||||
{
|
||||
n=n->right;
|
||||
n = n->right;
|
||||
}
|
||||
return n->val;
|
||||
}
|
||||
|
||||
void Remove(node *p, node *n, int x)
|
||||
{
|
||||
if (n->val==x)
|
||||
if (n->val == x)
|
||||
{
|
||||
if (n->right==NULL && n->left==NULL)
|
||||
if (n->right == NULL && n->left == NULL)
|
||||
{
|
||||
if (x<p->val)
|
||||
if (x < p->val)
|
||||
{
|
||||
p->right=NULL;
|
||||
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;
|
||||
p->left = NULL;
|
||||
}
|
||||
}
|
||||
else if (n->left==NULL)
|
||||
else if (n->right == NULL)
|
||||
{
|
||||
if (x<p->val)
|
||||
if (x < p->val)
|
||||
{
|
||||
p->right=n->right;
|
||||
p->right = n->left;
|
||||
}
|
||||
else
|
||||
{
|
||||
p->left=n->right;
|
||||
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;
|
||||
int y = findMaxInLeftST(n->left);
|
||||
n->val = y;
|
||||
Remove(n, n->right, y);
|
||||
}
|
||||
}
|
||||
else if (x<n->val)
|
||||
else if (x < n->val)
|
||||
{
|
||||
Remove(n, n->left, x);
|
||||
}
|
||||
@@ -131,14 +124,11 @@ void Remove(node *p, node *n, int x)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void BFT(node *n)
|
||||
{
|
||||
if(n!=NULL)
|
||||
if (n != NULL)
|
||||
{
|
||||
cout<<n->val<<" ";
|
||||
cout << n->val << " ";
|
||||
enqueue(n->left);
|
||||
enqueue(n->right);
|
||||
BFT(dequeue());
|
||||
@@ -147,9 +137,9 @@ void BFT(node *n)
|
||||
|
||||
void Pre(node *n)
|
||||
{
|
||||
if (n!=NULL)
|
||||
if (n != NULL)
|
||||
{
|
||||
cout<<n->val<<" ";
|
||||
cout << n->val << " ";
|
||||
Pre(n->left);
|
||||
Pre(n->right);
|
||||
}
|
||||
@@ -157,76 +147,72 @@ void Pre(node *n)
|
||||
|
||||
void In(node *n)
|
||||
{
|
||||
if (n!=NULL)
|
||||
if (n != NULL)
|
||||
{
|
||||
In(n->left);
|
||||
cout<<n->val<<" ";
|
||||
cout << n->val << " ";
|
||||
In(n->right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Post(node *n)
|
||||
{
|
||||
if (n!=NULL)
|
||||
if (n != NULL)
|
||||
{
|
||||
Post(n->left);
|
||||
Post(n->right);
|
||||
cout<<n->val<<" ";
|
||||
cout << n->val << " ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
q.front=0;
|
||||
q.rear=0;
|
||||
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;
|
||||
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 << "\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;
|
||||
cout << "\nEnter Your Choice : ";
|
||||
cin >> ch;
|
||||
int x;
|
||||
switch(ch)
|
||||
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;
|
||||
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);
|
||||
} while (ch != 0);
|
||||
}
|
||||
|
||||
@@ -1,159 +1,158 @@
|
||||
// A C++ program to demonstrate common Binary Heap Operations
|
||||
#include<iostream>
|
||||
#include<climits>
|
||||
using namespace std;
|
||||
|
||||
// Prototype of a utility function to swap two integers
|
||||
void swap(int *x, int *y);
|
||||
|
||||
// A class for Min Heap
|
||||
class MinHeap
|
||||
{
|
||||
int *harr; // pointer to array of elements in heap
|
||||
int capacity; // maximum possible size of min heap
|
||||
int heap_size; // Current number of elements in min heap
|
||||
public:
|
||||
// Constructor
|
||||
MinHeap(int capacity);
|
||||
|
||||
// to heapify a subtree with the root at given index
|
||||
void MinHeapify(int );
|
||||
|
||||
int parent(int i) { return (i-1)/2; }
|
||||
|
||||
// to get index of left child of node at index i
|
||||
int left(int i) { return (2*i + 1); }
|
||||
|
||||
// to get index of right child of node at index i
|
||||
int right(int i) { return (2*i + 2); }
|
||||
|
||||
// to extract the root which is the minimum element
|
||||
int extractMin();
|
||||
|
||||
// Decreases key value of key at index i to new_val
|
||||
void decreaseKey(int i, int new_val);
|
||||
|
||||
// Returns the minimum key (key at root) from min heap
|
||||
int getMin() { return harr[0]; }
|
||||
|
||||
// Deletes a key stored at index i
|
||||
void deleteKey(int i);
|
||||
|
||||
// Inserts a new key 'k'
|
||||
void insertKey(int k);
|
||||
};
|
||||
|
||||
// Constructor: Builds a heap from a given array a[] of given size
|
||||
MinHeap::MinHeap(int cap)
|
||||
{
|
||||
heap_size = 0;
|
||||
capacity = cap;
|
||||
harr = new int[cap];
|
||||
}
|
||||
|
||||
// Inserts a new key 'k'
|
||||
void MinHeap::insertKey(int k)
|
||||
{
|
||||
if (heap_size == capacity)
|
||||
{
|
||||
cout << "\nOverflow: Could not insertKey\n";
|
||||
return;
|
||||
}
|
||||
|
||||
// First insert the new key at the end
|
||||
heap_size++;
|
||||
int i = heap_size - 1;
|
||||
harr[i] = k;
|
||||
|
||||
// Fix the min heap property if it is violated
|
||||
while (i != 0 && harr[parent(i)] > harr[i])
|
||||
{
|
||||
swap(&harr[i], &harr[parent(i)]);
|
||||
i = parent(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Decreases value of key at index 'i' to new_val. It is assumed that
|
||||
// new_val is smaller than harr[i].
|
||||
void MinHeap::decreaseKey(int i, int new_val)
|
||||
{
|
||||
harr[i] = new_val;
|
||||
while (i != 0 && harr[parent(i)] > harr[i])
|
||||
{
|
||||
swap(&harr[i], &harr[parent(i)]);
|
||||
i = parent(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Method to remove minimum element (or root) from min heap
|
||||
int MinHeap::extractMin()
|
||||
{
|
||||
if (heap_size <= 0)
|
||||
return INT_MAX;
|
||||
if (heap_size == 1)
|
||||
{
|
||||
heap_size--;
|
||||
return harr[0];
|
||||
}
|
||||
|
||||
// Store the minimum value, and remove it from heap
|
||||
int root = harr[0];
|
||||
harr[0] = harr[heap_size-1];
|
||||
heap_size--;
|
||||
MinHeapify(0);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
|
||||
// This function deletes key at index i. It first reduced value to minus
|
||||
// infinite, then calls extractMin()
|
||||
void MinHeap::deleteKey(int i)
|
||||
{
|
||||
decreaseKey(i, INT_MIN);
|
||||
extractMin();
|
||||
}
|
||||
|
||||
// A recursive method to heapify a subtree with the root at given index
|
||||
// This method assumes that the subtrees are already heapified
|
||||
void MinHeap::MinHeapify(int i)
|
||||
{
|
||||
int l = left(i);
|
||||
int r = right(i);
|
||||
int smallest = i;
|
||||
if (l < heap_size && harr[l] < harr[i])
|
||||
smallest = l;
|
||||
if (r < heap_size && harr[r] < harr[smallest])
|
||||
smallest = r;
|
||||
if (smallest != i)
|
||||
{
|
||||
swap(&harr[i], &harr[smallest]);
|
||||
MinHeapify(smallest);
|
||||
}
|
||||
}
|
||||
|
||||
// A utility function to swap two elements
|
||||
void swap(int *x, int *y)
|
||||
{
|
||||
int temp = *x;
|
||||
*x = *y;
|
||||
*y = temp;
|
||||
}
|
||||
|
||||
// Driver program to test above functions
|
||||
int main()
|
||||
{
|
||||
MinHeap h(11);
|
||||
h.insertKey(3);
|
||||
h.insertKey(2);
|
||||
h.deleteKey(1);
|
||||
h.insertKey(15);
|
||||
h.insertKey(5);
|
||||
h.insertKey(4);
|
||||
h.insertKey(45);
|
||||
cout << h.extractMin() << " ";
|
||||
cout << h.getMin() << " ";
|
||||
h.decreaseKey(2, 1);
|
||||
cout << h.getMin();
|
||||
return 0;
|
||||
}
|
||||
// A C++ program to demonstrate common Binary Heap Operations
|
||||
#include <iostream>
|
||||
#include <climits>
|
||||
using namespace std;
|
||||
|
||||
// Prototype of a utility function to swap two integers
|
||||
void swap(int *x, int *y);
|
||||
|
||||
// A class for Min Heap
|
||||
class MinHeap
|
||||
{
|
||||
int *harr; // pointer to array of elements in heap
|
||||
int capacity; // maximum possible size of min heap
|
||||
int heap_size; // Current number of elements in min heap
|
||||
public:
|
||||
// Constructor
|
||||
MinHeap(int capacity);
|
||||
|
||||
// to heapify a subtree with the root at given index
|
||||
void MinHeapify(int);
|
||||
|
||||
int parent(int i) { return (i - 1) / 2; }
|
||||
|
||||
// to get index of left child of node at index i
|
||||
int left(int i) { return (2 * i + 1); }
|
||||
|
||||
// to get index of right child of node at index i
|
||||
int right(int i) { return (2 * i + 2); }
|
||||
|
||||
// to extract the root which is the minimum element
|
||||
int extractMin();
|
||||
|
||||
// Decreases key value of key at index i to new_val
|
||||
void decreaseKey(int i, int new_val);
|
||||
|
||||
// Returns the minimum key (key at root) from min heap
|
||||
int getMin() { return harr[0]; }
|
||||
|
||||
// Deletes a key stored at index i
|
||||
void deleteKey(int i);
|
||||
|
||||
// Inserts a new key 'k'
|
||||
void insertKey(int k);
|
||||
};
|
||||
|
||||
// Constructor: Builds a heap from a given array a[] of given size
|
||||
MinHeap::MinHeap(int cap)
|
||||
{
|
||||
heap_size = 0;
|
||||
capacity = cap;
|
||||
harr = new int[cap];
|
||||
}
|
||||
|
||||
// Inserts a new key 'k'
|
||||
void MinHeap::insertKey(int k)
|
||||
{
|
||||
if (heap_size == capacity)
|
||||
{
|
||||
cout << "\nOverflow: Could not insertKey\n";
|
||||
return;
|
||||
}
|
||||
|
||||
// First insert the new key at the end
|
||||
heap_size++;
|
||||
int i = heap_size - 1;
|
||||
harr[i] = k;
|
||||
|
||||
// Fix the min heap property if it is violated
|
||||
while (i != 0 && harr[parent(i)] > harr[i])
|
||||
{
|
||||
swap(&harr[i], &harr[parent(i)]);
|
||||
i = parent(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Decreases value of key at index 'i' to new_val. It is assumed that
|
||||
// new_val is smaller than harr[i].
|
||||
void MinHeap::decreaseKey(int i, int new_val)
|
||||
{
|
||||
harr[i] = new_val;
|
||||
while (i != 0 && harr[parent(i)] > harr[i])
|
||||
{
|
||||
swap(&harr[i], &harr[parent(i)]);
|
||||
i = parent(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Method to remove minimum element (or root) from min heap
|
||||
int MinHeap::extractMin()
|
||||
{
|
||||
if (heap_size <= 0)
|
||||
return INT_MAX;
|
||||
if (heap_size == 1)
|
||||
{
|
||||
heap_size--;
|
||||
return harr[0];
|
||||
}
|
||||
|
||||
// Store the minimum value, and remove it from heap
|
||||
int root = harr[0];
|
||||
harr[0] = harr[heap_size - 1];
|
||||
heap_size--;
|
||||
MinHeapify(0);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
// This function deletes key at index i. It first reduced value to minus
|
||||
// infinite, then calls extractMin()
|
||||
void MinHeap::deleteKey(int i)
|
||||
{
|
||||
decreaseKey(i, INT_MIN);
|
||||
extractMin();
|
||||
}
|
||||
|
||||
// A recursive method to heapify a subtree with the root at given index
|
||||
// This method assumes that the subtrees are already heapified
|
||||
void MinHeap::MinHeapify(int i)
|
||||
{
|
||||
int l = left(i);
|
||||
int r = right(i);
|
||||
int smallest = i;
|
||||
if (l < heap_size && harr[l] < harr[i])
|
||||
smallest = l;
|
||||
if (r < heap_size && harr[r] < harr[smallest])
|
||||
smallest = r;
|
||||
if (smallest != i)
|
||||
{
|
||||
swap(&harr[i], &harr[smallest]);
|
||||
MinHeapify(smallest);
|
||||
}
|
||||
}
|
||||
|
||||
// A utility function to swap two elements
|
||||
void swap(int *x, int *y)
|
||||
{
|
||||
int temp = *x;
|
||||
*x = *y;
|
||||
*y = temp;
|
||||
}
|
||||
|
||||
// Driver program to test above functions
|
||||
int main()
|
||||
{
|
||||
MinHeap h(11);
|
||||
h.insertKey(3);
|
||||
h.insertKey(2);
|
||||
h.deleteKey(1);
|
||||
h.insertKey(15);
|
||||
h.insertKey(5);
|
||||
h.insertKey(4);
|
||||
h.insertKey(45);
|
||||
cout << h.extractMin() << " ";
|
||||
cout << h.getMin() << " ";
|
||||
h.decreaseKey(2, 1);
|
||||
cout << h.getMin();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
@@ -12,83 +12,82 @@ node *start;
|
||||
|
||||
void insert(int x)
|
||||
{
|
||||
node *t=start;
|
||||
if (start!=NULL)
|
||||
node *t = start;
|
||||
if (start != NULL)
|
||||
{
|
||||
while(t->next!=NULL)
|
||||
while (t->next != NULL)
|
||||
{
|
||||
t=t->next;
|
||||
t = t->next;
|
||||
}
|
||||
node *n= new node;
|
||||
t->next=n;
|
||||
n->prev=t;
|
||||
n->val=x;
|
||||
n->next=NULL;
|
||||
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;
|
||||
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)
|
||||
node *t = start;
|
||||
while (t->val != x)
|
||||
{
|
||||
t=t->next;
|
||||
t = t->next;
|
||||
}
|
||||
t->prev->next=t->next;
|
||||
t->next->prev=t->prev;
|
||||
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)
|
||||
node *t = start;
|
||||
int found = 0;
|
||||
while (t != NULL)
|
||||
{
|
||||
if(t->val==x)
|
||||
if (t->val == x)
|
||||
{
|
||||
cout<<"\nFound";
|
||||
found=1;
|
||||
cout << "\nFound";
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
t=t->next;
|
||||
t = t->next;
|
||||
}
|
||||
if(found==0)
|
||||
if (found == 0)
|
||||
{
|
||||
cout<<"\nNot Found";
|
||||
cout << "\nNot Found";
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
node *t=start;
|
||||
while(t!=NULL)
|
||||
node *t = start;
|
||||
while (t != NULL)
|
||||
{
|
||||
cout<<t->val<<"\t";
|
||||
t=t->next;
|
||||
cout << t->val << "\t";
|
||||
t = t->next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void reverseShow()
|
||||
{
|
||||
node *t=start;
|
||||
while(t->next!=NULL)
|
||||
node *t = start;
|
||||
while (t->next != NULL)
|
||||
{
|
||||
t=t->next;
|
||||
t = t->next;
|
||||
}
|
||||
while(t!=NULL)
|
||||
while (t != NULL)
|
||||
{
|
||||
cout<<t->val<<"\t";
|
||||
t=t->prev;
|
||||
cout << t->val << "\t";
|
||||
t = t->prev;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,29 +96,39 @@ 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;
|
||||
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;
|
||||
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);
|
||||
} while (choice != 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
@@ -11,50 +11,56 @@ node *start;
|
||||
|
||||
void insert(int x)
|
||||
{
|
||||
node *t=start;
|
||||
if (start!=NULL)
|
||||
node *t = start;
|
||||
if (start != NULL)
|
||||
{
|
||||
while(t->next!=NULL)
|
||||
while (t->next != NULL)
|
||||
{
|
||||
t=t->next;
|
||||
t = t->next;
|
||||
}
|
||||
node *n= new node;
|
||||
t->next=n;
|
||||
n->val=x;
|
||||
n->next=NULL;
|
||||
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;
|
||||
node *n = new node;
|
||||
n->val = x;
|
||||
n->next = NULL;
|
||||
start = n;
|
||||
}
|
||||
}
|
||||
|
||||
void remove(int x){
|
||||
void remove(int x)
|
||||
{
|
||||
|
||||
if( start == NULL ){
|
||||
cout<<"\nLinked List is empty\n";
|
||||
return ;
|
||||
if (start == NULL)
|
||||
{
|
||||
cout << "\nLinked List is empty\n";
|
||||
return;
|
||||
}
|
||||
else if( start->val == x ){
|
||||
else if (start->val == x)
|
||||
{
|
||||
node *temp = start;
|
||||
start = start->next;
|
||||
delete temp;
|
||||
return ;
|
||||
return;
|
||||
}
|
||||
|
||||
node *temp = start, *parent = start;
|
||||
|
||||
while( temp != NULL && temp->val != x ){
|
||||
while (temp != NULL && temp->val != x)
|
||||
{
|
||||
parent = temp;
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
if( temp == NULL ){
|
||||
cout <<endl <<x <<" not found in list\n";
|
||||
return ;
|
||||
if (temp == NULL)
|
||||
{
|
||||
cout << endl
|
||||
<< x << " not found in list\n";
|
||||
return;
|
||||
}
|
||||
|
||||
parent->next = temp->next;
|
||||
@@ -63,47 +69,48 @@ void remove(int x){
|
||||
|
||||
void search(int x)
|
||||
{
|
||||
node *t= start;
|
||||
int found =0;
|
||||
while(t!=NULL)
|
||||
node *t = start;
|
||||
int found = 0;
|
||||
while (t != NULL)
|
||||
{
|
||||
if(t->val==x)
|
||||
if (t->val == x)
|
||||
{
|
||||
cout<<"\nFound";
|
||||
found=1;
|
||||
cout << "\nFound";
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
t=t->next;
|
||||
t = t->next;
|
||||
}
|
||||
if(found==0)
|
||||
if (found == 0)
|
||||
{
|
||||
cout<<"\nNot Found";
|
||||
cout << "\nNot Found";
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
node *t=start;
|
||||
while(t!=NULL)
|
||||
node *t = start;
|
||||
while (t != NULL)
|
||||
{
|
||||
cout<<t->val<<"\t";
|
||||
t=t->next;
|
||||
cout << t->val << "\t";
|
||||
t = t->next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void reverse(){
|
||||
node* first = start;
|
||||
node* second = first->next;
|
||||
while(second != NULL){
|
||||
node* tem = second->next;
|
||||
second->next = first;
|
||||
first = second;
|
||||
second = tem;
|
||||
}
|
||||
void reverse()
|
||||
{
|
||||
node *first = start;
|
||||
node *second = first->next;
|
||||
while (second != NULL)
|
||||
{
|
||||
node *tem = second->next;
|
||||
second->next = first;
|
||||
first = second;
|
||||
second = tem;
|
||||
}
|
||||
|
||||
start->next = NULL;
|
||||
start = first;
|
||||
start->next = NULL;
|
||||
start = first;
|
||||
}
|
||||
|
||||
int main()
|
||||
@@ -111,33 +118,44 @@ int main()
|
||||
int choice, x;
|
||||
do
|
||||
{
|
||||
cout<<"\n1. Insert";
|
||||
cout<<"\n2. Delete";
|
||||
cout<<"\n3. Search";
|
||||
cout<<"\n4. Print";
|
||||
cout<<"\n5. Reverse";
|
||||
cout<<"\n0. Exit";
|
||||
cout<<"\n\nEnter you choice : ";
|
||||
cin>>choice;
|
||||
cout << "\n1. Insert";
|
||||
cout << "\n2. Delete";
|
||||
cout << "\n3. Search";
|
||||
cout << "\n4. Print";
|
||||
cout << "\n5. Reverse";
|
||||
cout << "\n0. Exit";
|
||||
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();
|
||||
cout<<"\n"; break;
|
||||
case 5 : cout<<"The reversed list: \n";
|
||||
reverse(); show();
|
||||
cout<<"\n"; break;
|
||||
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();
|
||||
cout << "\n";
|
||||
break;
|
||||
case 5:
|
||||
cout << "The reversed list: \n";
|
||||
reverse();
|
||||
show();
|
||||
cout << "\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
while(choice!=0);
|
||||
} while (choice != 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,32 +1,32 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
struct list
|
||||
{
|
||||
int data[50];
|
||||
int top=0;
|
||||
bool isSorted=false;
|
||||
|
||||
int top = 0;
|
||||
bool isSorted = false;
|
||||
|
||||
int BinarySearch(int *array, int first, int last, int x)
|
||||
{
|
||||
if(last<first)
|
||||
if (last < first)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
int mid=(first+last)/2;
|
||||
if(array[mid]==x)
|
||||
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));
|
||||
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)
|
||||
if (array[i] == x)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
@@ -37,102 +37,101 @@ struct list
|
||||
|
||||
int Search(int x)
|
||||
{
|
||||
int pos=-1;
|
||||
int pos = -1;
|
||||
|
||||
if (isSorted)
|
||||
{
|
||||
pos=BinarySearch(data, 0, top-1, x);
|
||||
pos = BinarySearch(data, 0, top - 1, x);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
pos=LinarSearch(data, x);
|
||||
pos = LinarSearch(data, x);
|
||||
}
|
||||
|
||||
if (pos!=-1)
|
||||
if (pos != -1)
|
||||
{
|
||||
cout<<"\nElement found at position : "<<pos;
|
||||
cout << "\nElement found at position : " << pos;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout<<"\nElement not found";
|
||||
cout << "\nElement not found";
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
|
||||
void Sort()
|
||||
{
|
||||
int i, j, pos;
|
||||
for(i=0; i<top; i++)
|
||||
for (i = 0; i < top; i++)
|
||||
{
|
||||
int min=data[i];
|
||||
for(j=i+1; j<top; j++)
|
||||
int min = data[i];
|
||||
for (j = i + 1; j < top; j++)
|
||||
{
|
||||
if(data[j]<min)
|
||||
if (data[j] < min)
|
||||
{
|
||||
pos=j;
|
||||
min=data[pos];
|
||||
pos = j;
|
||||
min = data[pos];
|
||||
}
|
||||
}
|
||||
|
||||
int temp=data[i];
|
||||
data[i]=data[pos];
|
||||
data[pos]=temp;
|
||||
|
||||
int temp = data[i];
|
||||
data[i] = data[pos];
|
||||
data[pos] = temp;
|
||||
}
|
||||
isSorted=true;
|
||||
isSorted = true;
|
||||
}
|
||||
|
||||
|
||||
void insert(int x)
|
||||
{
|
||||
if(!isSorted)
|
||||
if (!isSorted)
|
||||
{
|
||||
|
||||
if(top==49)
|
||||
if (top == 49)
|
||||
{
|
||||
cout<<"\nOverflow";
|
||||
cout << "\nOverflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
data[top]=x;
|
||||
data[top] = x;
|
||||
top++;
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
int pos=0;
|
||||
|
||||
for (int i = 0; i < top-1; i++)
|
||||
int pos = 0;
|
||||
|
||||
for (int i = 0; i < top - 1; i++)
|
||||
{
|
||||
if(data[i]<=x && x<=data[i+1])
|
||||
if (data[i] <= x && x <= data[i + 1])
|
||||
{
|
||||
pos=i+1;
|
||||
pos = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (pos==0)
|
||||
if (pos == 0)
|
||||
{
|
||||
pos=top-1;
|
||||
pos = top - 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
for (int i = top; i > pos; i--)
|
||||
{
|
||||
data[i]=data[i-1];
|
||||
data[i] = data[i - 1];
|
||||
}
|
||||
top++;
|
||||
data[pos]=x;
|
||||
data[pos] = x;
|
||||
}
|
||||
}
|
||||
|
||||
void Remove(int x)
|
||||
{
|
||||
int pos=Search(x);
|
||||
cout<<"\n"<<data[pos]<<" deleted";
|
||||
int pos = Search(x);
|
||||
cout << "\n"
|
||||
<< data[pos] << " deleted";
|
||||
for (int i = pos; i < top; i++)
|
||||
{
|
||||
data[i]=data[i+1];
|
||||
data[i] = data[i + 1];
|
||||
}
|
||||
top--;
|
||||
}
|
||||
@@ -141,7 +140,7 @@ struct list
|
||||
{
|
||||
for (int i = 0; i < top; i++)
|
||||
{
|
||||
cout<<data[i]<<"\t";
|
||||
cout << data[i] << "\t";
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -152,34 +151,38 @@ int main()
|
||||
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;
|
||||
{
|
||||
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;
|
||||
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);
|
||||
} while (choice != 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -7,73 +7,86 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Btree {
|
||||
struct Btree
|
||||
{
|
||||
int data;
|
||||
struct Btree* left; //Pointer to left subtree
|
||||
struct Btree* right; //Pointer to right subtree
|
||||
struct Btree *left; //Pointer to left subtree
|
||||
struct Btree *right; //Pointer to right subtree
|
||||
};
|
||||
|
||||
void insert(Btree **root, int d) {
|
||||
Btree *nn = new Btree(); //Creating new node
|
||||
void insert(Btree **root, int d)
|
||||
{
|
||||
Btree *nn = new Btree(); //Creating new node
|
||||
nn->data = d;
|
||||
nn->left = NULL;
|
||||
nn->right = NULL;
|
||||
if(*root == NULL) {
|
||||
if (*root == NULL)
|
||||
{
|
||||
*root = nn;
|
||||
return;
|
||||
}
|
||||
else {
|
||||
queue<Btree*> q;
|
||||
else
|
||||
{
|
||||
queue<Btree *> q;
|
||||
// Adding root node to queue
|
||||
q.push(*root);
|
||||
while(!q.empty()) {
|
||||
while (!q.empty())
|
||||
{
|
||||
Btree *node = q.front();
|
||||
// Removing parent node from queue
|
||||
q.pop();
|
||||
if(node->left)
|
||||
if (node->left)
|
||||
// Adding left child of removed node to queue
|
||||
q.push(node->left);
|
||||
else {
|
||||
else
|
||||
{
|
||||
// Adding new node if no left child is present
|
||||
node->left = nn;
|
||||
return;
|
||||
}
|
||||
if(node->right)
|
||||
if (node->right)
|
||||
// Adding right child of removed node to queue
|
||||
q.push(node->right);
|
||||
else {
|
||||
else
|
||||
{
|
||||
// Adding new node if no right child is present
|
||||
node->right = nn;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void morrisInorder(Btree *root) {
|
||||
void morrisInorder(Btree *root)
|
||||
{
|
||||
Btree *curr = root;
|
||||
Btree *temp;
|
||||
while(curr) {
|
||||
if(curr->left==NULL) {
|
||||
cout<<curr->data<<" ";
|
||||
while (curr)
|
||||
{
|
||||
if (curr->left == NULL)
|
||||
{
|
||||
cout << curr->data << " ";
|
||||
// If left of current node is NULL then curr is shifted to right
|
||||
curr = curr->right;
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
// Left of current node is stored in temp
|
||||
temp = curr->left;
|
||||
// Moving to extreme right of temp
|
||||
while(temp->right&&temp->right!=curr)
|
||||
while (temp->right && temp->right != curr)
|
||||
temp = temp->right;
|
||||
// If extreme right is null it is made to point to currrent node (will be used for backtracking)
|
||||
if(temp->right == NULL) {
|
||||
if (temp->right == NULL)
|
||||
{
|
||||
temp->right = curr;
|
||||
// current node is made to point its left subtree
|
||||
curr = curr->left;
|
||||
}
|
||||
// If extreme right already points to currrent node it it set to null
|
||||
else if(temp->right == curr) {
|
||||
cout<<curr->data<<" ";
|
||||
else if (temp->right == curr)
|
||||
{
|
||||
cout << curr->data << " ";
|
||||
temp->right = NULL;
|
||||
// current node is made to point its right subtree
|
||||
curr = curr->right;
|
||||
@@ -82,13 +95,14 @@ void morrisInorder(Btree *root) {
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
// Testing morrisInorder funtion
|
||||
Btree *root = NULL;
|
||||
int i;
|
||||
for(i = 1 ; i <= 7 ; i++)
|
||||
for (i = 1; i <= 7; i++)
|
||||
insert(&root, i);
|
||||
cout<<"Morris Inorder: ";
|
||||
cout << "Morris Inorder: ";
|
||||
morrisInorder(root);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,48 +1,47 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int queue[10];
|
||||
int front=0;
|
||||
int rear=0;
|
||||
int front = 0;
|
||||
int rear = 0;
|
||||
|
||||
void Enque(int x)
|
||||
{
|
||||
if(rear==10)
|
||||
if (rear == 10)
|
||||
{
|
||||
cout<<"\nOverflow";
|
||||
cout << "\nOverflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
queue[rear++]=x;
|
||||
queue[rear++] = x;
|
||||
}
|
||||
}
|
||||
|
||||
void Deque()
|
||||
{
|
||||
if (front==rear)
|
||||
if (front == rear)
|
||||
{
|
||||
cout<<"\nUnderflow";
|
||||
cout << "\nUnderflow";
|
||||
}
|
||||
|
||||
|
||||
else
|
||||
{
|
||||
cout<<"\n"<<queue[front++]<<" deleted";
|
||||
cout << "\n"
|
||||
<< queue[front++] << " deleted";
|
||||
for (int i = front; i < rear; i++)
|
||||
{
|
||||
queue[i-front]=queue[i];
|
||||
queue[i - front] = queue[i];
|
||||
}
|
||||
rear=rear-front;
|
||||
front=0;
|
||||
rear = rear - front;
|
||||
front = 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
for (int i = front; i < rear; i++)
|
||||
{
|
||||
cout<<queue[i]<<"\t";
|
||||
cout << queue[i] << "\t";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,28 +50,26 @@ 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 << "\n1. Enque";
|
||||
cout << "\n2. Deque";
|
||||
cout << "\n3. Print";
|
||||
cout << "\nEnter Your Choice : ";
|
||||
cin >> ch;
|
||||
if (ch == 1)
|
||||
{
|
||||
cout<<"\nInsert : ";
|
||||
cin>>x;
|
||||
cout << "\nInsert : ";
|
||||
cin >> x;
|
||||
Enque(x);
|
||||
}
|
||||
else if (ch==2)
|
||||
else if (ch == 2)
|
||||
{
|
||||
Deque();
|
||||
}
|
||||
else if (ch==3)
|
||||
else if (ch == 3)
|
||||
{
|
||||
show();
|
||||
}
|
||||
}
|
||||
while(ch!=0);
|
||||
} while (ch != 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,90 +1,89 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
{
|
||||
int val;
|
||||
node *next;
|
||||
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;
|
||||
}
|
||||
if (rear == NULL)
|
||||
{
|
||||
node *n = new node;
|
||||
n->val = x;
|
||||
n->next = NULL;
|
||||
rear = n;
|
||||
front = n;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
else
|
||||
{
|
||||
|
||||
node *n = new node;
|
||||
n->val=x;
|
||||
n->next=NULL;
|
||||
rear->next=n;
|
||||
rear=n;
|
||||
}
|
||||
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;
|
||||
}
|
||||
if (rear == NULL && front == NULL)
|
||||
{
|
||||
cout << "\nUnderflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
node *t = front;
|
||||
cout << "\n"
|
||||
<< t->val << " deleted";
|
||||
front = front->next;
|
||||
delete t;
|
||||
if (front == NULL)
|
||||
rear = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
node *t=front;
|
||||
while(t!=NULL)
|
||||
{
|
||||
cout<<t->val<<"\t";
|
||||
t=t->next;
|
||||
}
|
||||
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);
|
||||
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;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,30 +1,31 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int *stack;
|
||||
int top=0, size;
|
||||
int top = 0, size;
|
||||
|
||||
void push(int x)
|
||||
{
|
||||
if(top==size)
|
||||
if (top == size)
|
||||
{
|
||||
cout<<"\nOverflow";
|
||||
cout << "\nOverflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
stack[top++]=x;
|
||||
stack[top++] = x;
|
||||
}
|
||||
}
|
||||
|
||||
void pop()
|
||||
{
|
||||
if (top==0)
|
||||
if (top == 0)
|
||||
{
|
||||
cout<<"\nUnderflow";
|
||||
cout << "\nUnderflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout<<"\n"<<stack[--top]<<" deleted";
|
||||
cout << "\n"
|
||||
<< stack[--top] << " deleted";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,49 +33,47 @@ void show()
|
||||
{
|
||||
for (int i = 0; i < top; i++)
|
||||
{
|
||||
cout<<stack[i]<<"\n";
|
||||
cout << stack[i] << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void topmost()
|
||||
{
|
||||
cout << "\nTopmost element: "<<stack[top-1];
|
||||
cout << "\nTopmost element: " << stack[top - 1];
|
||||
}
|
||||
int main()
|
||||
{
|
||||
cout<<"\nEnter Size of stack : ";
|
||||
cin>>size;
|
||||
cout << "\nEnter Size of stack : ";
|
||||
cin >> size;
|
||||
stack = new int[size];
|
||||
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 << "\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;
|
||||
cout << "\nInsert : ";
|
||||
cin >> x;
|
||||
push(x);
|
||||
}
|
||||
else if (ch==2)
|
||||
else if (ch == 2)
|
||||
{
|
||||
pop();
|
||||
}
|
||||
else if (ch==3)
|
||||
else if (ch == 3)
|
||||
{
|
||||
show();
|
||||
}
|
||||
else if(ch==4)
|
||||
{
|
||||
topmost();
|
||||
}
|
||||
}
|
||||
while(ch!=0);
|
||||
else if (ch == 4)
|
||||
{
|
||||
topmost();
|
||||
}
|
||||
} while (ch != 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
@@ -7,39 +7,39 @@ struct node
|
||||
node *next;
|
||||
};
|
||||
|
||||
|
||||
node *top;
|
||||
|
||||
void push(int x)
|
||||
{
|
||||
node *n = new node;
|
||||
n->val=x;
|
||||
n->next=top;
|
||||
top=n;
|
||||
n->val = x;
|
||||
n->next = top;
|
||||
top = n;
|
||||
}
|
||||
|
||||
void pop()
|
||||
{
|
||||
if (top==NULL)
|
||||
if (top == NULL)
|
||||
{
|
||||
cout<<"\nUnderflow";
|
||||
cout << "\nUnderflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
node *t = top;
|
||||
cout<<"\n"<<t->val<<" deleted";
|
||||
top=top->next;
|
||||
cout << "\n"
|
||||
<< t->val << " deleted";
|
||||
top = top->next;
|
||||
delete t;
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
node *t=top;
|
||||
while(t!=NULL)
|
||||
node *t = top;
|
||||
while (t != NULL)
|
||||
{
|
||||
cout<<t->val<<"\n";
|
||||
t=t->next;
|
||||
cout << t->val << "\n";
|
||||
t = t->next;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,28 +48,26 @@ 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 << "\n1. Push";
|
||||
cout << "\n2. Pop";
|
||||
cout << "\n3. Print";
|
||||
cout << "\nEnter Your Choice : ";
|
||||
cin >> ch;
|
||||
if (ch == 1)
|
||||
{
|
||||
cout<<"\nInsert : ";
|
||||
cin>>x;
|
||||
cout << "\nInsert : ";
|
||||
cin >> x;
|
||||
push(x);
|
||||
}
|
||||
else if (ch==2)
|
||||
else if (ch == 2)
|
||||
{
|
||||
pop();
|
||||
}
|
||||
else if (ch==3)
|
||||
else if (ch == 3)
|
||||
{
|
||||
show();
|
||||
}
|
||||
}
|
||||
while(ch!=0);
|
||||
} while (ch != 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,47 +1,43 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
using namespace std;
|
||||
|
||||
|
||||
struct node
|
||||
{
|
||||
int val;
|
||||
node *left;
|
||||
node *right;
|
||||
int val;
|
||||
node *left;
|
||||
node *right;
|
||||
};
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
list<node*> queue;
|
||||
@@ -63,84 +59,80 @@ void BFT(node *n)
|
||||
|
||||
void Pre(node *n)
|
||||
{
|
||||
if (n!=NULL)
|
||||
{
|
||||
cout<<n->val<<" ";
|
||||
Pre(n->left);
|
||||
Pre(n->right);
|
||||
}
|
||||
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);
|
||||
}
|
||||
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<<" ";
|
||||
}
|
||||
if (n != NULL)
|
||||
{
|
||||
Post(n->left);
|
||||
Post(n->right);
|
||||
cout << n->val << " ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
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";
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include<iostream>
|
||||
#include<string.h>
|
||||
#include<stdbool.h>
|
||||
#include <iostream>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
using namespace std;
|
||||
// structure definition
|
||||
typedef struct trie
|
||||
@@ -8,7 +8,7 @@ typedef struct trie
|
||||
struct trie *arr[26];
|
||||
bool isEndofWord;
|
||||
} trie;
|
||||
// create a new node for trie
|
||||
// create a new node for trie
|
||||
trie *createNode()
|
||||
{
|
||||
trie *nn = new trie();
|
||||
@@ -18,8 +18,8 @@ trie *createNode()
|
||||
return nn;
|
||||
}
|
||||
|
||||
// insert string into the trie
|
||||
void insert(trie *root, char* str)
|
||||
// insert string into the trie
|
||||
void insert(trie *root, char *str)
|
||||
{
|
||||
for (int i = 0; i < strlen(str); i++)
|
||||
{
|
||||
@@ -38,7 +38,7 @@ void insert(trie *root, char* str)
|
||||
}
|
||||
|
||||
// search a string exists inside the trie
|
||||
bool search(trie *root, char* str, int index)
|
||||
bool search(trie *root, char *str, int index)
|
||||
{
|
||||
if (index == strlen(str))
|
||||
{
|
||||
@@ -54,7 +54,7 @@ bool search(trie *root, char* str, int index)
|
||||
/* removes the string if it is not a prefix of any other
|
||||
string, if it is then just sets the endofword to false, else
|
||||
removes the given string*/
|
||||
bool deleteString (trie *root, char* str, int index)
|
||||
bool deleteString(trie *root, char *str, int index)
|
||||
{
|
||||
if (index == strlen(str))
|
||||
{
|
||||
@@ -68,7 +68,7 @@ bool deleteString (trie *root, char* str, int index)
|
||||
int j = str[index] - 'a';
|
||||
if (!root->arr[j])
|
||||
return false;
|
||||
bool var = deleteString (root, str, index + 1);
|
||||
bool var = deleteString(root, str, index + 1);
|
||||
if (var)
|
||||
{
|
||||
root->arr[j] = NULL;
|
||||
|
||||
@@ -1,36 +1,44 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node{
|
||||
struct node
|
||||
{
|
||||
int data;
|
||||
struct node *next;
|
||||
};
|
||||
class Queue{
|
||||
class Queue
|
||||
{
|
||||
node *front;
|
||||
node *rear;
|
||||
|
||||
public:
|
||||
Queue(){
|
||||
Queue()
|
||||
{
|
||||
front = NULL;
|
||||
rear = NULL;
|
||||
}
|
||||
void createNode(int val){
|
||||
void createNode(int val)
|
||||
{
|
||||
node *ptr;
|
||||
node *nn;
|
||||
nn = new node;
|
||||
ptr = front;
|
||||
nn->data = val;
|
||||
nn->next = NULL;
|
||||
front=nn;
|
||||
rear=nn;
|
||||
front = nn;
|
||||
rear = nn;
|
||||
}
|
||||
void enqueue(int val){
|
||||
if(front==NULL || rear==NULL){
|
||||
void enqueue(int val)
|
||||
{
|
||||
if (front == NULL || rear == NULL)
|
||||
{
|
||||
createNode(val);
|
||||
}
|
||||
else{
|
||||
else
|
||||
{
|
||||
node *ptr;
|
||||
node *nn;
|
||||
ptr=front;
|
||||
ptr = front;
|
||||
nn = new node;
|
||||
nn->data = val;
|
||||
rear->next = nn;
|
||||
@@ -38,24 +46,28 @@ public:
|
||||
rear = nn;
|
||||
}
|
||||
}
|
||||
void dequeue(){
|
||||
void dequeue()
|
||||
{
|
||||
node *n;
|
||||
n = front;
|
||||
front = front->next;
|
||||
delete(n);
|
||||
delete (n);
|
||||
}
|
||||
void traverse(){
|
||||
void traverse()
|
||||
{
|
||||
node *ptr;
|
||||
ptr=front;
|
||||
do{
|
||||
cout<<ptr->data<<" ";
|
||||
ptr=ptr->next;
|
||||
}while(ptr!=rear->next);
|
||||
cout<<front->data;
|
||||
cout<<endl;
|
||||
ptr = front;
|
||||
do
|
||||
{
|
||||
cout << ptr->data << " ";
|
||||
ptr = ptr->next;
|
||||
} while (ptr != rear->next);
|
||||
cout << front->data;
|
||||
cout << endl;
|
||||
}
|
||||
};
|
||||
int main(void){
|
||||
int main(void)
|
||||
{
|
||||
Queue q;
|
||||
q.enqueue(10);
|
||||
q.enqueue(20);
|
||||
|
||||
@@ -3,107 +3,104 @@
|
||||
2. Limited size. (in the following case it is 100 nodes at max). But we can reuse the nodes that are to be deleted by again linking it bacj to the list.
|
||||
*/
|
||||
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
struct Node{
|
||||
struct Node
|
||||
{
|
||||
int data;
|
||||
int next;
|
||||
};
|
||||
Node AvailArray[100]; //array that will act as nodes of a linked list.
|
||||
int head=-1;
|
||||
int avail=0;
|
||||
Node AvailArray[100]; //array that will act as nodes of a linked list.
|
||||
int head = -1;
|
||||
int avail = 0;
|
||||
void initialise_list()
|
||||
{
|
||||
for(int i=0;i<=98;i++)
|
||||
for (int i = 0; i <= 98; i++)
|
||||
{
|
||||
AvailArray[i].next=i+1;
|
||||
AvailArray[i].next = i + 1;
|
||||
}
|
||||
AvailArray[99].next=-1; //indicating the end of the linked list.
|
||||
|
||||
AvailArray[99].next = -1; //indicating the end of the linked list.
|
||||
}
|
||||
|
||||
int getnode() //This will return the index of the first free node present in the avail list
|
||||
int getnode() //This will return the index of the first free node present in the avail list
|
||||
{
|
||||
int NodeIndexToBeReturned=avail;
|
||||
avail=AvailArray[avail].next;
|
||||
int NodeIndexToBeReturned = avail;
|
||||
avail = AvailArray[avail].next;
|
||||
return NodeIndexToBeReturned;
|
||||
}
|
||||
|
||||
void freeNode(int nodeToBeDeleted) //This function when called will delete the node with the index presented as an argument, and will put back that node into the array.
|
||||
void freeNode(int nodeToBeDeleted) //This function when called will delete the node with the index presented as an argument, and will put back that node into the array.
|
||||
{
|
||||
AvailArray[nodeToBeDeleted].next=avail;
|
||||
avail=nodeToBeDeleted;
|
||||
AvailArray[nodeToBeDeleted].next = avail;
|
||||
avail = nodeToBeDeleted;
|
||||
}
|
||||
|
||||
void insertAtTheBeginning(int data) //The function will insert the given data into the front of the linked list.
|
||||
void insertAtTheBeginning(int data) //The function will insert the given data into the front of the linked list.
|
||||
{
|
||||
int newNode=getnode();
|
||||
AvailArray[newNode].data=data;
|
||||
AvailArray[newNode].next=head;
|
||||
head=newNode;
|
||||
|
||||
int newNode = getnode();
|
||||
AvailArray[newNode].data = data;
|
||||
AvailArray[newNode].next = head;
|
||||
head = newNode;
|
||||
}
|
||||
|
||||
void insertAtTheEnd(int data)
|
||||
{
|
||||
int newNode=getnode();
|
||||
int temp=head;
|
||||
while(AvailArray[temp].next!=-1)
|
||||
int newNode = getnode();
|
||||
int temp = head;
|
||||
while (AvailArray[temp].next != -1)
|
||||
{
|
||||
temp=AvailArray[temp].next;
|
||||
|
||||
temp = AvailArray[temp].next;
|
||||
}
|
||||
//temp is now pointing to the end node.
|
||||
AvailArray[newNode].data=data;
|
||||
AvailArray[newNode].next=-1;
|
||||
AvailArray[temp].next=newNode;
|
||||
AvailArray[newNode].data = data;
|
||||
AvailArray[newNode].next = -1;
|
||||
AvailArray[temp].next = newNode;
|
||||
}
|
||||
|
||||
|
||||
void display()
|
||||
{
|
||||
int temp=head;
|
||||
while(temp!=-1)
|
||||
int temp = head;
|
||||
while (temp != -1)
|
||||
{
|
||||
cout<<AvailArray[temp].data<<"->";
|
||||
temp=AvailArray[temp].next;
|
||||
cout << AvailArray[temp].data << "->";
|
||||
temp = AvailArray[temp].next;
|
||||
}
|
||||
cout<<"-1"<<endl;;
|
||||
cout << "-1" << endl;
|
||||
;
|
||||
}
|
||||
|
||||
int main()
|
||||
{ initialise_list();
|
||||
int x,y,z;
|
||||
for(;;)
|
||||
{
|
||||
initialise_list();
|
||||
int x, y, z;
|
||||
for (;;)
|
||||
{
|
||||
cout<<"1. Insert At The Beginning"<<endl;
|
||||
cout<<"2. Insert At The End"<<endl;
|
||||
cout<<"3. Display"<<endl;
|
||||
cout<<"4.Exit"<<endl;
|
||||
cout<<"Enter Your choice"<<endl;
|
||||
cin>>z;
|
||||
switch(z)
|
||||
cout << "1. Insert At The Beginning" << endl;
|
||||
cout << "2. Insert At The End" << endl;
|
||||
cout << "3. Display" << endl;
|
||||
cout << "4.Exit" << endl;
|
||||
cout << "Enter Your choice" << endl;
|
||||
cin >> z;
|
||||
switch (z)
|
||||
{
|
||||
case 1:
|
||||
cout<<"Enter the number you want to enter"<<endl;
|
||||
cin>>x;
|
||||
insertAtTheBeginning(x);
|
||||
break;
|
||||
case 2:
|
||||
cout<<"Enter the number you want to enter"<<endl;
|
||||
cin>>y;
|
||||
insertAtTheEnd(y);
|
||||
break;
|
||||
case 3:
|
||||
cout<<"The linked list contains the following element in order"<<endl;
|
||||
display();
|
||||
break;
|
||||
case 4:
|
||||
exit(0);
|
||||
default:
|
||||
cout<<"The entered choice is not correct"<<endl;
|
||||
|
||||
|
||||
case 1:
|
||||
cout << "Enter the number you want to enter" << endl;
|
||||
cin >> x;
|
||||
insertAtTheBeginning(x);
|
||||
break;
|
||||
case 2:
|
||||
cout << "Enter the number you want to enter" << endl;
|
||||
cin >> y;
|
||||
insertAtTheEnd(y);
|
||||
break;
|
||||
case 3:
|
||||
cout << "The linked list contains the following element in order" << endl;
|
||||
display();
|
||||
break;
|
||||
case 4:
|
||||
exit(0);
|
||||
default:
|
||||
cout << "The entered choice is not correct" << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user