0% found this document useful (0 votes)
11 views64 pages

23HCS4103 DS1 Merged

Uploaded by

guptaabhinav211
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)
11 views64 pages

23HCS4103 DS1 Merged

Uploaded by

guptaabhinav211
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/ 64

PRACTICAL 1

DATA STRUCTURE

ABHINAV GUPTA
23HCS4103
SEMESTER: 3
Write a program to implement singly linked list (represented by head and tail) that supports the
following operations:

#include <iostream>
using namespace std;
class node{
private:
int elem;
node* next;
node(): next(NULL) {};
friend class sll;
};
class sll{
private:
node* head;
node*tail;
public:
sll(){
head=NULL;
tail = NULL;
}
~sll(){
node *p=NULL;
while(head!=NULL)
{
p=head;
head=head->next;
delete p;
}
}
bool empty(){
return head==tail;
}
int top(){
return head->elem;
}
int back(){
return tail->elem;
}
void addfrnt(int i){
node* ptr= new node;
ptr->elem=i;
if( head==NULL){
head=ptr;
tail = ptr;
}
else{
ptr->next=head;
head=ptr;
}
}
void display(){
node* ptr=head;
while(ptr!=NULL){
cout<<ptr->elem<<"->";
ptr=ptr->next;
}
cout<<endl;
}
void addtail(int i) {
node* temp = new node;
temp->elem = i;
temp->next = NULL;
if (tail != NULL) {
tail->next = temp;
tail = temp;
} else {
head = temp;
tail = temp;
}
}
void removefront(){
node*old= head;
head=old->next;
delete old;

}
void deletetail(){
node*ptr=head;
while(ptr->next!=tail){
ptr=ptr->next;
}
tail=ptr;
ptr=ptr->next;
delete ptr;
tail->next=NULL;
}
void insert( int pos, int i){
node* ptr=head;
int count=1;
while(count<pos-1){
ptr=ptr->next;
count++;
}
node*temp=new node;
temp->elem=i;
temp->next=ptr->next;
ptr->next=temp;
}
void deletepos(int pos){
node* ptr=head;
int count=1;
while(count<pos-1){
ptr=ptr->next;
count++;
}
node*temp = ptr->next;
ptr->next=temp->next;
delete temp;

}
void search(int i){
node*ptr=head;
int count=1;
while(ptr->elem!=i){
ptr=ptr->next;
count++;
}
cout<<count<<endl;
}
int count(){
int ct=0;
node* pt= head;
while(pt!=NULL){
ct++;
pt=pt->next;
}
return ct;
}
void middle(){
int ct=count();
node*ptr=head;
int count=1;
while(count<(ct/2+1)){
ptr=ptr->next;
count++;
}
cout<<ptr->elem<<endl;

}
void reverse(){
node* p,*p1,*p2;
p=head;
p1=p->next;
while(p1!=NULL){
p2=p1->next;
p1->next=p;
p=p1;
p1=p2;
}

head->next=NULL;
tail=head;
head=p;
return;

};

int main() {
string ans="Y";
cout<<"1. Add an element at front."<<endl;
cout<<"2. Add an element at last."<<endl;
cout<<"3. Add an element at a given index."<<endl;
cout<<"4. Delete an element from front."<<endl;
cout<<"5. Delete an element from last."<<endl;
cout<<"6. Delete an element at given index."<<endl;
cout<<"7. Search an element."<<endl;
cout<<"8. Reverse the list."<<endl;
cout<<"9. Return Middle element."<<endl;
cout<<"10. Display the list."<<endl;
sll A;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
switch(choice){
case 1:
n=0;
cout<<" Enter element to add at first: ";cin>>n;
A.addfrnt(n);
break;
case 2:
n=0;
cout<<" Enter element to add at last: ";cin>>n;
A.addtail(n);
break;
case 3:
ind=0;
n=0;
cout<<" Enter index: ";cin>>ind;
cout<<" Enter element to add: ";cin>>n;
A.insert(ind,n);
break;
case 4:
A.removefront();
cout<<"First element deleted."<<endl;
break;
case 5:
A.deletetail();
cout<<"Last element deleted."<<endl;
break;
case 6:
ind=0;
cout<<" Enter index: ";cin>>ind;
A.deletepos(ind);
cout<<" Element "<<ind<<" deleted."<<endl;
break;
case 7:
n=0;
cout<<" Enter element to search: ";cin>>n;
A.search(n);
break;
case 8:
A.reverse();
cout<<"List Reversed."<<endl;
break;
case 9:
cout<<" Middle Element is:";
A.middle();
break;
case 10:
A.display();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;

}
OUTPUTS:

1. insert an element x at the beginning of the singly linked list

2. Insert an element x at ith position in the singly linked list

3. Remove an element from the beginning of the singly linked list'


4. . Remove an element from the ith postion

5. delete an element from the end


6. search an element

7. insert an element at the end

8. reverse the linked list

9. finding the middle element of the list


10. display the linked list

-- THANK YOU --
PRACTICAL 2
DATA STRUCTURE

ABHINAV GUPTA
23HCS4103
SEMESTER :3RD
QUESTION : Write a program to implement a Sorted Singly Linked List that supports the following
functions:

#include <iostream>
using namespace std;
class node{
private:
int elem;
node* next;
node(): next(NULL) {};
friend class sll;
};
class sll{
private:
node* head;
node*tail;
public:
sll(){
head=NULL;
tail = NULL;
}
~sll(){
node *p=NULL;
while(head!=NULL)
{
p=head;
head=head->next;
delete p;
}
}
bool empty(){
return head==tail;
}
int top(){
return head->elem;
}
int back(){
return tail->elem;
}
void add(int i) {
node* ptr = new node;
ptr->elem = i;
if (head == NULL) {
head = ptr;
tail = ptr;
} else if (i <= head->elem) {
ptr->next = head;
head = ptr;
} else {
node* temp = head;
while (temp->next != NULL && temp->next->elem < i) {
temp = temp->next;
}
ptr->next = temp->next;
temp->next = ptr;
if (ptr->next == NULL) {
tail = ptr;
}
}
}
void display(){
node* ptr=head;
while(ptr!=NULL){
cout<<ptr->elem<<"->";
ptr=ptr->next;
}
cout<<endl;
}
void removefront(){
node*old= head;
head=old->next;
delete old;

}
void deletetail(){
node*ptr=head;
while(ptr->next!=tail){
ptr=ptr->next;
}
tail=ptr;
ptr=ptr->next;
delete ptr;
tail->next=NULL;
}
void deleteElement(int i) {
if (empty()) {
cout << "List is empty, nothing to delete" << endl;
return;
}
else if (head->elem == i) {
removefront();
return;
}
else{
node* ptr = head;
node* prev = NULL;
while (ptr != NULL && ptr->elem != i) {
prev = ptr;
ptr = ptr->next;
}
if (ptr == NULL) {
cout << "Element " << i << " not found in the list" << endl;
return;
}
else{
prev->next = ptr->next;
if (ptr == tail) {
tail = prev;
}
delete ptr;
cout << "Element " << i << " deleted from the list" << endl;
}
} }
void search(int i){
node*ptr=head;
int count=1;
while(ptr->elem!=i){
ptr=ptr->next;
count++;
}
cout<<count<<endl;
}
int count(){
int ct=0;
node* pt= head;
while(pt!=NULL){
ct++;
pt=pt->next;
}
return ct;
}
};
int main() {
string ans="Y";
cout<<"1. Add an element"<<endl;
cout<<"2. Delete an element from front."<<endl;
cout<<"3. Delete an element from last."<<endl;
cout<<"4. Delete an element at given index."<<endl;
cout<<"5. Search an element."<<endl;
cout<<"6. Display the list."<<endl;
sll A;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
switch(choice){
case 1:
n=0;
cout<<" Enter element to add: ";cin>>n;
A.add(n);
break;
case 2:
A.removefront();
cout<<"First element deleted."<<endl;
break;
case 3:
A.deletetail();
cout<<"Last element deleted."<<endl;
break;
case 4:
ind=0;
cout<<" Enter element: ";cin>>ind;
A.deleteElement(ind);
cout<<" Element "<<ind<<" deleted."<<endl;
break;
case 5:
n=0;
cout<<" Enter element to search: ";cin>>n;
A.search(n);
break;
case 6:
A.display();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;

1. Insert an element
2. Delete an element from the beginning

3 .Delete an element from the LAST

4. Delete a given element


5. Search an element

6. Display the list

== THANK YOU ==
DEEN DAYAL UPADHYAYA COLLEGE
BSC(H) Computer Science

Data structure
Assignment-III
Name : Abhinav Gupta
Roll no. : 23HCS4103
Ques: Write a program to implement doubly linked list as an ADT that
supports the following operations. List should be implemented using
two dummy nodes header and trailer.

// Online C++ compiler to run C++ program online


#include <iostream>
using namespace std;

class node{
int elem;
node* prev;
node* next;

friend class dll;


};
class dll{
private:
node* header;
node* trailer;
public:
dll(){
header= new node;
trailer= new node;
header->next=trailer;
trailer->prev=header;
}
bool empty(){
return header->next==trailer->prev;
}
int toop(){
return header->next->elem;
}
int back(){
return trailer->prev->elem;
}
void add(int i){
node * ptr= new node;
ptr->elem=i;
ptr->prev=header;
ptr->next=header->next;
header->next->prev=ptr;
header->next=ptr;
}

void display(){
cout<<"forward display"<<endl;
node * ptr = header->next;
while(ptr!=trailer){
cout<<ptr->elem<<" ";
ptr=ptr->next;
}
cout<<endl;
}
void b_display(){
cout<<"backward display"<<endl;
node * ptr = trailer->prev;
while(ptr!=header){
cout<<ptr->elem<<" ";
ptr=ptr->prev;
}
cout<<endl;
}
void addback(int i){
node * ptr= new node;
ptr->elem=i;
ptr->next=trailer;
ptr->prev=trailer->prev;
trailer->prev->next=ptr;
trailer->prev=ptr;

}
void removefront(){
if (empty()) {
cout << "List is empty, cannot remove from beginning!" << endl;
return;
}
node * ptr= header->next;
header->next= ptr->next;
ptr->next->prev=header;
delete ptr;
}
void removeback(){
if (empty()) {
cout << "List is empty, cannot remove from end!" << endl;
return;
}
node * ptr=trailer->prev;
trailer->prev= ptr->prev;
ptr->prev->next=trailer;
delete ptr;
}
int countNodes(){
node * temp = header->next;
int count = 0;
while(temp != trailer){
count++;
temp = temp->next;
}
return count;
}
void reverseList() {
if (empty() || header->next == trailer->prev) {
return;
}
node* current = header->next;
node* temp = NULL;
while (current != trailer) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
temp = header->next;
header->next = trailer->prev;
trailer->prev = temp;
header->next->prev = header;
trailer->prev->next = trailer;
}
};

int main() {
string ans="Y";
cout<<"1. Add an element at front."<<endl;
cout<<"2. Add an element at last."<<endl;
cout<<"3. Delete an element from front."<<endl;
cout<<"4. Delete an element from last."<<endl;
cout<<"5. Reverse the list."<<endl;
cout<<"6. Count the number of node"<<endl;
cout<<"7. Display the list."<<endl;
cout<<"8. Display the Reverse of list."<<endl;
dll A;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
int p;
switch(choice){
case 1:
n=0;
cout<<" Enter element to add at front: ";cin>>n;
A.add(n);
break;
case 2:
n=0;
cout<<" Enter element to add at last: ";cin>>n;
A.addback(n);
break;
case 3:
A.removefront();
cout<<"Front element deleted."<<endl;
break;
case 4:
A.removeback();
cout<<"Last element deleted."<<endl;
break;
case 5:
A.reverseList();
cout<<"List Reversed."<<endl;
break;
case 6:
p = A.countNodes();
cout<<endl<<"the number of nodes are "<<p<<endl;
break;
case 7:
A.display();
break;
case 8:
A.b_display();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;
}

Part i. Insert an element x at the beginning of the doubly linked list

Part ii. Insert an element x at the end of the doubly linked list

Part iii. Remove an element from the beginning of the doubly linked list

Part iv. Remove an element from the end of the doubly linked list
Part v. reverse the list

Part vi. traversing the list in forward/backward direction


Part vii. counting the number of nodes

Thank you
DEEN DAYAL UPADHYAYA COLLEGE
BSC(H) Computer Science

Data structure
Assignment-IV
Name : Abhinav Gupta
Roll no. : 23HCS4103
Ques: Write a program to implement circular linked list as an ADT
which supports the following operations
#include <iostream>

using namespace std;


class node{
int elem;
node* next;

friend class cll;


};
class cll{
private:
node* cursor;
public:
cll(){
cursor=NULL;
}
~cll(){
if (!empty())
{
removeaftercur();
}
}
bool empty(){
return cursor==NULL;
}
int back(){
return cursor->elem;
}
int front(){
return cursor->next->elem;
}
void display(){
if(empty()){
cout<<"list is empty";
return;
}
node* ptr= cursor->next;
while(ptr!=cursor){
cout<<ptr->elem<<" ";
ptr=ptr->next;
}
cout<<ptr->elem;
cout<<endl;
}
void addaftercursor(int i){
node * ptr= new node;
ptr->elem=i;
if (empty()){
ptr->next=ptr;
cursor=ptr;
}
else{
ptr->next=cursor->next;
cursor->next=ptr;
}
}
void addbeforecursor(int i){
node * ptr= new node;
ptr->elem=i;
if (empty()){
ptr->next=ptr;
cursor=ptr;
}
else{
node * ptr1=cursor->next;
while(ptr1->next!=cursor){
ptr1=ptr1->next;
}
ptr1->next=ptr;
ptr->next=cursor;
}

}
void advance(){
cursor=cursor->next;
}
void removeaftercur(){
if(empty()){
cout<<"list is empty"<<endl;
}
node * ptr=cursor->next;
cursor->next=ptr->next;
delete ptr;

}
void remove(int x){
if(empty()){
cout<<"list is empty"<<endl;
}
node * ptr=cursor->next;
while(ptr->next->elem!=x){
ptr=ptr->next;
}
node *temp=ptr->next;
ptr->next=temp->next;
delete temp;

}
void removecursor(){
if(empty()){
cout<<"list is empty"<<endl;
}
node * ptr=cursor->next;
while(ptr->next!=cursor){
ptr=ptr->next;
}
cursor=ptr;
ptr=cursor->next;
cursor->next=ptr->next;
delete ptr;
}
void removebeforecur(){
if(empty()){
cout<<"list is empty"<<endl;
}
node * ptr=cursor->next;
while(ptr->next->next!=cursor){
ptr=ptr->next;
}
node* temp= ptr->next;
ptr->next=cursor;
delete temp;
}
node* search(int x){
node * ptr=cursor->next;
while(ptr!=cursor){
if(ptr->elem==x){
return ptr;
}
ptr=ptr->next;
}
if(cursor->elem==x){
return cursor;
}
return NULL;
}
};

int main() {
string ans="Y";
cout<<"1. Add an element after cursor."<<endl;
cout<<"2. Add an element before cursor."<<endl;
cout<<"3. Delete a element X "<<endl;
cout<<"4. Search an element X."<<endl;
cout<<"5. Delete an element at cursor."<<endl;
cout<<"6. Display the list."<<endl;
cout<<"7. Remove element after cursor ."<<endl;
cout<<"8. Remove element before cursor."<<endl;
cll A;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
node *p;
switch(choice){
case 1:
n=0;
cout<<" Enter element to add after cursor: ";cin>>n;
A.addaftercursor(n);
break;
case 2:
n=0;
cout<<" Enter element to add before cursor: ";cin>>n;
A.addbeforecursor(n);
break;
case 3:
cout<<"enter an elemnt to delete.";cin>>n;
A.remove(n);
break;
case 4:
cout<<"enter an elemnt to search.";cin>>n;
p=A.search(n);
if (p == NULL) {
cout << "Element not found." << std::endl;
} else {
cout << "Element found at node with address: " << p <<
std::endl;
}
break;
case 5:
A.removecursor();
break;
case 6:
A.display();
break;
case 7:
A.removeaftercur();
break;
case 8:
A.removebeforecur();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;
}

i. Insert an element x in the list before/after the cursor

ii. Remove an element x from the list


iii. Search for an element x in the list and return its pointer

iv deleting an element at the cursor. Cursor should point to the next element.

v. traversing the list

vi. deleting the element before and after the cursor


Thank You Mam
DEEN DAYAL UPADHYAYA COLLEGE
BSC(H) Computer Science

Data structure
Assignment-V
Name : Abhinav Gupta
Roll no. : 23HCS4103
Implement Stack using arrays:
#include <iostream>
using namespace std;

class arrayStack{
private:
int *arr;
int top;
int capacity;

public:
arrayStack(int size){
capacity = size;
arr = new int[size];
top = -1;
}

~arrayStack(){
delete[] arr;
}

bool isEmpty(){
return top == -1;
}

int getSize(){
return capacity;
}

bool isFull(){
return top == capacity - 1;
}

void push(int value){


if(isFull()) {
cout << "Stack Overflow\n";
}
else{
arr[++top] = value;
}
}

int pop(){
if(isEmpty()){
std::cout << "Stack Underflow\n";
return -1;
}
return arr[top--];
}

int peek() {
if(isEmpty()){
cout << "Stack is Empty\n";
return -1;
}
return arr[top];
}

void display() {
if(isEmpty()){
cout << "Stack is empty.\n";
return;
}
for(int i = top; i >= 0; i--){
cout << arr[i] << " ";
}
cout << endl;
}
};

int main(){
int size;
cout << "Enter size of your Stack: "; cin >> size;
arrayStack s(size);

string ans="Y";
cout << "\nMenu: \n";
cout << "1. Push\n";
cout << "2. Pop\n";
cout << "3. Peek\n";
cout << "4. Display Stack\n";
cout << "5. Get Stack Size\n";
while(ans!="n" and ans!="N"){
int choice, value;
cout<<"Enter Your Choice: "; cin>>choice;
switch (choice) {
case 1:
cout << "Enter the value to Push: ";
cin >> value;
s.push(value);
break;

case 2:
value = s.pop();
if (value != -1)
cout << "Popped element: " << value << endl;
break;

case 3:
value = s.peek();
if (value != -1)
cout << "Front element: " << value << endl;
break;

case 4:
cout << "Stack contents: ";
s.display();
break;

case 5:
cout << "Current Stack size: " << s.getSize() << endl;
break;

default:
cout << "Invalid choice. Please try again.\n";
}
cout<<"Do you want to continue?(y/n): ";
cin>>ans;
cout<<endl;
}
arrayStack A(5);
A.push(4);
A.push(5);
A.push(12);
A.push(15);
A.push(3);
cout << "Top element is: " << A.peek();
cout << endl;
A.display();
A.push(90);
A.pop();
A.display();
cout << "Top element is: " << A.peek();

return 0;
}

Output:
Implement Stack using linked list. Use it to convert an infix
expression to postfix and evaluate the postfix expression
#include <iostream>
#include <cmath>
#include <cctype>
#include <string>
using namespace std;

class Node {
int data;
Node* next;
friend class Stack;
};

class Stack {
Node* top;

public:
Stack():top(nullptr){}

~Stack(){
while(top != nullptr){
Node* temp = top;
top = top->next;
delete temp;
}
}

void push(int value){


Node* newnode = new Node();
newnode->data = value;
newnode->next = top;
top = newnode;
}

int pop(){
if(isEmpty()){
cout << "Stack underflow!" << endl;
exit(1);
}
int value = top->data;
Node* temp = top;
top = top->next;
delete temp;
return value;
}
int peek(){
if(isEmpty()){
cout << "Stack is empty!" << endl;
exit(1);
}
return top->data;
}

bool isEmpty(){
return top == nullptr;
}
};

int precedence(char op){


if(op == '+' || op == '-') return 1;
if(op == '*' || op == '/') return 2;
if(op == '^') return 3;
return 0;
}

bool isOperator(char ch){


return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^';
}

string infixToPostfix(const string& infix){


Stack stack;
string postfix;
for(char ch : infix){
if(isdigit(ch)){
postfix += ch;
}
else if(ch == '('){
stack.push(ch);
}
else if(ch == ')'){
while(!stack.isEmpty() && stack.peek() != '('){
postfix += stack.pop();
}
stack.pop();
}
else if(isOperator(ch)){
while(!stack.isEmpty() && precedence(stack.peek()) >=
precedence(ch)){
postfix += stack.pop();
}
stack.push(ch);
}
}
while(!stack.isEmpty()){
postfix += stack.pop();
}
return postfix;
}

int evaluatePostfix(const string& postfix){


Stack stack;
for(char ch : postfix){
if(isdigit(ch)){
stack.push(ch - '0');
}
else if(isOperator(ch)){
int operand2 = stack.pop();
int operand1 = stack.pop();
int result;
switch(ch){
case '+': result = operand1 + operand2; break;
case '-': result = operand1 - operand2; break;
case '*': result = operand1 * operand2; break;
case '/': result = operand1 / operand2; break;
case '^': result = pow(operand1, operand2); break;
default:
cout << "Invalid operator!" << endl;
exit(1);
}
stack.push(result);
}
}
return stack.pop();
}

int main() {

string infix;
cout << "Enter an infix expression: ";
cin >> infix;

string postfix = infixToPostfix(infix);


cout << "Postfix expression: " << postfix << endl;

int result = evaluatePostfix(postfix);


cout << "The result of the postfix evaluation is: " << result << endl;

return 0;
}
DEEN DAYAL UPADHYAYA COLLEGE
BSC(H) Computer Science

Data structure
Assignment-VI
Name : Abhinav Gupta
Roll no. : 23HCS4103
1. Implement Queue using circular arrays
#include <iostream>
using namespace std;
class queue{
int *array;
int n,f,r;
int capacity;
public:
queue(int size){
array= new int[size];
capacity=size;
n=0;f=0;r=0;
}
void enqueue(int i){
if(n==capacity){
cout<<"list is full"<<endl;
return;
}
array[r]=i;
r=(r+1)%capacity;
n++;
}
bool empty(){
return (n==0);
}
int size(){
return n;
}
int front(){
if(empty()){
cout<<"empty"<<endl;
return 0;
}
return array[f];
}
int back(){
return array[r-1];
}
void display(){
int counter=0;
while(counter<(n-1)){
cout<<array[(f+counter)%capacity] <<" ";
counter++;
}
cout<<array[(f+counter)%capacity]<< " ";
}
void dequeue(){
if(empty()){
cout<<"Queue is empty"<<endl;
return ;
}
f=(f+1)%capacity;
n--;
}
};
int main() {
string ans="Y";
cout<<"1. Push an element."<<endl;
cout<<"2. Pop an element."<<endl;
cout<<"3. print front element."<<endl;
cout<<"4. print back element."<<endl;
cout<<"5. Size of Queue ."<<endl;
cout<<"6. Display Queue ."<<endl;
int p;
cout<<"Enter the capacity of Queue . "; cin>>p;
queue A(p);
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
switch(choice){
case 1:
n=0;
cout<<" Enter element : ";cin>>n;
A.enqueue(n);
break;
case 2:
A.dequeue();
break;
case 3:
n=A.front();
cout<<n<<endl;
break;
case 4:
n=A.back();
cout<<n<<endl;
break;
case 5:
n=A.size();
cout<<n<<endl;
break;
case 6:
A.display();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
}

Output:
2. implement Queues using linked list
#include <iostream>
using namespace std;
class node{
int elem;
node* next;

friend class cll;


};
class cll{
private:
node* cursor;
public:
cll(){
cursor=NULL;
}
bool empty(){
return cursor==NULL;
}
int back(){
return cursor->elem;
}
int front(){
return cursor->next->elem;
}
void display(){
node* ptr= cursor->next;
while(ptr!=cursor){
cout<<ptr->elem<<" ";
ptr=ptr->next;
}
cout<<ptr->elem;
cout<<endl;
}
void add(int i){
node * ptr= new node;
ptr->elem=i;
if (empty()){
ptr->next=ptr;
cursor=ptr;
}
else{
ptr->next=cursor->next;
cursor->next=ptr;
}
}
void advance(){
cursor=cursor->next;
}

void rmcur(){
if(empty()){
cout<<"list is empty"<<endl;
}
node * ptr=cursor->next;
cursor->next=ptr->next;
delete ptr;
}
friend class CLLQueue;
};
class CLLQueue:private cll{
int n=0;

public:
void enqueue(int e){
add(e);
advance();
n++;
}
void dequeue(){
if(n==0){
cout<<"list is empty";
return;
}
rmcur();
n--;
}
int front(){
return cll::front();
}
int back(){
return cll::back();
}
int size(){
return n;
}
bool empty(){
return (n==0);
}
void display(){
cll::display();
}
};

int main() {
string ans="Y";
cout<<"1. Push an element."<<endl;
cout<<"2. Pop an element."<<endl;
cout<<"3. print front element."<<endl;
cout<<"4. print back element."<<endl;
cout<<"5. Size of Queue ."<<endl;
cout<<"6. Display Queue ."<<endl;
CLLQueue A;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
switch(choice){
case 1:
n=0;
cout<<" Enter element : ";cin>>n;
A.enqueue(n);
break;
case 2:
A.dequeue();
break;
case 3:
n=A.front();
cout<<n<<endl;
break;
case 4:
n=A.back();
cout<<n<<endl;
break;
case 5:
n=A.size();
cout<<n<<endl;
break;
case 6:
A.display();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}

return 0;
}
Thank You Mam
DEEN DAYAL UPADHYAYA COLLEGE
BSC(H) Computer Science

Data structure
Assignment-VII
Name : Abhinav Gupta
Roll no. : 23HCS4103

#include <iostream>
using namespace std;

class node{
int elem;
node* prev;
node* next;

friend class dll;


};
class dll{
private:
node* header;
node* trailer;
public:
dll(){
header= new node;
trailer= new node;
header->next=trailer;
trailer->prev=header;
}
bool empty(){
return header->next==trailer->prev;
}
int toop(){
return header->next->elem;
}
int back(){
return trailer->prev->elem;
}
void add(int i){
node * ptr= new node;
ptr->elem=i;
ptr->prev=header;
ptr->next=header->next;
header->next->prev=ptr;
header->next=ptr;
}

void display(){
cout<<"forward display"<<endl;
node * ptr = header->next;
while(ptr!=trailer){
cout<<ptr->elem<<" ";
ptr=ptr->next;
}
cout<<endl;
}
void addback(int i){
node * ptr= new node;
ptr->elem=i;
ptr->next=trailer;
ptr->prev=trailer->prev;
trailer->prev->next=ptr;
trailer->prev=ptr;

}
void removefront(){
if (empty()) {
cout << "List is empty, cannot remove from beginning!" << endl;
return;
}
node * ptr= header->next;
header->next= ptr->next;
ptr->next->prev=header;
delete ptr;
}
void removeback(){
if (empty()) {
cout << "List is empty, cannot remove from end!" << endl;
return;
}
node * ptr=trailer->prev;
trailer->prev= ptr->prev;
ptr->prev->next=trailer;
delete ptr;
}
friend class dequeue;
};
class dequeue{
private :
int n=0;
dll l1;
public:
void addfront(int i){
l1.add(i);
n++;
}
void addback(int i){
l1.addback(i);
n++;
}
void rmfr(){
if(l1.empty()){
cout<<"list is empty"<<endl;
return;
}
l1.removefront();
n--;
}
void rmbk(){
if(l1.empty()){
cout<<"list is empty"<<endl;
return;
}
l1.removeback();
n--;
}
void display(){
l1.display();
}
int front(){
return l1.toop();
}
int back(){
return l1.back();
}
int size(){
return n;
}
};

int main() {
string ans="Y";
cout<<"1. Push an element at beggining."<<endl;
cout<<"2. Pop an element at beggining."<<endl;
cout<<"3. Push an element at end."<<endl;
cout<<"4. Pop an element at end."<<endl;
cout<<"5. print front element."<<endl;
cout<<"6. print back element."<<endl;
cout<<"7. Size of Queue ."<<endl;
cout<<"8. Display Queue ."<<endl;
dequeue A;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
switch(choice){
case 1:
n=0;
cout<<" Enter element : ";cin>>n;
A.addfront(n);
break;
case 2:
A.rmfr();
break;
case 3:
n=0;
cout<<" Enter element : ";cin>>n;
A.addback(n);
break;
case 4:
A.rmbk();
break;
case 5:
n=A.front();
cout<<n<<endl;
break;
case 6:
n=A.back();
cout<<n<<endl;
break;
case 7:
n=A.size();
cout<<n<<endl;
break;
case 8:
A.display();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;
}
Output
:
Thank you Mam
DEEN DAYAL UPADHYAYA COLLEGE
BSC(H) Computer Science

Data structure
Assignment-VIII
Name : Abhinav Gupta
Roll no. : 23HCS4103
#include <iostream>
#include <queue>
using namespace std;

template <class T>


class Node{
public:
T data;
Node<T>* left;
Node<T>* right;

Node(T d){
this->data=d;
this->left=NULL;
this->right=NULL;
}
};

template <class T>


class BST{

private:
void Insert(Node<T>* & root,T d){
if(root==NULL){ root=new Node<T>(d); return;}
else if(d<root->data)Insert(root->left,d);
else Insert(root->right,d);
}

Node<T>* Search(Node<T>* root,T d){


while(root!=NULL){
if(root->data==d)return root;
if(root->data>d)root=root->left;
else root=root->right;
}
return NULL;
}

void preOrder(Node<T>* root){


if(root==NULL)return;
cout<<root->data<<" "; //N
preOrder(root->left); //L
preOrder(root->right); //R
}

void inOrder(Node<T>* root){


if(root==NULL)return;
inOrder(root->left); //L
cout<<root->data<<" "; //N
inOrder(root->right); //R
}

void postOrder(Node<T>* root){


if(root==NULL)return;
postOrder(root->left); //L
postOrder(root->right); //R
cout<<root->data<<" "; //
}

int Count(Node<T>* root){


if(root==NULL)return 0;
if(root->left==NULL && root->right==NULL)return 1;
return (1+Count(root->left)+Count(root->right));
}

int CountLeafNode(Node<T>* root){


if(root==NULL)return 0;
if(root->left==NULL && root->right==NULL)return 1;
return (CountLeafNode(root->left)+CountLeafNode(root->right));
}

int CountNonLeafNode(Node<T>* root){


if(root==NULL)return 0;
if(root->left==NULL && root->right==NULL)return 0;
return (1+CountNonLeafNode(root->left)+CountNonLeafNode(root->right));
}

int Height(Node<T>* root){


if(root==NULL)return -1;
if(root->left==NULL && root->right==NULL)return 0;
return (max(Height(root->left),Height(root->right))+1);
}

bool CheckEqual(Node<T>* r1,Node<T>* r2){


if(r1==NULL && r2==NULL)return 1;
if((r1==NULL && r2!=NULL)||(r1!=NULL && r2==NULL))return 0;
if(r1->data==r2->data){
return (CheckEqual(r1->left,r2->left) && CheckEqual(r1->right,r2-
>right));
}
return 0;
}

bool IsBST(Node<T>* root,Node<T>* minNode,Node<T>* maxNode){


if(root==NULL)return 1;
if(minNode && minNode->data>=root->data) return 0;
if(maxNode && maxNode->data<=root->data)return 0;
return (IsBST(root->left,minNode,root) && IsBST(root-
>right,root,maxNode));
}

Node<T>* findMin(Node<T>* root){


while(root && root->left){
root=root->left;
}
return root;
}

Node<T>* DeleteValue(Node<T>*& root,T d){


if(root==NULL)return NULL;
if(d<root->data)root->left=DeleteValue(root->left,d);
else if(d>root->data)root->right=DeleteValue(root->right,d);
else{
if(root->left==NULL){
Node<T>* temp=root->right;
delete root;
return temp;
}
else if(root->right==NULL){
Node<T>* temp=root->left;
delete root;
return temp;
}

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

public:
Node<T>* root;
BST(){
root=NULL;
}

void insert(T d){


Insert(root,d);
}

Node<T>* search(T d){


return Search(root,d);
}

void displayPreOrder(){
preOrder(root); cout<<endl;
}

void displayInOrder(){
inOrder(root); cout<<endl;
}

void displayPostOrder(){
postOrder(root); cout<<endl;
}

void displayLevelByLevel(){
if(root==NULL)return;
queue<Node<T>*> q;
q.push(root);
while (!q.empty())
{
Node<T>* temp=q.front();
q.pop();
cout<<temp->data<<" ";
if(temp->left)q.push(temp->left);
if(temp->right)q.push(temp->right);
}
cout<<endl;
}

int countTotalNode(){
return Count(root);
}

int countLeafNode(){
return CountLeafNode(root);
}
int countNonLeafNode(){
return CountNonLeafNode(root);
}

int height(){
return Height(root);
}

bool checkEqual(BST<T> &t){


return CheckEqual(this->root,t.root);
}

bool isBST(){
return IsBST(root,NULL,NULL);
}

void deleteValue(T d){


if(root==NULL){
cout<<"Tree is empty\n";
return;
}
else this->root=DeleteValue(this->root,d);
}

};

int main(){
BST<int> bst1;

bst1.insert(15);
bst1.insert(13);
bst1.insert(17);
bst1.insert(12);
bst1.insert(14);
bst1.insert(16);
bst1.insert(18);

cout<<"InOrder Traversal: ";


bst1.displayInOrder();

cout<<"PreOrder Traversal: ";


bst1.displayPreOrder();

cout<<"PostOrder Traversal: ";

cout<<"Level-By-Level Traversal: ";


bst1.displayLevelByLevel();
int value;
cout<<"Enter the element to search: ";
cin>>value;
Node<int>* found=bst1.search(value);
if(found){
cout<<"Element "<<value<<" is found in BSt.\n";
}
else{
cout<<"Element "<<value<<" is not found in BST.\n";
}

cout<<"Total nodes: "<<bst1.countTotalNode()<<endl;

cout<<"Leaf Nodes: "<<bst1.countLeafNode()<<endl;

cout<<"Non-Leaf Nodes: "<<bst1.countNonLeafNode()<<endl;

cout<<"Height of BST: "<<bst1.height()<<endl;

cout<<"Is BSt: "<<(bst1.isBST()?"Yes":"No")<<endl;

bst1.deleteValue(17);
cout<<"InOrder Traversal after deleting 70: ";
bst1.displayInOrder();

BST<int> bst2;
bst2.insert(15);
bst2.insert(13);
bst2.insert(17);
bst2.insert(12);
bst2.insert(14);
bst2.insert(16);
bst2.insert(18);

cout<<"InOrder Traversal after deleting 70: ";


bst2.displayInOrder();

if(bst1.checkEqual(bst2)){
cout<<"The two BSTs are equal.\n";
}
else{
cout<<"The two BSTs are not equal\n";
}

return 0;
}

Output:

THANK YOU
DEEN DAYAL UPADHYAYA COLLEGE
BSC(H) Computer Science

Data structure
Assignment-IX
Name : Abhinav Gupta
Roll no. : 23HCS4103
Ques : Write a program to implement insert and search operation in
AVL trees.

Code:
#include <iostream>
using namespace std;

class Node{
public:
int data;
Node* left;
Node* right;
int height;

Node(int d){
this->data=d;
this->left=NULL;
this->right=NULL;
this->height=0;
}
};

class AVL_tree{
Node * root;

int Height(Node *root) {


if (root == NULL)
return -1;
return root->height;
}

int getBalance(Node *root) {


if (root == NULL)
return 0;
return Height(root->left) - Height(root->right);
}

Node* leftRotate(Node* x){


Node* y=x->right;
Node* temp=y->left;

y->left=x;
x->right=temp;

x->height=1+max(Height(x->left),Height(x->right));
y->height=1+max(Height(y->left),Height(y->right));

return y;
}

Node* rightRotate(Node* x){


Node* y=x->left;
Node* temp=y->right;

y->right=x;
x->left=temp;

x->height=1+max(Height(x->left),Height(x->right));
y->height=1+max(Height(y->left),Height(y->right));

return y;
}

Node* Insert(Node* root,int d){


if(root==NULL){
return new Node(d);
}
if(d<root->data)
root->left=Insert(root->left,d);
else if(d>root->data)
root->right=Insert(root->right,d);
else return root;

root->height=1+max(Height(root->left),Height(root->right));

int balance=getBalance(root);

//left left case


if (balance > 1 && d < root->left->data)
return rightRotate(root);
//right right case
if (balance < -1 && d > root->right->data)
return leftRotate(root);

//left right case


if (balance > 1 && d > root->left->data){
root->left = leftRotate(root->left);
return rightRotate(root);
}
//right left case
if (balance < -1 && d < root->right->data){
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
}

Node* Search(Node* root, int key) {


if (root == NULL || root->data == key)
return root;

if (key > root->data)


return Search(root->right, key);

return Search(root->left, key);


}

void inOrder(Node* root){


if(root==NULL)return;
inOrder(root->left); //L
cout<<root->data<<" "; //N
inOrder(root->right); //R
}

public:
AVL_tree(){
this->root=NULL;
}

void insert(int d){


this->root=Insert(this->root,d);
}

void displayInOrder(){
inOrder(this->root); cout<<endl;
}

Node* search(int key) {


return Search(this->root, key);
}

};

int main(){
string ans="Y";
cout<<"1. Add an element after cursor."<<endl;
cout<<"2. search an elemnet."<<endl;
cout<<"3. Display . "<<endl; ;
AVL_tree t1;
while(ans!="n" and ans!="N"){
int choice;
cout<<" Enter Choice: ";cin>>choice;
int n;
int ind;
Node *result;
switch(choice){
case 1:
n=0;
cout<<" Enter element to inert: ";cin>>n;
t1.insert(n);
break;
case 2:
n=0;
cout<<" Enter element to search: ";cin>>n;
result = t1.search(n);
if (result != NULL) {
cout << "Found " << n << " in the tree." << endl;
} else {
cout << n << " not found in the tree." << endl;
}
break;
case 3:
cout<<"Inorder display."<<endl;
t1.displayInOrder();
break;
default:
cout<<"Invalid Choice."<<endl;
break;
}
cout<<"Do you want to continue?(y/n): ";cin>>ans;
cout<<endl;
}
return 0;
}
Output:

THANK YOU

You might also like