mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-24 10:33:10 +08:00
formatting source-code for d7af6fdc8c
This commit is contained in:
@@ -5,172 +5,165 @@ using namespace std;
|
||||
|
||||
typedef struct node
|
||||
{
|
||||
int data;
|
||||
int height;
|
||||
struct node *left;
|
||||
struct node *right;
|
||||
int data;
|
||||
int height;
|
||||
struct node *left;
|
||||
struct node *right;
|
||||
} node;
|
||||
|
||||
int max(int a, int b)
|
||||
{
|
||||
return a > b ? a : b;
|
||||
}
|
||||
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;
|
||||
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));
|
||||
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);
|
||||
}
|
||||
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;
|
||||
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;
|
||||
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);
|
||||
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;
|
||||
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);
|
||||
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;
|
||||
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);
|
||||
}
|
||||
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;
|
||||
// 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;
|
||||
}
|
||||
|
||||
@@ -3,216 +3,210 @@ using namespace std;
|
||||
|
||||
struct node
|
||||
{
|
||||
int val;
|
||||
node *left;
|
||||
node *right;
|
||||
int val;
|
||||
node *left;
|
||||
node *right;
|
||||
};
|
||||
|
||||
struct queue
|
||||
{
|
||||
node *t[100];
|
||||
int front;
|
||||
int rear;
|
||||
node *t[100];
|
||||
int front;
|
||||
int rear;
|
||||
};
|
||||
|
||||
queue q;
|
||||
|
||||
void enqueue(node *n)
|
||||
{
|
||||
q.t[q.rear++] = n;
|
||||
}
|
||||
void enqueue(node *n) { q.t[q.rear++] = n; }
|
||||
|
||||
node *dequeue()
|
||||
{
|
||||
return (q.t[q.front++]);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
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;
|
||||
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);
|
||||
}
|
||||
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());
|
||||
}
|
||||
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);
|
||||
}
|
||||
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()
|
||||
{
|
||||
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";
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// A C++ program to demonstrate common Binary Heap Operations
|
||||
#include <iostream>
|
||||
#include <climits>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// Prototype of a utility function to swap two integers
|
||||
@@ -9,10 +9,10 @@ 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:
|
||||
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);
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ class Queue
|
||||
node *front;
|
||||
node *rear;
|
||||
|
||||
public:
|
||||
public:
|
||||
Queue()
|
||||
{
|
||||
front = NULL;
|
||||
|
||||
@@ -11,10 +11,7 @@ cll::cll()
|
||||
total = 0;
|
||||
}
|
||||
|
||||
cll::~cll()
|
||||
{
|
||||
/* Desstructure, no need to fill */
|
||||
}
|
||||
cll::~cll() { /* Desstructure, no need to fill */ }
|
||||
|
||||
/* Display a list. and total element */
|
||||
void cll::display()
|
||||
@@ -28,10 +25,10 @@ void cll::display()
|
||||
for (int i = 0; i < total; i++)
|
||||
{
|
||||
cout << current->data << " -> ";
|
||||
current = current ->next;
|
||||
current = current->next;
|
||||
}
|
||||
cout << head->data << endl;
|
||||
cout << "Total element: "<< total <<endl;
|
||||
cout << "Total element: " << total << endl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,12 +39,16 @@ void cll::insert_front(int new_data)
|
||||
newNode = new node;
|
||||
newNode->data = new_data;
|
||||
newNode->next = NULL;
|
||||
if(head==NULL) {
|
||||
if (head == NULL)
|
||||
{
|
||||
head = newNode;
|
||||
head -> next = head;
|
||||
} else {
|
||||
head->next = head;
|
||||
}
|
||||
else
|
||||
{
|
||||
node *current = head;
|
||||
while (current -> next != head) {
|
||||
while (current->next != head)
|
||||
{
|
||||
current = current->next;
|
||||
}
|
||||
newNode->next = head;
|
||||
@@ -64,12 +65,16 @@ void cll::insert_tail(int new_data)
|
||||
newNode = new node;
|
||||
newNode->data = new_data;
|
||||
newNode->next = NULL;
|
||||
if(head==NULL) {
|
||||
if (head == NULL)
|
||||
{
|
||||
head = newNode;
|
||||
head -> next = head;
|
||||
} else {
|
||||
head->next = head;
|
||||
}
|
||||
else
|
||||
{
|
||||
node *current = head;
|
||||
while (current -> next != head) {
|
||||
while (current->next != head)
|
||||
{
|
||||
current = current->next;
|
||||
}
|
||||
current->next = newNode;
|
||||
@@ -79,22 +84,22 @@ void cll::insert_tail(int new_data)
|
||||
}
|
||||
|
||||
/* Get total element in list */
|
||||
int cll::get_size()
|
||||
{
|
||||
return total;
|
||||
}
|
||||
|
||||
int cll::get_size() { return total; }
|
||||
|
||||
/* Return true if the requested item (sent in as an argument)
|
||||
is in the list, otherwise return false */
|
||||
bool cll::find_item(int item_to_find)
|
||||
{
|
||||
if (head == NULL) {
|
||||
if (head == NULL)
|
||||
{
|
||||
cout << "List is empty !" << endl;
|
||||
return false;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
node *current = head;
|
||||
while (current -> next != head) {
|
||||
while (current->next != head)
|
||||
{
|
||||
if (current->data == item_to_find)
|
||||
return true;
|
||||
current = current->next;
|
||||
@@ -104,24 +109,25 @@ bool cll::find_item(int item_to_find)
|
||||
}
|
||||
|
||||
/* Overloading method*/
|
||||
int cll::operator*()
|
||||
{
|
||||
return head->data;
|
||||
}
|
||||
int cll::operator*() { return head->data; }
|
||||
|
||||
/* Overload the pre-increment operator.
|
||||
The iterator is advanced to the next node. */
|
||||
void cll::operator++()
|
||||
{
|
||||
if (head == NULL) {
|
||||
if (head == NULL)
|
||||
{
|
||||
cout << "List is empty !" << endl;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
node *current = head;
|
||||
while (current -> next != head) {
|
||||
current = current -> next;
|
||||
while (current->next != head)
|
||||
{
|
||||
current = current->next;
|
||||
}
|
||||
current->next = head -> next;
|
||||
head = head -> next;
|
||||
current->next = head->next;
|
||||
head = head->next;
|
||||
}
|
||||
total--;
|
||||
}
|
||||
|
||||
@@ -1,45 +1,45 @@
|
||||
/*
|
||||
* Simple data structure CLL (Cicular Linear Linked List)
|
||||
* */
|
||||
#include <cstring>
|
||||
#include <cctype>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
#ifndef CLL_H
|
||||
#define CLL_H
|
||||
/*The data structure is a linear linked list of integers */
|
||||
struct node
|
||||
{
|
||||
int data;
|
||||
node * next;
|
||||
int data;
|
||||
node* next;
|
||||
};
|
||||
|
||||
class cll
|
||||
{
|
||||
public:
|
||||
cll(); /* Construct without parameter */
|
||||
~cll();
|
||||
void display(); /* Show the list */
|
||||
public:
|
||||
cll(); /* Construct without parameter */
|
||||
~cll();
|
||||
void display(); /* Show the list */
|
||||
|
||||
/******************************************************
|
||||
* Useful method for list
|
||||
*******************************************************/
|
||||
void insert_front(int new_data); /* Insert a new value at head */
|
||||
void insert_tail(int new_data); /* Insert a new value at tail */
|
||||
int get_size(); /* Get total element in list */
|
||||
bool find_item(int item_to_find); /* Find an item in list */
|
||||
/******************************************************
|
||||
* Useful method for list
|
||||
*******************************************************/
|
||||
void insert_front(int new_data); /* Insert a new value at head */
|
||||
void insert_tail(int new_data); /* Insert a new value at tail */
|
||||
int get_size(); /* Get total element in list */
|
||||
bool find_item(int item_to_find); /* Find an item in list */
|
||||
|
||||
/******************************************************
|
||||
* Overloading method for list
|
||||
*******************************************************/
|
||||
int operator*(); /* Returns the info contained in head */
|
||||
/* Overload the pre-increment operator.
|
||||
The iterator is advanced to the next node. */
|
||||
void operator++();
|
||||
/******************************************************
|
||||
* Overloading method for list
|
||||
*******************************************************/
|
||||
int operator*(); /* Returns the info contained in head */
|
||||
/* Overload the pre-increment operator.
|
||||
The iterator is advanced to the next node. */
|
||||
void operator++();
|
||||
|
||||
protected:
|
||||
node * head;
|
||||
int total; /* Total element in a list */
|
||||
protected:
|
||||
node* head;
|
||||
int total; /* Total element in a list */
|
||||
};
|
||||
#endif
|
||||
|
||||
@@ -3,42 +3,42 @@ using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
/* Test CLL */
|
||||
cout << "----------- Test construct -----------" << endl;
|
||||
cll list1;
|
||||
list1.display();
|
||||
cout << "----------- Test insert front -----------" << endl;
|
||||
list1.insert_front(5);
|
||||
cout << "After insert 5 at front: "<<endl;
|
||||
/* Test CLL */
|
||||
cout << "----------- Test construct -----------" << endl;
|
||||
cll list1;
|
||||
list1.display();
|
||||
cout << "----------- Test insert front -----------" << endl;
|
||||
list1.insert_front(5);
|
||||
cout << "After insert 5 at front: " << endl;
|
||||
list1.display();
|
||||
cout << "After insert 10 3 7 at front: " << endl;
|
||||
list1.insert_front(10);
|
||||
list1.insert_front(3);
|
||||
list1.insert_front(7);
|
||||
list1.display();
|
||||
cout << "----------- Test insert tail -----------" << endl;
|
||||
cout << "After insert 18 19 20 at tail: " << endl;
|
||||
list1.insert_tail(18);
|
||||
list1.insert_tail(19);
|
||||
list1.insert_tail(20);
|
||||
list1.display();
|
||||
cout << "----------- Test find item -----------" << endl;
|
||||
if (list1.find_item(10))
|
||||
cout << "PASS" << endl;
|
||||
else
|
||||
cout << "FAIL" << endl;
|
||||
if (!list1.find_item(30))
|
||||
cout << "PASS" << endl;
|
||||
else
|
||||
cout << "FAIL" << endl;
|
||||
cout << "----------- Test * operator -----------" << endl;
|
||||
int value = *list1;
|
||||
cout << "Value at *list1: " << value << endl;
|
||||
cout << "----------- Test ++ operator -----------" << endl;
|
||||
list1.display();
|
||||
++list1;
|
||||
cout << "After ++list1: " << endl;
|
||||
list1.display();
|
||||
cout << "After insert 10 3 7 at front: "<<endl;
|
||||
list1.insert_front(10);
|
||||
list1.insert_front(3);
|
||||
list1.insert_front(7);
|
||||
list1.display();
|
||||
cout << "----------- Test insert tail -----------" << endl;
|
||||
cout << "After insert 18 19 20 at tail: "<<endl;
|
||||
list1.insert_tail(18);
|
||||
list1.insert_tail(19);
|
||||
list1.insert_tail(20);
|
||||
list1.display();
|
||||
cout << "----------- Test find item -----------" << endl;
|
||||
if (list1.find_item(10))
|
||||
cout << "PASS" << endl;
|
||||
else
|
||||
cout << "FAIL" << endl;
|
||||
if (!list1.find_item(30))
|
||||
cout << "PASS" << endl;
|
||||
else
|
||||
cout << "FAIL" << endl;
|
||||
cout << "----------- Test * operator -----------" << endl;
|
||||
int value = *list1;
|
||||
cout << "Value at *list1: " << value <<endl;
|
||||
cout << "----------- Test ++ operator -----------" << endl;
|
||||
list1.display();
|
||||
++list1;
|
||||
cout << "After ++list1: " <<endl;
|
||||
list1.display();
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -7,58 +7,72 @@ using std::vector;
|
||||
|
||||
vector<int> root, rnk;
|
||||
|
||||
void CreateSet(int n) {
|
||||
root = vector<int> (n+1);
|
||||
rnk = vector<int> (n+1, 1);
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
root[i] = i;
|
||||
}
|
||||
}
|
||||
|
||||
int Find(int x) {
|
||||
if (root[x] == x) {
|
||||
return x;
|
||||
}
|
||||
return root[x] = Find(root[x]);
|
||||
}
|
||||
|
||||
bool InSameUnion(int x, int y) {
|
||||
return Find(x) == Find(y);
|
||||
}
|
||||
|
||||
void Union(int x, int y) {
|
||||
int a = Find(x), b = Find(y);
|
||||
if (a != b) {
|
||||
if (rnk[a] < rnk[b]) {
|
||||
root[a] = b;
|
||||
} else if (rnk[a] > rnk[b]) {
|
||||
root[b] = a;
|
||||
} else {
|
||||
root[a] = b;
|
||||
++rnk[b];
|
||||
void CreateSet(int n)
|
||||
{
|
||||
root = vector<int>(n + 1);
|
||||
rnk = vector<int>(n + 1, 1);
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
root[i] = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// tests CreateSet & Find
|
||||
int n = 100;
|
||||
CreateSet(n);
|
||||
for (int i = 1; i <= 100; ++i) {
|
||||
if (root[i] != i) {
|
||||
cout << "Fail" << endl;
|
||||
break;
|
||||
int Find(int x)
|
||||
{
|
||||
if (root[x] == x)
|
||||
{
|
||||
return x;
|
||||
}
|
||||
}
|
||||
// tests InSameUnion & Union
|
||||
cout << "1 and 2 are initially not in the same subset" << endl;
|
||||
if (InSameUnion(1, 2)) {
|
||||
cout << "Fail" << endl;
|
||||
}
|
||||
Union(1, 2);
|
||||
cout << "1 and 2 are now in the same subset" << endl;
|
||||
if (!InSameUnion(1, 2)) {
|
||||
cout << "Fail" << endl;
|
||||
}
|
||||
return 0;
|
||||
return root[x] = Find(root[x]);
|
||||
}
|
||||
|
||||
bool InSameUnion(int x, int y) { return Find(x) == Find(y); }
|
||||
|
||||
void Union(int x, int y)
|
||||
{
|
||||
int a = Find(x), b = Find(y);
|
||||
if (a != b)
|
||||
{
|
||||
if (rnk[a] < rnk[b])
|
||||
{
|
||||
root[a] = b;
|
||||
}
|
||||
else if (rnk[a] > rnk[b])
|
||||
{
|
||||
root[b] = a;
|
||||
}
|
||||
else
|
||||
{
|
||||
root[a] = b;
|
||||
++rnk[b];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// tests CreateSet & Find
|
||||
int n = 100;
|
||||
CreateSet(n);
|
||||
for (int i = 1; i <= 100; ++i)
|
||||
{
|
||||
if (root[i] != i)
|
||||
{
|
||||
cout << "Fail" << endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// tests InSameUnion & Union
|
||||
cout << "1 and 2 are initially not in the same subset" << endl;
|
||||
if (InSameUnion(1, 2))
|
||||
{
|
||||
cout << "Fail" << endl;
|
||||
}
|
||||
Union(1, 2);
|
||||
cout << "1 and 2 are now in the same subset" << endl;
|
||||
if (!InSameUnion(1, 2))
|
||||
{
|
||||
cout << "Fail" << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,138 +1,166 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include<cstdio>
|
||||
#include<cstdlib>
|
||||
|
||||
struct node {
|
||||
int val;
|
||||
node *prev;
|
||||
node *next;
|
||||
}*start;
|
||||
struct node
|
||||
{
|
||||
int val;
|
||||
node *prev;
|
||||
node *next;
|
||||
} * start;
|
||||
|
||||
class double_linked_list {
|
||||
class double_linked_list
|
||||
{
|
||||
public:
|
||||
double_linked_list() {
|
||||
start = NULL;
|
||||
}
|
||||
void insert(int x);
|
||||
void remove(int x);
|
||||
void search(int x);
|
||||
void show();
|
||||
void reverseShow();
|
||||
double_linked_list() { start = NULL; }
|
||||
void insert(int x);
|
||||
void remove(int x);
|
||||
void search(int x);
|
||||
void show();
|
||||
void reverseShow();
|
||||
};
|
||||
|
||||
void double_linked_list::insert(int x) {
|
||||
node *t = start;
|
||||
if (start != NULL) {
|
||||
while (t->next != NULL) {
|
||||
t = t->next;
|
||||
void double_linked_list::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;
|
||||
}
|
||||
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 double_linked_list::remove(int x) {
|
||||
node *t = start;
|
||||
while (t != NULL && t->val != x) {
|
||||
t = t-> next;
|
||||
}
|
||||
if (t == NULL) {
|
||||
return;
|
||||
}
|
||||
if (t->prev == NULL) {
|
||||
if (t->next == NULL) {
|
||||
start = NULL;
|
||||
} else {
|
||||
start = t->next;
|
||||
start->prev = NULL;
|
||||
else
|
||||
{
|
||||
node *n = new node;
|
||||
n->val = x;
|
||||
n->prev = NULL;
|
||||
n->next = NULL;
|
||||
start = n;
|
||||
}
|
||||
} else if (t->next == NULL) {
|
||||
t->prev->next = NULL;
|
||||
} else {
|
||||
t->prev->next = t->next;
|
||||
t->next->prev = t->prev;
|
||||
}
|
||||
delete t;
|
||||
}
|
||||
|
||||
void double_linked_list::search(int x) {
|
||||
node *t = start;
|
||||
int found = 0;
|
||||
while (t != NULL) {
|
||||
if (t->val == x) {
|
||||
std::cout << "\nFound";
|
||||
found = 1;
|
||||
break;
|
||||
void double_linked_list::remove(int x)
|
||||
{
|
||||
node *t = start;
|
||||
while (t != NULL && t->val != x)
|
||||
{
|
||||
t = t->next;
|
||||
}
|
||||
t = t->next;
|
||||
}
|
||||
if (found == 0) {
|
||||
std::cout << "\nNot Found";
|
||||
}
|
||||
}
|
||||
|
||||
void double_linked_list::show() {
|
||||
node *t = start;
|
||||
while (t != NULL) {
|
||||
std::cout << t->val << "\t";
|
||||
t = t->next;
|
||||
}
|
||||
}
|
||||
|
||||
void double_linked_list::reverseShow() {
|
||||
node *t = start;
|
||||
while (t != NULL && t->next != NULL) {
|
||||
t = t->next;
|
||||
}
|
||||
while (t != NULL) {
|
||||
std::cout << t->val << "\t";
|
||||
t = t->prev;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int choice, x;
|
||||
double_linked_list ob;
|
||||
do {
|
||||
std::cout << "\n1. Insert";
|
||||
std::cout << "\n2. Delete";
|
||||
std::cout << "\n3. Search";
|
||||
std::cout << "\n4. Forward print";
|
||||
std::cout << "\n5. Reverse print";
|
||||
std::cout << "\n\nEnter you choice : ";
|
||||
std::cin >> choice;
|
||||
switch (choice) {
|
||||
case 1:
|
||||
std::cout << "\nEnter the element to be inserted : ";
|
||||
std::cin >> x;
|
||||
ob.insert(x);
|
||||
break;
|
||||
case 2:
|
||||
std::cout << "\nEnter the element to be removed : ";
|
||||
std::cin >> x;
|
||||
ob.remove(x);
|
||||
break;
|
||||
case 3:
|
||||
std::cout << "\nEnter the element to be searched : ";
|
||||
std::cin >> x;
|
||||
ob.search(x);
|
||||
break;
|
||||
case 4:
|
||||
ob.show();
|
||||
break;
|
||||
case 5:
|
||||
ob.reverseShow();
|
||||
break;
|
||||
if (t == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
} while (choice != 0);
|
||||
return 0;
|
||||
if (t->prev == NULL)
|
||||
{
|
||||
if (t->next == NULL)
|
||||
{
|
||||
start = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
start = t->next;
|
||||
start->prev = NULL;
|
||||
}
|
||||
}
|
||||
else if (t->next == NULL)
|
||||
{
|
||||
t->prev->next = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
t->prev->next = t->next;
|
||||
t->next->prev = t->prev;
|
||||
}
|
||||
delete t;
|
||||
}
|
||||
|
||||
void double_linked_list::search(int x)
|
||||
{
|
||||
node *t = start;
|
||||
int found = 0;
|
||||
while (t != NULL)
|
||||
{
|
||||
if (t->val == x)
|
||||
{
|
||||
std::cout << "\nFound";
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
t = t->next;
|
||||
}
|
||||
if (found == 0)
|
||||
{
|
||||
std::cout << "\nNot Found";
|
||||
}
|
||||
}
|
||||
|
||||
void double_linked_list::show()
|
||||
{
|
||||
node *t = start;
|
||||
while (t != NULL)
|
||||
{
|
||||
std::cout << t->val << "\t";
|
||||
t = t->next;
|
||||
}
|
||||
}
|
||||
|
||||
void double_linked_list::reverseShow()
|
||||
{
|
||||
node *t = start;
|
||||
while (t != NULL && t->next != NULL)
|
||||
{
|
||||
t = t->next;
|
||||
}
|
||||
while (t != NULL)
|
||||
{
|
||||
std::cout << t->val << "\t";
|
||||
t = t->prev;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int choice, x;
|
||||
double_linked_list ob;
|
||||
do
|
||||
{
|
||||
std::cout << "\n1. Insert";
|
||||
std::cout << "\n2. Delete";
|
||||
std::cout << "\n3. Search";
|
||||
std::cout << "\n4. Forward print";
|
||||
std::cout << "\n5. Reverse print";
|
||||
std::cout << "\n\nEnter you choice : ";
|
||||
std::cin >> choice;
|
||||
switch (choice)
|
||||
{
|
||||
case 1:
|
||||
std::cout << "\nEnter the element to be inserted : ";
|
||||
std::cin >> x;
|
||||
ob.insert(x);
|
||||
break;
|
||||
case 2:
|
||||
std::cout << "\nEnter the element to be removed : ";
|
||||
std::cin >> x;
|
||||
ob.remove(x);
|
||||
break;
|
||||
case 3:
|
||||
std::cout << "\nEnter the element to be searched : ";
|
||||
std::cin >> x;
|
||||
ob.search(x);
|
||||
break;
|
||||
case 4:
|
||||
ob.show();
|
||||
break;
|
||||
case 5:
|
||||
ob.reverseShow();
|
||||
break;
|
||||
}
|
||||
} while (choice != 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,135 +1,160 @@
|
||||
#include <iostream>
|
||||
|
||||
struct node {
|
||||
int val;
|
||||
node *next;
|
||||
struct node
|
||||
{
|
||||
int val;
|
||||
node *next;
|
||||
};
|
||||
|
||||
node *start;
|
||||
|
||||
void insert(int x) {
|
||||
node *t = start;
|
||||
node *n = new node;
|
||||
n->val = x;
|
||||
n->next = NULL;
|
||||
if (start != NULL) {
|
||||
while (t->next != NULL) {
|
||||
t = t->next;
|
||||
}
|
||||
t->next = n;
|
||||
} else {
|
||||
start = n;
|
||||
void insert(int x)
|
||||
{
|
||||
node *t = start;
|
||||
node *n = new node;
|
||||
n->val = x;
|
||||
n->next = NULL;
|
||||
if (start != NULL)
|
||||
{
|
||||
while (t->next != NULL)
|
||||
{
|
||||
t = t->next;
|
||||
}
|
||||
|
||||
t->next = n;
|
||||
}
|
||||
else
|
||||
{
|
||||
start = n;
|
||||
}
|
||||
}
|
||||
|
||||
void remove(int x) {
|
||||
if (start == NULL) {
|
||||
std::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) {
|
||||
std::cout << std::endl << x << " not found in list\n";
|
||||
return;
|
||||
}
|
||||
|
||||
parent->next = temp->next;
|
||||
void remove(int x)
|
||||
{
|
||||
if (start == NULL)
|
||||
{
|
||||
std::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)
|
||||
{
|
||||
std::cout << std::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) {
|
||||
std::cout << "\nFound";
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
t = t->next;
|
||||
void search(int x)
|
||||
{
|
||||
node *t = start;
|
||||
int found = 0;
|
||||
while (t != NULL)
|
||||
{
|
||||
if (t->val == x)
|
||||
{
|
||||
std::cout << "\nFound";
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
if (found == 0) {
|
||||
std::cout << "\nNot Found";
|
||||
t = t->next;
|
||||
}
|
||||
if (found == 0)
|
||||
{
|
||||
std::cout << "\nNot Found";
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
node *t = start;
|
||||
while (t != NULL)
|
||||
{
|
||||
std::cout << t->val << "\t";
|
||||
t = t->next;
|
||||
}
|
||||
}
|
||||
|
||||
void reverse()
|
||||
{
|
||||
node *first = start;
|
||||
if (first != NULL)
|
||||
{
|
||||
node *second = first->next;
|
||||
while (second != NULL)
|
||||
{
|
||||
node *tem = second->next;
|
||||
second->next = first;
|
||||
first = second;
|
||||
second = tem;
|
||||
}
|
||||
start->next = NULL;
|
||||
start = first;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "\nEmpty list";
|
||||
}
|
||||
}
|
||||
|
||||
void show() {
|
||||
node *t = start;
|
||||
while (t != NULL) {
|
||||
std::cout << t->val << "\t";
|
||||
t = t->next;
|
||||
int main()
|
||||
{
|
||||
int choice, x;
|
||||
do
|
||||
{
|
||||
std::cout << "\n1. Insert";
|
||||
std::cout << "\n2. Delete";
|
||||
std::cout << "\n3. Search";
|
||||
std::cout << "\n4. Print";
|
||||
std::cout << "\n5. Reverse";
|
||||
std::cout << "\n0. Exit";
|
||||
std::cout << "\n\nEnter you choice : ";
|
||||
std::cin >> choice;
|
||||
switch (choice)
|
||||
{
|
||||
case 1:
|
||||
std::cout << "\nEnter the element to be inserted : ";
|
||||
std::cin >> x;
|
||||
insert(x);
|
||||
break;
|
||||
case 2:
|
||||
std::cout << "\nEnter the element to be removed : ";
|
||||
std::cin >> x;
|
||||
remove(x);
|
||||
break;
|
||||
case 3:
|
||||
std::cout << "\nEnter the element to be searched : ";
|
||||
std::cin >> x;
|
||||
search(x);
|
||||
break;
|
||||
case 4:
|
||||
show();
|
||||
std::cout << "\n";
|
||||
break;
|
||||
case 5:
|
||||
std::cout << "The reversed list: \n";
|
||||
reverse();
|
||||
show();
|
||||
std::cout << "\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while (choice != 0);
|
||||
|
||||
void reverse() {
|
||||
node *first = start;
|
||||
if (first != NULL) {
|
||||
node *second = first->next;
|
||||
while (second != NULL) {
|
||||
node *tem = second->next;
|
||||
second->next = first;
|
||||
first = second;
|
||||
second = tem;
|
||||
}
|
||||
start->next = NULL;
|
||||
start = first;
|
||||
} else {
|
||||
std::cout << "\nEmpty list";
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int choice, x;
|
||||
do {
|
||||
std::cout << "\n1. Insert";
|
||||
std::cout << "\n2. Delete";
|
||||
std::cout << "\n3. Search";
|
||||
std::cout << "\n4. Print";
|
||||
std::cout << "\n5. Reverse";
|
||||
std::cout << "\n0. Exit";
|
||||
std::cout << "\n\nEnter you choice : ";
|
||||
std::cin >> choice;
|
||||
switch (choice) {
|
||||
case 1:
|
||||
std::cout << "\nEnter the element to be inserted : ";
|
||||
std::cin >> x;
|
||||
insert(x);
|
||||
break;
|
||||
case 2:
|
||||
std::cout << "\nEnter the element to be removed : ";
|
||||
std::cin >> x;
|
||||
remove(x);
|
||||
break;
|
||||
case 3:
|
||||
std::cout << "\nEnter the element to be searched : ";
|
||||
std::cin >> x;
|
||||
search(x);
|
||||
break;
|
||||
case 4:
|
||||
show();
|
||||
std::cout << "\n";
|
||||
break;
|
||||
case 5:
|
||||
std::cout << "The reversed list: \n";
|
||||
reverse();
|
||||
show();
|
||||
std::cout << "\n";
|
||||
break;
|
||||
}
|
||||
} while (choice != 0);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
/* The difference between the pointer implementation of linked list and array implementation of linked 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.
|
||||
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>
|
||||
@@ -10,7 +12,7 @@ struct Node
|
||||
int data;
|
||||
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 avail = 0;
|
||||
void initialise_list()
|
||||
@@ -19,23 +21,28 @@ void initialise_list()
|
||||
{
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -51,7 +58,7 @@ void insertAtTheEnd(int data)
|
||||
{
|
||||
temp = AvailArray[temp].next;
|
||||
}
|
||||
//temp is now pointing to the end node.
|
||||
// temp is now pointing to the end node.
|
||||
AvailArray[newNode].data = data;
|
||||
AvailArray[newNode].next = -1;
|
||||
AvailArray[temp].next = newNode;
|
||||
@@ -94,7 +101,8 @@ int main()
|
||||
insertAtTheEnd(y);
|
||||
break;
|
||||
case 3:
|
||||
cout << "The linked list contains the following element in order" << endl;
|
||||
cout << "The linked list contains the following element in order"
|
||||
<< endl;
|
||||
display();
|
||||
break;
|
||||
case 4:
|
||||
|
||||
@@ -3,186 +3,184 @@ using namespace std;
|
||||
|
||||
struct list
|
||||
{
|
||||
int data[50];
|
||||
int top = 0;
|
||||
bool isSorted = false;
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
int LinarSearch(int *array, int x)
|
||||
{
|
||||
for (int i = 0; i < top; i++)
|
||||
{
|
||||
if (array[i] == x)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int Search(int x)
|
||||
{
|
||||
int pos = -1;
|
||||
int Search(int x)
|
||||
{
|
||||
int pos = -1;
|
||||
|
||||
if (isSorted)
|
||||
{
|
||||
pos = BinarySearch(data, 0, top - 1, x);
|
||||
}
|
||||
if (isSorted)
|
||||
{
|
||||
pos = BinarySearch(data, 0, top - 1, x);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
pos = LinarSearch(data, x);
|
||||
}
|
||||
else
|
||||
{
|
||||
pos = LinarSearch(data, x);
|
||||
}
|
||||
|
||||
if (pos != -1)
|
||||
{
|
||||
cout << "\nElement found at position : " << pos;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "\nElement not found";
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
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];
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
int temp = data[i];
|
||||
data[i] = data[pos];
|
||||
data[pos] = temp;
|
||||
}
|
||||
isSorted = true;
|
||||
}
|
||||
|
||||
void insert(int x)
|
||||
{
|
||||
if (!isSorted)
|
||||
{
|
||||
void insert(int x)
|
||||
{
|
||||
if (!isSorted)
|
||||
{
|
||||
if (top == 49)
|
||||
{
|
||||
cout << "\nOverflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
data[top] = x;
|
||||
top++;
|
||||
}
|
||||
}
|
||||
|
||||
if (top == 49)
|
||||
{
|
||||
cout << "\nOverflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
data[top] = x;
|
||||
top++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int pos = 0;
|
||||
|
||||
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 = 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;
|
||||
}
|
||||
}
|
||||
|
||||
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 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";
|
||||
}
|
||||
}
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -2,107 +2,107 @@
|
||||
#include <queue>
|
||||
|
||||
/**************************
|
||||
@author shrutisheoran
|
||||
@author shrutisheoran
|
||||
**************************/
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Btree
|
||||
{
|
||||
int data;
|
||||
struct Btree *left; //Pointer to left subtree
|
||||
struct Btree *right; //Pointer to right subtree
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
// Testing morrisInorder funtion
|
||||
Btree *root = NULL;
|
||||
int i;
|
||||
for (i = 1; i <= 7; i++) insert(&root, i);
|
||||
cout << "Morris Inorder: ";
|
||||
morrisInorder(root);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <iostream>
|
||||
#include <assert.h>
|
||||
#include "queue.h"
|
||||
#include <assert.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -25,11 +25,12 @@ void queue<Kind>::display()
|
||||
{
|
||||
node<Kind> *current = queueFront;
|
||||
cout << "Front --> ";
|
||||
while(current != NULL) {
|
||||
cout<<current->data<< " ";
|
||||
current = current -> next;
|
||||
while (current != NULL)
|
||||
{
|
||||
cout << current->data << " ";
|
||||
current = current->next;
|
||||
}
|
||||
cout <<endl;
|
||||
cout << endl;
|
||||
cout << "Size of queue: " << size << endl;
|
||||
}
|
||||
|
||||
@@ -55,10 +56,13 @@ void queue<Kind>::enQueue(Kind item)
|
||||
newNode = new node<Kind>;
|
||||
newNode->data = item;
|
||||
newNode->next = NULL;
|
||||
if (queueFront == NULL) {
|
||||
if (queueFront == NULL)
|
||||
{
|
||||
queueFront = newNode;
|
||||
queueRear = newNode;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
queueRear->next = newNode;
|
||||
queueRear = queueRear->next;
|
||||
}
|
||||
@@ -78,13 +82,15 @@ template <class Kind>
|
||||
void queue<Kind>::deQueue()
|
||||
{
|
||||
node<Kind> *temp;
|
||||
if(!isEmptyQueue()) {
|
||||
if (!isEmptyQueue())
|
||||
{
|
||||
temp = queueFront;
|
||||
queueFront = queueFront->next;
|
||||
delete temp;
|
||||
size--;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "Queue is empty !" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,21 +14,20 @@ struct node
|
||||
template <class Kind>
|
||||
class queue
|
||||
{
|
||||
public:
|
||||
void display(); /* Show queue */
|
||||
queue(); /* Default constructor*/
|
||||
~queue(); /* Destructor */
|
||||
bool isEmptyQueue(); /* Determine whether the queue is empty */
|
||||
void enQueue (Kind item); /* Add new item to the queue */
|
||||
Kind front(); /* Return the first element of the queue */
|
||||
void deQueue(); /* Remove the top element of the queue */
|
||||
void clear();
|
||||
public:
|
||||
void display(); /* Show queue */
|
||||
queue(); /* Default constructor*/
|
||||
~queue(); /* Destructor */
|
||||
bool isEmptyQueue(); /* Determine whether the queue is empty */
|
||||
void enQueue(Kind item); /* Add new item to the queue */
|
||||
Kind front(); /* Return the first element of the queue */
|
||||
void deQueue(); /* Remove the top element of the queue */
|
||||
void clear();
|
||||
|
||||
private:
|
||||
node<Kind> *queueFront; /* Pointer to the front of the queue */
|
||||
node<Kind> *queueRear; /* Pointer to the rear of the queue */
|
||||
int size;
|
||||
private:
|
||||
node<Kind> *queueFront; /* Pointer to the front of the queue */
|
||||
node<Kind> *queueRear; /* Pointer to the rear of the queue */
|
||||
int size;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,22 +1,25 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "queue.h"
|
||||
#include "queue.cpp"
|
||||
#include "queue.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
queue<string> q;
|
||||
cout << "---------------------- Test construct ----------------------" << endl;
|
||||
cout << "---------------------- Test construct ----------------------"
|
||||
<< endl;
|
||||
q.display();
|
||||
cout << "---------------------- Test isEmptyQueue ----------------------" << endl;
|
||||
if(q.isEmptyQueue())
|
||||
cout << "PASS" <<endl;
|
||||
cout << "---------------------- Test isEmptyQueue ----------------------"
|
||||
<< endl;
|
||||
if (q.isEmptyQueue())
|
||||
cout << "PASS" << endl;
|
||||
else
|
||||
cout << "FAIL" <<endl;
|
||||
cout << "---------------------- Test enQueue ----------------------" << endl;
|
||||
cout << "After Hai, Jeff, Tom, Jkingston go into queue: "<<endl;
|
||||
cout << "FAIL" << endl;
|
||||
cout << "---------------------- Test enQueue ----------------------"
|
||||
<< endl;
|
||||
cout << "After Hai, Jeff, Tom, Jkingston go into queue: " << endl;
|
||||
q.enQueue("Hai");
|
||||
q.enQueue("Jeff");
|
||||
q.enQueue("Tom");
|
||||
@@ -25,14 +28,15 @@ int main()
|
||||
cout << "---------------------- Test front ----------------------" << endl;
|
||||
string value = q.front();
|
||||
if (value == "Hai")
|
||||
cout << "PASS" <<endl;
|
||||
cout << "PASS" << endl;
|
||||
else
|
||||
cout << "FAIL" <<endl;
|
||||
cout << "---------------------- Test deQueue ----------------------" << endl;
|
||||
cout << "FAIL" << endl;
|
||||
cout << "---------------------- Test deQueue ----------------------"
|
||||
<< endl;
|
||||
q.display();
|
||||
q.deQueue();
|
||||
q.deQueue();
|
||||
cout << "After Hai, Jeff left the queue: "<< endl;
|
||||
cout << "After Hai, Jeff left the queue: " << endl;
|
||||
q.display();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -10,13 +10,15 @@
|
||||
|
||||
#define MAXSIZE 10
|
||||
|
||||
class Queue_Array {
|
||||
class Queue_Array
|
||||
{
|
||||
int front;
|
||||
int rear;
|
||||
int size;
|
||||
|
||||
public:
|
||||
Queue_Array() {
|
||||
Queue_Array()
|
||||
{
|
||||
front = -1;
|
||||
rear = -1;
|
||||
size = MAXSIZE;
|
||||
@@ -27,43 +29,59 @@ class Queue_Array {
|
||||
void display();
|
||||
};
|
||||
|
||||
void Queue_Array::enqueue(int ele) {
|
||||
if (rear == size - 1) {
|
||||
void Queue_Array::enqueue(int ele)
|
||||
{
|
||||
if (rear == size - 1)
|
||||
{
|
||||
std::cout << "\nStack is full";
|
||||
} else if (front == -1 && rear == -1) {
|
||||
}
|
||||
else if (front == -1 && rear == -1)
|
||||
{
|
||||
front = rear = 0;
|
||||
arr[rear] = ele;
|
||||
} else if (rear < size) {
|
||||
}
|
||||
else if (rear < size)
|
||||
{
|
||||
rear++;
|
||||
arr[rear] = ele;
|
||||
}
|
||||
}
|
||||
|
||||
int Queue_Array::dequeue() {
|
||||
int Queue_Array::dequeue()
|
||||
{
|
||||
int d;
|
||||
if (front == -1) {
|
||||
if (front == -1)
|
||||
{
|
||||
std::cout << "\nstack is empty ";
|
||||
return 0;
|
||||
} else if (front == rear) {
|
||||
}
|
||||
else if (front == rear)
|
||||
{
|
||||
d = arr[front];
|
||||
front = rear = -1;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
d = arr[front++];
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
void Queue_Array::display() {
|
||||
if (front == -1) {
|
||||
void Queue_Array::display()
|
||||
{
|
||||
if (front == -1)
|
||||
{
|
||||
std::cout << "\nStack is empty";
|
||||
} else {
|
||||
for (int i = front; i <= rear; i++)
|
||||
std::cout << arr[i] << " ";
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = front; i <= rear; i++) std::cout << arr[i] << " ";
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
int op, data;
|
||||
|
||||
Queue_Array ob;
|
||||
@@ -72,21 +90,31 @@ int main() {
|
||||
std::cout << "\n2. dequeue(Deletion)";
|
||||
std::cout << "\n3. Display";
|
||||
std::cout << "\n4. Exit";
|
||||
while (1) {
|
||||
while (1)
|
||||
{
|
||||
std::cout << "\nEnter your choice ";
|
||||
std::cin >> op;
|
||||
if (op == 1) {
|
||||
if (op == 1)
|
||||
{
|
||||
std::cout << "Enter data ";
|
||||
std::cin >> data;
|
||||
ob.enqueue(data);
|
||||
} else if (op == 2) {
|
||||
}
|
||||
else if (op == 2)
|
||||
{
|
||||
data = ob.dequeue();
|
||||
std::cout << "\ndequeue element is:\t" << data;
|
||||
} else if (op == 3) {
|
||||
}
|
||||
else if (op == 3)
|
||||
{
|
||||
ob.display();
|
||||
} else if (op == 4) {
|
||||
}
|
||||
else if (op == 4)
|
||||
{
|
||||
exit(0);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "\nWrong choice ";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,69 +7,68 @@ int rear = 0;
|
||||
|
||||
void Enque(int x)
|
||||
{
|
||||
if (rear == 10)
|
||||
{
|
||||
cout << "\nOverflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
queue[rear++] = x;
|
||||
}
|
||||
if (rear == 10)
|
||||
{
|
||||
cout << "\nOverflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
queue[rear++] = x;
|
||||
}
|
||||
}
|
||||
|
||||
void Deque()
|
||||
{
|
||||
if (front == rear)
|
||||
{
|
||||
cout << "\nUnderflow";
|
||||
}
|
||||
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;
|
||||
}
|
||||
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";
|
||||
}
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ void Enque(int x)
|
||||
|
||||
else
|
||||
{
|
||||
|
||||
node *n = new node;
|
||||
n->val = x;
|
||||
n->next = NULL;
|
||||
@@ -40,8 +39,7 @@ void Deque()
|
||||
else
|
||||
{
|
||||
node *t = front;
|
||||
cout << "\n"
|
||||
<< t->val << " deleted";
|
||||
cout << "\n" << t->val << " deleted";
|
||||
front = front->next;
|
||||
delete t;
|
||||
if (front == NULL)
|
||||
|
||||
@@ -1,98 +1,99 @@
|
||||
/*
|
||||
Write a program to implement Queue using linkedlist.
|
||||
*/
|
||||
#include<iostream>
|
||||
|
||||
|
||||
struct linkedlist{
|
||||
int data;
|
||||
linkedlist *next;
|
||||
#include <iostream>
|
||||
|
||||
struct linkedlist
|
||||
{
|
||||
int data;
|
||||
linkedlist *next;
|
||||
};
|
||||
class stack_linkedList{
|
||||
public:
|
||||
class stack_linkedList
|
||||
{
|
||||
public:
|
||||
linkedlist *front;
|
||||
linkedlist *rear;
|
||||
|
||||
stack_linkedList(){
|
||||
front=rear=NULL;
|
||||
}
|
||||
|
||||
stack_linkedList() { front = rear = NULL; }
|
||||
void enqueue(int);
|
||||
int dequeue();
|
||||
void display();
|
||||
|
||||
};
|
||||
void stack_linkedList::enqueue(int ele){
|
||||
|
||||
linkedlist *temp=new linkedlist();
|
||||
temp->data=ele;
|
||||
temp->next=NULL;
|
||||
void stack_linkedList::enqueue(int ele)
|
||||
{
|
||||
linkedlist *temp = new linkedlist();
|
||||
temp->data = ele;
|
||||
temp->next = NULL;
|
||||
|
||||
if(front==NULL)
|
||||
front=rear=temp;
|
||||
else{
|
||||
rear->next=temp;
|
||||
rear=temp;
|
||||
if (front == NULL)
|
||||
front = rear = temp;
|
||||
else
|
||||
{
|
||||
rear->next = temp;
|
||||
rear = temp;
|
||||
}
|
||||
}
|
||||
int stack_linkedList::dequeue(){
|
||||
int stack_linkedList::dequeue()
|
||||
{
|
||||
linkedlist *temp;
|
||||
int ele;
|
||||
if(front==NULL)
|
||||
std::cout<<"\nStack is empty";
|
||||
else{
|
||||
temp=front;
|
||||
ele=temp->data;
|
||||
if(front==rear) //if length of queue is 1;
|
||||
rear=rear->next;
|
||||
front=front->next;
|
||||
delete(temp);
|
||||
if (front == NULL)
|
||||
std::cout << "\nStack is empty";
|
||||
else
|
||||
{
|
||||
temp = front;
|
||||
ele = temp->data;
|
||||
if (front == rear) // if length of queue is 1;
|
||||
rear = rear->next;
|
||||
front = front->next;
|
||||
delete (temp);
|
||||
}
|
||||
return ele;
|
||||
}
|
||||
void stack_linkedList::display(){
|
||||
|
||||
if(front==NULL)
|
||||
std::cout<<"\nStack is empty";
|
||||
|
||||
else {
|
||||
|
||||
void stack_linkedList::display()
|
||||
{
|
||||
if (front == NULL)
|
||||
std::cout << "\nStack is empty";
|
||||
|
||||
else
|
||||
{
|
||||
linkedlist *temp;
|
||||
temp=front;
|
||||
while(temp!=NULL){
|
||||
std::cout<<temp->data<<" ";
|
||||
temp=temp->next;
|
||||
temp = front;
|
||||
while (temp != NULL)
|
||||
{
|
||||
std::cout << temp->data << " ";
|
||||
temp = temp->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
|
||||
int op,data;
|
||||
int main()
|
||||
{
|
||||
int op, data;
|
||||
stack_linkedList ob;
|
||||
std::cout<<"\n1. enqueue(Insertion) ";
|
||||
std::cout<<"\n2. dequeue(Deletion)";
|
||||
std::cout<<"\n3. Display";
|
||||
std::cout<<"\n4. Exit";
|
||||
|
||||
while(1){
|
||||
std::cout<<"\nEnter your choice ";
|
||||
std::cin>>op;
|
||||
if(op==1)
|
||||
std::cout << "\n1. enqueue(Insertion) ";
|
||||
std::cout << "\n2. dequeue(Deletion)";
|
||||
std::cout << "\n3. Display";
|
||||
std::cout << "\n4. Exit";
|
||||
|
||||
while (1)
|
||||
{
|
||||
std::cout << "\nEnter your choice ";
|
||||
std::cin >> op;
|
||||
if (op == 1)
|
||||
{
|
||||
std::cout<<"Enter data ";
|
||||
std::cin>>data;
|
||||
std::cout << "Enter data ";
|
||||
std::cin >> data;
|
||||
ob.enqueue(data);
|
||||
}
|
||||
else if(op==2)
|
||||
data=ob.dequeue();
|
||||
else if(op==3)
|
||||
else if (op == 2)
|
||||
data = ob.dequeue();
|
||||
else if (op == 3)
|
||||
ob.display();
|
||||
else if(op==4)
|
||||
else if (op == 4)
|
||||
exit(0);
|
||||
else
|
||||
std::cout<<"\nWrong choice ";
|
||||
|
||||
std::cout << "\nWrong choice ";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,74 +6,70 @@ int top = 0, size;
|
||||
|
||||
void push(int x)
|
||||
{
|
||||
if (top == size)
|
||||
{
|
||||
cout << "\nOverflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
stack[top++] = x;
|
||||
}
|
||||
if (top == size)
|
||||
{
|
||||
cout << "\nOverflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
stack[top++] = x;
|
||||
}
|
||||
}
|
||||
|
||||
void pop()
|
||||
{
|
||||
if (top == 0)
|
||||
{
|
||||
cout << "\nUnderflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "\n"
|
||||
<< stack[--top] << " deleted";
|
||||
}
|
||||
if (top == 0)
|
||||
{
|
||||
cout << "\nUnderflow";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "\n" << stack[--top] << " deleted";
|
||||
}
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
for (int i = 0; i < top; i++)
|
||||
{
|
||||
cout << stack[i] << "\n";
|
||||
}
|
||||
for (int i = 0; i < top; i++)
|
||||
{
|
||||
cout << stack[i] << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void topmost()
|
||||
{
|
||||
cout << "\nTopmost element: " << stack[top - 1];
|
||||
}
|
||||
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);
|
||||
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;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -3,71 +3,70 @@ using namespace std;
|
||||
|
||||
struct node
|
||||
{
|
||||
int val;
|
||||
node *next;
|
||||
int val;
|
||||
node *next;
|
||||
};
|
||||
|
||||
node *top;
|
||||
|
||||
void push(int x)
|
||||
{
|
||||
node *n = new node;
|
||||
n->val = x;
|
||||
n->next = top;
|
||||
top = n;
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
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);
|
||||
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;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -8,18 +8,19 @@
|
||||
* ./main student.txt
|
||||
************************************************************
|
||||
* */
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <assert.h>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "stack.h"
|
||||
#include "stack.cpp"
|
||||
#include "stack.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
double GPA;
|
||||
double highestGPA;
|
||||
string name;
|
||||
@@ -34,19 +35,24 @@ int main(int argc, char * argv[]) {
|
||||
infile >> GPA >> name;
|
||||
highestGPA = GPA;
|
||||
|
||||
while (infile) {
|
||||
if (GPA > highestGPA) {
|
||||
while (infile)
|
||||
{
|
||||
if (GPA > highestGPA)
|
||||
{
|
||||
stk.clear();
|
||||
stk.push(name);
|
||||
highestGPA = GPA;
|
||||
} else if (GPA == highestGPA) {
|
||||
}
|
||||
else if (GPA == highestGPA)
|
||||
{
|
||||
stk.push(name);
|
||||
}
|
||||
infile >> GPA >> name;
|
||||
}
|
||||
cout << "Highest GPA: " << highestGPA <<endl;
|
||||
cout << "Highest GPA: " << highestGPA << endl;
|
||||
cout << "Students the highest GPA are: " << endl;
|
||||
while (!stk.isEmptyStack()) {
|
||||
while (!stk.isEmptyStack())
|
||||
{
|
||||
cout << stk.top() << endl;
|
||||
stk.pop();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <iostream>
|
||||
#include <assert.h>
|
||||
#include "stack.h"
|
||||
#include <assert.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -24,11 +24,12 @@ void stack<Type>::display()
|
||||
{
|
||||
node<Type> *current = stackTop;
|
||||
cout << "Top --> ";
|
||||
while(current != NULL) {
|
||||
cout<<current->data<< " ";
|
||||
current = current -> next;
|
||||
while (current != NULL)
|
||||
{
|
||||
cout << current->data << " ";
|
||||
current = current->next;
|
||||
}
|
||||
cout <<endl;
|
||||
cout << endl;
|
||||
cout << "Size of stack: " << size << endl;
|
||||
}
|
||||
|
||||
@@ -71,19 +72,22 @@ template <class Type>
|
||||
void stack<Type>::pop()
|
||||
{
|
||||
node<Type> *temp;
|
||||
if(!isEmptyStack()) {
|
||||
if (!isEmptyStack())
|
||||
{
|
||||
temp = stackTop;
|
||||
stackTop = stackTop->next;
|
||||
delete temp;
|
||||
size--;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "Stack is empty !" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
/* Operator "=" */
|
||||
template <class Type>
|
||||
stack<Type> stack<Type>::operator=(stack<Type> & otherStack)
|
||||
stack<Type> stack<Type>::operator=(stack<Type> &otherStack)
|
||||
{
|
||||
node<Type> *newNode, *current, *last;
|
||||
|
||||
@@ -91,13 +95,14 @@ stack<Type> stack<Type>::operator=(stack<Type> & otherStack)
|
||||
stackTop = NULL;
|
||||
if (otherStack.stackTop == NULL)
|
||||
stackTop = NULL;
|
||||
else {
|
||||
else
|
||||
{
|
||||
current = otherStack.stackTop;
|
||||
stackTop = new node<Type>;
|
||||
stackTop->data = current->data;
|
||||
stackTop->next = NULL;
|
||||
last = stackTop;
|
||||
current = current ->next;
|
||||
current = current->next;
|
||||
/* Copy the remaining stack */
|
||||
while (current != NULL)
|
||||
{
|
||||
|
||||
@@ -14,22 +14,21 @@ struct node
|
||||
template <class Type>
|
||||
class stack
|
||||
{
|
||||
public:
|
||||
void display(); /* Show stack */
|
||||
stack(); /* Default constructor*/
|
||||
~stack(); /* Destructor */
|
||||
bool isEmptyStack(); /* Determine whether the stack is empty */
|
||||
void push (Type item); /* Add new item to the stack */
|
||||
Type top(); /* Return the top element of the stack */
|
||||
void pop(); /* Remove the top element of the stack */
|
||||
void clear();
|
||||
public:
|
||||
void display(); /* Show stack */
|
||||
stack(); /* Default constructor*/
|
||||
~stack(); /* Destructor */
|
||||
bool isEmptyStack(); /* Determine whether the stack is empty */
|
||||
void push(Type item); /* Add new item to the stack */
|
||||
Type top(); /* Return the top element of the stack */
|
||||
void pop(); /* Remove the top element of the stack */
|
||||
void clear();
|
||||
|
||||
stack<Type> operator=(stack<Type> & otherStack);
|
||||
// Overload "=" the assignment operator.
|
||||
private:
|
||||
node<Type> *stackTop; /* Pointer to the stack */
|
||||
int size;
|
||||
stack<Type> operator=(stack<Type> &otherStack);
|
||||
// Overload "=" the assignment operator.
|
||||
private:
|
||||
node<Type> *stackTop; /* Pointer to the stack */
|
||||
int size;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,21 +1,23 @@
|
||||
#include <iostream>
|
||||
#include "stack.h"
|
||||
#include "stack.cpp"
|
||||
#include "stack.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
stack<int> stk;
|
||||
cout << "---------------------- Test construct ----------------------" << endl;
|
||||
cout << "---------------------- Test construct ----------------------"
|
||||
<< endl;
|
||||
stk.display();
|
||||
cout << "---------------------- Test isEmptyStack ----------------------" << endl;
|
||||
if(stk.isEmptyStack())
|
||||
cout << "PASS" <<endl;
|
||||
cout << "---------------------- Test isEmptyStack ----------------------"
|
||||
<< endl;
|
||||
if (stk.isEmptyStack())
|
||||
cout << "PASS" << endl;
|
||||
else
|
||||
cout << "FAIL" <<endl;
|
||||
cout << "FAIL" << endl;
|
||||
cout << "---------------------- Test push ----------------------" << endl;
|
||||
cout << "After pushing 10 20 30 40 into stack: "<<endl;
|
||||
cout << "After pushing 10 20 30 40 into stack: " << endl;
|
||||
stk.push(10);
|
||||
stk.push(20);
|
||||
stk.push(30);
|
||||
@@ -24,28 +26,30 @@ int main()
|
||||
cout << "---------------------- Test top ----------------------" << endl;
|
||||
int value = stk.top();
|
||||
if (value == 40)
|
||||
cout << "PASS" <<endl;
|
||||
cout << "PASS" << endl;
|
||||
else
|
||||
cout << "FAIL" <<endl;
|
||||
cout << "FAIL" << endl;
|
||||
cout << "---------------------- Test pop ----------------------" << endl;
|
||||
stk.display();
|
||||
stk.pop();
|
||||
stk.pop();
|
||||
cout << "After popping 2 times: "<< endl;
|
||||
cout << "After popping 2 times: " << endl;
|
||||
stk.display();
|
||||
cout << "---------------------- Test overload = operator ----------------------" << endl;
|
||||
cout << "---------------------- Test overload = operator "
|
||||
"----------------------"
|
||||
<< endl;
|
||||
stack<int> stk1;
|
||||
cout << "stk current: "<< endl;
|
||||
cout << "stk current: " << endl;
|
||||
stk.display();
|
||||
cout << endl << "Assign stk1 = stk "<< endl;
|
||||
cout << endl << "Assign stk1 = stk " << endl;
|
||||
stk1 = stk;
|
||||
stk1.display();
|
||||
cout << endl<< "After pushing 8 9 10 into stk1:" <<endl;
|
||||
cout << endl << "After pushing 8 9 10 into stk1:" << endl;
|
||||
stk1.push(8);
|
||||
stk1.push(9);
|
||||
stk1.push(10);
|
||||
stk1.display();
|
||||
cout << endl << "stk current: " <<endl;
|
||||
cout << endl << "stk current: " << endl;
|
||||
stk.display();
|
||||
cout << "Assign back stk = stk1:" << endl;
|
||||
stk = stk1;
|
||||
|
||||
@@ -14,12 +14,12 @@ 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);
|
||||
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
|
||||
{
|
||||
@@ -40,19 +40,19 @@ void CreateTree(node *curr, node *n, int x, char pos)
|
||||
|
||||
void BFT(node *n)
|
||||
{
|
||||
list<node*> queue;
|
||||
list<node *> queue;
|
||||
|
||||
queue.push_back(n);
|
||||
|
||||
while(!queue.empty())
|
||||
while (!queue.empty())
|
||||
{
|
||||
n = queue.front();
|
||||
cout << n->val << " ";
|
||||
queue.pop_front();
|
||||
|
||||
if(n->left != NULL)
|
||||
if (n->left != NULL)
|
||||
queue.push_back(n->left);
|
||||
if(n->right != NULL)
|
||||
if (n->right != NULL)
|
||||
queue.push_back(n->right);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,86 +1,99 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
// structure definition
|
||||
typedef struct trie {
|
||||
struct trie * arr[26];
|
||||
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;
|
||||
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, std::string str) {
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
void insert(trie* root, std::string str)
|
||||
{
|
||||
for (int i = 0; i < str.length(); i++)
|
||||
{
|
||||
int j = str[i] - 'a';
|
||||
if (root -> arr[j]) {
|
||||
root = root -> arr[j];
|
||||
} else {
|
||||
root -> arr[j] = createNode();
|
||||
root = root -> arr[j];
|
||||
if (root->arr[j])
|
||||
{
|
||||
root = root->arr[j];
|
||||
}
|
||||
else
|
||||
{
|
||||
root->arr[j] = createNode();
|
||||
root = root->arr[j];
|
||||
}
|
||||
}
|
||||
root -> isEndofWord = true;
|
||||
root->isEndofWord = true;
|
||||
}
|
||||
|
||||
// search a string exists inside the trie
|
||||
bool search(trie * root, std::string str, int index) {
|
||||
if (index == str.length()) {
|
||||
if (!root -> isEndofWord)
|
||||
bool search(trie* root, std::string str, int index)
|
||||
{
|
||||
if (index == str.length())
|
||||
{
|
||||
if (!root->isEndofWord)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
int j = str[index] - 'a';
|
||||
if (!root -> arr[j])
|
||||
if (!root->arr[j])
|
||||
return false;
|
||||
return search(root -> arr[j], str, index + 1);
|
||||
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 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, std::string str, int index) {
|
||||
if (index == str.length()) {
|
||||
if (!root -> isEndofWord)
|
||||
return false;
|
||||
root -> isEndofWord = false;
|
||||
for (int i = 0; i < 26; i++)
|
||||
bool deleteString(trie* root, std::string str, int index)
|
||||
{
|
||||
if (index == str.length())
|
||||
{
|
||||
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])
|
||||
if (!root->arr[j])
|
||||
return false;
|
||||
bool
|
||||
var = deleteString(root, str, index + 1);
|
||||
if (var) {
|
||||
root -> arr[j] = NULL;
|
||||
if (root -> isEndofWord) {
|
||||
bool var = deleteString(root, str, index + 1);
|
||||
if (var)
|
||||
{
|
||||
root->arr[j] = NULL;
|
||||
if (root->isEndofWord)
|
||||
{
|
||||
return false;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < 26; i++)
|
||||
if (root -> arr[i])
|
||||
if (root->arr[i])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
trie * root = createNode();
|
||||
int main()
|
||||
{
|
||||
trie* root = createNode();
|
||||
insert(root, "hello");
|
||||
insert(root, "world");
|
||||
int a = search(root, "hello", 0);
|
||||
|
||||
Reference in New Issue
Block a user