mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-05 03:29:46 +08:00
rename Data Structure -> data_structure (#644)
This commit is contained in:
176
data_structure/AVLtree.cpp
Normal file
176
data_structure/AVLtree.cpp
Normal file
@@ -0,0 +1,176 @@
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef struct node
|
||||
{
|
||||
int data;
|
||||
int height;
|
||||
struct node *left;
|
||||
struct node *right;
|
||||
} node;
|
||||
|
||||
int max(int a, int b)
|
||||
{
|
||||
return a > b ? a : b;
|
||||
}
|
||||
|
||||
// Returns a new Node
|
||||
|
||||
node *createNode(int data)
|
||||
{
|
||||
node *nn = new node();
|
||||
nn->data = data;
|
||||
nn->height = 0;
|
||||
nn->left = NULL;
|
||||
nn->right = NULL;
|
||||
return nn;
|
||||
}
|
||||
|
||||
// Returns height of tree
|
||||
|
||||
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)
|
||||
{
|
||||
return height(root->left) - height(root->right);
|
||||
}
|
||||
|
||||
// Returns Node after Right Rotation
|
||||
|
||||
node *rightRotate(node *root)
|
||||
{
|
||||
node *t = root->left;
|
||||
node *u = t->right;
|
||||
t->right = root;
|
||||
root->left = u;
|
||||
return t;
|
||||
}
|
||||
|
||||
// Returns Node after Left Rotation
|
||||
|
||||
node *leftRotate(node *root)
|
||||
{
|
||||
node *t = root->right;
|
||||
node *u = t->left;
|
||||
t->left = root;
|
||||
root->right = u;
|
||||
return t;
|
||||
}
|
||||
|
||||
// Returns node with minimum value in the tree
|
||||
|
||||
node *minValue(node *root)
|
||||
{
|
||||
if (root->left == NULL)
|
||||
return root;
|
||||
return minValue(root->left);
|
||||
}
|
||||
|
||||
// Balanced Insertion
|
||||
|
||||
node *insert(node *root, int item)
|
||||
{
|
||||
node *nn = createNode(item);
|
||||
if (root == NULL)
|
||||
return nn;
|
||||
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
|
||||
}
|
||||
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)
|
||||
return root;
|
||||
if (key < root->data)
|
||||
root->left = deleteNode(root->left, key);
|
||||
else if (key > root->data)
|
||||
root->right = deleteNode(root->right, key);
|
||||
|
||||
else
|
||||
{
|
||||
// Node to be deleted is leaf node or have only one Child
|
||||
if (!root->right)
|
||||
{
|
||||
node *temp = root->left;
|
||||
delete (root);
|
||||
root = NULL;
|
||||
return temp;
|
||||
}
|
||||
else if (!root->left)
|
||||
{
|
||||
node *temp = root->right;
|
||||
delete (root);
|
||||
root = NULL;
|
||||
return temp;
|
||||
}
|
||||
// Node to be deleted have both left and right subtrees
|
||||
node *temp = minValue(root->right);
|
||||
root->data = temp->data;
|
||||
root->right = deleteNode(root->right, temp->data);
|
||||
}
|
||||
// Balancing Tree after deletion
|
||||
return root;
|
||||
}
|
||||
|
||||
// LevelOrder (Breadth First Search)
|
||||
|
||||
void levelOrder(node *root)
|
||||
{
|
||||
queue<node *> q;
|
||||
q.push(root);
|
||||
while (!q.empty())
|
||||
{
|
||||
root = q.front();
|
||||
cout << root->data << " ";
|
||||
q.pop();
|
||||
if (root->left)
|
||||
q.push(root->left);
|
||||
if (root->right)
|
||||
q.push(root->right);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// Testing AVL Tree
|
||||
node *root = NULL;
|
||||
int i;
|
||||
for (i = 1; i <= 7; i++)
|
||||
root = insert(root, i);
|
||||
cout << "LevelOrder: ";
|
||||
levelOrder(root);
|
||||
root = deleteNode(root, 1); // Deleting key with value 1
|
||||
cout << "\nLevelOrder: ";
|
||||
levelOrder(root);
|
||||
root = deleteNode(root, 4); // Deletin key with value 4
|
||||
cout << "\nLevelOrder: ";
|
||||
levelOrder(root);
|
||||
return 0;
|
||||
}
|
||||
218
data_structure/Binary Search Tree.cpp
Normal file
218
data_structure/Binary Search Tree.cpp
Normal file
@@ -0,0 +1,218 @@
|
||||
#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);
|
||||
}
|
||||
158
data_structure/Binaryheap.cpp
Normal file
158
data_structure/Binaryheap.cpp
Normal file
@@ -0,0 +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;
|
||||
}
|
||||
134
data_structure/Doubly Linked List.cpp
Normal file
134
data_structure/Doubly Linked List.cpp
Normal file
@@ -0,0 +1,134 @@
|
||||
#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;
|
||||
}
|
||||
160
data_structure/Linked List.cpp
Normal file
160
data_structure/Linked List.cpp
Normal file
@@ -0,0 +1,160 @@
|
||||
#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)
|
||||
{
|
||||
|
||||
if (start == NULL)
|
||||
{
|
||||
cout << "\nLinked List is empty\n";
|
||||
return;
|
||||
}
|
||||
else if (start->val == x)
|
||||
{
|
||||
node *temp = start;
|
||||
start = start->next;
|
||||
delete temp;
|
||||
return;
|
||||
}
|
||||
|
||||
node *temp = start, *parent = start;
|
||||
|
||||
while (temp != NULL && temp->val != x)
|
||||
{
|
||||
parent = temp;
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
if (temp == NULL)
|
||||
{
|
||||
cout << endl
|
||||
<< x << " not found in list\n";
|
||||
return;
|
||||
}
|
||||
|
||||
parent->next = temp->next;
|
||||
delete temp;
|
||||
}
|
||||
|
||||
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 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;
|
||||
}
|
||||
|
||||
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;
|
||||
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;
|
||||
}
|
||||
} while (choice != 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
188
data_structure/List Array.cpp
Normal file
188
data_structure/List Array.cpp
Normal file
@@ -0,0 +1,188 @@
|
||||
#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;
|
||||
}
|
||||
108
data_structure/MorrisInorder.cpp
Normal file
108
data_structure/MorrisInorder.cpp
Normal file
@@ -0,0 +1,108 @@
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
|
||||
/**************************
|
||||
@author shrutisheoran
|
||||
**************************/
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Btree
|
||||
{
|
||||
int data;
|
||||
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
|
||||
nn->data = d;
|
||||
nn->left = NULL;
|
||||
nn->right = NULL;
|
||||
if (*root == NULL)
|
||||
{
|
||||
*root = nn;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
queue<Btree *> q;
|
||||
// Adding root node to queue
|
||||
q.push(*root);
|
||||
while (!q.empty())
|
||||
{
|
||||
Btree *node = q.front();
|
||||
// Removing parent node from queue
|
||||
q.pop();
|
||||
if (node->left)
|
||||
// Adding left child of removed node to queue
|
||||
q.push(node->left);
|
||||
else
|
||||
{
|
||||
// Adding new node if no left child is present
|
||||
node->left = nn;
|
||||
return;
|
||||
}
|
||||
if (node->right)
|
||||
// Adding right child of removed node to queue
|
||||
q.push(node->right);
|
||||
else
|
||||
{
|
||||
// Adding new node if no right child is present
|
||||
node->right = nn;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void morrisInorder(Btree *root)
|
||||
{
|
||||
Btree *curr = root;
|
||||
Btree *temp;
|
||||
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
|
||||
{
|
||||
// Left of current node is stored in temp
|
||||
temp = curr->left;
|
||||
// Moving to extreme right of temp
|
||||
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)
|
||||
{
|
||||
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 << " ";
|
||||
temp->right = NULL;
|
||||
// current node is made to point its right subtree
|
||||
curr = curr->right;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// Testing morrisInorder funtion
|
||||
Btree *root = NULL;
|
||||
int i;
|
||||
for (i = 1; i <= 7; i++)
|
||||
insert(&root, i);
|
||||
cout << "Morris Inorder: ";
|
||||
morrisInorder(root);
|
||||
return 0;
|
||||
}
|
||||
75
data_structure/Queue Using Array.cpp
Normal file
75
data_structure/Queue Using Array.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#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;
|
||||
}
|
||||
89
data_structure/Queue Using Linked List.cpp
Normal file
89
data_structure/Queue Using Linked List.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
#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 == 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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
79
data_structure/Stack Using Array.cpp
Normal file
79
data_structure/Stack Using Array.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int *stack;
|
||||
int top = 0, size;
|
||||
|
||||
void push(int x)
|
||||
{
|
||||
if (top == size)
|
||||
{
|
||||
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()
|
||||
{
|
||||
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 << "\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;
|
||||
}
|
||||
73
data_structure/Stack Using Linked List.cpp
Normal file
73
data_structure/Stack Using Linked List.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
#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;
|
||||
}
|
||||
138
data_structure/Tree.cpp
Normal file
138
data_structure/Tree.cpp
Normal file
@@ -0,0 +1,138 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BFT(node *n)
|
||||
{
|
||||
list<node*> queue;
|
||||
|
||||
queue.push_back(n);
|
||||
|
||||
while(!queue.empty())
|
||||
{
|
||||
n = queue.front();
|
||||
cout << n->val << " ";
|
||||
queue.pop_front();
|
||||
|
||||
if(n->left != NULL)
|
||||
queue.push_back(n->left);
|
||||
if(n->right != NULL)
|
||||
queue.push_back(n->right);
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
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);
|
||||
}
|
||||
97
data_structure/TrieTree.cpp
Normal file
97
data_structure/TrieTree.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
#include <iostream>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
using namespace std;
|
||||
// structure definition
|
||||
typedef struct trie
|
||||
{
|
||||
struct trie *arr[26];
|
||||
bool isEndofWord;
|
||||
} trie;
|
||||
// create a new node for trie
|
||||
trie *createNode()
|
||||
{
|
||||
trie *nn = new trie();
|
||||
for (int i = 0; i < 26; i++)
|
||||
nn->arr[i] = NULL;
|
||||
nn->isEndofWord = false;
|
||||
return nn;
|
||||
}
|
||||
|
||||
// insert string into the trie
|
||||
void insert(trie *root, char *str)
|
||||
{
|
||||
for (int i = 0; i < strlen(str); i++)
|
||||
{
|
||||
int j = str[i] - 'a';
|
||||
if (root->arr[j])
|
||||
{
|
||||
root = root->arr[j];
|
||||
}
|
||||
else
|
||||
{
|
||||
root->arr[j] = createNode();
|
||||
root = root->arr[j];
|
||||
}
|
||||
}
|
||||
root->isEndofWord = true;
|
||||
}
|
||||
|
||||
// search a string exists inside the trie
|
||||
bool search(trie *root, char *str, int index)
|
||||
{
|
||||
if (index == strlen(str))
|
||||
{
|
||||
if (!root->isEndofWord)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
int j = str[index] - 'a';
|
||||
if (!root->arr[j])
|
||||
return false;
|
||||
return search(root->arr[j], str, index + 1);
|
||||
}
|
||||
/* 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)
|
||||
{
|
||||
if (index == strlen(str))
|
||||
{
|
||||
if (!root->isEndofWord)
|
||||
return false;
|
||||
root->isEndofWord = false;
|
||||
for (int i = 0; i < 26; i++)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
int j = str[index] - 'a';
|
||||
if (!root->arr[j])
|
||||
return false;
|
||||
bool var = deleteString(root, str, index + 1);
|
||||
if (var)
|
||||
{
|
||||
root->arr[j] = NULL;
|
||||
if (root->isEndofWord)
|
||||
return false;
|
||||
else
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < 26; i++)
|
||||
if (root->arr[i])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
trie *root = createNode();
|
||||
insert(root, "hello");
|
||||
insert(root, "world");
|
||||
int a = search(root, "hello", 0);
|
||||
int b = search(root, "word", 0);
|
||||
printf("%d %d ", a, b);
|
||||
return 0;
|
||||
}
|
||||
83
data_structure/circular_Queue_using_Linked_List.cpp
Normal file
83
data_structure/circular_Queue_using_Linked_List.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
{
|
||||
int data;
|
||||
struct node *next;
|
||||
};
|
||||
class Queue
|
||||
{
|
||||
node *front;
|
||||
node *rear;
|
||||
|
||||
public:
|
||||
Queue()
|
||||
{
|
||||
front = NULL;
|
||||
rear = NULL;
|
||||
}
|
||||
void createNode(int val)
|
||||
{
|
||||
node *ptr;
|
||||
node *nn;
|
||||
nn = new node;
|
||||
ptr = front;
|
||||
nn->data = val;
|
||||
nn->next = NULL;
|
||||
front = nn;
|
||||
rear = nn;
|
||||
}
|
||||
void enqueue(int val)
|
||||
{
|
||||
if (front == NULL || rear == NULL)
|
||||
{
|
||||
createNode(val);
|
||||
}
|
||||
else
|
||||
{
|
||||
node *ptr;
|
||||
node *nn;
|
||||
ptr = front;
|
||||
nn = new node;
|
||||
nn->data = val;
|
||||
rear->next = nn;
|
||||
nn->next = front;
|
||||
rear = nn;
|
||||
}
|
||||
}
|
||||
void dequeue()
|
||||
{
|
||||
node *n;
|
||||
n = front;
|
||||
front = front->next;
|
||||
delete (n);
|
||||
}
|
||||
void traverse()
|
||||
{
|
||||
node *ptr;
|
||||
ptr = front;
|
||||
do
|
||||
{
|
||||
cout << ptr->data << " ";
|
||||
ptr = ptr->next;
|
||||
} while (ptr != rear->next);
|
||||
cout << front->data;
|
||||
cout << endl;
|
||||
}
|
||||
};
|
||||
int main(void)
|
||||
{
|
||||
Queue q;
|
||||
q.enqueue(10);
|
||||
q.enqueue(20);
|
||||
q.enqueue(30);
|
||||
q.enqueue(40);
|
||||
q.enqueue(50);
|
||||
q.enqueue(60);
|
||||
q.enqueue(70);
|
||||
q.traverse();
|
||||
q.dequeue();
|
||||
q.traverse();
|
||||
return 0;
|
||||
}
|
||||
106
data_structure/linkedList_implentation_usingArray.cpp
Normal file
106
data_structure/linkedList_implentation_usingArray.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
/* 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>
|
||||
using namespace std;
|
||||
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;
|
||||
void initialise_list()
|
||||
{
|
||||
for (int i = 0; i <= 98; i++)
|
||||
{
|
||||
AvailArray[i].next = i + 1;
|
||||
}
|
||||
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 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.
|
||||
{
|
||||
AvailArray[nodeToBeDeleted].next = avail;
|
||||
avail = nodeToBeDeleted;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void insertAtTheEnd(int data)
|
||||
{
|
||||
int newNode = getnode();
|
||||
int temp = head;
|
||||
while (AvailArray[temp].next != -1)
|
||||
{
|
||||
temp = AvailArray[temp].next;
|
||||
}
|
||||
//temp is now pointing to the end node.
|
||||
AvailArray[newNode].data = data;
|
||||
AvailArray[newNode].next = -1;
|
||||
AvailArray[temp].next = newNode;
|
||||
}
|
||||
|
||||
void display()
|
||||
{
|
||||
int temp = head;
|
||||
while (temp != -1)
|
||||
{
|
||||
cout << AvailArray[temp].data << "->";
|
||||
temp = AvailArray[temp].next;
|
||||
}
|
||||
cout << "-1" << endl;
|
||||
;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
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)
|
||||
{
|
||||
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