Вы находитесь на странице: 1из 29

Assignment Question

#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;
}
}
}

void InsertNode(int val)


{
Node* newNode = new Node(val);
if (head == NULL)
{
head = tail = newNode;
return;
}

tail->Next = newNode;
tail = newNode;
count++;
}

void SelfDuplicate() {
Node* temp = head;

int totalNodes = count;


for (int i = 0; i < totalNodes; i++) {
InsertNode(temp->Value);
temp = temp->Next;
}
}

//1 based indexting


bool InsertNodeAfter( int val, int NodeIndex)
{
if (head == NULL)
{
cout << "LinkList is Empty";
return false;
}

//check if index is out of bound


if (NodeIndex > count || NodeIndex <= 0) {
cout << "Invalid Node Index";
return false;
}

Node* NewNode = new Node(val);

int counter = 0;
Node* currentNode = head;
while (counter < NodeIndex) {
currentNode = currentNode->Next;
counter++;
}

//check if current node is tail then update tail as well


if (currentNode == tail) {
tail = NewNode;
}

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;

cout << "Choose your option:";


cin >> opt;
return opt;
}

Deleting Node In trees.

#include "pch.h"
#include<iostream>
using namespace std;

bool c = false;

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;
}

void inorder(struct node* root)


{
if (root != NULL)
{
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
}

node* findMin(node*root)
{
while (root->left != NULL)
{
root = root->left;
}
return root;
}

struct node* Insert(struct node* root, int data)


{
if (root == NULL)
return getNode(data);

if (data < root->data)


root->left = Insert(root->left, data);
else if (data > root->data)
root->right = Insert(root->right, data);

return root;
}

bool Search(node*root, int value)


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

node* Delete(node* root, int value)


{
c = Search(root, value);
if (root == NULL)
return root;
else if (value < root->data)
{
root->left = Delete(root->left, value);
}
else if (value > root->data)
{
root->right = Delete(root->right, value);
}

// 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);

cout << "Before Deletion: " << endl;


cout << "Inorder: ";
inorder(root);
cout << endl << endl;

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;

Finding Height Of trees

#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;
}

void inorder(struct node* root)


{
if (root != NULL)
{
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
}

struct node* Insert(struct node* root, int data)


{
if (root == NULL)
return getNode(data);

if (data < root->data)


root->left = Insert(root->left, data);
else if (data > root->data)
root->right = Insert(root->right, data);

return root;
}

int FindHeight(node* root)


{
if (root == NULL)
return 0;

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 << "Inorder: ";


inorder(root);
cout << endl;

cout << "Height of the tree is " << FindHeight(root) << endl;
cout << "Max. Depth of the tree is " << FindHeight(root) - 1;

return 0;
}

Checking of trees structurally Identical or not

#include "pch.h"
#include<iostream>
//#include<bits/stdc++.h>
using namespace std;

// tree node is defined


class tree {
public:
int data;
tree *left;
tree *right;
};

bool IsSimilar(tree *root1, tree* root2) {


// both tree empty
if (root1 == NULL && root2 == NULL)
return true;
// one tree empty, another is not
if (root1 == NULL || root2 == NULL)
return false;

// above two are base conditions

//if both tree is not empty


// recursively check for left and right subtrees
return (IsSimilar(root1->left, root2->left) && IsSimilar(root1->right,
root2->right));
}

// creating new node


tree* newnode(int data)
{
tree* node = (tree*)malloc(sizeof(tree));
node->data = data;
node->left = NULL;
node->right = NULL;

return(node);
}

int main()
{
//**same trees are built as shown in example**

// building first tree


tree *root1 = newnode(2);
root1->left = newnode(7);
root1->right = newnode(5);
root1->right->right = newnode(9);
root1->right->right->left = newnode(4);
root1->left->left = newnode(2);
root1->left->right = newnode(6);
root1->left->right->left = newnode(5);
root1->left->right->right = newnode(11);
cout << "first tree is built,same as image1" << endl;

// building second tree


tree *root2 = newnode(8);
root2->left = newnode(3);
root2->right = newnode(10);
root2->right->right = newnode(14);
root2->right->right->left = newnode(13);
root2->left->left = newnode(1);
root2->left->right = newnode(6);
root2->left->right->left = newnode(4);
//root2->left->right->right = newnode(7);

cout << "second tree is built,same as image2" << endl;


if (IsSimilar(root1, root2))
cout << "Both are structurally similar" << endl;
else
cout << "Both aren't structurally similar" << endl;

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 After Node


//t->Next = afterNode;
temp->Next = t->Next;
t->Next = temp;
return true;
}
else
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();

//cout << "After Insertion" << endl;


//obj.insert_At_Beginning("sami",21,3,3.9);
//obj.display();

/*cout << "After Isertion" << endl;


obj.Insert_After("Ahmed",1,2,3.2,1);
obj.display();
std::cout << "Hello World!\n"; */

cout << "After deletion ";


obj.delete_Node(2);
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;

bool createNode(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;
}
else
{
return true;

}
}
bool CreateAtHead(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->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 *ptr = new Node;


ptr = head;

while (ptr != NULL)


{
if (ptr->next != NULL && n == NodeNO)
{

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;

Node *ptr = head;


while (ptr != NULL)
{
cout << " Node No : " << ++count << endl;

cout << "------------------" << endl;


cout << "ID =" << ptr->id << "\t";
cout << "Name = " << ptr->name << "\t" <<
endl;
cout << "::::::::::::::::::::" << endl <<
endl;

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();

/*cout << "After Insertion";

obj.InsertAfter(123, "Ali", 2);


obj.display();*/
//2

/*obj.DeleteAtHead();
cout << "After deletion";
obj.display();*/

//3

cout << "After Deletion";


obj.DeleteAtTail();
obj.display();
}

InsertInMiddle

#include "pch.h"

#include<conio.h>
#include <iostream>
using namespace std;

// A linked list node


class Node
{
public:
int data;
Node* next;
Node* prev;
};

void push(Node** head_ref, int new_data)


{

Node* new_node = new Node();


new_node->data = new_data;
new_node->next = (*head_ref);
new_node->prev = NULL;

if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;

(*head_ref) = new_node;
}

void insertAfter(Node* prev_node, int new_data)


{

if (prev_node == NULL)
{
cout << "the given previous node cannot be NULL";
return;
}

Node* new_node = new Node();

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;
}

void append(Node** head_ref, int new_data)


{

Node* new_node = new Node();

Node* last = *head_ref;

new_node->data = new_data;

new_node->next = NULL;

if (*head_ref == NULL)
{
new_node->prev = NULL;
*head_ref = new_node;
return;
}

while (last->next != NULL)


last = last->next;

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;
}

void printList(Node* node)


{
Node* last=NULL;
cout << "\nTraversal in forward direction \n";
while (node != NULL)
{
cout << " " << node->data << " ";
last = node;
node = node->next;
}

cout << "\nTraversal in reverse direction \n";


while (last != NULL)
{
cout << " " << last->data << " ";
last = last->prev;
}
}

/* Driver program to test above functions*/


int main()
{
/* Start with the empty list */
Node* head = NULL;

append(&head, 6);

push(&head, 7);

push(&head, 1);

append(&head, 4);

insertAfter(head->next, 8);

insertinmiddle(head, 67);

cout << "Created DLL is: ";


printList(head);

_getch();
}
Counts Node In tree.

#include "pch.h"
#include<iostream>
using namespace std;

int n = 1;

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;
}

struct node* Insert(struct node* root, int data)


{
if (root == NULL)
return getNode(data);

if (data < root->data)


root->left = Insert(root->left, data);
else if (data > root->data)
root->right = Insert(root->right, data);

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;
}

Вам также может понравиться