0% found this document useful (0 votes)
3 views11 pages

Array and Data Structure Algorithms

The document contains various C++ code snippets demonstrating fundamental data structures and algorithms. It includes examples for finding max/min in an array, reversing an array, finding the Kth smallest element, implementing a singly linked list, queue operations using an array, binary tree structures, and different tree traversal methods. Additionally, it presents a graph implementation using an adjacency list.
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)
3 views11 pages

Array and Data Structure Algorithms

The document contains various C++ code snippets demonstrating fundamental data structures and algorithms. It includes examples for finding max/min in an array, reversing an array, finding the Kth smallest element, implementing a singly linked list, queue operations using an array, binary tree structures, and different tree traversal methods. Additionally, it presents a graph implementation using an adjacency list.
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

1.

Max and Min in Array


#include <iostream>
using namespace std;
int main() {
int a[] = {3, 5, 1, 9, 2};
int max = a[0], min = a[0];
for(int i=1;i<5;i++){
if(a[i]>max) max=a[i];
if(a[i]<min) min=a[i];
}
cout<<"Max="<<max<<" Min="<<min;
}
2. Reverse Array
#include <iostream>
using namespace std;
int main(){
int a[]={1,2,3,4,5};
int n=5;
for(int i=0;i<n/2;i++)
swap(a[i],a[n-i-1]);
for(int i=0;i<n;i++) cout<<a[i]<<" ";
}
3. Kth Smallest Element
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int a[]={7,10,4,3,20,15};
int k=3;
sort(a,a+6);
cout<<a[k-1];
}
4. Singly Linked List
#include <iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
7. Queue using Array
#include <iostream>
using namespace std;
#define N 5
int q[N], front=-1, rear=-1;
void enqueue(int x){
if(rear==N-1) return;
if(front==-1) front=0;
q[++rear]=x;
}
void dequeue(){
if(front>rear) return;
front++;
}
10. Binary Tree
#include <iostream>
using namespace std;
struct Node{
int data;
Node* left;
Node* right;
};
11. Inorder Traversal
void inorder(Node* root){
if(root){
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
}
12. Preorder Traversal
void preorder(Node* root){
if(root){
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
}
13. Postorder Traversal
void postorder(Node* root){
if(root){
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
}
}
14. Level Order Traversal
#include <queue>
void levelOrder(Node* root){
queue<Node*> q;
[Link](root);
while(![Link]()){
Node* temp=[Link](); [Link]();
cout<<temp->data<<" ";
if(temp->left) [Link](temp->left);
if(temp->right) [Link](temp->right);
}
}
15. Graph using Adjacency List
#include <iostream>
#include <vector>
using namespace std;
class Graph{
vector<vector<int>> adj;
public:
Graph(int v){ [Link](v); }
void addEdge(int u,int v){
adj[u].push_back(v);
adj[v].push_back(u);
}
};

You might also like