Assignment Question: "PCH.H"
Assignment Question: "PCH.H"
Assignment Question: "PCH.H"
#include "pch.h"
#include <iostream>
#include<string>
#include<cstring>
using namespace std;
class Node
{
public:
int Value;
Node *Next;
Node() {
Value = 0;
Next = NULL;
}
Node(int val) {
Next = NULL;
Value = val;
}
};
class LinkedList
{
Node *head, *tail;
int count;
public:
LinkedList()
{
count = 0;
head = NULL;
tail = NULL;
}
~LinkedList() {
if (head)
{
Node *t = head;
head = head->Next;
while (head != NULL)
{
delete t;
t = head;
head = head->Next;
}
}
}
tail->Next = newNode;
tail = newNode;
count++;
}
void SelfDuplicate() {
Node* temp = head;
int counter = 0;
Node* currentNode = head;
while (counter < NodeIndex) {
currentNode = currentNode->Next;
counter++;
}
NewNode->Next = currentNode->Next;
currentNode->Next = NewNode;
count++;
return true;
}
void display()
{
if (!head)
{
cout << "Linklist is Empty";
return;
}
Node*p = head;
int index = 1;
while (p != NULL)
{
cout << index++<< " : [Value = " << p->Value <<
"] " <<endl;
p = p->Next;
}
}
};
int GetUserOption();
int main()
{
LinkedList List;
int val, pos;
while (1) {
int opt = GetUserOption();
if (opt == 0) exit(0);
switch (opt)
{
case 1:
cout << "Enter value for new node: ";
cin >> val;
List.InsertNode(val);
break;
case 2:
cout << "Enter value for new node: ";
cin >> val;
cout << "Enter node position";
cin >> pos;
List.InsertNodeAfter(val,pos);
break;
case 3:
List.display();
break;
case 4:
List.SelfDuplicate();
break;
default:
cout << "Invalid Choice" <<endl;
break;
}
}
int GetUserOption() {
int opt=0;
cout << endl << "--------------------------------------------" <<
endl;
cout << "1 : Insert a new node at the end of linked list" <<
endl;
cout << "2 : Insert a new node at specific position" << endl;
cout << "3 : print linked list" << endl;
cout << "4 : Self append linked list" << endl;
cout << "0 : Exit program" << endl;
#include "pch.h"
#include<iostream>
using namespace std;
bool c = false;
struct node
{
int data;
node* left;
node* right;
};
node* findMin(node*root)
{
while (root->left != NULL)
{
root = root->left;
}
return root;
}
return root;
}
// Node deletion
else
{
//case 1: Leaf Node
if (root->left == NULL && root->right == NULL)
{
delete root;
root = NULL;
return root;
}
//case 2: one child
else if (root->left == NULL)
{
struct node* temp = root;
root = root->right;
delete temp;
return root;
}
else if (root->right == NULL)
{
struct node* temp = root;
root = root->left;
delete temp;
return root;
}
//case 3: 2 child
else
{
struct node*temp = findMin(root->right);
root->data = temp->data;
root->right = Delete(root->right, temp->data);
}
}
return root;
int main()
{
node* root = NULL;
root = Insert(root, 20);
Insert(root, 15);
Insert(root, 25);
Insert(root, 18);
Insert(root, 10);
Insert(root, 16);
Insert(root, 19);
Insert(root, 17);
Delete(root, 15);
if (c)
{
cout << "Node Deleted" << endl;
cout << "\nAfter Deletion: " << endl;
cout << "Inorder: ";
inorder(root);
cout << endl;
}
else
cout << "Node Not Found" << endl;
return 0;
#include "pch.h"
#include<iostream>
using namespace std;
struct node
{
int data;
node* left;
node* right;
};
struct node* getNode(int data)
{
node* newNode = new node();
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
return root;
}
else
{
int lb = FindHeight(root->left);
int rb = FindHeight(root->right);
if (lb > rb)
return( lb+1);
else return( rb+1);
}
}
int main()
{
node* root = NULL;
root = Insert(root, 7);
Insert(root, 9);
Insert(root, 4);
Insert(root, 1);
Insert(root, 5);
cout << "Height of the tree is " << FindHeight(root) << endl;
cout << "Max. Depth of the tree is " << FindHeight(root) - 1;
return 0;
}
#include "pch.h"
#include<iostream>
//#include<bits/stdc++.h>
using namespace std;
return(node);
}
int main()
{
//**same trees are built as shown in example**
return 0;
LinkList Programs No =1
#include "pch.h"
#include <iostream>
#include<string>
#include<cstring>
using namespace std;
struct Node
{
string name;
int id, age;
float gpa;
Node *Next;
};
class list
{
Node *head,*tail;
public:
list()
{
head = NULL;
tail = NULL;
}
~list(){
if (head)
{
Node *t = head;
head = head->Next;
while (head != NULL)
{
delete t;
t = head;
head = head->Next;
}
}
}
bool createNode(string name, int id, int age, float gpa)
{
Node*temp = new Node();
temp->name = name;
temp->id = id;
temp->age = age;
temp->gpa = gpa;
temp->Next = NULL;
if (!head)
{
head = tail = temp;
temp = NULL;
}
else
{
tail->Next = temp; // tail->Next pointing the temp(Next
Node) or store the address of temp in tail->Next
tail = tail->Next; //tail's next become itself tail
temp = NULL;
return true;
}
return false;
}
bool insert_At_Beginning(string name, int id,int age,float gpa)
{
Node *temp = new Node;
temp->id = id;
temp->name = name;
temp->age = age;
temp->gpa = gpa;
temp->Next = NULL;
if (!head)
{
cout << "link list does Not Exist first create a node ";
return false;
}
else
{
temp->Next = head;
head = temp;
return true;
}
return false;
}
bool Insert_After(string name,int id,int age,float gpa,int Node_No)
{
if (!head)
{
cout << "LinkList does Not Exist";
}
else
{
int count = 1;
Node *t = head;
while (t != NULL)
{// for traversing a particular a node where new Node
insert
if (count == Node_No)
{
Node *temp = new Node;
temp->id = id;
temp->name = name;
temp->age = age;
temp->gpa = gpa;
temp->Next = NULL;
if (t != tail)
{
//Insert at end
tail->Next = temp;
tail = tail->Next;
return true;
}
}
count++;
t = t->Next;
}
}
return false;
}
bool delete_Node(int node_no)
{
int count = 1;
Node *current= NULL;
// delete first Node
if (head)
{
current = head;
}
if (node_no == 1)
{
head = head->Next;
current->Next = NULL;
delete current;
}
else
if (node_no > 1)
{
Node *previous = NULL;
while (current != NULL)
{ //delete Last Node
if (count == node_no && current == tail)
{
previous->Next = NULL;
tail = previous;
delete current;
}
//delete particular node
else if (count == node_no && current != tail)
{
previous->Next = current->Next;
current->Next = NULL;
delete current;
return true;
}
count++;
previous = current;
current = current->Next;
}
}
else
return false;
}
void display()
{
if (!head)
{
cout << "Linklist does Not Exist";
}
else
{
int count = 0;
Node*p=head;
while (p != NULL)
{
cout << "Node : " << ++count << endl;
cout << "---------------------------" << endl;;
cout << "Id = " << p->id << endl;
cout << "Name = " << p->name << endl;
cout << "Age = " << p->age << endl;
cout << "GPA = " << p->gpa << endl;
cout << "::::::::::::::::::::::::::::" << endl;;
p = p->Next;
}
}
};
int main()
{
list obj;
obj.createNode("Sameh ul haq", 1, 21, 3.62);
obj.createNode("fasi ul haq", 2, 34, 4);
obj.createNode("ALi", 3, 21, 2.3);
obj.display();
}
Doubly Linklist program NO 2
#include "pch.h"
#include <iostream>
#include<cstring>;
#include<string>
using namespace std;
struct Node
{
public:
int id;
string name;
Node *pre, *next;
};
class doubly
{
Node *head, *tail;
public:
doubly()
{
head = NULL;
tail = NULL;
if (!head)
{
head = temp;
tail = temp;
temp = NULL;
return false;
}
else
{
return true;
}
}
bool CreateAtHead(int id, string name)
{
if (!head)
{
head = temp;
tail = temp;
temp = NULL;
return false;
cout << "LinkList Not exist";
}
else
{
temp->next = head;
head->pre = temp;
head = temp;
temp = NULL;
return true;
}
}
bool createAtTail(int id, string name)
{
Node *temp = new Node;
temp->id = id;
temp->name = name;
temp->next = NULL;
temp->pre = NULL;
if (!head)
{
head = temp;
tail = temp;
temp = NULL;
return false;
cout << "LinkList Not exist";
}
else
{
temp->pre = tail;
tail->next = temp;
tail = temp;
temp = NULL;
return true;
}
}
bool InsertAfter(int id, string name, int NodeNO)
{
int n = 1;
Node *temp = new Node;
temp->id = id;
temp->name = name;
temp->next = NULL;
temp->pre = NULL;
Node *t = ptr->next;
ptr->next = temp;
temp->pre = ptr;
temp->next = t;
t->pre = temp;
temp = NULL;
return true;
break;
}
n++;
ptr = ptr->next;
}
}
bool DeleteAtHead()
{
if (head->next == NULL)
{
delete head;
head=NULL;
tail=NULL;
}
if (head)
{
Node *temp = head;
head = head->next;
head -> pre = NULL;
temp->next = NULL;
delete temp;
temp = NULL;
return true;
else
{
cout << "LinkList does Not Exist";
return false;
}
}
bool DeleteAtTail()
{
if (head->next == NULL)
{
delete head;
head = NULL;
tail = NULL;
}
if (head)
{
Node*temp = tail;
tail = tail->pre;
tail->next = NULL;
temp->next = NULL;
delete temp;
temp = NULL;
else
{
cout << "LinkList does Not Exist";
return false;
}
}
void display()
{
if (!head)
{
cout << "Linklist Does Not Exist";
}
else
{
int count = 0;
ptr = ptr->next;
}
}
};
int main()
{
doubly obj;
//1
obj.createNode(123, "Sami");
//2
obj.CreateAtHead(32, "Faizan");
//3
obj.createAtTail(34, "Fasi");
obj.createAtTail(23, "rizwan");
obj.display();
/*obj.DeleteAtHead();
cout << "After deletion";
obj.display();*/
//3
InsertInMiddle
#include "pch.h"
#include<conio.h>
#include <iostream>
using namespace std;
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
if (prev_node == NULL)
{
cout << "the given previous node cannot be NULL";
return;
}
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
new_node->prev = prev_node;
if (new_node->next != NULL)
new_node->next->prev = new_node;
}
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL)
{
new_node->prev = NULL;
*head_ref = new_node;
return;
}
last->next = new_node;
new_node->prev = last;
return;
}
void insertinmiddle(Node* head, int newdata)
{
int count = 0;
Node *newnode = new Node();
newnode->data = newdata;
Node *nodeptr = new Node();
nodeptr = head;
while (head->next != NULL)
{
count++;
head = head->next;
}
for (int i = 1; i < count / 2; i++)
{
nodeptr = nodeptr->next;
}
newnode->next = nodeptr->next;
nodeptr->next = newnode;
newnode->prev = nodeptr;
}
append(&head, 6);
push(&head, 7);
push(&head, 1);
append(&head, 4);
insertAfter(head->next, 8);
insertinmiddle(head, 67);
_getch();
}
Counts Node In tree.
#include "pch.h"
#include<iostream>
using namespace std;
int n = 1;
struct node
{
int data;
node* left;
node* right;
};
return root;
}
int CountNodes(node*root)
{
if (root == NULL)
return 0;
if (root->left != NULL)
{
n = n + 1;
n = CountNodes(root->left);
}
if (root->right != NULL)
{
n = n + 1;
n = CountNodes(root->right);
}
return n;
}
int main()
{
node* root = NULL;
root = Insert(root, 3);
Insert(root, 4);
Insert(root, 2);
Insert(root, 5);
Insert(root, 1);
cout << "Total No. of Nodes in the BST = " << CountNodes(root) <<
endl;
return 0;
}