0% found this document useful (0 votes)
15 views62 pages

Document 22

The document contains multiple C++ code snippets demonstrating various data structures and algorithms, including linked lists (singly, doubly, circular), depth-first search (DFS) for graphs, quadratic probing for hash tables, and basic operations on these structures. Each section provides functions for insertion, deletion, and traversal of the respective data structures, along with examples of their usage. The code is structured to allow user input for creating and manipulating these data structures.

Uploaded by

727824TUCS437
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views62 pages

Document 22

The document contains multiple C++ code snippets demonstrating various data structures and algorithms, including linked lists (singly, doubly, circular), depth-first search (DFS) for graphs, quadratic probing for hash tables, and basic operations on these structures. Each section provides functions for insertion, deletion, and traversal of the respective data structures, along with examples of their usage. The code is structured to allow user input for creating and manipulating these data structures.

Uploaded by

727824TUCS437
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

#include <iostream>

using namespace std;

struct Node {

int data;

Node* next;

};

Node* head = NULL; // Global head pointer

// Insert at the beginning

void insertAtBeginning(int value) {

Node* newNode = new Node{value, head};

head = newNode;

// Insert at the end

void insertAtEnd(int value) {

Node* newNode = new Node{value, NULL};

if (head == NULL) {

head = newNode;

return;

Node* temp = head;

while (temp->next != NULL)

temp = temp->next;
temp->next = newNode;

// Insert at a specific position (1-based index)

void insertAtPosition(int value, int position) {

if (position <= 0) {

cout << "Invalid position\n";

return;

if (position == 1) {

insertAtBeginning(value);

return;

Node* newNode = new Node{value, NULL};

Node* temp = head;

for (int i = 1; temp != NULL && i < position - 1; i++) {

temp = temp->next;

if (temp == NULL) {

cout << "Position out of range\n";

return;

}
newNode->next = temp->next;

temp->next = newNode;

// Delete at beginning

void deleteAtBeginning() {

if (head == NULL) {

cout << "List is empty\n";

return;

Node* temp = head;

head = head->next;

delete temp;

// Delete at end

void deleteAtEnd() {

if (head == NULL) {

cout << "List is empty\n";

return;

if (head->next == NULL) {

delete head;

head = NULL;

return;
}

Node* temp = head;

while (temp->next->next != NULL)

temp = temp->next;

delete temp->next;

temp->next = NULL;

// Delete at specific position

void deleteAtPosition(int position) {

if (head == NULL) {

cout << "List is empty\n";

return;

if (position == 1) {

deleteAtBeginning();

return;

Node* temp = head;

for (int i = 1; temp != NULL && i < position - 1; i++) {

temp = temp->next;

}
if (temp == NULL || temp->next == NULL) {

cout << "Position out of range\n";

return;

Node* del = temp->next;

temp->next = del->next;

delete del;

// Display the list

void display() {

if (head == NULL) {

cout << "List is empty\n";

return;

Node* temp = head;

while (temp != NULL) {

cout << temp->data << " -> ";

temp = temp->next;

cout << "NULL\n";

Dfs
#include <iostream>

using namespace std;

const int MAX = 100; // Maximum number of nodes

int graph[MAX][MAX];

bool visited[MAX];

// DFS function

void dfs(int node, int n) {

visited[node] = true;

cout << node << " ";

for (int i = 0; i < n; i++) {

if (graph[node][i] == 1 && !visited[i]) {

dfs(i, n);

int main() {

int n;

cout << "Enter number of vertices: ";

cin >> n;

// Input adjacency matrix

cout << "Enter adjacency matrix:\n";


for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

cin >> graph[i][j];

// Initialize visited array

for (int i = 0; i < n; i++) {

visited[i] = false;

int start;

cout << "Enter starting node: ";

cin >> start;

cout << "DFS traversal:\n";

dfs(start, n);

return 0;

Quadratic probing

#include <iostream>

using namespace std;

const int TABLE_SIZE = 10;


class HashTable {

int hashTable[TABLE_SIZE];

public:

HashTable() {

for (int i = 0; i < TABLE_SIZE; i++)

hashTable[i] = -1; // -1 indicates empty

// Hash function

int hash(int key) {

return key % TABLE_SIZE;

// Insert using Quadratic Probing

void insert(int key) {

int index = hash(key);

int i = 1;

while (hashTable[index] != -1) {

index = (hash(key) + i * i) % TABLE_SIZE;

i++;

if (i == TABLE_SIZE) {

cout << "Hash table is full\n";

return;

}
}

hashTable[index] = key;

// Display the hash table

void display() {

cout << "Hash Table (using Quadratic Probing):\n";

for (int i = 0; i < TABLE_SIZE; i++) {

if (hashTable[i] != -1)

cout << i << " --> " << hashTable[i] << endl;

else

cout << i << " --> " << "Empty" << endl;

};

int main() {

HashTable ht;

int keys[] = {19, 27, 36, 10, 64, 29};

for (int i = 0; i < 6; i++)

ht.insert(keys[i]);

ht.display();
return 0;

Doubly linked list

#include<bits/stdc++.h>

using namespace std ;

struct node{

int data;

node*prev;

node*next;

node(int val){

data= val;

prev=0;

next=0;

};

void beg(node* &head,int val){

node* newnode = new node(val);

newnode->next=head;

if(head!=0){

head->prev=newnode;

head=newnode;

void de(node* &head,int val){

node * newnode=new node(val);


if(head==0){

head=newnode;

return;

node* temp=head;

while(temp->next!=0){

temp=temp->next;

temp->next=newnode;

newnode->prev=temp;

void p(node* &head,int val,int pos){

if(pos<=0){

cout<<"Invalid input";

return ;

if(pos==1){

beg(head,val);

return ;

node* temp=head;

for(int i=1;temp!=0 && i<pos-1;i++){

temp=temp->next;

if(temp==0){
cout<<"Invalid input";

return;

node* newnode=new node(val);

newnode->next=temp->next;

newnode->prev=temp;

temp->next=newnode;

if(temp->next!=0)

temp->next->prev=newnode;

temp->next=newnode;

void display(node* head){

node* temp=head;

while(temp!=0){

cout<<temp->data<<" ";

temp=temp->next;

void t(node*head){

node* temp=head;

if(head==0) return ;

while(temp->next!=0){

temp=temp->next;

while(temp!=0){
cout<<temp->data<<" ";

temp=temp->prev;

void sort(node* head){

if(head==0)return ;

node*i;

node*j;

for(i=head;i->next!=0;i=i->next){

for(j=i->next;j!=0;j=j->next){

if(i->data > j->data){

swap(i->data,j->data);

int main(){

int n;

cin>>n;

node* head=0;

if(n<0){

cout<<"invalid input"<<endl;

return 0;

}
// int pos;

// cin>>pos;

for(int i=0;i<n;i++){

int val;

cin>>val;

if(val==0){

cout<<"Invalid input";

return 0;

//beg(head,val);

de(head,val);

// p(head,5,5);

sort(head);

display(head);

cout<<endl;

t(head);

return 0;

Singly linked list reversal

#include<bits/stdc++.h>

using namespace std ;

struct node{

int data;

node * next;
node(int val){

data= val;

next=0;

};

void traversal(node* &head){

node* temp=head;

while(temp!=0){

cout<<temp->data<<" ";

temp=temp->next;

void reversal(node* &head){

node* prev=0;

node* next=0;

node* cur= head;

while(cur!=0){

next=cur->next;

cur->next=prev;

prev=cur;

cur=next;

}
head=prev;

while(head!=0){

cout<<head->data<<" ";

head= head->next;

int main(){

int n;

cin>>n;

if(n<=0)

cout<<"Invalid input";

return 0;

node* head=0;

node* tail=0;

for(int i=0;i<n;i++){

int val;

cin>>val;

if(val<0) return 0;

node * newnode= new node(val);

newnode->data=val;

newnode->next=0;

if(head==0){

head=newnode;

tail=newnode;
}

tail->next=newnode;

tail=newnode;

traversal(head);

cout<<endl;

reversal(head);

return 0;

Circularly linked list

#include <iostream>

using namespace std;

struct node {

int data;

node* next;

node(int val) {

data = val;

next = nullptr;

};

void insertAtBeginning(node* &head, int val) {

node* newnode = new node(val);

if (head == nullptr) {

newnode->next = newnode;
head = newnode;

return;

node* temp = head;

while (temp->next != head)

temp = temp->next;

newnode->next = head;

temp->next = newnode;

head = newnode;

void insertAtEnd(node* &head, int val) {

node* newnode = new node(val);

if (head == nullptr) {

newnode->next = newnode;

head = newnode;

return;

node* temp = head;

while (temp->next != head)

temp = temp->next;

temp->next = newnode;

newnode->next = head;

void insertAtPosition(node* &head, int val, int pos) {


if (pos <= 0) {

cout << "Invalid Position\n";

return;

if (pos == 1) {

insertAtBeginning(head, val);

return;

node* temp = head;

for (int i = 1; i < pos - 1 && temp->next != head; i++) {

temp = temp->next;

node* newnode = new node(val);

newnode->next = temp->next;

temp->next = newnode;

void deleteFromBeginning(node* &head) {

if (head == nullptr) {

cout << "List is empty\n";

return;

if (head->next == head) {

delete head;

head = nullptr;

return;
}

node* temp = head;

while (temp->next != head)

temp = temp->next;

node* del = head;

head = head->next;

temp->next = head;

delete del;

void deleteFromEnd(node* &head) {

if (head == nullptr) {

cout << "List is empty\n";

return;

if (head->next == head) {

delete head;

head = nullptr;

return;

node* temp = head;

while (temp->next->next != head)

temp = temp->next;

node* del = temp->next;

temp->next = head;

delete del;
}

void deleteAtPosition(node* &head, int pos) {

if (head == nullptr || pos <= 0) {

cout << "Invalid Operation\n";

return;

if (pos == 1) {

deleteFromBeginning(head);

return;

node* temp = head;

for (int i = 1; i < pos - 1 && temp->next != head; i++) {

temp = temp->next;

if (temp->next == head) {

cout << "Position out of range\n";

return;

node* del = temp->next;

temp->next = temp->next->next;

delete del;

void display(node* head) {

if (head == nullptr) {
cout << "List is empty\n";

return;

node* temp = head;

do {

cout << temp->data << " ";

temp = temp->next;

} while (temp != head);

cout << endl;

Circularly my code

#include<bits/stdc++.h>

using namespace std ;

struct node{

int data;

node*next;

node(int val){

data=val;

next=0;

};

void delbeg(node* &head){

if(head==0){

return ;

if(head->next==0){
delete head;

head=0;

return;

node*temp=head;

head=head->next;

delete temp;

temp=0;

void delend(node* &head){

if(head==0){

return ;

if(head->next==0){

delete head;

head=0;

return;

node*temp=head;

while(temp->next->next!=0)

temp=temp->next;

delete temp->next;

temp->next=0;

void display(node* head){


node*temp=head;

while(temp!=0){

cout<<temp->data<<" ";

temp=temp->next;

cout<<endl;

int main(){

int n;

cin>>n;

if(n<=0)return 0;

node* head=0;

node* temp=0;

for(int i=0;i<n;i++){

int val;

cin>>val;

if(val<0)return 0;

node* newnode= new node(val);

newnode->data=val;

newnode->next=0;

if(head==0){

head=newnode;

temp=newnode;

temp->next=newnode;

temp=newnode;
}

display(head);

delend(head);

delbeg(head);

display(head);

Next ques

void insertbeg(node * &head,int val){

node* newnode= new node(val);

if(head==0){

newnode->next=newnode;

head=newnode;

return;

};

node*temp=head;

while(temp->next!=head)

temp=temp->next;

newnode->next=head;

temp->next=newnode;

head=newnode;

void display(node* head){

node*temp=head;

do{
cout<<temp->data<<" ";

temp=temp->next;

}while(temp!=head);

cout<<endl;

int main(){

int n;

cin>>n;

if(n<=0)return 0;

node* head=0;

node* temp=0;

for(int i=0;i<n;i++){

int val;

cin>>val;

if(val<0)return 0;

insertbeg(head,val);

display(head);

// delend(head);

// delbeg(head);

// display(head);

Hashing

#include<bits/stdc++.h>

using namespace std ;


struct employee{

string name;

float salary;

};

int main(){

int n;

cin>>n;

unordered_map <int,employee> m;

for(int i=0;i<n;i++){

int id;

string name;

float salary;

cin>>id>>name>>salary;

m[id]={name,salary};

int d;

cin>>d;

if(m.find(d)!=m.end()){

cout<<m[d].name<<" "<<m[d].salary<<" ";

else{

cout<<"-1"<<" ";

return 0;

}
}

Balancing the brackets

#include<bits/stdc++.h>

using namespace std;

int check(string ex){

stack<char>st;

for(char ch:ex){

if(ch=='(' || ch=='{' || ch=='['){

st.push(ch);

else{

if(st.empty()){

return 0;

char top=st.top();

if((ch==')' && top!='(' )||

(ch=='}' && top!='{' )||

(ch==']' && top!='[' )){

return 0;

st.pop();

}
if(st.empty())

return 1;

return 0;

int main(){

string ex;

cin>>ex;

int result=check(ex);

if(result==1){

cout<<"Balanced"<<" ";

else{

cout<<"Unbalanced"<<" ";

Tree deletion

#include using namespace std;

// Node structure struct Node { int data; Node* left; Node* right;

Node(int value) {
data = value;
left = right = nullptr;
}

};

// Inorder Traversal (Left → Root → Right) void inorder(Node* root) { if (root == nullptr) return;
inorder(root->left); cout << root->data << " "; inorder(root->right); }
// Recursive Insertion in BST Node* insert(Node* root, int key) { if (root == nullptr) return
new Node(key);

if (key < root->data)


root->left = insert(root->left, key);
else
root->right = insert(root->right, key);

return root;

// Find the minimum value node in right subtree Node* findMin(Node* root) { while (root &&
root->left) root = root->left; return root; }

// Recursive Deletion in BST Node* deleteNode(Node* root, int key) { if (root == nullptr)
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 found
if (root->left == nullptr) {
Node* temp = root->right;
delete root;
return temp;
}
else if (root->right == nullptr) {
Node* temp = root->left;
delete root;
return temp;
}

// Node with two children


Node* temp = findMin(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
return root;

// MAIN int main() { Node* root = nullptr;

int n;
cout << "Enter number of nodes to insert: ";
cin >> n;
cout << "Enter node values:\n";
for (int i = 0; i < n; ++i) {
int val;
cin >> val;
root = insert(root, val);
}

cout << "Inorder Traversal: ";


inorder(root);
cout << endl;

int delVal;
cout << "Enter node value to delete: ";
cin >> delVal;
root = deleteNode(root, delVal);

cout << "Inorder after deletion: ";


inorder(root);
cout << endl;

return 0;

My code tree
#include<bits/stdc++.h> using namespace std ; struct node{ int data; node* left; node*
right; node(int val){ data=val; left=0; right=0; } }; node* insert(node *root,int
val){ if(root==0){ return new node(val);

}
if(val<root->data){
root->left=insert(root->left,val);
}
else if(val> root->data){
root->right=insert(root->right,val);
}
return root;

} void inorder(node root){ if(root==0)return; inorder(root->left); cout<data<<" "; inorder(root-


>right); } node findmin(node root){ while(root!=0){ root=root->left; } return root; } node
deletenode(node* root,int val){ if(root==0){return root;}

if(val<root->data){
root->left=deletenode(root->left,val);
}
else if(val> root->data){
root->right=deletenode(root->right,val);
}
else{
if(root->left==0){
node* temp=root->right;
delete root;
return temp;
}
else if(root->right==0){
node* temp=root->left;
delete root;
return temp;
}
else{
node *temp=findmin(root->right);
root->data=temp->data;
root->right=deletenode(root->right,temp->data);
}
}
return root;
}
int main(){
int n;
cin>>n;
node * root=0;
for(int i=0;i<n;i++){
int val;
cin>>val;
if(val<1) return 0;
root=insert(root,val);
}
cout<<"Inorder"<<endl;
inorder(root);
cout<<endl;
int a;
cin>>a;
deletenode(root,a);
cout << "Inorder after deletion: "<<endl;
inorder(root);
cout << endl;
}
delete mid value linked list

#include<bits/stdc++.h>

using namespace std ;

struct node{

int data;

node* next;

node(int val){

data=val;

next=0;
}

};

void deletemid(node* &head,int n){

int mid=n/2;

if(head==0||head->next==head){

delete head;

head=0;

return ;

node* temp=head;

for(int i=1;i<mid;i++){

temp=temp->next;

node* del=temp->next;

temp->next=temp->next->next;

delete del;

void display(node* &head){

if(head==0)return;

node* temp=head;

do{

cout<<temp->data<<" ";

temp=temp->next;

} while(temp!=head);

}
int main(){

int n;

cin>>n;

if(n<0){return 0;}

node* head=0;

node* temp=0;

for(int i=0;i<n;i++){

int val;

cin>>val;

node* newnode=new node(val);

newnode->data=val;

newnode->next=0;

if(head==0){

head=newnode;

temp=newnode;

temp->next=newnode;

temp=newnode;

temp->next=head;

display(head);

deletemid(head,n);

cout<<endl;

display(head);

deletemid(head,n);
cout<<endl;

display(head);

Dfs

#include <iostream>

using namespace std;

const int MAX = 100; // Maximum number of nodes

int graph[MAX][MAX];

bool visited[MAX];

// DFS function

void dfs(int node, int n) {

visited[node] = true;

cout << node << " ";

for (int i = 0; i < n; i++) {

if (graph[node][i] == 1 && !visited[i]) {

dfs(i, n);

int main() {

int n;

cout << "Enter number of vertices: ";


cin >> n;

// Input adjacency matrix

cout << "Enter adjacency matrix:\n";

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

cin >> graph[i][j];

// Initialize visited array

for (int i = 0; i < n; i++) {

visited[i] = false;

int start;

cout << "Enter starting node: ";

cin >> start;

cout << "DFS traversal:\n";

dfs(start, n);

return 0;

Bfs

#include <iostream>
#include <queue>

using namespace std;

const int MAX = 100;

int graph[MAX][MAX];

bool visited[MAX];

void bfs(int start, int n) {

queue<int> q;

visited[start] = true;

q.push(start);

while (!q.empty()) {

int node = q.front();

q.pop();

cout << node << " ";

for (int i = 0; i < n; i++) {

if (graph[node][i] == 1 && !visited[i]) {

visited[i] = true;

q.push(i);

}
int main() {

int n;

cout << "Enter number of vertices: ";

cin >> n;

// Input adjacency matrix

cout << "Enter adjacency matrix:\n";

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

cin >> graph[i][j];

// Initialize visited array

for (int i = 0; i < n; i++) {

visited[i] = false;

int start;

cout << "Enter starting node: ";

cin >> start;

cout << "BFS traversal:\n";

bfs(start, n);

return 0;
}

Rotating the queue over from middle

#include<bits/stdc++.h>

using namespace std ;

int main(){

int n,k;

if(n<0){

cout<<"invalid input";

return 0;

cin>>n>>k;

vector<string>q(n);

for(int i=0;i<n;i++){

cin>>q[i];

k=k%n;

for(int i=k;i<n;i++){

cout<<q[i]<<" ";

for(int i=0;i<k;i++){

cout<<q[i]<<" ";

return 0;

Dfs with number

#include <iostream>
using namespace std;

const int MAX = 100; // Max number of vertices

int graph[MAX][MAX];

bool visited[MAX];

// DFS function

void dfs(int node, int n) {

visited[node] = true;

cout << node << " ";

for (int i = 0; i < n; i++) {

if (graph[node][i] == 1 && !visited[i]) {

dfs(i, n);

int main() {

int V, E;

cin >> V >> E;

// Initialize graph and visited array

for (int i = 0; i < V; i++) {

visited[i] = false;

for (int j = 0; j < V; j++) {


graph[i][j] = 0;

// Read edges

for (int i = 0; i < E; i++) {

int v, w;

cin >> v >> w;

graph[v][w] = 1;

graph[w][v] = 1; // Undirected graph

int startVertex;

cin >> startVertex;

cout << "Depth First Traversal starting from vertex " << startVertex << ":\n";

dfs(startVertex, V);

cout << endl;

return 0;

Bfs with numbers

#include <iostream>

#include <queue>

using namespace std;


const int MAX = 100;

int graph[MAX][MAX];

bool visited[MAX];

// BFS function

void bfs(int start, int n) {

queue<int> q;

q.push(start);

visited[start] = true;

while (!q.empty()) {

int current = q.front();

q.pop();

cout << current << " ";

for (int i = 0; i < n; i++) {

if (graph[current][i] == 1 && !visited[i]) {

q.push(i);

visited[i] = true;

int main() {

int V, E;
cin >> V >> E;

// Initialize graph and visited array

for (int i = 0; i < V; i++) {

visited[i] = false;

for (int j = 0; j < V; j++) {

graph[i][j] = 0;

// Input edges

for (int i = 0; i < E; i++) {

int v, w;

cin >> v >> w;

graph[v][w] = 1;

graph[w][v] = 1; // Undirected

int startVertex;

cin >> startVertex;

cout << "Breadth First Traversal starting from vertex " << startVertex << ":\n";

bfs(startVertex, V);

cout << endl;

return 0;
}

Weighted graph(bfs) dfs is also similar to this

#include <bits/stdc++.h>

using namespace std ;

int const MAX=100;

int graph[MAX][MAX];

bool visited[MAX];

void bfs(int node,int n){

visited[node]=true;

queue<int>q;

q.push(node);

while(!q.empty()){

int s=q.front();

q.pop();

cout<<s<<" ";

for(int i=0;i<n;i++){

if(graph[s][i]!=0 && !visited[i]){

visited[i]=true;

q.push(i);

int main(){
int v,e;

cin>>v>>e;

for(int i=0;i<v;i++){

visited[i]=false;

for(int j=0;j<v;j++){

graph[i][j]=0;

for(int i=0;i<e;i++){

int a,b,w;

cin>>a>>b>>w;

graph[a][b]=w;

graph[b][a]=w;

int start;

cin>>start;

bfs(start,v);

Directed graph bfs

#include <bits/stdc++.h>

using namespace std;

const int MAX = 100;

int graph[MAX][MAX]; // adjacency matrix to store weights


bool visited[MAX];

void bfs(int node, int n) {

visited[node] = true;

queue<int> q;

q.push(node);

while (!q.empty()) {

int s = q.front();

q.pop();

cout << s << " ";

for (int i = 0; i < n; i++) {

// for directed graph, check only graph[s][i]

if (graph[s][i] != 0 && !visited[i]) {

visited[i] = true;

q.push(i);

int main() {

int v, e;

cin >> v >> e;


// initialize graph and visited array

for (int i = 0; i < v; i++) {

visited[i] = false;

for (int j = 0; j < v; j++) {

graph[i][j] = 0;

// input edges: from, to, weight

for (int i = 0; i < e; i++) {

int a, b, w;

cin >> a >> b >> w;

graph[a][b] = w; // only one direction

int start;

cin >> start;

bfs(start, v);

return 0;

Dfs and bfs using adjacency list

#include <bits/stdc++.h>

using namespace std;

int const MAX=100;


vector<int>graph[MAX];

bool visited[MAX];

// DFS Function

void dfs(int node) {

visited[node]=true;

cout<<node<<" ";

for(int nei :graph[node]){

if(!visited[nei]){

dfs(nei);

// BFS Function

void bfs(int start) {

visited[start]=true;

queue<int>q;

q.push(start);

while(!q.empty()){

int s=q.front();

q.pop();

cout<<s<<" ";

for(int nei:graph[s]){

if(!visited[nei]){

visited[nei]=true;
q.push(nei);

int main() {

int v,e;

cin>>v>>e;

for(int i=0;i<e;i++){

int a,b;

cin>>a>>b;

graph[a].push_back(b);

graph[b].push_back(a);

int start;

cin>>start;

fill(visited,visited+v,false);

cout<<"DFS"<<endl;

dfs(start);

fill(visited,visited+v,false);

cout<<endl;

cout<<"BFS"<<endl;
bfs(start);

Hashing

#include<bits/stdc++.h>

using namespace std ;

int const S=10;

class T{

private:

int table[S];

public :

T(){

for(int i=0;i<S;i++){

table[i]=-1;

int hashkey(int key){

return key%S;

void insert(int key){

int index=hashkey(key);

int si=index;

while(table[index]!=-1){

index=(index+1)%S;
if(table[index]==si){

cout<<"The table is Full"<<endl;

return;

table[index]=key;

void display(){

for(int i=0;i<S;i++){

if(table[i]!=-1)

cout<<i<<" -->"<<table[i]<<endl;

else

cout<<i<<" -->"<<"Empty"<<endl;

};

int main(){

T h;

int n;

cin>>n;

for(int i=0;i<n;i++){

int key;

cin>>key;
h.insert(key);

h.display();

Double hashing

#include<bits/stdc++.h>

using namespace std ;

int const S=10;

class H{

private:

int table[S];

public:

H(){

for(int i=0;i<S;i++){

table[i]=-1;

int hash1(int key){

return key%S;

int hash2(int key){

return 7-(key%7);

void insert (int key){

int index=hash1(key);
int s=hash2(key);

int i=0;

int start=index;

while(i<S){

index=(index+i*s)%S;

if(table[index]==-1){

table[index]=key;

return;

i++;

cout<<"Table is Full"<<endl;

void display(){

for(int i =0;i<S;i++){

if(table[i] != -1){

cout<<i<<" --> "<<table[i]<<endl;

else{

cout<<i<<" -->"<<" Empty"<<endl;

};

int main(){
H h;

int key;

int n;

cin>>n;

for(int i=0;i<n;i++){

cin>>key;

h.insert(key);

h.display();

Hashing quadratic probing

#include <iostream>

using namespace std;

const int S = 10;

class HashTable {

private:

int table[S];

public:

HashTable() {

for (int i = 0; i < S; i++) {

table[i] = -1;

}
int hashKey(int key) {

return key % S;

void insert(int key) {

int index = hashKey(key);

int i = 1;

int originalIndex = index;

while (table[index] != -1) {

index = (originalIndex + i * i) % S;

if (index == originalIndex) {

cout << "The table is full. Could not insert key: " << key << endl;

return;

i++;

if (i == S) {

cout << "The table is full. Could not insert key: " << key << endl;

return;

table[index] = key;

}
void display() {

cout << "\nHash Table (Quadratic Probing):" << endl;

for (int i = 0; i < S; i++) {

if (table[i] != -1) {

cout << i << " --> " << table[i] << endl;

} else {

cout << i << " --> empty" << endl;

};

int main() {

HashTable ht;

int n;

cout << "Enter number of keys to insert: ";

cin >> n;

cout << "Enter " << n << " keys:" << endl;

for (int i = 0; i < n; i++) {

int key;

cin >> key;

ht.insert(key);

ht.display();
return 0;

Stack

#include<bits/stdc++.h>

using namespace std ;

struct node{

int data;

node* next;

};

node* top=0;

void push(int val){

node* newnode= new node();

newnode->data= val;

newnode->next=top;

top=newnode;

void pop(){

if(top==0){

cout<<"Empty";

return;

node* temp=top;

top=top->next;

delete temp;
}

void peek(){

if(top==0){

cout<<"Empty";

return;

cout<<"Top: "<<top;

void display(){

if(top==0){

cout<<"Empty";

return;

node* temp=top;

while(temp!=0){

cout<<temp->data<<" ";

temp=temp->next;

cout<<endl;

int main(){

int choice;
int val;

do{

cin>>val;

cin>>choice;

if(choice==1){

push(val);

else if(choice==2){

pop();

else if(choice==3){

peek();

}while(choice!=4);

return 0;

Infix to postfix expression

#include <bits/stdc++.h>

using namespace std ;

int p(char c){

if(c=='^') return 3;

else if(c=='*' || c=='/') return 2;


else if (c=='+' || c=='-') return 1;

return 0;

int main(){

string a;

cin>>a;

string r="";

stack <char> st;

for(char c : a){

if(c>='a' && c<='z'){

r+=c;

else{

if(c==')'){

while(st.top()=='('){

r+=st.top();

st.pop();

st.pop();

else if(c==' '|| st.empty()){

st.push(c);

else if(p(st.top()) <p(c)){

st.push(c);

}
else if(p(st.top()) >= p(c)){

while(!st.empty() && p(st.top()) >= p(c)){

r+=st.top();

st.pop();

st.push(c);

while(!st.empty()){

r+=st.top();

st.pop();

cout<<r<<" "<<endl;return 0;

You might also like