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

Linked List

Uploaded by

subitabhdey132
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)
10 views

Linked List

Uploaded by

subitabhdey132
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/ 10

LINKED LIST:-

/******************************************************************************

Online C++ Compiler.


Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

using namespace std;


class node{
public:
int data ;
node * next;

node(int value)
{
data = value;
next = nullptr;
}
};

class singlylinkedlist{
private:
node* head;

public:
singlylinkedlist()
{
head = nullptr;
}
void insertAtBeginning(int value)
{
node * newnode = new node(value);
newnode->next = head;
head = newnode;
}
void insertAtEnd(int value)
{
node * newnode = new node(value);
if(head == nullptr)
{
cout<<"head i s empty"<<endl ;
head = newnode;

}
node * temp = head;
while(temp->next != nullptr)
{
temp= temp->next;
}
temp->next = newnode;
}
void display()
{
node * temp= head;
while(temp != nullptr)
{
cout<<"this node has"<<temp->data<<endl;
temp = temp->next;
}
}
void deleteNode(int key)
{
if(head == nullptr)
{
cout<<"sorry nothing to delete here"<<endl;
return;

}
if(head-> data == key)
{
node * temp = head;
head = head->next;
delete(temp);
cout<<" your head is gone now start acting like zombie now"<<endl;
return;
}
node * current = head;
node * previous = nullptr;

while(current->data != key && current!=nullptr)


{

previous = current;
current = current->next;
}
if(current == nullptr)
{
cout<< "cant find the key in the linked list"<<endl;
}
else
{
node * temp = current;
previous->next = current->next;
delete(temp);
cout<<"well done deleting that piece of shit"<<endl;
}

}
};

int main()
{
singlylinkedlist s;

s.insertAtBeginning(10);
s.insertAtEnd(20);
s.insertAtEnd(30);
s.display();
s.deleteNode(30);
s.display();
}

# BINARY SEARCH TREE

/******************************************************************************

Online C++ Compiler.


Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node* right;
Node* left;
Node(int value)
{
data = value;
right = nullptr;
left = nullptr;
}
};

class binarytree{

private:
Node * root;

Node * insert(Node * node, int value)


{
if(node == nullptr)
{
return new Node(value);
}
if(value < node->data)
{
node->left = insert(node->left,value);
}
else
{
node->right = insert(node->right, value);
}
return node;

bool search(Node * newnode, int key)


{
if(newnode == nullptr )
{
return false;
}

if(newnode->data== key)
{
return true;
}
else if(key<newnode->data)
{
return search(newnode->left, key);
}
else
{
return search(newnode->right, key);
}

Node* find_min(Node* node) {


while (node->left != nullptr) {
node = node->left;
}
return node;
}
void inOrder(Node* node) {
if (node != nullptr) {
inOrder(node->left);
cout << node->data << " ";
inOrder(node->right);
}
}

Node * delete_node(Node * node, int value)


{
if(node == nullptr)
{
return node;
}
if(value<node->data)
{
node->left = delete_node(node->left, value);
}
else if(value>node->data)
{
node->right = delete_node(node->right,value);
}
else{
if(node->left==nullptr)
{
Node * temp = node->right;
delete(node);
return temp;
}
else if(node->right == nullptr)
{
Node * temp = node->left;
delete(node);
return temp;

Node * temp = find_min(node->right);


node->data = temp->data;
node->right = delete_node(node->right,temp->data);

}
return node;
}

public:
binarytree()
{
root = nullptr;
}

void insert(int value)


{
root = insert(root, value);

bool find(int key)


{
return search(root,key);
}

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

void DeleteNode(int value) {


root = delete_node(root, value);
}

};

int main()
{
binarytree bst;
bst.insert(10);
bst.insert(20);
bst.insert(30);
bst.insert(14);
bst.insert(17);
bst.insert(12);

if(bst.find(17))
{
cout<< "key found in the node"<<endl;
}
bst.inOrder();
bst.DeleteNode(17);
bst.inOrder();
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy