1.
Stack using arrays
#include<iostream>
#include<stdlib.h>
using namespace std;
#define MAX 5
int top=-1;
void push(int stack[], int data){
if(top == MAX-1)
cout << "The stack is Full.";
else
{
top++;
stack[top] = data;
}
}
int pop(int stack[]){
int ret_val;
if(top == -1){
ret_val = 0;
cout << "The queue is empty." << endl;
}
else{
ret_val = stack[top];
top--;
}
return ret_val;
}
void display(int stack[]){
int i = 0;
cout << "Stack: " << endl;
if(top == -1)
cout << "The Stack is Empty." << endl;
else{
for(i=top;i>=0;i--){
cout << stack[i] << " ";
}
cout << endl;
}
}
int main(){
int ch;
int stack[MAX], data, var;
do{
cout << "Menu: " << endl;
cout << "1. Push" << endl;
cout << "2. Pop" << endl;
cout << "3. Display" << endl;
cout << "4. Exit" << endl;
cout << "Enter a choice: ";
cin >> ch;
switch(ch){
case 1: cout << "Enter data to push: ";
cin >> data;
push(stack, data);
break;
case 2: var = pop(stack);
cout << var << " was popped." << endl;
break;
case 3: display(stack);
break;
case 4: exit(0);
default: cout << "Wrong choice." << endl;
}
}while(ch >= 1 && ch <=4);
}
2. Queue using arrays
#include<iostream>
#include<stdlib.h>
using namespace std;
#define MAX 5
int front=-1, rear=-1;
void insert(int queue[], int data){
if(rear == (MAX - 1))
cout << "Queue is Full." << endl;
else{
if(front == -1)
front = 0;
rear++;
queue[rear] = data;
}
}
void delete_element(int queue[]){
if(front == -1){
cout << "The queue is empty." << endl;
}
else{
cout << queue[front] << " was deleted." << endl;
front++;
}
if(front > rear){
front = -1;
rear = -1;
}
}
void display(int queue[]){
cout << "The queue: " << endl;
if(front == -1)
cout << "is empty." << endl;
else{
int i;
for(i=front;i<=rear;i++){
cout << queue[i] << " ";
}
cout << endl;
}
}
int main(){
int ch;
int queue[MAX], data, var;
do{
cout << "Menu: " << endl;
cout << "1. Insert" << endl;
cout << "2. Delete" << endl;
cout << "3. Display" << endl;
cout << "4. Exit" << endl;
cout << "Enter a choice: ";
cin >> ch;
switch(ch){
case 1: cout << "Enter data to insert: ";
cin >> data;
insert(queue, data);
break;
case 2: delete_element(queue);
break;
case 3: display(queue);
break;
case 4: exit(0);
default: cout << "Wrong choice." << endl;
}
}while(ch >= 1 && ch <=4);
}
3. Stack using Linked List
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node{
int data;
struct node *next;
};
typedef struct node NODE;
NODE *top = NULL;
void push(int newdata){
NODE *temp = (NODE*)malloc(sizeof(NODE));
temp -> data = newdata;
if(top == NULL){
top = temp;
top -> next = NULL;
}
else{
temp -> next = top;
top = temp;
}
}
void pop(){
NODE *temp;
temp = top;
if(top == NULL)
cout << "The stack is empty." << endl;
else{
top = top -> next;
free(temp);
}
}
void display(){
cout << "The Stack: " << endl;
NODE *temp;
temp = top;
if(top == NULL)
cout << "is empty" << endl;
else{
while(temp -> next != NULL){
cout << temp -> data << " ";
temp = temp -> next;
}
cout << temp -> data;
cout << endl;
}
}
int main(){
int ch;
int data;
do{
cout << "Menu: " << endl;
cout << "1. Push" << endl;
cout << "2. Pop" << endl;
cout << "3. Display" << endl;
cout << "4. Exit" << endl;
cout << "Enter a choice: ";
cin >> ch;
switch(ch){
case 1: cout << "Enter data to push: ";
cin >> data;
push(data);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: cout << "Wrong choice." << endl;
}
}while(ch >= 1 && ch <=4);
}
4. Queue using Linked List
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node{
int data;
struct node *next;
};
typedef struct node NODE;
NODE *front, *rear;
void insert(int newdata){
NODE *temp = (NODE*)malloc(sizeof(NODE));
temp -> data = newdata;
if(front == NULL){
front = temp;
front -> next = NULL;
rear = front;
}
else{
rear -> next = temp;
temp -> next = NULL;
rear = temp;
}
}
void remove(){
NODE *temp;
temp = front;
if(front == NULL)
cout << "The Queue is empty." << endl;
else{
front = front -> next;
free(temp);
}
}
void display(){
NODE *temp;
temp = front;
cout << "The Queue: " << endl;
if(front == NULL)
cout << "is empty." << endl;
else{
while(temp -> next != NULL){
cout << temp -> data << " ";
temp = temp-> next;
}
cout << temp -> data << endl;
}
}
int main(){
int ch;
int data;
do{
cout << "Menu: " << endl;
cout << "1. Insert" << endl;
cout << "2. Remove" << endl;
cout << "3. Display" << endl;
cout << "4. Exit" << endl;
cout << "Enter a choice: ";
cin >> ch;
switch(ch){
case 1: cout << "Enter data to push: ";
cin >> data;
insert(data);
break;
case 2: remove();
break;
case 3: display();
break;
case 4: exit(0);
default: cout << "Wrong choice." << endl;
}
}while(ch >= 1 && ch <=4);
}
5. Singly Linked List
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node{
int data;
struct node *next;
};
typedef struct node NODE;
NODE *start;
void insert(int newdata){
NODE *temp = (NODE*)malloc(sizeof(NODE));
temp -> data = newdata;
if(start == NULL){
start = temp;
start -> next = NULL;
}
else{
temp -> next = start;
start = temp;
}
}
void remove(int newdata){
NODE *temp, *temp2;
temp = start;
temp2 = start;
if(start == NULL)
cout << "The List is empty." << endl;
else{
while(temp -> data != newdata){
temp = temp-> next;
}
while(temp2 -> next != temp)
temp2 = temp2 -> next;
temp2 -> next = temp -> next;
free(temp);
}
}
void display(){
NODE *temp;
temp = start;
cout << "The Queue: " << endl;
if(start == NULL)
cout << "is empty." << endl;
else{
while(temp -> next != NULL){
cout << temp -> data << " ";
temp = temp-> next;
}
cout << temp -> data << endl;
}
}
int main(){
int ch;
int data;
do{
cout << "Menu: " << endl;
cout << "1. Insert" << endl;
cout << "2. Remove" << endl;
cout << "3. Display" << endl;
cout << "4. Exit" << endl;
cout << "Enter a choice: ";
cin >> ch;
switch(ch){
case 1: cout << "Enter data to insert: ";
cin >> data;
insert(data);
break;
case 2: cout << "Enter data to delete: ";
cin >> data;
remove(data);
break;
case 3: display();
break;
case 4: exit(0);
default: cout << "Wrong choice." << endl;
}
}while(ch >= 1 && ch <=4);
}
///// THIS PROGRAM IS INCOMPLETE //////
// IT DOESN’T HAVE THE INSERT_MID AND INSERT_END functions //
6. Doubly Linked List
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node{
int data;
struct node *next, *prev;
};
typedef struct node NODE;
NODE *start;
void insert(int newdata){
NODE *temp = (NODE*)malloc(sizeof(NODE));
temp -> data = newdata;
if(start == NULL){
start = temp;
start -> next = NULL;
start -> prev = NULL;
}
else{
start -> prev = temp;
temp -> next = start;
temp -> prev = NULL;
start = temp;
}
}
void insert_end(int newdata){
NODE *temp = (NODE*)malloc(sizeof(NODE));
temp -> data = newdata;
NODE *temp2;
if(start == NULL){
insert(newdata);
}
else{
temp2 = start;
while(temp2 -> next != NULL)
temp2 = temp2 -> next;
temp2 -> next = temp;
temp -> prev = temp2;
temp -> next = NULL;
}
}
void insert_mid(int newdata, int pos){
NODE *temp = (NODE*)malloc(sizeof(NODE));
temp -> data = newdata;
NODE *temp2;
if(start == NULL){
insert(newdata);
}
else{
temp2 = start;
while(temp2 -> data != pos)
temp2 = temp2 -> next;
temp2 -> prev -> next = temp;
temp -> next = temp2;
temp -> prev = temp2 -> prev;
temp2 -> prev = temp;
}
}
void remove(int newdata){
NODE *temp, *temp2;
temp = start;
if(start == NULL)
cout << "The List is empty." << endl;
else{
while(temp -> data != newdata){
temp = temp -> next;
}
temp -> next -> prev = temp -> prev;
temp -> prev -> next = temp -> next;
free(temp);
}
}
void display(){
NODE *temp;
temp = start;
cout << "The Queue: " << endl;
if(start == NULL)
cout << "is empty." << endl;
else{
while(temp -> next != NULL){
cout << temp -> data << " ";
temp = temp-> next;
}
cout << temp -> data << endl;
}
}
int main(){
int ch;
int data, pos;
do{
cout << "Menu: " << endl;
cout << "1. Insert" << endl;
cout << "2. insert_mid" << endl;
cout << "3. insert_end" << endl;
cout << "4. Remove" << endl;
cout << "5. Display" << endl;
cout << "6. Exit" << endl;
cout << "Enter a choice: ";
cin >> ch;
switch(ch){
case 1: cout << "Enter data to insert: ";
cin >> data;
insert(data);
break;
case 2: cout << "Enter data to insert, position: ";
cin >> data >> pos;
insert_mid(data, pos);
break;
case 3: cout << "Enter data to insert: ";
cin >> data;
insert_end(data);
break;
case 4: cout << "Enter data to delete: ";
cin >> data;
remove(data);
break;
case 5: display();
break;
case 6: exit(0);
default: cout << "Wrong choice." << endl;
}
}while(ch >= 1 && ch <=6);
}
7. Tree Traversal
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node{
char data;
struct node *left, *right;
};
typedef struct node NODE;
NODE* makeNode(char n){
NODE *newn;
newn = (NODE*)malloc(sizeof(NODE));
newn -> data = n;
newn -> left = NULL;
newn -> right = NULL;
return newn;
}
void preOrder(NODE *temp){
if(temp != NULL){
cout << temp -> data << endl;
preOrder(temp -> left);
preOrder(temp -> right);
}
}
void inOrder(NODE *temp){
if(temp != NULL){
inOrder(temp -> left);
cout << temp -> data << endl;
inOrder(temp -> right);
}
}
void postOrder(NODE *temp){
if(temp != NULL){
postOrder(temp -> left);
postOrder(temp -> right);
cout << temp -> data << endl;
}
}
int main(){
NODE *tree;
tree = makeNode('A');
tree -> left = makeNode('B');
tree -> right = makeNode('C');
cout << "PreOrder: " << endl;
preOrder(tree);
cout << "InOrder: " << endl;
inOrder(tree);
cout << "PostOrder: " << endl;
postOrder(tree);
}
8. Selection Sort
#include<iostream>
using namespace std;
int main()
{
int n, arr[50], i, j, temp;
cout<<"Enter no of elements: ";
cin>>n;
cout<<"Enter Elements: ";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<"Sorted: " << endl;
for(i=0; i<n; i++)
{
cout<<arr[i]<<" ";
}
}
9. Bubble Sort
#include<iostream>
using namespace std;
int main()
{
int n, arr[50], i, j, temp;
cout<<"Enter no of elements: ";
cin>>n;
cout<<"Enter Elements: ";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
for(i=0; i<n; i++)
{
for(j=0; j<(n-i-1); j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<"Sorted: " << endl;
for(i=0; i<n; i++)
{
cout<<arr[i]<<" ";
}
}
10. Merge Sort
#include <iostream>
using namespace std;
void merge(int a[], int p, int q, int r){
int *b = new int[(q-p)+1];
int i, j, k;
k = 0;
i = p;
j = q + 1;
while (i <= q && j <= r){
if (a[i] < a[j])
b[k++] = a[i++];
else
b[k++] = a[j++];
}
while (i <= q)
b[k++] = a[i++];
while (j <= r)
b[k++] = a[j++];
for (i = r; i >= p; i--)
a[i] = b[--k];
}
void mergeSort(int a[], int p, int r){
int q;
if (p < r){
q = (p + r) / 2;
mergeSort(a, p, q);
mergeSort(a, q + 1, r);
merge(a, p, q, r);
}
}
void printArray(int a[], int size){
int i;
for (i = 0; i < size; i++)
cout << a[i] << " ";
cout << "\n";
}
int main(){
int n, i;
cout << "Enter length: ";
cin >> n;
int *arr = new int[n];
cout << "Enter the elements: ";
for(i=0;i<n;i++){
cin >> arr[i];
}
cout << "Given array: \n";
printArray(arr, n);
mergeSort(arr, 0, n - 1);
cout << "\nSorted array: \n";
printArray(arr, n);
}
11. Quick Sort
#include <iostream>
using namespace std;
void quicksort(int number[25], int first, int last){
int i, j, pivot, temp;
if (first < last){
pivot = first;
i = first;
j = last;
while (i < j){
while (number[i] <= number[pivot] && i < last)
i++;
while (number[j] > number[pivot])
j--;
if (i < j){
temp = number[i];
number[i] = number[j];
number[j] = temp;
}
}
temp = number[pivot];
number[pivot] = number[j];
number[j] = temp;
quicksort(number, first, j - 1);
quicksort(number, j + 1, last);
}
}
int main(){
int i, count, number[25];
cout << "Enter Length: ";
cin >> count;
cout << "Enter elements: ";
for (i = 0; i < count; i++)
cin>>number[i];
quicksort(number, 0, count - 1);
cout << "The Sorted Order is: ";
for (i = 0; i < count; i++)
cout << number[i];
return 0;
}
12. BFS
#include<iostream>
using namespace std;
int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];
int main()
{
int m;
cout <<"Enter no of vertices:";
cin >> n;
cout <<"Enter no of edges:";
cin >> m;
cout <<"\nEDGES \n";
for(k=1; k<=m; k++)
{
cin >>i>>j;
cost[i][j]=1;
}
cout <<"Enter initial vertex to traverse from:";
cin >>v;
cout <<"Visitied vertices:";
cout <<v<<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=1; j<=n; j++)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
qu[rare++]=j;
}
v=qu[front++];
cout<<v <<" ";
k++;
visit[v]=0;
visited[v]=1;
}
}
13. DFS
#include<iostream>
using namespace std;
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
int main()
{
int m;
cout <<"Enter no of vertices:";
cin >> n;
cout <<"Enter no of edges:";
cin >> m;
cout <<"\nEDGES \n";
for(k=1; k<=m; k++)
{
cin >>i>>j;
cost[i][j]=1;
}
cout <<"Enter initial vertex to traverse from:";
cin >>v;
cout <<"DFS ORDER OF VISITED VERTICES:";
cout << v <<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=n; j>=1; j--)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
}
v=stk[--top];
cout<<v << " ";
k++;
visit[v]=0;
visited[v]=1;
}
return 0;
}
14. Linear Search
#include<iostream>
using namespace std;
void main()
{
int arr[10], i, num, n, c=0, pos;
cout<<"Enter Length: ";
cin>>n;
cout<<"Enter Elements : ";
for(i=0; i<n; i++)
cin>>arr[i];
cout<<"Enter the search term: ";
cin>>num;
for(i=0; i<n; i++){
if(arr[i]==num){
c=1;
pos=i+1;
break;
}
}
if(c==0)
{
cout<<"Element not found";
}
else
{
cout<<num<<" found at position "<< pos << endl;
}
}
15. Binary Search
#include <iostream>
using namespace std;
int binarySearch(int arr[], int l, int r, int x) {
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
int main(void) {
int n, i, x;
cout << "Enter length: ";
cin >> n;
int *arr = new int[n];
cout << "Enter the elements: ";
for(i=0;i<n;i++){
cin >> arr[i];
}
cout<<"Enter the search term: ";
cin >> x;
int result = binarySearch(arr, 0, n - 1, x);
(result == -
1) ? cout << "Element is not present in array." : cout << "Element is present at
index: " << result << endl;
return 0;
}