0% found this document useful (0 votes)
72 views

Data Structure and Algorithms Lab Manual: Submitted To

The document contains code for implementing various data structures like stack, queue, doubly linked list and doubly linked list recursively using C++. It includes functions to perform operations like push, pop, enqueue, dequeue, insert, delete and traverse for each data structure. Main functions contain menus to demonstrate calling each operation and getting the expected output.

Uploaded by

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

Data Structure and Algorithms Lab Manual: Submitted To

The document contains code for implementing various data structures like stack, queue, doubly linked list and doubly linked list recursively using C++. It includes functions to perform operations like push, pop, enqueue, dequeue, insert, delete and traverse for each data structure. Main functions contain menus to demonstrate calling each operation and getting the expected output.

Uploaded by

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

Data Structure and Algorithms Lab Manual

Submitted To:

Sir Mudassar

Submitted By:
Muhammad Faizan Rasool

Roll No:
5549

Class:
BS SE 4th B
Stack

#include <iostream>
using namespace std;
int stack[10];
int a = 0;
void push(int x) {
if (stack[a] == NULL) {
stack[a] = x;
cout << "Push Succesfull!" << endl;}
else {
cout << "Stack Full!" << endl;}
a++;}
void pop() {
int b = a - 1;
cout << stack[b] << endl;
stack[b] = NULL;
cout << "Pop Succesfull" << endl;}
bool isEmpty(int arr[]) {
if (arr[0] == NULL) {
cout << "Stack is Empty!" << endl;
return true;}
else {
cout << "Stack is not Empty!" << endl;
return false;}
}
bool isFull(int arr[]) {
for (int i = 0; i < 10; i++) {
if (arr[i] != NULL) {
cout << "Stack is Full!" << endl;
return true;}
else {
cout << "Stack is not Full!" << endl;
return false;}
}
}
void Elements() {
int e = 0, i = 0;
while (stack[i] != NULL) {
cout << "Element on index " << i << " = ";
cout << stack[i] << endl;
e++;
i++;
}
cout << "Total number of elements in the stack = " << e << endl;
}
int main(){
int choice = 1, v;
while (choice != 6) {
cout << "***Select appropiate option to perform stack operation***" << endl;
cout << "1. Push Element" << endl;
cout << "2. Pop Element" << endl;
cout << "3. Check whether the stack is FULL" << endl;
cout << "4. Check whether the stack is EMPTY" << endl;
cout << "5. Check elements by index and total number of elements in the stack"
<< endl;
cout << "6. Exit" << endl;
cin >> choice;
if (choice == 1) {
system("cls");
cout << "Enter value to push: ";
cin >> v;
push(v);}
else if (choice == 2) {
system("cls");
cout << "Latest value in stack is: ";
pop();}
else if (choice == 3) {
system("cls");
isFull(stack);}
else if (choice == 4) {
system("cls");
isEmpty(stack);}
else if (choice == 5) {
system("cls");
Elements();}
}
system("pause");
return 0;
}
OUTPUT:

Queue

#include <iostream>
using namespace std;
int a[5];
int size=0,rear=0,front=0;
void enqueue(){
int count=0;
for (int i=rear;i<=front-1;i++){
count++;
}
if(count<=4)
{
if(front>4)
{
front=0;}
cout<<"Enter Value in Queue\n";
cin>>a[front];
front++;
size++;}
else
cout<<"The Queue Is Full"<<endl;}
void dequeue(){
if(front!=0){
cout<<a[rear]<<endl;
a[rear]=NULL;
size--;
rear++;}
else{
cout<< "The Queue Is Empty"<<endl;}}
void Is_Full(){
if(size==front){
cout<<"The Queue Is Full"<<endl;}
else
cout<<"The Queue Is Not Full"<<endl;}
void Is_Empty(){
if(front==0){
cout<<"The Queue Is Empty"<<endl;}
else
cout<<"The Queue Is Not Empty"<<endl;}
void display(){
cout<<"The Elements In Queue"<<endl;
for (int i=rear;i<=front-1;i++){
cout<<a[i]<<" - ";}
}
int main (){
int o=0;
while(o!=6){
cout<<"Press
\n1.Enqueue\n2.Dequeue\n3.isFull\n4.isEmpty\n5.Display\n6.exit\n";
cin>>o;
switch(o){
case 1:
system("cls");
enqueue();
break;
case 2:
system("cls");
dequeue();
break;
case 3:
system("cls");
Is_Full();
break;
case 4:
system("cls");
Is_Empty();
break;
case 5:
system("cls");
display();
break;
case 6:
cout<<"End\n";}
}
return 0;
system ("pause");
}

OUTPUT:
Doubly Link List

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node{
int info;
struct node *next;
struct node *prev;
}*start;
class double_llist{
public:
void create_list(int value);
void add_begin(int value);
void add_after(int value, int position);
void delete_element(int value);
void display_dlist();
double_llist() {
start = NULL; }
};
int main(){
int choice, element, position;
double_llist dl;
while (1)
{
cout<<endl<<"----------------------------"<<endl;
cout<<endl<<"Operations on Doubly linked list"<<endl;
cout<<endl<<"----------------------------"<<endl;
cout<<"1.Create Node"<<endl;
cout<<"2.Add at begining"<<endl;
cout<<"3.Add after position"<<endl;
cout<<"4.Delete"<<endl;
cout<<"5.Display"<<endl;
cout<<"6.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch ( choice ) {
case 1:
cout<<"Enter the element: ";
cin>>element;
dl.create_list(element);
cout<<endl;
break;
case 2:
cout<<"Enter the element: ";
cin>>element;
dl.add_begin(element);
cout<<endl;
break;
case 3:
cout<<"Enter the element: ";
cin>>element;
cout<<"Insert Element after postion: ";
cin>>position;
dl.add_after(element, position);
cout<<endl;
break;
case 4:
if (start == NULL){
cout<<"List empty,nothing to delete"<<endl;
break; }
cout<<"Enter the element for deletion: ";
cin>>element;
dl.delete_element(element);
cout<<endl;
break;
case 5:
dl.display_dlist();
cout<<endl;
break;
case 6:
exit(1);
default:
cout<<"Wrong choice"<<endl; }
}
return 0;
}
void double_llist::create_list(int value){
struct node *s, *temp;
temp = new(struct node);
temp->info = value;
temp->next = NULL;
if (start == NULL){
temp->prev = NULL;
start = temp; }
else {
s = start;
while (s->next != NULL)
s = s->next;
s->next = temp;
temp->prev = s; }
}
void double_llist::add_begin(int value){
if (start == NULL) {
cout<<"First Create the list."<<endl;
return; }
struct node *temp;
temp = new(struct node);
temp->prev = NULL;
temp->info = value;
temp->next = start;
start->prev = temp;
start = temp;
cout<<"Element Inserted"<<endl;}
void double_llist::add_after(int value, int pos){
if (start == NULL){
cout<<"First Create the list."<<endl;
return; }
struct node *tmp, *q;
int i;
q = start;
for (i = 0;i < pos - 1;i++) {
q = q->next;
if (q == NULL) {
cout<<"There are less than ";
cout<<pos<<" elements."<<endl;
return; }
}
tmp = new(struct node);
tmp->info = value;
if (q->next == NULL) {
q->next = tmp;
tmp->next = NULL;
tmp->prev = q; }
else {
tmp->next = q->next;
tmp->next->prev = tmp;
q->next = tmp;
tmp->prev = q;}
cout<<"Element Inserted"<<endl;}
void double_llist::delete_element(int value){
struct node *tmp, *q;
if (start->info == value) {
tmp = start;
start = start->next;
start->prev = NULL;
cout<<"Element Deleted"<<endl;
free(tmp);
return;}
q = start;
while (q->next->next != NULL){
if (q->next->info == value) {
tmp = q->next;
q->next = tmp->next;
tmp->next->prev = q;
cout<<"Element Deleted"<<endl;
free(tmp);
return; }
q = q->next;}
if (q->next->info == value) {
tmp = q->next;
free(tmp);
q->next = NULL;
cout<<"Element Deleted"<<endl;
return; }
cout<<"Element "<<value<<" not found"<<endl;}
void double_llist::display_dlist(){
struct node *q;
if (start == NULL) {
cout<<"List empty,nothing to display"<<endl;
return; }
q = start;
cout<<"The Doubly Link List is :"<<endl;
while (q != NULL) {
cout<<q->info<<" <-> ";
q = q->next; }
cout<<"NULL"<<endl;}

OUTPUT:

Doubly Link List Recursive


#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *next, *prev;
};
Node* getNode(int data){
Node* new_node = new Node;
new_node->data = data;
new_node->next = new_node->prev = NULL;
return new_node;}
void push(Node** head_ref, Node* new_node){
new_node->prev = NULL;
new_node->next = (*head_ref);
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;}
Node* Reverse(Node* node){
if (!node)
return NULL;
Node* temp = node->next;
node->next = node->prev;
node->prev = temp;
if (!node->prev)
return node;
return Reverse(node->prev);}
void printList(Node* head){
while (head != NULL) {
cout << head->data << " ";
head = head->next; }
}
int main(){
Node* head = NULL;
push(&head, getNode(2));
push(&head, getNode(4));
push(&head, getNode(8));
push(&head, getNode(10));
cout << "Original list: ";
printList(head);
// Reverse doubly linked list
head = Reverse(head);
cout << "\nReversed list: ";
printList(head);
system("pause");
return 0;}

OUTPUT:
Binary Tree
# include <iostream>
using namespace std;
struct node{
int data;
node *left;
node *right;
};
node* root = NULL;
class BST{
private:
node * root;
node* getnewnode(int key){
node* newNode = new node();
newNode->data = key;
newNode->left = newNode->right = NULL;
return newNode;}
public:
BST(BST &ob){
this->root = ob.root;}
void insertNode(int key){
this->root = insert(this->root, key);}
node* insert(node* root, int key) {
if (root == NULL){
root = getnewnode(key);}
else if (key < root->data){
cout << " At left after " << root->data;
root->left = insert(root->left, key);}
else{
cout << " At right after " << root->data;
root->right = insert(root->right, key);}
return root;
}
bool searchNode(int key) {
return this->Search(this->root, key);}
bool Search(node* root, int key) /// Search Function{
if (root == NULL)
return false;
else if (root->data == key){
cout << "Value Found";
return true;}
else if (key <= root->data){
if (left == NULL)
return false;

else
cout << root->data;
return Search(root->left, key);}
else{
if (right == NULL)
return false;

else {
cout << root->data;
return Search(root->right, key);}
}
}
bool DeleteNode(int key){
return this->Delete(this->root, key);}
node* findMaxNode(node* root){
if (root->right == NULL)
return root;
findMaxNode(root->right);}
node* Delete(node* root, int key){
if (root == NULL) return root;
else if (root->data > key) root->left = Delete(root->left, key);
else if (root->data < key) root->right = Delete(root->right, key);
else{
if (root->left == NULL && root->right == NULL){
delete root;
root = NULL;}
else if (root->right == NULL){
node* temp = root;
root = root->left;
delete temp;}
else if (root->left == NULL){
node* temp = root;
root = root->right;
delete temp;}
else{
node* maxNode = findMaxNode(root->left);//finding the
maximum in LEFT sub-tree
root->data = maxNode->data; //Overwriting the root node with
MAX-LEFT
root->left = Delete(root->left, maxNode->data);//deleted the
MAX-LEFT node}
return root;}}
void print(){
this->inorder(this->root);}
void inorder(node* root) {
if (root == NULL)
return;
if (root != NULL){
inorder(root->left);
cout << root->data << " ";
this->inorder(root->right);}
}
void print1(){
this->preorder(this->root);}
void preorder(node *root){
if (root != NULL){
cout << root->data << " ";
preorder(root->left);
preorder(root->right);}
}
void print2(){
this->postorder(this->root);}
void postorder(node* root){
if (root == NULL)
return;
postorder(root->left);
postorder(root->right);
cout << root->data << " ";}
int countN(){
return count(this->root);}
int count(node* root){
int c = 1;
if (root == NULL)
return 0;
else{
c += count(root->left);
c += count(root->right);
return c;}
}
int countL(){
return countleaf(this->root);}
int countleaf(node* root){
int TotalNode;
if (root == NULL)
return 0;
else
if (root->left == NULL && root->right == NULL)
return 1;
else
TotalNode = countleaf(root->left) + countleaf(root->right);
return TotalNode;}
/*int totalNode(node* root){
int TotalNode;
if(root == NULL)
return 0;
else
if(root->left == NULL && root->right == NULL)
return 1;
else
TotalNode=(totalNode(root->left) + totalNode(root->right)) + 1;
return TotalNode;}*/
bool isEmpty() const { return root == NULL; }
//int* Delete(node *,int key);
// void inorder(node *root);
//void preorder(node *);
//void postorder(node *);
//int totalNode(node* );
//int countleaf(node* );
BST(){
root = NULL;}
virtual ~BST(){
cout << "Destructor Called";
this->print();
system("pause");}
};
node* getnewnode(int key){
node* newNode = new node();
newNode->data = key;
newNode->left = newNode->right = NULL;
return newNode;}
node* insert(node* root, int key){
if (root == NULL){
root = getnewnode(key);}
else if (key < root->data){
cout << " At left after " << root->data;
root->left = insert(root->left, key);}
else{
cout << " At right after " << root->data;
root->right = insert(root->right, key);}
//cout<<root->data;
return root;}
bool Search(node* root, int key) /// Search Function{
if (root == NULL)
return false;
else if (root->data == key){
cout << "Value Found";
return true;}
else if (key <= root->data){
if (left == NULL)
return false;
else
cout << root->data;
return Search(root->left, key);}
else{
if (right == NULL)
return false;
else{
cout << root->data;
return Search(root->right, key);}
}
}
node* findMaxNode(node* root){
if (root->right == NULL) return root;
findMaxNode(root->right);}
{
if(root == NULL) return root;
else if(root->data > key) root->left = Delete(root->left, key);
else if(root->data < key) root->right = Delete(root->right, key);
else{
if(root->left == NULL && root->right == NULL){
delete root;
root = NULL;}
else if(root->right == NULL){
node* temp = root;
root = root->left;
delete temp;}
else if(root->left == NULL){
node* temp = root;
root = root->right;
delete temp;}
else{
node* maxNode = findMaxNode(root->left);//finding the maximum in LEFT sub-tree
root->data = maxNode->data; //Overwriting the root node with MAX-LEFT
root->left = Delete(root->left, maxNode->data);//deleted the MAX-LEFT node}
return root;}
}
void inorder(node* root){
if (root == NULL)
return;

if (root != NULL){
inorder(root->left);
cout << root->data << " ";
inorder(root->right);}
}
/*void inorder(node* root){
if (root==NULL)
return;
if (root!= NULL){
this->inorder(root->left);
cout<<root->data<<" ";
this->inorder(root->right);}
}
*/
/*void preorder(node *root){
if (root!= NULL){
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);}
}*/
/*void postorder(node* root){
if (root==NULL)
return;
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";}
int totalNode(node* root){
int TotalNode = 0;
if (root == NULL)
return 0;
else
if (root->left == NULL && root->right == NULL)
return 1;
else
TotalNode = totalNode(root->left) + totalNode(root->right) + 1;
return TotalNode;}
int countleaf(node* root){
int TotalNode;
if (root == NULL)
return 0;
else
if (root->left == NULL && root->right == NULL)
return 1;
else
TotalNode = countleaf(root->left) + countleaf(root->right);
return TotalNode;}

int main() {
int data,find,option,c;
BST bst;
BST bst1;
do{
cout << "1. Insertion" << endl;
cout << "2. Searching" << endl;
cout << "3. Deletion" << endl;
cout << "4. Inorder" << endl;
cout << "5. Preoder:" << endl;
cout << "6. Postorder:" << endl;
cout << "7. Total Nodes:" << endl;
cout << "8. countleaf:" << endl;
cout << "9. Copy Constructor" << endl;
cout << "0. To Exit" << endl;
cin >> option;
switch (option){
case 1:
cout << "Enter the Number" << endl;
cin >> data;
bst.insertNode(data);
break;
case 2:
cout << "Enter the Number You find" << endl;
cin >> find;
if (bst.searchNode(find))
cout << "You Number is found";
else
cout << "your Number is not Found";
break;
case 3:
cout << "Enter the Data" << endl;
cin >> data;
bst.DeleteNode(data);
break;
case 4:
bst.print();
break;
case 5:
bst.print1();
break;
case 6:
bst.print2();
break;
case 7:
//cout<<"Total Node "<<totalNode(root)<<endl;
c = bst.countN();
cout << c;
break;
case 8:
cout << "Total Leaf " << bst.countL() << endl;
break;
case 9:
cout << "Copy Constructor" << endl;
bst1 = bst;
break;}
system("pause");
system("cls");
} while (option != 0);
return 0;

OUTPUT:
Lab Task
#include<iostream>
#include <stdio.h>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int a[10];
int size=0,rear=0,front=0;
void insert(){
int count=0;
for (int i=rear;i<=front-1;i++){
count++;}
if(count<=9){
if(front>9){
front=0;}
cout<<"Enter Value in Queue\n";
cin>>a[front];
front++;
size++;}
else
cout<<"The Queue Is Full"<<endl;}
void del(){
if(front!=0){
cout<<a[rear]<<endl;
a[rear]=NULL;
size--;
rear++;}
else{
cout<< "The Queue Is Empty"<<endl;}
}
void linear_search( int element){
int i;
for (i = 0; i <10; i++) {
if (a[i] == element) {
cout << "\nLinear Search : Element : " << element << " : Found : Position : " << i + 1 <<
".\n";
break;}
}
if (i == 9){
cout << "\nSearch Element : " << element << " : Not Found \n";}
void swap(int *xp, int *yp){
int temp = *xp;
*xp = *yp;
*yp = temp;}
void selectionSort(int a[], int n){
int i, j, min_idx;
for (i = 0; i < n-1; i++)
{ min_idx = i;
for (j = i+1; j < n; j++)
if (a[j] < a[min_idx])
min_idx = j;
swap(&a[min_idx], &a[i]);}
}
void printArray(int arr[], int size){
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");}
int main(){
int o=0,v;
while(o!=5){
cout<<"Press \n1.Insert\n2.Delete\n3.Search\n4.Sort\n5.exit\n";
cin>>o;
switch(o){
case 1:
system("cls");
insert();
break;
case 2:
system("cls");
del();
break;
case 3:{
system("cls");
int i, element;
cout << "Simple C++ Linear Search Example - Array and Functions\n";
cout << "\nYour Data :";
for (i = 0; i < 10; i++) {
cout << "\t" << a[i];}
cout << "\nEnter Element to Search : ";
cin>>element;
linear_search( element);}
case 4:{
system("cls");
int n = sizeof(a)/sizeof(a[0]);
selectionSort(a, n);
printf("Sorted array: \n");
printArray(a, n);
break;}
case 5:
cout<<"End\n";}
}
system ("pause");
return 0;
}

OUTPUT:

You might also like