Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This code are related to linked list topic. #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions C++/LINKED LIST/createlinkedlist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include<iostream>
using namespace std;

class Node {
public:

int val;
Node* next;

Node (int data){
val = data;
next = NULL;
}
};

class linklist{
public:
Node * head;
linklist(){

head = NULL;
}

void insertattail(int value){
Node* new_node = new Node(value);
if (head == NULL){
head = new_node;// head ko hi new node maan liya
return;
}
Node * temp = head;
while(temp->next!=NULL){
temp = temp->next;
}
temp->next= new_node;

}
void display(){
Node *temp = head;
while(temp!=NULL){
cout<<temp->val<<"->";
temp = temp->next;

} cout<< "NULL"<<endl;
}
};
int main()
{


linklist ll;

ll.insertattail(1);
ll.insertattail(2);
ll.insertattail(3);
ll.insertattail(4);
ll.insertattail(5);
ll.insertattail(6);

ll.display();

return 0 ;
}
96 changes: 96 additions & 0 deletions C++/LINKED LIST/cyclicdetection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <iostream>
using namespace std;
#define INT_MIN (-2147483647 - 1)

struct Node
{
int val;
Node *next;

Node(int data)
{
val = data;
next = nullptr;
}
};

void insert(Node *&head, int value)
{
Node *newNode = new Node(value);
Node *temp = head;
if (!head)
{
head = newNode;
return;
}

while (temp->next != nullptr)
temp = temp->next;

temp->next = newNode;
}

int cyclePoint(Node *&head)
{
if (!head)
{
return INT_MIN;
}
Node *slow = head, *fast = head;

bool check = false;
while (fast != nullptr && fast->next != nullptr)
{
fast = fast->next->next;
slow = slow->next;
if (fast == slow)
{
check = true;
break;
}
}

if (!check)
return INT_MIN;

fast = head;
while (fast != slow)
{
fast = fast->next;
slow = slow->next;
}

return fast->val;
}

void display(Node *head)
{
while (head != nullptr)
{
cout << head->val << "->";
head = head->next;
}
cout << "NULL\n";
}
int main()
{
Node *ll = NULL;
// for (int i = 0; i < 10; i++)
// {
// insert(ll, i);
// }

insert(ll, 1);
insert(ll, 2);
insert(ll, 3);
insert(ll, 4);
insert(ll, 5);
insert(ll, 6);
insert(ll, 7);

ll->next->next->next->next->next->next->next = ll->next->next->next->next;
cout << cyclePoint(ll) << endl;

// display(ll);
return 0;
}
77 changes: 77 additions & 0 deletions C++/LINKED LIST/deleteduplicate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include<iostream>
using namespace std;

class Node {
public:

int val;
Node* next;

Node (int data){
val = data;
next = NULL;
}
};

class linklist{
public:
Node * head;
linklist(){

head = NULL;
}

void insertattail(int value){
Node* new_node = new Node(value);
if (head == NULL){
head = new_node;// head ko hi new node maan liya
return;
}
Node * temp = head;
while(temp->next!=NULL){
temp = temp->next;
}
temp->next= new_node;

}
void display(){
Node *temp = head;
while(temp!=NULL){
cout<<temp->val<<"->";
temp = temp->next;

} cout<< "NULL"<<endl;
}

};

void deleteduplicatenodes(Node* &head){
Node * curr_node = head;
while( curr_node ){ //ye traverse krr rhe hai lat tak
while ( curr_node->next && curr_node->val == curr_node->next->val){
//DELETE CURRENT NEXT
Node* temp = curr_node->next;
curr_node->next= curr_node->next->next;
free(temp);

}
// YE LOOP TAB LHATAM HOGA JAB VA;UE DIIFRENT AYEGI CURRENT KI AND USKE NEXT KI
// LL KHATAM
curr_node = curr_node->next; }

};

int main()
{
linklist ll;
ll.insertattail(1);
ll.insertattail(2);
ll.insertattail(2);
ll.insertattail(3);
ll.insertattail(3);
ll.insertattail(3);
ll.display();
deleteduplicatenodes(ll.head);
ll.display();
return 0;
}
82 changes: 82 additions & 0 deletions C++/LINKED LIST/findMIDDLE.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include<iostream>
using namespace std;

class Node{
public:

int val;
Node* next;

Node( int data){

val = data;
next = NULL;

}
};

class linklist{
public:

Node* head;

linklist(){
head = NULL;

}

void insertattail(int value){

Node* new_node = new Node (value);
if(head==NULL){
head = new_node;
}
Node* temp = head;
while(temp->next != NULL){
temp = temp->next;

}
temp->next = new_node;
new_node->next= NULL;


}

void display(){
Node* temp = head;
while (temp!= NULL){
cout<<temp->val<< "->";
temp = temp->next;

}
cout << "NULL"<< endl;
}
};
Node* middleelement(Node* &head){
Node* slow = head;
Node* fast = head;

//STOPPING CONDITITON IF EVEN AND ODD HONGYE TAB
while (fast && fast->next){
slow = slow->next;
fast = fast->next->next;

}
return slow;

}

int main()
{
linklist ll;
ll.insertattail(1);
ll.insertattail(2);
ll.insertattail(3);
ll.insertattail(4);
ll.insertattail(5);
ll.display();
Node* middlenode = middleelement(ll.head);
cout<<middlenode->val<<endl;

return 0;
}
Loading