Posts

Showing posts with the label Data Structures

Binary Search Tree Operations

#include "iostream" #include "cstdlib" using namespace std; class BinarySearchTree { private: struct tree_node { int data; tree_node* left; tree_node* right; }; tree_node* root; public: BinarySearchTree() { root = NULL; } void Print_PreOrder(); void Print_PostOrder(); void Print_InOrder(); void inorder(tree_node*); void preorder(tree_node*); void postorder(tree_node*); void insert(int); }; void BinarySearchTree::insert(int d) { tree_node* current = new tree_node; current->data = d; current->left = NULL; current->right = NULL; tree_node* parent; parent = root; tree_node* temp; if(root == NULL) { root = current; } else { while(parent) { temp = parent; if(current->data > parent->data ) { parent = parent->right; } else { parent = parent->left; } } if(current->data > temp->data) { temp->right = current; } else { temp->left = current; } } } void Bina...

First repeated character

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace FirstRepeatedCharacter1 { class Program { static void Main(string[] args) { Console.WriteLine("Enter the string"); string str = Console.ReadLine(); if (str == "") { Console.WriteLine("string is empty"); Console.ReadLine(); return; } bool repeated = false; Hashtable strhash = new Hashtable(); foreach (char ch in str) { if (strhash.ContainsValue(ch)) { Console.WriteLine("First non repeated character: " + ch); repeated = true; break; } else { strhash.Add((int)ch, ch); } } if (repeated == false) { Console.WriteLine("No repeated characters"); } Console.ReadLine(); } } }

ReverseLinkedList

#include using namespace std; class LinkedList { private: struct Node { int data; Node *next; }; Node *root; public: LinkedList() { root = NULL; } void insert(int); void Print(); void Reverse(); }; void LinkedList::insert(int d) { Node* current = new Node; current->data = d; current->next = NULL; Node* parent; parent = root; Node* temp; if(root == NULL) { root = current; } else { while(parent) { temp = parent; parent = parent->next; } temp->next = current; } } void LinkedList::Print() { Node* temp; temp = root; while(temp) { cout< data< temp = temp->next; } } void LinkedList::Reverse() { Node* temp; Node* previous; bool first = true; while(root) { temp = root->next; if(first) { first = false; root->next = NULL; } else { root->next = previous; } previous = root; root = temp; } root = previous; } int main() { LinkedList ll ; int choice, value; do { cout<<"Insert the elements"< cin>>value; ll.insert(value); cout<<"...