From 15fd0809cdd02253689c4c73d2c7655567d222d5 Mon Sep 17 00:00:00 2001 From: asingh4451 <108614474+asingh4451@users.noreply.github.com> Date: Wed, 18 Oct 2023 16:15:03 +0530 Subject: [PATCH] Added Files for Linked List programs in C++ --- c++/CircularLL.cpp | 66 +++++++++++++++++++++++++ c++/DoublyLL.cpp | 60 +++++++++++++++++++++++ c++/LoopinLL.cpp | 98 ++++++++++++++++++++++++++++++++++++++ c++/SearchinLinkedList.cpp | 70 +++++++++++++++++++++++++++ 4 files changed, 294 insertions(+) create mode 100644 c++/CircularLL.cpp create mode 100644 c++/DoublyLL.cpp create mode 100644 c++/LoopinLL.cpp create mode 100644 c++/SearchinLinkedList.cpp diff --git a/c++/CircularLL.cpp b/c++/CircularLL.cpp new file mode 100644 index 0000000..bcf3109 --- /dev/null +++ b/c++/CircularLL.cpp @@ -0,0 +1,66 @@ +#include +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<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; +} \ No newline at end of file diff --git a/c++/DoublyLL.cpp b/c++/DoublyLL.cpp new file mode 100644 index 0000000..93849b0 --- /dev/null +++ b/c++/DoublyLL.cpp @@ -0,0 +1,60 @@ +#include +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;inext; + 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<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>x; + first=Insert(i,x,first); + } + display(first); +return 0; +} \ No newline at end of file diff --git a/c++/LoopinLL.cpp b/c++/LoopinLL.cpp new file mode 100644 index 0000000..f1b27b0 --- /dev/null +++ b/c++/LoopinLL.cpp @@ -0,0 +1,98 @@ +#include +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;inext; + 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<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 "<>n; +for(int i=0;i>x; + first=Insert(i,x,first); +} +// Display(first); +// cout<next->next; +// t2=first->next->next->next->next; +// t2->next=t1; +if(Loop(first)) +cout<<" linked List has a Loop "< +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;idata=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>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; +} \ No newline at end of file