CS301 & CS301P Important Topics With Coding


***** Paid Services Available Please Contact Us *****

All Departments LMS Handling
All Types Assignments
All Types Quiz
All Types GDBs
Online Classes
Final Year Projects CS519 & CS619

Whatsapp: +92 3472506073 (0347-2506073) 


Single Dimensional Array:

#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int array[5] = {10, 20, 30, 40, 50};
cout << "Array Index 0 = " << array[0];
cout << "\n\nArray Index 1 = " << array[1];
cout << "\n\nArray Index 2 = " << array[2];
cout << "\n\nArray Index 3 = " << array[3];
cout << "\n\nArray Index 4 = " << array[4];
getch();
return 0;
}

Multi Dimensional Array:

#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int array[2][3] = { {10, 20, 30} , {40, 50, 60} };
cout << "Array Row 0 Col 0 = " << array[0][0];
cout << "\n\nArray Row 0 Col 1 = " << array[0][1];
cout << "\n\nArray Row 0 Col 2 = " << array[0][2];
cout << "\n\nArray Row 1 Col 0 = " << array[1][0];
cout << "\n\nArray Row 1 Col 1 = " << array[1][1];
cout << "\n\nArray Row 1 Col 2 = " << array[0][2];
getch();
return 0;
}

Singly Linked List:

#include<iostream>
#include<conio.h>
using namespace std;
class singly_linklist
{
private:
struct node
{
   int data;
   node *next_add;
};
node *head = NULL;
public:
void insert(int &n);
void display();
void search();
void sum_count();
void update();
void del();
};
void singly_linklist::insert(int &n)
{
node *new_node = new node;
new_node ->  data = n;
new_node ->  next_add = NULL;
if(head == NULL)
{
   head = new_node;
}
else
{
node *ptr = head;
while(ptr -> next_add != NULL)
{
   ptr = ptr -> next_add;
}
ptr -> next_add = new_node;
}
}
void singly_linklist::display()
{
system("cls");
cout<<"\n\n\t\t\t\t Display Record";
if(head == NULL)
{
cout<<"\n\n Link List is Empty...";
}
else
{
node *ptr = head;
while(ptr != NULL)
{
cout<<"\n Value : "<<ptr -> data;
ptr = ptr -> next_add; 
}
}
}
void singly_linklist::search()
{
system("cls");
int n,count=0;
cout<<"\n\n\t\t\t\tSearch Record";
if(head == NULL)
{
cout<<"\n\n Link List is Empty...";
}
else
{
cout<<"\n\n Search Value : ";
cin>>n;
node *ptr = head;
while(ptr != NULL)
{
if(n == ptr -> data)
{
cout<<"\n\n Value is Found : "<<n;
count++;
}
ptr = ptr -> next_add;
}
if(count == 0)
{
cout<<"\n\n Value Not Found : "<<n;
}
}
}
void singly_linklist::sum_count()
{
system("cls");
int sum=0,count=0;
cout<<"\n\n\t\t\t\tSum & Count Nodes";
if(head == NULL)
{
cout<<"\n\n Link List is Empty...";
}
else
{
node *ptr = head;
while(ptr != NULL)
{
sum = sum + ptr -> data;
count++;
ptr = ptr -> next_add; 
}
cout<<"\n\n Total Values of Nodes in Link List : "<<sum;
cout<<"\n\n Total Nodes in Link List : "<<count;
}
}
void singly_linklist::update()
{
system("cls");
int n,count=0;
cout<<"\n\n\t\t\t\tUpdate Record";
if(head == NULL)
{
cout<<"\n\n Link List is Empty...";
}
else
{
cout<<"\n\n Value For Update : ";
cin>>n;
node *ptr = head;
while(ptr != NULL)
{
if(n == ptr -> data)
{
cout<<"\n\n New Value : ";
cin>>n;
ptr -> data = n;
count++;
break;
}
ptr = ptr -> next_add;
}
if(count == 0)
{
cout<<"\n\n Record Not Found...";
}
}
}
void singly_linklist::del()
{
system("cls");
int n,count=0;
cout<<"\n\n\t\t\t\tDelete Record";
if(head == NULL)
{
cout<<"\n\n Link List is Empty...";
}
else
{
cout<<"\n\n Value For Delete : ";
cin>>n;
if(n == head -> data)
{
node *ptr = head;
head = ptr -> next_add;
cout<<"\n\n Node is Deleted...";
delete ptr;
count++;
}
else
{
node *pre = head;
node *ptr = head -> next_add;
while(ptr != NULL)
{
if(n == ptr -> data)
{
pre -> next_add = ptr -> next_add;
cout<<"\n\n Node is Deleted...";
count++;
delete ptr;
break;
}
pre = ptr;
ptr = ptr -> next_add;
}
}
if(count == 0)
{
cout<<"\n\n Record Not Found...";
}
}
main()
{
int n,choice;
singly_linklist s;
p:
system("cls");
    cout<<"\n\n\t\t\t\t Control Panel";
    cout<<"\n\n 1. Insert Record";
    cout<<"\n 2. Display Record";
    cout<<"\n 3. Search Record";
    cout<<"\n 4. Sum & Count";
    cout<<"\n 5. Update Record";
    cout<<"\n 6. Delete Record";
    cout<<"\n 7. Exit";
    cout<<"\n\n Enter Your Choice : ";
    cin>>choice;
    switch(choice)
    {
    case 1:
    system("cls");
    cout<<"\n\n\t\t\t\tInsert Record";
    cout<<"\n\n Enter Value : ";
    cin>>n;
    s.insert(n);
    break;
    case 2:
    s.display();
        break;
    case 3:
    s.search();
    break;
    case 4:
s.sum_count();
break;
case 5:
s.update();
break;
case 6:
s.del();
break;
    case 7:
    exit(0);
    default:
    cout<<"\n\n Invalid Value...Please Try Again...";
}
getch();
goto p;
}

Doubly Linked List:

#include<iostream>
#include<conio.h>
using namespace std;
class Node
{
public:
Node *pre_add;
int data;
Node *next_add;
};
class Linked_List
{
public:
Node *head = NULL;
Node *tail = NULL;
void Insert()
{
int n;
cout<<"\n\n Enter Value : ";
cin>>n;
Node *new_node = new Node;
new_node -> pre_add = NULL;
new_node -> data = n;
new_node -> next_add = NULL;
if(head == NULL)
{
head = new_node;
tail = new_node;
}
else
{
tail -> next_add = new_node;
new_node -> pre_add = tail;
tail = new_node;
}
}
void Count()
{
if(head == NULL)
{
cout<<"\n\n Linked List is Empty...";
}
else
{
int n=0;
Node *ptr = head;
while(ptr != NULL)
{
n++;
ptr = ptr -> next_add;
}
cout<<"\n\n Total Nodes : "<<n;
}
}
void Search()
{
if(head == NULL)
{
cout<<"\n\n Linked List is Empty...";
}
else
{
int n,found=0,c=1;
cout<<"\n\n Enter Value For Search : ";
cin>>n;
Node *ptr = head;
while(ptr != NULL)
{
if(ptr -> data == n)
{
cout<<"\n\n Search Value "<<n<<" Found At "<<c<<" Node";
found++;
break;
}
c++;
ptr = ptr -> next_add;
}
if(found == 0)
{
cout<<"\n\n Search Value "<<n<<" Can't Found...";
}
}
}
void Update()
{
if(head == NULL)
{
cout<<"\n\n Linked List is Empty...";
}
else
{
int n,found=0;
cout<<"\n\n Enter Value For Updation : ";
cin>>n;
Node *ptr = head;
while(ptr != NULL)
{
if(n == ptr -> data)
{
cout<<"\n\n Enter New Value : ";
cin>>ptr -> data;
found++;
break;
}
ptr = ptr -> next_add;
}
if(found == 0)
{
cout<<"\n\n Updation Value "<<n<<" Can't Found...";
}
}
}
void del()
{
if(head == NULL)
{
cout<<"\n\n Linked List is Empty...";
}
else
{
int n,found=0;
cout<<"\n\n Enter Value For Deletion : ";
cin>>n;
// First Node & Last Node
if(head == tail && n == head -> data)
{
Node *ptr = head;
head = NULL;
tail = NULL;
delete ptr;
cout<<"\n\n Node Deleted Successfully...";
found++;
}
// First Node
else if(n == head -> data)
{
Node *ptr = head;
head = head -> next_add;
head -> pre_add = NULL;
delete ptr;
cout<<"\n\n Node Deleted Successfully...";
found++;
}
// Last Node
else if(n == tail -> data)
{
Node *ptr = tail;
tail = tail -> pre_add;
tail -> next_add = NULL;
delete ptr;
cout<<"\n\n Node Deleted Successfully...";
found++;
}
// Between First & Last
else
{
Node *pre_ptr = head; // 10 
Node *ptr = head -> next_add; // 20
Node *next_ptr = ptr -> next_add; // 30 
while(ptr -> next_add != NULL)
{
if(n == ptr -> data)
{
pre_ptr -> next_add = next_ptr;
next_ptr -> pre_add = pre_ptr;
delete ptr;
cout<<"\n\n Node Deleted Successfully...";
found++;
break;
}
pre_ptr = ptr;
ptr = next_ptr; 
next_ptr = next_ptr -> next_add;
}
if(found == 0)
{
if(n == ptr -> data)
{
delete ptr;
cout<<"\n\n Node Deleted Successfully...";
found++;
}
}
}
if(found == 0)
{
cout<<"\n\n Value "<<n<<" Can't Found...";
}
}
}
void ShowF()
{
cout<<"\n\n";
Node *ptr = head;
while(ptr != NULL)
{
cout<<ptr -> data<<" ";
ptr = ptr -> next_add;
}
}
void ShowR()
{
cout<<"\n\n";
Node *ptr = tail;
while(ptr != NULL)
{
cout<<ptr -> data<<" ";
ptr = ptr -> pre_add;
}
}
};
main()
{
Linked_List obj;
p:
system("cls");
int choice;
cout<<"\n\n 1. Insert Node";
cout<<"\n\n 2. Count Nodes";
cout<<"\n\n 3. Search Node";
cout<<"\n\n 4. Update Node";
cout<<"\n\n 5. Delete Node";
cout<<"\n\n 6. Show Nodes";
cout<<"\n\n 7. Exit";
cout<<"\n\n\n Your Choice : ";
cin>>choice;
switch(choice)
{
case 1:
obj.Insert();
break;
case 2:
obj.Count();
break;
case 3:
obj.Search();
break;
case 4:
obj.Update();
break;
case 5:
obj.del();
break;
case 6:
if(obj.head == NULL)
cout<<"\n\n Linked List is Empty...";
else
{
obj.ShowF();
obj.ShowR();
}
break;
case 7:
exit(0);
default:
cout<<"\n\n Invalid Choice...Please Try Again...";
}
getch();
goto p;
}

Circular Linked List:

#include<iostream>
#include<conio.h>
using namespace std;
int size=0;
class linklist
{
private:
struct node
{
int data;
node *next;
};
node *head = NULL;
node *tail = NULL;
public:
void insert(int &n);
void display();
void search();
void update();
void sum_count_avg();
void del();
};
void linklist::insert(int &n)
{
node *new_ptr = new node;
new_ptr -> data = n;
new_ptr -> next = NULL;
size++;
if(head == NULL)
{
head = new_ptr;
tail = new_ptr;
    new_ptr -> next = head;
}
else
{
tail -> next = new_ptr;
tail = new_ptr;
tail -> next = head;
}
cout<<"\n\n Node is Created Successfully...";
}
void linklist::display()
{
system("cls");
int c=0;
cout<<"\n\n\t\t\t\tDisplay Record";
if(head == NULL)
{
cout<<"\n\n Linked List is Empty...";
}
else
{
node *ptr = head;
while(c != size)
{
cout<<"\n\n Value : "<<ptr -> data;
ptr = ptr -> next;
c++;
}
}
}
void linklist::search()
{
system("cls");
int c=0,n,found=0;
cout<<"\n\n\t\t\t\tSearch Record";
if(head == NULL)
{
cout<<"\n\n Linked List is Empty...";
}
else
{
cout<<"\n\n Enter Value : ";
cin>>n;
node *ptr = head;
while(c != size)
{
if(n == ptr -> data)
{
cout<<"\n\n Value "<<n<<" is Found...";
found++;
break;
}
ptr = ptr -> next;
c++;
}
if(found == 0)
cout<<"\n\n Value "<<n<<" can't Found...";
}
}
void linklist::update()
{
system("cls");
int n,c=0,found=0;
cout<<"\n\n\t\t\t\tUpdate Record";
if(head == NULL)
{
cout<<"\n\n Linked List is Empty...";
}
else
{
cout<<"\n\n Enter Value : ";
cin>>n;
node *ptr = head;
while(c != size)
{
if(n == ptr -> data)
{
cout<<"\n\n New Value : ";
cin>>n;
ptr -> data = n;
found++;
cout<<"\n\n Value Updated Successfully...";
break;
}
ptr = ptr -> next;
c++;
}
if(found == 0)
cout<<"\n\n Value "<<n<<" Can't Found...";
}
}
void linklist::sum_count_avg()
{
system("cls");
int c=0,sum=0;
cout<<"\n\n\t\t\t Sum, Count, Avg Record";
if(head == NULL)
{
cout<<"\n\n Linked List is Empty...";
}
else
{
node *ptr = head;
while(c != size)
{
sum = sum + ptr -> data;
ptr = ptr -> next;
c++;
}
cout<<"\n\n Number of Nodes : "<<size;
cout<<"\n\n Sum : "<<sum;
cout<<"\n\n Average : "<<sum/size;
}
}
void linklist::del()
{
system("cls");
int n,found=0,c=1;
cout<<"\n\n\t\t\t\tDelete Record";
if(head == NULL)
{
cout<<"\n\n Linked List is Empty...";
}
else
{
node *ptr = head;
cout<<"\n\n Enter Value : ";
cin>>n;
if(size == 1 && n == head -> data)
{
head = NULL;
tail = NULL;
size--;
cout<<"\n\n Node Deleted Successfully...";
found++;
delete ptr;
}
else if(n == head -> data)
{
head = head -> next;
tail -> next = head;
size--;
cout<<"\n\n Node Deleted Successfully...";
found++;
delete ptr;
}
else if(n == tail -> data)
{
while(ptr -> next != tail)
{
ptr = ptr -> next;
}
node *pre = tail;
tail = ptr;
tail -> next = head;
size--;
cout<<"\n\n Node Deleted Successfully...";
found++;
delete pre;
}
else
{
node *pre_ptr = head;
ptr = head -> next;
while(c != size)
{
if(n == ptr -> data)
{
pre_ptr -> next = ptr -> next;
size--;
cout<<"\n\n Node Deleted Successfully...";
found++;
delete ptr;
break;
}
pre_ptr = ptr;
ptr = ptr -> next;
c++;
}
}
if(found == 0)
cout<<"\n\n Value "<<n<<" Can't Found...";
}
}
main()
{
linklist l;
int choice,n;
p:
system("cls");
cout<<"\n\n\t\t\t\tControl Panel";
cout<<"\n\n 1. Insert Record";
cout<<"\n 2. Display Record";
cout<<"\n 3. Search Record";
cout<<"\n 4. Update Record";
cout<<"\n 5. Sum, Count, Avg Record";
cout<<"\n 6. Delete Record";
cout<<"\n 7. Exit";
cout<<"\n\n Your Choice : ";
cin>>choice;
switch(choice)
{
case 1:
system("cls");
cout<<"\n\n\t\t\t\tInsert Record";
cout<<"\n\n Enter Value : ";
cin>>n;
l.insert(n);
break;
case 2:
l.display();
break;
case 3:
l.search();
break;
case 4:
l.update();
break;
case 5:
l.sum_count_avg();
break;
case 6:
l.del();
break;
case 7:
exit(0);
default:
cout<<"\n\n Invalid Value...Please Try Again...";
}
getch();
goto p;
}

Stack With Array:

#include<iostream>
#include<conio.h>
using namespace std;
int i=-1;
class stack
{
private:
string stk[5];
public:
void push();
void pop();
void display();
};
void stack::push()
{
system("cls");
string name;
cout<<"\n\n\t\t\t\tPush Record";
if(i > 3)
{
cout<<"\n\n Stack is Full...";
}
else
{
cout<<"\n\n Your Name : ";
cin>>name;
stk[++i] = name;
cout<<"\n\n Name "<<name<<" is Inserted Successfully...";
}
}
void stack::pop()
{
system("cls");
cout<<"\n\n\t\t\t\tPop Record";
if(i < 0)
{
cout<<"\n\n Stack is Empty...";
}
else
{
cout<<"\n\n "<<stk[i--]<<" Name Deleted Successfully...";
}
}
void stack::display()
{
system("cls");
cout<<"\n\n\t\t\t\tDisplay Record";
if(i < 0)
{
cout<<"\n\n Stack is Empty...";
}
else
{
for(int a=i;a>=0;a--)
{
cout<<"\n\n Name : "<<stk[a];
}
}
main()
{
stack s;
int choice;
p:
system("cls");
cout<<"\n\n\t\t\t\tControl Panel";
cout<<"\n\n 1. Push Record";
cout<<"\n 2. Pop Record";
cout<<"\n 3. Display Record";
cout<<"\n 4. Exit";
cout<<"\n\n Your Choice : ";
cin>>choice;
switch(choice)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
case 4:
exit(0);
default:
cout<<"\n\n Invalid Value...Please Try Again...";
}
getch();
goto p;
}

Stack With Linked List:

#include<iostream>
#include<conio.h>
using namespace std;
class Node
{
private:
int data;
Node *next;
public:
void setData(int n)
{
data = n;
}
int getData()
{
return data;
}
void setNext(Node *ptr)
{
next = ptr;
}
Node* getNext()
{
return next;
}
};
class Stack
{
private:
Node *head;
public:
Stack()
{
head = NULL;
}
void push(int n)
{
Node *current = new Node;
current -> setData(n);
current -> setNext(head);
head = current;
cout<<"\n\n Value Push "<<n;
}
void pop()
{
if(head == NULL)
{
cout<<"\n\n Stack is Empty...";
}
else
{
Node *ptr = head;
head = head -> getNext();
cout<<"\n\n Value Pop "<<ptr -> getData();
delete ptr;
}
}
void display()
{
if(head == NULL)
{
cout<<"\n\n Stack is Empty...";
}
else
{
cout<<"\n\n Stack Values:";
Node *ptr = head;
while(ptr != NULL)
{
cout<<" "<<ptr -> getData();
ptr = ptr -> getNext();
}
}
}
void search(int n)
{
if(head == NULL)
{
cout<<"\n\n Stack is Empty...";
}
else
{
int found=0;
Node *ptr = head;
while(ptr != NULL)
{
if(n == ptr -> getData())
{
cout<<"\n\n Search Value : "<<ptr -> getData();
found++;
}
ptr = ptr -> getNext();
}
if(found == 0)
{
cout<<"\n\n Value Can't Found...";
}
}
}
void update(int n)
{
if(head == NULL)
{
cout<<"\n\n Stack is Empty...";
}
else
{
int found=0;
Node *ptr = head;
while(ptr != NULL)
{
if(n == ptr -> getData())
{
cout<<"\n\n New Value : ";
cin>>n;
ptr -> setData(n);
cout<<"\n\n Value Updated Successfully...";
found++;
}
ptr = ptr -> getNext();
}
if(found == 0)
{
cout<<"\n\n Value Can't Found...";
}
}
}
};
main()
{
Stack s;
p:
system("cls");
int choice,n;
cout<<"\n\n 1. Push";
cout<<"\n\n 2. Pop";
cout<<"\n\n 3. Display";
cout<<"\n\n 4. Search";
cout<<"\n\n 5. Update";
cout<<"\n\n 6. Exit";
cout<<"\n\n\n Select One Option (1-6) : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\n\n Enter Value : ";
cin>>n;
s.push(n);
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
case 4:
cout<<"\n\n Enter Value : ";
cin>>n;
s.search(n);
break;
case 5:
cout<<"\n\n Enter Value : ";
cin>>n;
s.update(n);
break;
case 6:
exit(0);
default:
cout<<"\n\n\n Invalid Option...Please Try Again...";
}
getch();
goto p;
}

Queue With Array:

#include<iostream>
#include<conio.h>
using namespace std;
int i=-1,j=-1,array[10];
void Enqueue(int value)
{
if(i > 8)
{
cout<<"\n\n *** Queue is Full ***";
}
else
{
array[++i] = value;
cout<<"\n\n Enqueue "<<array[i];
}
}
void Dequeue()
{
if(i == -1 || j == 9)
{
cout<<"\n\n *** Queue is Empty ***";
}
else
{
cout<<"\n\n Dequeue "<<array[++j];
}
}
void Display()
{
if(i == -1 || j == 9)
{
cout<<"\n\n *** Queue is Empty ***";
}
else
{
cout<<"\n\n Array: ";
for(int a=j+1;a<=i;a++)
{
cout<<array[a]<<" ";
}
}
}
main()
{
Enqueue(10);
Enqueue(20);
Enqueue(30);
Enqueue(40);
Enqueue(50);
Dequeue();
Dequeue();
Dequeue();
Enqueue(60);
Display();
getch();
return 0;
}

Queue With Linked List:

#include<iostream>
#include<conio.h>
using namespace std;
class Node
{
public:
int data;
Node *next_add;
};
class Linked_List
{
private:
Node *Head;
Node *Tail;
public:
Linked_List()
{
Head = NULL;
Tail = NULL;
}
void Enqueue(int value)
{
Node *newNode = new Node;
newNode -> data = value;
newNode -> next_add = NULL;
if(Head == NULL)
{
Head = newNode;
Tail = newNode;
}
else
{
Tail -> next_add = newNode;
Tail = newNode;
}
cout<<"\n\n *** Node "<<value<<" Value Enqueue Successfully ***";
}
void Dequeue()
{
if(Head == NULL)
{
cout<<"\n\n *** Linked List is Empty ***";
}
else
{
Node *ptr = Head;
Head = Head -> next_add;
cout<<"\n\n *** Node "<<ptr -> data<<" Value Dequeue Successfully ***";
delete ptr;
}
}
void Display()
{
if(Head == NULL)
{
cout<<"\n\n *** Linked List is Empty ***";
}
else
{
cout<<"\n\n Queue Values: ";
Node *ptr = Head;
while(ptr != NULL)
{
cout<<ptr -> data<<" ";
ptr = ptr -> next_add;
}
}
}
~Linked_List()
{}
};
main()
{
Linked_List obj;
obj.Enqueue(5);
obj.Enqueue(2);
obj.Enqueue(12);
obj.Enqueue(1);
obj.Enqueue(14);
obj.Enqueue(18);
obj.Enqueue(22);
obj.Display();
getch();
return 0;
}

Priority Queue With Array:

#include<iostream>
#include<conio.h>
using namespace std;
int i=-1,j=-1,array[10];
void sort()
{
int temp;
for(int a=1;a<=i;a++)
{
for(int k=0;k<=i-1;k++)
{
if(array[k] < array[k+1])
{
temp = array[k];
array[k] = array[k+1];
array[k+1] = temp;
}
}
}
}
void Enqueue(int value)
{
if(i > 8)
{
cout<<"\n\n *** Queue is Full ***";
}
else
{
array[++i] = value;
cout<<"\n\n Enqueue "<<array[i];
sort();
}
}
void Dequeue()
{
if(i == -1 || j == 9)
{
cout<<"\n\n *** Queue is Empty ***";
}
else
{
cout<<"\n\n Dequeue "<<array[++j];
}
}
void Display()
{
if(i == -1 || j == 9)
{
cout<<"\n\n *** Queue is Empty ***";
}
else
{
cout<<"\n\n Array: ";
for(int a=j+1;a<=i;a++)
{
cout<<array[a]<<" ";
}
}
}
main()
{
Enqueue(12);
Enqueue(8);
Enqueue(3);
Enqueue(19);
Enqueue(1);
Display();
getch();
return 0;
}

Priority Queue With Linked List:

#include<iostream>
#include<conio.h>
using namespace std;
int count=0;
class Node
{
public:
int data;
Node *next_add;
};
class Linked_List
{
private:
Node *Head;
Node *Tail;
public:
Linked_List()
{
Head = NULL;
Tail = NULL;
}
void Enqueue(int value)
{
Node *newNode = new Node;
newNode -> data = value;
newNode -> next_add = NULL;
if(Head == NULL)
{
Head = newNode;
Tail = newNode;
}
else
{
Tail -> next_add = newNode;
Tail = newNode;
sort();
}
count++;
cout<<"\n\n *** Node "<<value<<" Value Enqueue Successfully ***";
}
void sort()
{
int temp;
for(int i=1;i<=count;i++)
{
Node *ptr = Head;
for(int j=1;j<=count;j++)
{
if(ptr -> data < ptr -> next_add -> data)
{
temp = ptr -> data;
ptr -> data = ptr -> next_add -> data;
ptr -> next_add -> data = temp;
}
ptr = ptr -> next_add;
}
}
}
void Dequeue()
{
if(Head == NULL)
{
cout<<"\n\n *** Linked List is Empty ***";
}
else
{
Node *ptr = Head;
Head = Head -> next_add;
cout<<"\n\n *** Node "<<ptr -> data<<" Value Dequeue Successfully ***";
delete ptr;
}
}
void Display()
{
if(Head == NULL)
{
cout<<"\n\n *** Linked List is Empty ***";
}
else
{
cout<<"\n\n Queue Values: ";
Node *ptr = Head;
while(ptr != NULL)
{
cout<<ptr -> data<<" ";
ptr = ptr -> next_add;
}
}
}
~Linked_List()
{}
};
main()
{
Linked_List obj;
obj.Enqueue(7);
obj.Enqueue(12);
obj.Enqueue(3);
obj.Enqueue(17);
obj.Enqueue(10);
obj.Display();
getch();
return 0;
}

Tree In-Order Traversal:

void InOrder(Node *ptr)
{
if(ptr != NULL)
{
InOrder(ptr -> Left);
cout<<ptr -> data<<" ";
InOrder(ptr -> Right);
}
}

Tree Pre-Order Traversal:

void PreOrder(Node *ptr)
{
if(ptr != NULL)
{
cout<<ptr -> data<<" ";
PreOrder(ptr -> Left);
PreOrder(ptr -> Right);
}
}

Tree Post-Order Traversal:

void PostOrder(Node *ptr)
{
if(ptr != NULL)
{
PostOrder(ptr -> Left);
PostOrder(ptr -> Right);
cout<<ptr -> data<<" ";
}
}

Bubble Sort Algorithm:

#include<iostream>
using namespace std;
main()
{
int array[5] = {12, 2, 5, 4, 1}, temp;
for(int i=1;i<=5;i++)
{
for(int j=0;j<=4-1;j++)
{
if(array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for(int i=0;i<=4;i++)
{
cout<<array[i]<<" ";
}
}

Insertion Sort Algorithm:

#include<iostream>
using namespace std;
main()
{
int array[5] = {2, 1, 12, 5, 9};
for(int i=1;i<=4;i++)
{
int j = i-1;
int key = array[i];
while(j >= 0 && key < array[j])
{
array[j+1] = array[j];
j--;
}
array[j+1] = key;
}
for(int i=0;i<=4;i++)
{
cout<<array[i]<<" ";
}
}

Selection Sort Algorithm:

#include<iostream>
using namespace std;
main()
{
int array[5] = {5, 8, 2, 4, 1};
for(int step=0;step<=4;step++)
{
int min = step,temp;
for(int i=min+1;i<=4;i++)
{
if(array[min] > array[i])
{
min = i;
}
}
temp = array[min];
array[min] = array[step];
array[step] = temp;
}
for(int i=0;i<=4;i++)
{
cout<<array[i]<<" ";
}
}

Merge Sort Algorithm:

#include<iostream>
#include<conio.h>
using namespace std;
void Merge(int *a,int low,int mid,int high)
{
int i,j,k,temp[high-low+1];
i = low;
j = mid+1;
k = 0;
while(i <= mid && j <= high)
{
if(a[i] < a[j])
{
temp[k] = a[i];
i++;
k++;
}
else
{
temp[k] = a[j];
j++;
k++;
}
}
while(i <= mid)
{
temp[k] = a[i];
i++;
k++;
}
while(j <= high)
{
temp[k] = a[j];
j++;
k++;
}
for(i = low;i <= high;i++)
{
a[i] = temp[i-low];
}
}
void MergeSort(int *a,int low,int high)
{
int mid;
if(low < high)
{
mid = (low+high)/2;
MergeSort(a,low,mid);
MergeSort(a,mid+1,high);
Merge(a,low,mid,high);
}
}
main()
{
int size;
cout<<" *** Merge Sort ***";
cout<<"\n\n Enter Elements You Want To Sorted: ";
cin>>size;
int array[size];
for(int i=0;i<size;i++)
{
cout<<"\n\n Enter Element "<<i+1<<": ";
cin>>array[i];
}
MergeSort(array,0,size-1);
cout<<"\n\n\n After Sorting: ";
for(int i=0;i<size;i++)
{
cout<<array[i]<<" ";
}
getch();
return 0;
}

Quick Sort Algorithm:

#include<iostream>
using namespace std;
int Sort(int *array,int low,int high)
{
int pivot = array[high];
int i = low-1,temp;
for(int j=low;j<high;j++)
{
if(array[j] <= pivot)
{
i++;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
i++;
temp = array[i];
array[i] = array[high];
array[high] = temp;
return i;
}
void QuickSort(int *array,int low,int high)
{
if(low < high)
{
int pivot = Sort(array,low,high);
QuickSort(array,low,pivot-1);
QuickSort(array,pivot+1,high);
}
}
main()
{
int array[5] = {5, 1, 12, 2, 4};
QuickSort(array,0,4);
for(int i=0;i<=4;i++)
{
cout<<array[i]<<" ";
}
}

Heap Sort Algorithm:

#include<iostream>
#include<conio.h>
using namespace std;
int a[10] = {12,10,4,1,7,14,22,21,8,6};
void maxHeap(int size)
{
int temp;
for(int j=(size/2)-1;j>=0;j--)
{
if(a[j] < a[j*2+1])
{
temp = a[j];
a[j] = a[j*2+1];
a[j*2+1] = temp;
}
if(a[j] < a[j*2+2] && j*2+2 < size)
{
temp = a[j];
a[j] = a[j*2+2];
a[j*2+2] = temp;
}
}
}
main()
{
int size = 10;
for(int i=1;i<=9;i++)
{
maxHeap(size);
a[0] = a[0] + a[size-1];
a[size-1] = a[0] - a[size-1];
a[0] = a[0] - a[size-1];
size--;
}
for(int i=0;i<=9;i++)
cout<<a[i]<<" ";
getch();
return 0;
}

Sequential Search Algorithm:

#include<iostream>
using namespace std;
main()
{
int array[5] = {2, 4, 0, 1, 9};
int k = 1, found = 0;
for(int x=0;x<=4;x++)
{
if(k == array[x])
{
cout<<"Element Found";
found++;
}
}
if(found == 0)
{
cout<<"Element Not Found";
}
}

Binary Search Algorithm:

#include<iostream>
using namespace std;
main()
{
int array[10] = {3,7,11,13,17,20,22,24,26,30};
int search = 17,low = 0,high = 9,mid,found=0;
while(low <= high)
{
mid = (low+high)/2;
if(search == array[mid])
{
found++;
cout<<"Value is Found";
break;
}
if(search < array[mid])
high = mid-1;
else
low = mid+1;
}
if(found == 0)
cout<<"Value is Not Found";
}

Post a Comment

Previous Post Next Post