Professional Documents
Culture Documents
LINKED LIST
LINKED LIST
LINKED LIST
/******************************************************************************
*******************************************************************************/
#include <iostream>
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;
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();
}
/******************************************************************************
*******************************************************************************/
#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;
if(newnode->data== key)
{
return true;
}
else if(key<newnode->data)
{
return search(newnode->left, key);
}
else
{
return search(newnode->right, key);
}
}
return node;
}
public:
binarytree()
{
root = nullptr;
}
void inOrder() {
inOrder(root);
cout << endl;
}
};
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();
}