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

Added Files for Linked List programs in C++ #192

Merged
merged 1 commit into from
Oct 18, 2023
Merged
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
66 changes: 66 additions & 0 deletions c++/CircularLL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
Node *Insert(int pos,int x,Node *head)
{
Node *t,*p;
if (pos ==0)
{
if(head == NULL){
t = new Node;
t->data = x;
t->next = t;
head=t;
}
else
{
Node *temp;
temp = new Node;
while(temp->next!=head) // for going towards the last node
temp=temp->next;
temp->next=t;
t->data=x;
t->next=head;
}
}
else if(pos >0 )
{
p = head;
for (int i = 0; i < pos-1 ; i++)
p=p->next;
t = new Node;
t->data = x;
t->next = p->next;
p->next = t;
}
return head;
}
void display(Node *head)
{
Node *p=head;
do
{
cout<<p->data<<" ";
p=p->next;
}while (p!=head);
}
int main()
{
int n,x;
Node *first=NULL;
cout<<"Enter the number of elements in the list: ";
cin>>n;
cout<<"Enter the elements of the list: ";
for (int i = 0; i < n; i++)
{
cin>>x;
first=Insert(i,x,first);
}
display(first);
return 0;
}
60 changes: 60 additions & 0 deletions c++/DoublyLL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
using namespace std;
class Node
{
public:
Node *previous;
int data;
Node *next;
};
Node *Insert(int pos,int x, Node *head)
{
Node *t,*p;
if(pos ==0)
{
t=new Node;
t->data=x;
t->previous=NULL;
t->next=head;
if(head !=NULL)
head->previous=t;
head=t;
}
else
{
p=head;
for(int i=0;i<pos-1;i++)
p=p->next;
t=new Node;
t->data=x;
t->previous=p;
t->next=p->next;
if(p->next)
p->next->previous=t;
p->next=t;
}
return head;
}
void display(Node *head)
{
Node *p=head;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
}
int main()
{
Node *first=NULL;
int n,x,pos;
cout<<"Enter the number of elements in the list: ";
cin>>n;
for(int i=0;i<n;i++)
{
cin>>x;
first=Insert(i,x,first);
}
display(first);
return 0;
}
98 changes: 98 additions & 0 deletions c++/LoopinLL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
Node *Insert(int pos,int x,Node *head)
{
Node *t,*p;
if(pos==0)
{
t=new Node;
t->data=x;
t->next=head;
head=t;
}
else if(pos >0)
{
p=head;
for(int i=0;i<pos-1 &&p;i++)
p=p->next;
if(p)
{
t=new Node;
t->data=x;
t->next=p->next;
p->next=t;
}
}
return head;
}
void Display(Node *head)
{
Node *p=head;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
}
bool Loop(Node *head)
{
bool var;
Node *p,*q;
p=head;
q=head;
// while(p && q && p!=q)
// {
// p=p->next;
// q=q->next;
// if(q!=NULL)
// q->next;
// else
// q=q;
// }
// if(p==q)
// var =true;
// else
// var = false;
do
{
p=p->next;
q=q->next;
q=q?q->next:q;
}
while(p && q && p!=q);
if(p==q)
var=true;
else
var=false;
return var;
}
int main()
{
Node *first=NULL;
int x;
int n;
cout<<" Enter the Number of elements you want to enter "<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>x;
first=Insert(i,x,first);
}
// Display(first);
// cout<<endl;
Node *t1,*t2;
// t1=first->next->next;
// t2=first->next->next->next->next;
// t2->next=t1;
if(Loop(first))
cout<<" linked List has a Loop "<<endl;
else
cout<<" linked list does not have a Loop "<<endl;
return 0;
}
70 changes: 70 additions & 0 deletions c++/SearchinLinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
}
*first;
void Create(int A[],int n)
{
Node *t,*last;
int i;
first=new Node;
first->data=A[0];
first->next=NULL;
last=first;
for(i=1;i<n;i++)
{
t=new Node;
t->data=A[i];
t->next=NULL;
last->next=t;
last=t;
}
}
Node* Search(Node *head,int key)
{
Node *p=head;
while(p!=NULL)
{
if(p->data==key)
{
cout<<"\nElement found";
return p;
}
p=p->next;
}
}
Node* RecSearch(Node *head,int key)
{
Node *p=head;
if(p==NULL)
return NULL;
if(p->data==key)
return p;
else
return RecSearch(p->next,key);
}
int main()
{
int n,i,a[100];
cout<<"Enter the size of the array : ";
cin>>n;
cout<<"Enter the elements of the array : ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
Create(a,n);
int key;
cout<<"\nEnter the element to be searched : ";
cin>>key;
Node *p=RecSearch(first,key);
if(p==NULL)
cout<<"\nElement not found";
else
cout<<"\nElement found ";
return 0;
}