mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
fix documentations and cpplint
This commit is contained in:
parent
6635c0a1af
commit
aacaf9828c
@ -1,8 +1,15 @@
|
|||||||
|
/**
|
||||||
|
* \file
|
||||||
|
* \brief A simple tree implementation using nodes
|
||||||
|
*
|
||||||
|
* \todo update code to use C++ STL library features and OO structure
|
||||||
|
* \warning This program is a poor implementation and does not utilize any of
|
||||||
|
* the C++ STL features.
|
||||||
|
*/
|
||||||
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
typedef struct node {
|
typedef struct node {
|
||||||
int data;
|
int data;
|
||||||
int height;
|
int height;
|
||||||
@ -10,10 +17,7 @@ typedef struct node {
|
|||||||
struct node *right;
|
struct node *right;
|
||||||
} node;
|
} node;
|
||||||
|
|
||||||
int max(int a, int b) { return a > b ? a : b; }
|
/** Create and return a new Node */
|
||||||
|
|
||||||
// Returns a new Node
|
|
||||||
|
|
||||||
node *createNode(int data) {
|
node *createNode(int data) {
|
||||||
node *nn = new node();
|
node *nn = new node();
|
||||||
nn->data = data;
|
nn->data = data;
|
||||||
@ -23,20 +27,17 @@ node *createNode(int data) {
|
|||||||
return nn;
|
return nn;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns height of tree
|
/** Returns height of tree */
|
||||||
|
|
||||||
int height(node *root) {
|
int height(node *root) {
|
||||||
if (root == NULL)
|
if (root == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
return 1 + max(height(root->left), height(root->right));
|
return 1 + std::max(height(root->left), height(root->right));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns difference between height of left and right subtree
|
/** Returns difference between height of left and right subtree */
|
||||||
|
|
||||||
int getBalance(node *root) { return height(root->left) - height(root->right); }
|
int getBalance(node *root) { return height(root->left) - height(root->right); }
|
||||||
|
|
||||||
// Returns Node after Right Rotation
|
/** Returns Node after Right Rotation */
|
||||||
|
|
||||||
node *rightRotate(node *root) {
|
node *rightRotate(node *root) {
|
||||||
node *t = root->left;
|
node *t = root->left;
|
||||||
node *u = t->right;
|
node *u = t->right;
|
||||||
@ -45,8 +46,7 @@ node *rightRotate(node *root) {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns Node after Left Rotation
|
/** Returns Node after Left Rotation */
|
||||||
|
|
||||||
node *leftRotate(node *root) {
|
node *leftRotate(node *root) {
|
||||||
node *t = root->right;
|
node *t = root->right;
|
||||||
node *u = t->left;
|
node *u = t->left;
|
||||||
@ -55,16 +55,14 @@ node *leftRotate(node *root) {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns node with minimum value in the tree
|
/** Returns node with minimum value in the tree */
|
||||||
|
|
||||||
node *minValue(node *root) {
|
node *minValue(node *root) {
|
||||||
if (root->left == NULL)
|
if (root->left == NULL)
|
||||||
return root;
|
return root;
|
||||||
return minValue(root->left);
|
return minValue(root->left);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Balanced Insertion
|
/** Balanced Insertion */
|
||||||
|
|
||||||
node *insert(node *root, int item) {
|
node *insert(node *root, int item) {
|
||||||
node *nn = createNode(item);
|
node *nn = createNode(item);
|
||||||
if (root == NULL)
|
if (root == NULL)
|
||||||
@ -86,8 +84,7 @@ node *insert(node *root, int item) {
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Balanced Deletion
|
/** Balanced Deletion */
|
||||||
|
|
||||||
node *deleteNode(node *root, int key) {
|
node *deleteNode(node *root, int key) {
|
||||||
if (root == NULL)
|
if (root == NULL)
|
||||||
return root;
|
return root;
|
||||||
@ -118,14 +115,13 @@ node *deleteNode(node *root, int key) {
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
// LevelOrder (Breadth First Search)
|
/** LevelOrder (Breadth First Search) */
|
||||||
|
|
||||||
void levelOrder(node *root) {
|
void levelOrder(node *root) {
|
||||||
queue<node *> q;
|
std::queue<node *> q;
|
||||||
q.push(root);
|
q.push(root);
|
||||||
while (!q.empty()) {
|
while (!q.empty()) {
|
||||||
root = q.front();
|
root = q.front();
|
||||||
cout << root->data << " ";
|
std::cout << root->data << " ";
|
||||||
q.pop();
|
q.pop();
|
||||||
if (root->left)
|
if (root->left)
|
||||||
q.push(root->left);
|
q.push(root->left);
|
||||||
@ -134,18 +130,19 @@ void levelOrder(node *root) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Main function */
|
||||||
int main() {
|
int main() {
|
||||||
// Testing AVL Tree
|
// Testing AVL Tree
|
||||||
node *root = NULL;
|
node *root = NULL;
|
||||||
int i;
|
int i;
|
||||||
for (i = 1; i <= 7; i++) root = insert(root, i);
|
for (i = 1; i <= 7; i++) root = insert(root, i);
|
||||||
cout << "LevelOrder: ";
|
std::cout << "LevelOrder: ";
|
||||||
levelOrder(root);
|
levelOrder(root);
|
||||||
root = deleteNode(root, 1); // Deleting key with value 1
|
root = deleteNode(root, 1); // Deleting key with value 1
|
||||||
cout << "\nLevelOrder: ";
|
std::cout << "\nLevelOrder: ";
|
||||||
levelOrder(root);
|
levelOrder(root);
|
||||||
root = deleteNode(root, 4); // Deletin key with value 4
|
root = deleteNode(root, 4); // Deletin key with value 4
|
||||||
cout << "\nLevelOrder: ";
|
std::cout << "\nLevelOrder: ";
|
||||||
levelOrder(root);
|
levelOrder(root);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,12 @@
|
|||||||
|
/**
|
||||||
|
* \file
|
||||||
|
* \brief A simple tree implementation using structured nodes
|
||||||
|
*
|
||||||
|
* \todo update code to use C++ STL library features and OO structure
|
||||||
|
* \warning This program is a poor implementation - C style - and does not
|
||||||
|
* utilize any of the C++ STL features.
|
||||||
|
*/
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
struct node {
|
struct node {
|
||||||
int val;
|
int val;
|
||||||
@ -84,7 +91,7 @@ void Remove(node *p, node *n, int x) {
|
|||||||
|
|
||||||
void BFT(node *n) {
|
void BFT(node *n) {
|
||||||
if (n != NULL) {
|
if (n != NULL) {
|
||||||
cout << n->val << " ";
|
std::cout << n->val << " ";
|
||||||
enqueue(n->left);
|
enqueue(n->left);
|
||||||
enqueue(n->right);
|
enqueue(n->right);
|
||||||
BFT(dequeue());
|
BFT(dequeue());
|
||||||
@ -93,7 +100,7 @@ void BFT(node *n) {
|
|||||||
|
|
||||||
void Pre(node *n) {
|
void Pre(node *n) {
|
||||||
if (n != NULL) {
|
if (n != NULL) {
|
||||||
cout << n->val << " ";
|
std::cout << n->val << " ";
|
||||||
Pre(n->left);
|
Pre(n->left);
|
||||||
Pre(n->right);
|
Pre(n->right);
|
||||||
}
|
}
|
||||||
@ -102,7 +109,7 @@ void Pre(node *n) {
|
|||||||
void In(node *n) {
|
void In(node *n) {
|
||||||
if (n != NULL) {
|
if (n != NULL) {
|
||||||
In(n->left);
|
In(n->left);
|
||||||
cout << n->val << " ";
|
std::cout << n->val << " ";
|
||||||
In(n->right);
|
In(n->right);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -111,7 +118,7 @@ void Post(node *n) {
|
|||||||
if (n != NULL) {
|
if (n != NULL) {
|
||||||
Post(n->left);
|
Post(n->left);
|
||||||
Post(n->right);
|
Post(n->right);
|
||||||
cout << n->val << " ";
|
std::cout << n->val << " ";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,45 +128,47 @@ int main() {
|
|||||||
int value;
|
int value;
|
||||||
int ch;
|
int ch;
|
||||||
node *root = new node;
|
node *root = new node;
|
||||||
cout << "\nEnter the value of root node :";
|
std::cout << "\nEnter the value of root node :";
|
||||||
cin >> value;
|
std::cin >> value;
|
||||||
root->val = value;
|
root->val = value;
|
||||||
root->left = NULL;
|
root->left = NULL;
|
||||||
root->right = NULL;
|
root->right = NULL;
|
||||||
do {
|
do {
|
||||||
cout << "\n1. Insert";
|
std::cout << "\n1. Insert"
|
||||||
cout << "\n2. Delete";
|
<< "\n2. Delete"
|
||||||
cout << "\n3. Breadth First";
|
<< "\n3. Breadth First"
|
||||||
cout << "\n4. Preorder Depth First";
|
<< "\n4. Preorder Depth First"
|
||||||
cout << "\n5. Inorder Depth First";
|
<< "\n5. Inorder Depth First"
|
||||||
cout << "\n6. Postorder Depth First";
|
<< "\n6. Postorder Depth First";
|
||||||
|
|
||||||
cout << "\nEnter Your Choice : ";
|
std::cout << "\nEnter Your Choice : ";
|
||||||
cin >> ch;
|
std::cin >> ch;
|
||||||
int x;
|
int x;
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case 1:
|
case 1:
|
||||||
cout << "\nEnter the value to be Inserted : ";
|
std::cout << "\nEnter the value to be Inserted : ";
|
||||||
cin >> x;
|
std::cin >> x;
|
||||||
Insert(root, x);
|
Insert(root, x);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
cout << "\nEnter the value to be Deleted : ";
|
std::cout << "\nEnter the value to be Deleted : ";
|
||||||
cin >> x;
|
std::cin >> x;
|
||||||
Remove(root, root, x);
|
Remove(root, root, x);
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
BFT(root);
|
BFT(root);
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
Pre(root);
|
Pre(root);
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
In(root);
|
In(root);
|
||||||
break;
|
break;
|
||||||
case 6:
|
case 6:
|
||||||
Post(root);
|
Post(root);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} while (ch != 0);
|
} while (ch != 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,48 +1,52 @@
|
|||||||
// A C++ program to demonstrate common Binary Heap Operations
|
/**
|
||||||
|
* \file
|
||||||
|
* \brief A C++ program to demonstrate common Binary Heap Operations
|
||||||
|
*/
|
||||||
#include <climits>
|
#include <climits>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
using namespace std;
|
#include <utility>
|
||||||
|
|
||||||
// Prototype of a utility function to swap two integers
|
/** A class for Min Heap */
|
||||||
void swap(int *x, int *y);
|
|
||||||
|
|
||||||
// A class for Min Heap
|
|
||||||
class MinHeap {
|
class MinHeap {
|
||||||
int *harr; // pointer to array of elements in heap
|
int *harr; ///< pointer to array of elements in heap
|
||||||
int capacity; // maximum possible size of min heap
|
int capacity; ///< maximum possible size of min heap
|
||||||
int heap_size; // Current number of elements in min heap
|
int heap_size; ///< Current number of elements in min heap
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
/** Constructor
|
||||||
|
* \param[in] capacity initial heap capacity
|
||||||
|
*/
|
||||||
MinHeap(int capacity);
|
MinHeap(int capacity);
|
||||||
|
|
||||||
// to heapify a subtree with the root at given index
|
/** to heapify a subtree with the root at given index
|
||||||
|
*/
|
||||||
void MinHeapify(int);
|
void MinHeapify(int);
|
||||||
|
|
||||||
int parent(int i) { return (i - 1) / 2; }
|
int parent(int i) { return (i - 1) / 2; }
|
||||||
|
|
||||||
// to get index of left child of node at index i
|
/** to get index of left child of node at index i */
|
||||||
int left(int i) { return (2 * i + 1); }
|
int left(int i) { return (2 * i + 1); }
|
||||||
|
|
||||||
// to get index of right child of node at index i
|
/** to get index of right child of node at index i */
|
||||||
int right(int i) { return (2 * i + 2); }
|
int right(int i) { return (2 * i + 2); }
|
||||||
|
|
||||||
// to extract the root which is the minimum element
|
/** to extract the root which is the minimum element */
|
||||||
int extractMin();
|
int extractMin();
|
||||||
|
|
||||||
// Decreases key value of key at index i to new_val
|
/** Decreases key value of key at index i to new_val */
|
||||||
void decreaseKey(int i, int new_val);
|
void decreaseKey(int i, int new_val);
|
||||||
|
|
||||||
// Returns the minimum key (key at root) from min heap
|
/** Returns the minimum key (key at root) from min heap */
|
||||||
int getMin() { return harr[0]; }
|
int getMin() { return harr[0]; }
|
||||||
|
|
||||||
// Deletes a key stored at index i
|
/** Deletes a key stored at index i */
|
||||||
void deleteKey(int i);
|
void deleteKey(int i);
|
||||||
|
|
||||||
// Inserts a new key 'k'
|
/** Inserts a new key 'k' */
|
||||||
void insertKey(int k);
|
void insertKey(int k);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Constructor: Builds a heap from a given array a[] of given size
|
/** Constructor: Builds a heap from a given array a[] of given size */
|
||||||
MinHeap::MinHeap(int cap) {
|
MinHeap::MinHeap(int cap) {
|
||||||
heap_size = 0;
|
heap_size = 0;
|
||||||
capacity = cap;
|
capacity = cap;
|
||||||
@ -52,7 +56,7 @@ MinHeap::MinHeap(int cap) {
|
|||||||
// Inserts a new key 'k'
|
// Inserts a new key 'k'
|
||||||
void MinHeap::insertKey(int k) {
|
void MinHeap::insertKey(int k) {
|
||||||
if (heap_size == capacity) {
|
if (heap_size == capacity) {
|
||||||
cout << "\nOverflow: Could not insertKey\n";
|
std::cout << "\nOverflow: Could not insertKey\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,17 +67,18 @@ void MinHeap::insertKey(int k) {
|
|||||||
|
|
||||||
// Fix the min heap property if it is violated
|
// Fix the min heap property if it is violated
|
||||||
while (i != 0 && harr[parent(i)] > harr[i]) {
|
while (i != 0 && harr[parent(i)] > harr[i]) {
|
||||||
swap(&harr[i], &harr[parent(i)]);
|
std::swap(harr[i], harr[parent(i)]);
|
||||||
i = parent(i);
|
i = parent(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decreases value of key at index 'i' to new_val. It is assumed that
|
/** Decreases value of key at index 'i' to new_val. It is assumed that new_val
|
||||||
// new_val is smaller than harr[i].
|
* is smaller than harr[i].
|
||||||
|
*/
|
||||||
void MinHeap::decreaseKey(int i, int new_val) {
|
void MinHeap::decreaseKey(int i, int new_val) {
|
||||||
harr[i] = new_val;
|
harr[i] = new_val;
|
||||||
while (i != 0 && harr[parent(i)] > harr[i]) {
|
while (i != 0 && harr[parent(i)] > harr[i]) {
|
||||||
swap(&harr[i], &harr[parent(i)]);
|
std::swap(harr[i], harr[parent(i)]);
|
||||||
i = parent(i);
|
i = parent(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -96,15 +101,17 @@ int MinHeap::extractMin() {
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function deletes key at index i. It first reduced value to minus
|
/** This function deletes key at index i. It first reduced value to minus
|
||||||
// infinite, then calls extractMin()
|
* infinite, then calls extractMin()
|
||||||
|
*/
|
||||||
void MinHeap::deleteKey(int i) {
|
void MinHeap::deleteKey(int i) {
|
||||||
decreaseKey(i, INT_MIN);
|
decreaseKey(i, INT_MIN);
|
||||||
extractMin();
|
extractMin();
|
||||||
}
|
}
|
||||||
|
|
||||||
// A recursive method to heapify a subtree with the root at given index
|
/** A recursive method to heapify a subtree with the root at given index
|
||||||
// This method assumes that the subtrees are already heapified
|
* This method assumes that the subtrees are already heapified
|
||||||
|
*/
|
||||||
void MinHeap::MinHeapify(int i) {
|
void MinHeap::MinHeapify(int i) {
|
||||||
int l = left(i);
|
int l = left(i);
|
||||||
int r = right(i);
|
int r = right(i);
|
||||||
@ -114,18 +121,11 @@ void MinHeap::MinHeapify(int i) {
|
|||||||
if (r < heap_size && harr[r] < harr[smallest])
|
if (r < heap_size && harr[r] < harr[smallest])
|
||||||
smallest = r;
|
smallest = r;
|
||||||
if (smallest != i) {
|
if (smallest != i) {
|
||||||
swap(&harr[i], &harr[smallest]);
|
std::swap(harr[i], harr[smallest]);
|
||||||
MinHeapify(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
|
// Driver program to test above functions
|
||||||
int main() {
|
int main() {
|
||||||
MinHeap h(11);
|
MinHeap h(11);
|
||||||
@ -136,9 +136,9 @@ int main() {
|
|||||||
h.insertKey(5);
|
h.insertKey(5);
|
||||||
h.insertKey(4);
|
h.insertKey(4);
|
||||||
h.insertKey(45);
|
h.insertKey(45);
|
||||||
cout << h.extractMin() << " ";
|
std::cout << h.extractMin() << " ";
|
||||||
cout << h.getMin() << " ";
|
std::cout << h.getMin() << " ";
|
||||||
h.decreaseKey(2, 1);
|
h.decreaseKey(2, 1);
|
||||||
cout << h.getMin();
|
std::cout << h.getMin();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
struct node {
|
struct node {
|
||||||
int data;
|
int data;
|
||||||
@ -48,11 +47,10 @@ class Queue {
|
|||||||
node *ptr;
|
node *ptr;
|
||||||
ptr = front;
|
ptr = front;
|
||||||
do {
|
do {
|
||||||
cout << ptr->data << " ";
|
std::cout << ptr->data << " ";
|
||||||
ptr = ptr->next;
|
ptr = ptr->next;
|
||||||
} while (ptr != rear->next);
|
} while (ptr != rear->next);
|
||||||
cout << front->data;
|
std::cout << front->data << std::endl;
|
||||||
cout << endl;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
@ -1,17 +1,23 @@
|
|||||||
/* The difference between the pointer implementation of linked list and array
|
/**
|
||||||
implementation of linked list:
|
* \file
|
||||||
1. The NULL is represented by -1;
|
* \brief Linked list implementation using Arrays
|
||||||
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.
|
* The difference between the pointer implementation of linked list and array
|
||||||
*/
|
* implementation of linked list:
|
||||||
|
* 1. The NULL is represented by -1;
|
||||||
|
* 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 data;
|
||||||
int next;
|
int next;
|
||||||
};
|
};
|
||||||
Node AvailArray[100]; // array that will act as nodes of a linked list.
|
|
||||||
|
Node AvailArray[100]; ///< array that will act as nodes of a linked list.
|
||||||
|
|
||||||
int head = -1;
|
int head = -1;
|
||||||
int avail = 0;
|
int avail = 0;
|
||||||
void initialise_list() {
|
void initialise_list() {
|
||||||
@ -21,26 +27,27 @@ void initialise_list() {
|
|||||||
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
|
/** This will return the index of the first free node present in the avail list
|
||||||
// the avail list
|
*/
|
||||||
{
|
int getnode() {
|
||||||
int NodeIndexToBeReturned = avail;
|
int NodeIndexToBeReturned = avail;
|
||||||
avail = AvailArray[avail].next;
|
avail = AvailArray[avail].next;
|
||||||
return NodeIndexToBeReturned;
|
return NodeIndexToBeReturned;
|
||||||
}
|
}
|
||||||
|
|
||||||
void freeNode(
|
/** This function when called will delete the node with
|
||||||
int nodeToBeDeleted) // This function when called will delete the node with
|
* the index presented as an argument, and will put
|
||||||
// the index presented as an argument, and will put
|
* back that node into the array.
|
||||||
// back that node into the array.
|
*/
|
||||||
{
|
void freeNode(int nodeToBeDeleted) {
|
||||||
AvailArray[nodeToBeDeleted].next = avail;
|
AvailArray[nodeToBeDeleted].next = avail;
|
||||||
avail = nodeToBeDeleted;
|
avail = nodeToBeDeleted;
|
||||||
}
|
}
|
||||||
|
|
||||||
void insertAtTheBeginning(int data) // The function will insert the given data
|
/** The function will insert the given data
|
||||||
// into the front of the linked list.
|
* into the front of the linked list.
|
||||||
{
|
*/
|
||||||
|
void insertAtTheBeginning(int data) {
|
||||||
int newNode = getnode();
|
int newNode = getnode();
|
||||||
AvailArray[newNode].data = data;
|
AvailArray[newNode].data = data;
|
||||||
AvailArray[newNode].next = head;
|
AvailArray[newNode].next = head;
|
||||||
@ -62,43 +69,46 @@ void insertAtTheEnd(int data) {
|
|||||||
void display() {
|
void display() {
|
||||||
int temp = head;
|
int temp = head;
|
||||||
while (temp != -1) {
|
while (temp != -1) {
|
||||||
cout << AvailArray[temp].data << "->";
|
std::cout << AvailArray[temp].data << "->";
|
||||||
temp = AvailArray[temp].next;
|
temp = AvailArray[temp].next;
|
||||||
}
|
}
|
||||||
cout << "-1" << endl;
|
std::cout << "-1" << std::endl;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Main function */
|
||||||
int main() {
|
int main() {
|
||||||
initialise_list();
|
initialise_list();
|
||||||
int x, y, z;
|
int x, y, z;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
cout << "1. Insert At The Beginning" << endl;
|
std::cout << "1. Insert At The Beginning" << std::endl;
|
||||||
cout << "2. Insert At The End" << endl;
|
std::cout << "2. Insert At The End" << std::endl;
|
||||||
cout << "3. Display" << endl;
|
std::cout << "3. Display" << std::endl;
|
||||||
cout << "4.Exit" << endl;
|
std::cout << "4.Exit" << std::endl;
|
||||||
cout << "Enter Your choice" << endl;
|
std::cout << "Enter Your choice" << std::endl;
|
||||||
cin >> z;
|
std::cin >> z;
|
||||||
switch (z) {
|
switch (z) {
|
||||||
case 1:
|
case 1:
|
||||||
cout << "Enter the number you want to enter" << endl;
|
std::cout << "Enter the number you want to enter" << std::endl;
|
||||||
cin >> x;
|
std::cin >> x;
|
||||||
insertAtTheBeginning(x);
|
insertAtTheBeginning(x);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
cout << "Enter the number you want to enter" << endl;
|
std::cout << "Enter the number you want to enter" << std::endl;
|
||||||
cin >> y;
|
std::cin >> y;
|
||||||
insertAtTheEnd(y);
|
insertAtTheEnd(y);
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
cout << "The linked list contains the following element in order"
|
std::cout
|
||||||
<< endl;
|
<< "The linked list contains the following element in order"
|
||||||
display();
|
<< std::endl;
|
||||||
break;
|
display();
|
||||||
case 4:
|
break;
|
||||||
exit(0);
|
case 4:
|
||||||
default:
|
return 0;
|
||||||
cout << "The entered choice is not correct" << endl;
|
default:
|
||||||
|
std::cout << "The entered choice is not correct" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright 2020 @author Anmol3299
|
|
||||||
* @file
|
* @file
|
||||||
*
|
*
|
||||||
* A basic implementation of trie class to store only lower-case strings.
|
* Copyright 2020 @author Anmol3299
|
||||||
|
* \brief A basic implementation of trie class to store only lower-case strings.
|
||||||
*/
|
*/
|
||||||
#include <iostream> // for io operations
|
#include <iostream> // for io operations
|
||||||
#include <memory> // for std::shared_ptr<>
|
#include <memory> // for std::shared_ptr<>
|
||||||
@ -35,7 +35,8 @@ class Trie {
|
|||||||
* Function to check if a node has some children which can form words.
|
* Function to check if a node has some children which can form words.
|
||||||
* @param node whose character array of pointers need to be checked for
|
* @param node whose character array of pointers need to be checked for
|
||||||
* children.
|
* children.
|
||||||
* @return if a child is found, it returns @ true, else it returns @ false.
|
* @return `true` if a child is found
|
||||||
|
* @return `false` if a child is not found
|
||||||
*/
|
*/
|
||||||
inline static bool hasChildren(std::shared_ptr<TrieNode> node) {
|
inline static bool hasChildren(std::shared_ptr<TrieNode> node) {
|
||||||
for (size_t i = 0; i < ALPHABETS; i++) {
|
for (size_t i = 0; i < ALPHABETS; i++) {
|
||||||
@ -98,7 +99,7 @@ class Trie {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// constructor to initialise the root of the trie.
|
/// constructor to initialise the root of the trie.
|
||||||
Trie() : m_root(std::make_shared<TrieNode>()) {}
|
Trie() : m_root(std::make_shared<TrieNode>()) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user