From 691995246a7d50570f10097f99cdc36335e57909 Mon Sep 17 00:00:00 2001 From: RITIK DAGAR <47457948+Ritik2703@users.noreply.github.com> Date: Fri, 8 Oct 2021 16:33:38 +0530 Subject: [PATCH 1/2] linked list --- Linked List Algo/01. singlylinkedlist.cpp | 298 ++++++++++++++++ .../02. circular singly linked list.cpp | 314 +++++++++++++++++ Linked List Algo/03. doubly linked list.cpp | 310 +++++++++++++++++ Linked List Algo/04. circular linked list.cpp | 317 ++++++++++++++++++ Linked List Algo/05. header linked list.cpp | 93 +++++ ...ion and Subtraction on two polynomials.cpp | 215 ++++++++++++ 6 files changed, 1547 insertions(+) create mode 100644 Linked List Algo/01. singlylinkedlist.cpp create mode 100644 Linked List Algo/02. circular singly linked list.cpp create mode 100644 Linked List Algo/03. doubly linked list.cpp create mode 100644 Linked List Algo/04. circular linked list.cpp create mode 100644 Linked List Algo/05. header linked list.cpp create mode 100644 Linked List Algo/06. Addition and Subtraction on two polynomials.cpp diff --git a/Linked List Algo/01. singlylinkedlist.cpp b/Linked List Algo/01. singlylinkedlist.cpp new file mode 100644 index 00000000..2bd4196d --- /dev/null +++ b/Linked List Algo/01. singlylinkedlist.cpp @@ -0,0 +1,298 @@ + +//// happy coding //// +//// chant hare krishna ///// +//// rk dagar /////// + +#include +using namespace std; + +class node{ + public: + int data; + node *next; +}; + +node *start = NULL; +node *createll(node *); +node *display(node *); +node *insertbeg(node *); +node *insertend(node *); +node *insertbefore(node *); +node *insertafter(node *); +node *deletebeg(node *); +node *deleteend(node *); +node *deletenode(node *); +node *deleteafter(node *); +node *deletelist(node *); +node *sortll(node *); +int main() +{ + int option; + do{ + cout<<"\n\n *******MAIN MENU*******"; + cout<<"\n 1: Create a list "; + cout<<"\n 2: Display the list "; + cout<<"\n 3: Add a node at the begining"; + cout<<"\n 4: Add a node at the end"; + cout<<"\n 5: Add a node before a given node"; + cout<<"\n 6: Add a node after a given node"; + cout<<"\n 7: delete a node from the begining"; + cout<<"\n 8: delete a node from the end"; + cout<<"\n 9: delete a given node"; + cout<<"\n 10: delete a node after given node"; + cout<<"\n 11: delete entire list"; + cout<<"\n 12: sort the list"; + cout<<"\n 13: exit\n"; + cin>>option; + switch(option) + { + case 1: + start= createll(start); + cout<<"\n linked list created"; + break; + case 2: + start= display(start); + break; + case 3: + start= insertbeg(start); + break; + case 4: + start= insertend(start); + break; + case 5: + start= insertbefore(start); + break; + case 6: + start= insertafter(start); + break; + case 7: + start= deletebeg(start); + break; + case 8: + start= deleteend(start); + break; + case 9: + start= deletenode(start); + break; + case 10: + start= deleteafter(start); + break; + case 11: + start= deletelist(start); + cout<<"\n linked list deleted"; + break; + case 12: + start= sortll(start); + break; + + } + }while(option!=13); + + return 0; +} + +node* createll(node *start) +{ + node* nn,*p; + int num; + cout<<"\nenter -1 to end\n"; + cin>>num; + while(num!=-1) + { + nn=new node(); + nn->data=num; + nn->next=NULL; + if(start==NULL) + { + start=nn; + } + else + { + p=start; + while(p->next!=NULL) + p=p->next; + p->next=nn; + } + cin>>num; + } + return start; +} +node *display(node* start) +{ + node *t=start; + while(t!=NULL) + { + cout<data<<" -> "; + t=t->next; + } + cout<<"NULL\n"; + return start; +} + +node *insertbeg(node *start) +{ + node *nn= new node(); + int num; + cin>>num; + nn->data=num; + nn->next=start; + start=nn; + + return start; +} + +node *insertend(node *start) +{ + node *nn=new node(); + node *p=start; + int num; + cin>>num; + nn->data=num; + nn->next=NULL; + while(p->next!=NULL) + { + p=p->next; + } + p->next=nn; + + + return start; + + +} +node *insertbefore(node *start) +{ + node *nn= new node(); + node *p=start; + node *preptr; + int num,val; + cin>>num>>val; + nn->data=num; + while(p->data!=val) + { + preptr=p; + p=p->next; + } + preptr->next=nn; + nn->next=p; + + return start; + +} + +node *insertafter(node *start) +{ + node *nn=new node(); + node *p= start,*pre=start; + int num,val; + cin>>num>>val; + nn->data=num; + while(pre->data!=val) + { + pre= p; + p=p->next; + } +nn->next=p; +pre->next=nn; + +return start; +} + +node *deletebeg(node *start) +{ + node *nn; + nn= start; + start=nn->next; + free(nn); + + return start; +} + +node *deleteend(node *start) +{ + node *nn=start,*p; + while(nn->next!=NULL) + { + p=nn; + nn=nn->next; + } + p->next=NULL; + free(nn); + + return start; +} + +node *deletenode(node *start) +{ + int val; + cin>>val; + node *p=start,*pre; + while(p->data==val) + { + start= deletebeg(start); + return start; +} +while(p->data!=val) +{ + pre=p; + p=p->next; +} +pre->next=p->next; +free(p); + +return start; + +} + +node *deleteafter(node *start) +{ + node *nn,*p=start; + nn=start; + int num; + cin>>num; + while(p->data!=num) + { + p=nn; + nn=nn->next; + } + p->next=nn->next; + free(nn); + + return start; +} + +node *deletelist(node *start) +{ + node *n=start; + while(n!=NULL) + { + start= deletebeg(start); + n=start; + } + + return start; +} + +node *sortll(node *start) +{ + node *p1,*p2; + int t; + p1=start; + while(p1->next!=NULL) + { + p2=p1->next; + while(p2!=NULL) + { + if(p1->data>p2->data) + { + t=p1->data; + p1->data=p2->data; + p2->data=t; + } + p2=p2->next; + } + p1=p1->next; + } + return start; +} + diff --git a/Linked List Algo/02. circular singly linked list.cpp b/Linked List Algo/02. circular singly linked list.cpp new file mode 100644 index 00000000..087b8b82 --- /dev/null +++ b/Linked List Algo/02. circular singly linked list.cpp @@ -0,0 +1,314 @@ +/* + hare krishna hare krishna krishna krishna hare hare + hare rama hare rama rama rama hare +*/ + +//// happy coding //// +//// chant hare krishna ///// +//// ritik dagar /////// + +#include +using namespace std; + +class node{ + public: + int data; + node *next; +}; + +node *start = NULL; +node *createll(node *); +node *display(node *); +node *insertbeg(node *); +node *insertend(node *); +node *insertbefore(node *); +node *insertafter(node *); +node *deletebeg(node *); +node *deleteend(node *); +node *deletenode(node *); +node *deleteafter(node *); +node *deletelist(node *); +node *sortll(node *); +int main() +{ + int option; + cout<<"hare krishna hare krishna\nkrishna krishna hare hare\nhare rama hare rama\nrama rama hare hare\n"; + cout<<"implementation of circular linked list\n"; + do{ + cout<<"\n\n *******MAIN MENU*******"; + cout<<"\n 1: Create a list "; + cout<<"\n 2: Display the list "; + cout<<"\n 3: Add a node at the begining"; + cout<<"\n 4: Add a node at the end"; + cout<<"\n 5: Add a node before a given node"; + cout<<"\n 6: Add a node after a given node"; + cout<<"\n 7: delete a node from the begining"; + cout<<"\n 8: delete a node from the end"; + cout<<"\n 9: delete a given node"; + cout<<"\n 10: delete a node after given node"; + cout<<"\n 11: delete entire list"; + cout<<"\n 12: sort the list"; + cout<<"\n 13: exit\n"; + cin>>option; + switch(option) + { + case 1: + start= createll(start); + cout<<"\ncircular linked list created"; + break; + case 2: + start= display(start); + break; + case 3: + start= insertbeg(start); + break; + case 4: + start= insertend(start); + break; + case 5: + start= insertbefore(start); + break; + case 6: + start= insertafter(start); + break; + case 7: + start= deletebeg(start); + break; + case 8: + start= deleteend(start); + break; + case 9: + start= deletenode(start); + break; + case 10: + start= deleteafter(start); + break; + case 11: + start= deletelist(start); + cout<<"\ncircular linked list deleted"; + break; + case 12: + start= sortll(start); + break; + + } + }while(option!=13); + + return 0; +} + +node* createll(node *start) +{ + node* nn,*p; + int num; + cout<<"\nenter -1 to end\n"; + cin>>num; + while(num!=-1) + { + nn=new node(); + nn->data=num; + if(start==NULL) + { + nn->next=nn; + start=nn; + } + else + { + p=start; + while(p->next!=start) + p=p->next; + p->next=nn; + nn->next=start; + } + cin>>num; + } + return start; +} +node *display(node* start) +{ + node *t=start; + while(t->next!=start) + { + cout<data<<" -> "; + t=t->next; + } + cout<data<<"\n"; + return start; +} + +node *insertbeg(node *start) +{ + node *p,*nn= new node(); + int num; + cin>>num; + p=start; + while(p->next!=start) + p=p->next; + p->next=nn; + nn->data=num; + nn->next=start; + start=nn; + + return start; +} + +node *insertend(node *start) +{ + node *nn=new node(); + node *p=start; + int num; + cin>>num; + nn->data=num; + nn->next=start; + while(p->next!=start) + { + p=p->next; + } + p->next=nn; + + + return start; + + +} +node *insertbefore(node *start) +{ + node *nn= new node(); + node *p=start; + node *preptr; + int num,val; + cin>>num>>val; + nn->data=num; + while(p->data!=val) + { + preptr=p; + p=p->next; + } + preptr->next=nn; + nn->next=p; + + return start; + +} + +node *insertafter(node *start) +{ + node *nn=new node(); + node *p= start,*pre=start; + int num,val; + cin>>num>>val; + nn->data=num; + while(pre->data!=val) + { + pre= p; + p=p->next; + } +nn->next=p; +pre->next=nn; + +return start; +} + +node *deletebeg(node *start) +{ + node *nn; + nn= start; + while(nn->next!=start) + nn= nn->next; + nn->next=start->next; + free(start); + start=nn->next; + + + return start; +} + +node *deleteend(node *start) +{ + node *nn=start,*p; + while(nn->next!=start) + { + p=nn; + nn=nn->next; + } + p->next=nn->next; + free(nn); + + return start; +} + +node *deletenode(node *start) +{ + int val; + cin>>val; + node *p=start,*pre; + while(p->data==val) + { + start= deletebeg(start); + return start; +} +while(p->data!=val) +{ + pre=p; + p=p->next; +} +pre->next=p->next; +free(p); + +return start; + +} + +node *deleteafter(node *start) +{ + node *nn,*p; + nn=start; + p=nn; + int num; + cin>>num; + while(p->data!=num) + { + p=nn; + nn=nn->next; + } + p->next=nn->next; + if(nn==start) + start=p->next; + free(nn); + + return start; +} + +node *deletelist(node *start) +{ + node *n=start; + while(n->next!=start) + { + start= deleteend(start); + } + free(start); + return start; +} + +node *sortll(node *start) +{ + node *p1,*p2; + int t; + p1=start; + while(p1->next!=start) + { + p2=p1->next; + while(p2!=start) + { + if(p1->data>p2->data) + { + t=p1->data; + p1->data=p2->data; + p2->data=t; + } + p2=p2->next; + } + p1=p1->next; + } + return start; +} diff --git a/Linked List Algo/03. doubly linked list.cpp b/Linked List Algo/03. doubly linked list.cpp new file mode 100644 index 00000000..d4501d08 --- /dev/null +++ b/Linked List Algo/03. doubly linked list.cpp @@ -0,0 +1,310 @@ +/* + hare krishna hare krishna krishna krishna hare hare + hare rama hare rama rama rama hare +*/ + +//// happy coding //// +//// chant hare krishna ///// +//// ritik dagar /////// + +#include +using namespace std; + +class node{ + public: + int data; + node *prev; + node *next; + node() + { + data=0; + prev=next=NULL; + } + node(int n) + { + data=n; + prev=NULL; + next=NULL; + } +}; +int num,val; +node *start = NULL; +node *createll(node *); +node *display(node *); +node *insertbeg(node *); +node *insertend(node *); +node *insertbefore(node *); +node *insertafter(node *); +node *deletebeg(node *); +node *deleteend(node *); +node *deletenode(node *); +node *deleteafter(node *); +node *deletebefore(node *); +node *deletelist(node *); +node *sortll(node *); +int main() +{ + int option; + cout<<"hare krishna hare krishna\nkrishna krishna hare hare\nhare rama hare rama\nrama rama hare hare\n"; + cout<<"representation of doubly linked list\n"; + do{ + cout<<"\n\n *******MAIN MENU*******"; + cout<<"\n 1: Create a list "; + cout<<"\n 2: Display the list "; + cout<<"\n 3: Add a node at the begining"; + cout<<"\n 4: Add a node at the end"; + cout<<"\n 5: Add a node before a given node"; + cout<<"\n 6: Add a node after a given node"; + cout<<"\n 7: delete a node from the begining"; + cout<<"\n 8: delete a node from the end"; + cout<<"\n 9: delete a given node"; + cout<<"\n 10: delete a node after given node"; + cout<<"\n 11: delete a node before given node"; + cout<<"\n 12: delete entire list"; + cout<<"\n 13: sort the list"; + cout<<"\n 14: exit\n"; + cin>>option; + switch(option) + { + case 1: + start= createll(start); + cout<<"\n linked list created"; + break; + case 2: + start= display(start); + break; + case 3: + start= insertbeg(start); + break; + case 4: + start= insertend(start); + break; + case 5: + start= insertbefore(start); + break; + case 6: + start= insertafter(start); + break; + case 7: + start= deletebeg(start); + break; + case 8: + start= deleteend(start); + break; + case 9: + start= deletenode(start); + break; + case 10: + start= deleteafter(start); + break; + case 11: + start=deletebefore(start); + break; + case 12: + start= deletelist(start); + cout<<"\n linked list deleted"; + break; + case 13: + start= sortll(start); + break; + + } + }while(option!=14); + + return 0; +} + +node* createll(node *start) +{ + node* nn,*p; + int num; + cout<<"\nenter -1 to end\n"; + cin>>num; + while(num!=-1) + { + nn=new node(num); + if(start==NULL) + { + start=nn; + } + else + { + p=start; + while(p->next!=NULL) + p=p->next; + p->next=nn; + nn->prev=p; + } + cin>>num; + } + return start; +} +node *display(node* start) +{ + node *t=start; + while(t!=NULL) + { + cout<data<<" -> "; + t=t->next; + } + cout<<"NULL\n"; + return start; +} + +node *insertbeg(node *start) +{ + cin>>num; + node *nn= new node(num); + nn->next=start; + start=nn; + return start; +} + +node *insertend(node *start) +{ + cin>>num; + node *nn=new node(num); + node *p=start; + while(p->next!=NULL) + { + p=p->next; + } + p->next=nn; + nn->prev=p; + + return start; +} +node *insertbefore(node *start) +{ + cin>>num>>val; + node *n= new node(num); + node *p=start; + while(p->data!=val) + p=p->next; + n->prev=p->prev; + n->next=p; + p->prev->next=n; + p->prev=n; + return start; +} + +node *insertafter(node *start) +{ + cin>>num>>val; + node *nn=new node(num); + node *p= start; + while(p->data!=val) + { + p=p->next; + } + nn->next=p->next; + nn->prev=p; + p->next->prev=nn; + p->next=nn; + +return start; +} + +node *deletebeg(node *start) +{ + node *nn; + nn= start; + start=nn->next; + start->prev=NULL; + free(nn); + + return start; +} + +node *deleteend(node *start) +{ + node *nn=start; + while(nn->next!=NULL) + { + nn=nn->next; + } + nn->prev->next=NULL; + free(nn); + return start; +} + +node *deletenode(node *start) +{ + cin>>val; + node *p=start; +while(p->data!=val) +{ + p=p->next; +} +p->prev->next= p->next; +free(p); + +return start; + +} + +node *deleteafter(node *start) +{ + node *p=start,*t; + int num; + cin>>num; + while(p->data!=num) + { + p=p->next; + } + t=p->next; + p->next=t->next; + t->next->prev=p; + free(t); + return start; +} +node *deletebefore(node *start) +{ + node *p=start; + cin>>num; + while(p->data!=num) + p=p->next; + node *t=p->prev; + if(t==start) + start=deletebeg(start); + else{ + p->prev=t->prev; + t->prev->next=p; +} + free(t); + return start; +} +node *deletelist(node *start) +{ + node *n=start; + while(n!=NULL) + { + start= deletebeg(start); + n=start; + } + + return start; +} + +node *sortll(node *start) +{ + node *p1,*p2; + int t; + p1=start; + while(p1->next!=NULL) + { + p2=p1->next; + while(p2!=NULL) + { + if(p1->data>p2->data) + { + t=p1->data; + p1->data=p2->data; + p2->data=t; + } + p2=p2->next; + } + p1=p1->next; + } + return start; +} diff --git a/Linked List Algo/04. circular linked list.cpp b/Linked List Algo/04. circular linked list.cpp new file mode 100644 index 00000000..d00ced8a --- /dev/null +++ b/Linked List Algo/04. circular linked list.cpp @@ -0,0 +1,317 @@ +/* + hare krishna hare krishna krishna krishna hare hare + hare rama hare rama rama rama hare +*/ + +//// happy coding //// +//// chant hare krishna ///// +//// ritik dagar /////// + +#include +using namespace std; + +class node{ + public: + int data; + node *prev; + node *next; + node() + { + data=0; + prev=next=NULL; + } + node(int n) + { + data=n; + prev=NULL; + next=NULL; + } +}; +int num,val; +node *start = NULL; +node *createll(node *); +node *display(node *); +node *insertbeg(node *); +node *insertend(node *); +node *insertbefore(node *); +node *insertafter(node *); +node *deletebeg(node *); +node *deleteend(node *); +node *deletenode(node *); +node *deleteafter(node *); +node *deletebefore(node *); +node *deletelist(node *); +node *sortll(node *); +int main() +{ + int option; + cout<<"hare krishna hare krishna\nkrishna krishna hare hare\nhare rama hare rama\nrama rama hare hare\n"; + cout<<"representation of circular doubly linked list\n"; + do{ + cout<<"\n\n *******MAIN MENU*******"; + cout<<"\n 1: Create a list "; + cout<<"\n 2: Display the list "; + cout<<"\n 3: Add a node at the begining"; + cout<<"\n 4: Add a node at the end"; + cout<<"\n 5: Add a node before a given node"; + cout<<"\n 6: Add a node after a given node"; + cout<<"\n 7: delete a node from the begining"; + cout<<"\n 8: delete a node from the end"; + cout<<"\n 9: delete a given node"; + cout<<"\n 10: delete a node after given node"; + cout<<"\n 11: delete a node before given node"; + cout<<"\n 12: delete entire list"; + cout<<"\n 13: sort the list"; + cout<<"\n 14: exit\n"; + cin>>option; + switch(option) + { + case 1: + start= createll(start); + cout<<"\n linked list created"; + break; + case 2: + start= display(start); + break; + case 3: + start= insertbeg(start); + break; + case 4: + start= insertend(start); + break; + case 5: + start= insertbefore(start); + break; + case 6: + start= insertafter(start); + break; + case 7: + start= deletebeg(start); + break; + case 8: + start= deleteend(start); + break; + case 9: + start= deletenode(start); + break; + case 10: + start= deleteafter(start); + break; + case 11: + start=deletebefore(start); + break; + case 12: + start= deletelist(start); + cout<<"\n linked list deleted"; + break; + case 13: + start= sortll(start); + break; + + } + }while(option!=14); + + return 0; +} + +node* createll(node *start) +{ + node* nn,*p; + int num; + cout<<"\nenter -1 to end\n"; + cin>>num; + while(num!=-1) + { + nn=new node(num); + if(start==NULL) + { + nn->next=start; + start=nn; + } + else + { + p=start; + while(p->next!=start) + p=p->next; + p->next=nn; + nn->prev=p; + nn->next=start; + start->prev=nn; + } + cin>>num; + } + return start; +} +node *display(node* start) +{ + node *t=start; + while(t->next!=start) + { + cout<data<<" -> "; + t=t->next; + } + cout<data; + return start; +} + +node *insertbeg(node *start) +{ + cin>>num; + node *nn= new node(num); + nn->next=start; + start->prev=nn; + start=nn; + return start; +} + +node *insertend(node *start) +{ + cin>>num; + node *nn=new node(num); + node *p=start; + while(p->next!=start) + { + p=p->next; + } + p->next=nn; + nn->prev=p; + nn->next=start; + start->prev=nn; + + return start; +} +node *insertbefore(node *start) +{ + cin>>num>>val; + node *n= new node(num); + node *p=start; + while(p->data!=val) + p=p->next; + n->prev=p->prev; + n->next=p; + p->prev->next=n; + p->prev=n; + return start; +} + +node *insertafter(node *start) +{ + cin>>num>>val; + node *nn=new node(num); + node *p= start; + while(p->data!=val) + { + p=p->next; + } + nn->next=p->next; + nn->prev=p; + p->next->prev=nn; + p->next=nn; + +return start; +} + +node *deletebeg(node *start) +{ + node *nn; + nn= start; + start=nn->next; + start->prev=NULL; + free(nn); + + return start; +} + +node *deleteend(node *start) +{ + node *nn=start; + while(nn->next!=start) + { + nn=nn->next; + } + nn->prev->next=start; + free(nn); + return start; +} + +node *deletenode(node *start) +{ + cin>>val; + node *p=start; +while(p->data!=val) +{ + p=p->next; +} +p->prev->next= p->next; +p->next->prev=p->prev; +free(p); + +return start; + +} + +node *deleteafter(node *start) +{ + node *p=start,*t; + int num; + cin>>num; + while(p->data!=num) + { + p=p->next; + } + t=p->next; + p->next=t->next; + t->next->prev=p; + free(t); + return start; +} +node *deletebefore(node *start) +{ + node *p=start; + cin>>num; + while(p->data!=num) + p=p->next; + node *t=p->prev; + if(t==start) + start=deletebeg(start); + else{ + p->prev=t->prev; + t->prev->next=p; +} + free(t); + return start; +} +node *deletelist(node *start) +{ + node *n=start; + while(n->next!=NULL) + { + start= deleteend(start); + } + free(start); + return start; +} + +node *sortll(node *start) +{ + node *p1,*p2; + int t; + p1=start; + while(p1->next!=NULL) + { + p2=p1->next; + while(p2!=NULL) + { + if(p1->data>p2->data) + { + t=p1->data; + p1->data=p2->data; + p2->data=t; + } + p2=p2->next; + } + p1=p1->next; + } + return start; +} + diff --git a/Linked List Algo/05. header linked list.cpp b/Linked List Algo/05. header linked list.cpp new file mode 100644 index 00000000..8a5d9c21 --- /dev/null +++ b/Linked List Algo/05. header linked list.cpp @@ -0,0 +1,93 @@ +/* + hare krishna hare krishna krishna krishna hare hare + hare rama hare rama rama rama hare +*/ + +//// happy coding //// +//// chant hare krishna ///// +//// rk dagar /////// + +#include +using namespace std; + +class node{ + public: + int data; + node *next; + node(int n) + { + data=n; + next=NULL; + } + node() + { + data=0; + next=NULL; + } +}; + +node *start = NULL; +node *createll(node *); +node *display(node *); +int main() +{ + int option; + do{ + cout<<"\n\n *******MAIN MENU*******"; + cout<<"\n 1: Create a header linked list "; + cout<<"\n 2: Display the header linked list "; + cout<<"\n 3: exit\n"; + cin>>option; + switch(option) + { + case 1: + start= createll(start); + cout<<"\nheader linked list created"; + break; + case 2: + start= display(start); + break; + + } + }while(option!=3); + + return 0; +} + +node* createll(node *start) +{ + node* n,*p; + int num; + cout<<"\nenter -1 to end\n"; + cin>>num; + while(num!=-1) + { + n=new node(num); + if(start==NULL) + { + start = new node(); + start->next=n; + } + else + { + p=start; + while(p->next!=NULL) + p=p->next; + p->next=n; + } + cin>>num; + } + return start; +} +node *display(node* start) +{ + node *t=start; + while(t!=NULL) + { + cout<data<<" -> "; + t=t->next; + } + cout<<"NULL\n"; + return start; +} + diff --git a/Linked List Algo/06. Addition and Subtraction on two polynomials.cpp b/Linked List Algo/06. Addition and Subtraction on two polynomials.cpp new file mode 100644 index 00000000..c6f0639c --- /dev/null +++ b/Linked List Algo/06. Addition and Subtraction on two polynomials.cpp @@ -0,0 +1,215 @@ +/* + hare krishna hare krishna krishna krishna hare hare + hare rama hare rama rama rama hare +*/ +#include +using namespace std; +int n,c,num,val; +class node{ + public: + int num,coeff; + node *next; + node(int n,int c) + { + num=n; + coeff=c; + next=NULL; + } + node() + { + num=0,coeff=0; + next=NULL; + } +}; + +node *start1 = NULL,*start2=NULL,*start3=NULL,*start4=NULL; + + +node* createll(node *start) +{ + node* n,*p; + cout<<"\nenter -1 to end\n"; + cin>>num>>c; + while(num!=-1) + { + n=new node(num,c); + if(start==NULL) + { + start=n; + } + else + { + p=start; + while(p->next!=NULL) + p=p->next; + p->next=n; + } + cin>>num; + if(num==-1) + break; + cin>>c; + } + return start; +} +node *display(node* start) +{ + node *t=start; + while(t!=NULL) + { + cout<num<<" x ^ "<coeff<<" -> "; + t=t->next; + } + cout<<"NULL\n"; + return start; +} +node *addnode(node *start,int num,int c) +{ + node *p,*n; + n=new node(num,c); + if(start==NULL) + { + start=n; + } + else + { + p=start; + while(p->next!=NULL) + p=p->next; + p->next=n; + } + return start; +} +node *addpoly(node *start3,node* a,node *b) +{ + node *p1=a,*p2=b; + int sum; + while(p1!=NULL&&p2!=NULL) + { + if(p1->coeff==p2->coeff) + { + sum= p1->num+p2->num; + start3 = addnode(start3,sum,p1->coeff); + p1=p1->next; + p2=p2->next; + } + else if(p1->coeff>p2->coeff) + { + start3= addnode(start3,p1->num,p1->coeff); + p1=p1->next; + } + else if(p2->coeff>p1->coeff) + { + start3= addnode(start3,p2->num,p2->coeff); + p2=p2->next; + } + } + if(p1==NULL) + { + while(p2!=NULL) + { + start3= addnode(start3,p2->num,p2->coeff); + p2=p2->next; + } + } + if(p2==NULL) + { + while(p1!=NULL) + { + start3= addnode(start3,p1->num,p1->coeff); + p1=p1->next; + } + } + return start3; +} +node *subpoly(node *s4,node *a,node *b) +{ + int diff; + node *p1=a, *p2=b; + while(p1!=NULL&&p2!=NULL) + { + if(p1->coeff==p2->coeff) + { + diff= p1->num - p2->num; + s4= addnode(s4,diff,p1->coeff); + p1=p1->next; + p2=p2->next; + } + else if(p1->coeff>p2->coeff) + { + s4= addnode(s4,p1->num,p1->coeff); + p1=p1->next; + } + else if(p2->coeff>p1->coeff) + { + s4= addnode(s4,p2->num,p2->coeff); + p2=p2->next; + } + } + if(p1==NULL) + { + while(p2!=NULL) + { + s4= addnode(s4,p2->num,p2->coeff); + p2=p2->next; + + } + } + if(p2==NULL) + { + while(p1!=NULL) + { + s4= addnode(s4,p1->num,p1->coeff); + p1=p1->next; + } + } + + return s4; +} +int main() +{ + int option; + do{ + cout<<"\n\n *******MAIN MENU*******"; + cout<<"\n 1: Enter First Polynomial "; + cout<<"\n 2: Display First Polynomial "; + cout<<"\n 3: Enter Second Polynomial"; + cout<<"\n 4: Display Second Polynomial"; + cout<<"\n 5: Add the Polynomials"; + cout<<"\n 6: Display the add result Polynomial"; + cout<<"\n 7: Subtract the polynomials"; + cout<<"\n 8: Display the subtract result polynomial"; + cout<<"\n 9: exit\n"; + cin>>option; + switch(option) + { + case 1: + start1= createll(start1); + break; + case 2: + display(start1); + break; + case 3: + start2= createll(start2); + break; + case 4: + display(start2); + break; + case 5: + start3= addpoly(start3,start1,start2); + break; + case 6: + display(start3); + break; + case 7: + start4= subpoly(start4,start1,start2); + break; + case 8: + display(start4); + break; + + } + }while(option!=9); + + return 0; +} + From 95d165a457c61466e88bf13f590f3f04f88ba7c8 Mon Sep 17 00:00:00 2001 From: RITIK DAGAR <47457948+Ritik2703@users.noreply.github.com> Date: Fri, 8 Oct 2021 16:35:34 +0530 Subject: [PATCH 2/2] Linked list implementation --- 01. singlylinkedlist.cpp | 298 ++++++++++++++++ 02. circular singly linked list.cpp | 314 +++++++++++++++++ 03. doubly linked list.cpp | 310 +++++++++++++++++ 04. circular linked list.cpp | 317 ++++++++++++++++++ 05. header linked list.cpp | 93 +++++ ...ion and Subtraction on two polynomials.cpp | 215 ++++++++++++ 6 files changed, 1547 insertions(+) create mode 100644 01. singlylinkedlist.cpp create mode 100644 02. circular singly linked list.cpp create mode 100644 03. doubly linked list.cpp create mode 100644 04. circular linked list.cpp create mode 100644 05. header linked list.cpp create mode 100644 06. Addition and Subtraction on two polynomials.cpp diff --git a/01. singlylinkedlist.cpp b/01. singlylinkedlist.cpp new file mode 100644 index 00000000..2bd4196d --- /dev/null +++ b/01. singlylinkedlist.cpp @@ -0,0 +1,298 @@ + +//// happy coding //// +//// chant hare krishna ///// +//// rk dagar /////// + +#include +using namespace std; + +class node{ + public: + int data; + node *next; +}; + +node *start = NULL; +node *createll(node *); +node *display(node *); +node *insertbeg(node *); +node *insertend(node *); +node *insertbefore(node *); +node *insertafter(node *); +node *deletebeg(node *); +node *deleteend(node *); +node *deletenode(node *); +node *deleteafter(node *); +node *deletelist(node *); +node *sortll(node *); +int main() +{ + int option; + do{ + cout<<"\n\n *******MAIN MENU*******"; + cout<<"\n 1: Create a list "; + cout<<"\n 2: Display the list "; + cout<<"\n 3: Add a node at the begining"; + cout<<"\n 4: Add a node at the end"; + cout<<"\n 5: Add a node before a given node"; + cout<<"\n 6: Add a node after a given node"; + cout<<"\n 7: delete a node from the begining"; + cout<<"\n 8: delete a node from the end"; + cout<<"\n 9: delete a given node"; + cout<<"\n 10: delete a node after given node"; + cout<<"\n 11: delete entire list"; + cout<<"\n 12: sort the list"; + cout<<"\n 13: exit\n"; + cin>>option; + switch(option) + { + case 1: + start= createll(start); + cout<<"\n linked list created"; + break; + case 2: + start= display(start); + break; + case 3: + start= insertbeg(start); + break; + case 4: + start= insertend(start); + break; + case 5: + start= insertbefore(start); + break; + case 6: + start= insertafter(start); + break; + case 7: + start= deletebeg(start); + break; + case 8: + start= deleteend(start); + break; + case 9: + start= deletenode(start); + break; + case 10: + start= deleteafter(start); + break; + case 11: + start= deletelist(start); + cout<<"\n linked list deleted"; + break; + case 12: + start= sortll(start); + break; + + } + }while(option!=13); + + return 0; +} + +node* createll(node *start) +{ + node* nn,*p; + int num; + cout<<"\nenter -1 to end\n"; + cin>>num; + while(num!=-1) + { + nn=new node(); + nn->data=num; + nn->next=NULL; + if(start==NULL) + { + start=nn; + } + else + { + p=start; + while(p->next!=NULL) + p=p->next; + p->next=nn; + } + cin>>num; + } + return start; +} +node *display(node* start) +{ + node *t=start; + while(t!=NULL) + { + cout<data<<" -> "; + t=t->next; + } + cout<<"NULL\n"; + return start; +} + +node *insertbeg(node *start) +{ + node *nn= new node(); + int num; + cin>>num; + nn->data=num; + nn->next=start; + start=nn; + + return start; +} + +node *insertend(node *start) +{ + node *nn=new node(); + node *p=start; + int num; + cin>>num; + nn->data=num; + nn->next=NULL; + while(p->next!=NULL) + { + p=p->next; + } + p->next=nn; + + + return start; + + +} +node *insertbefore(node *start) +{ + node *nn= new node(); + node *p=start; + node *preptr; + int num,val; + cin>>num>>val; + nn->data=num; + while(p->data!=val) + { + preptr=p; + p=p->next; + } + preptr->next=nn; + nn->next=p; + + return start; + +} + +node *insertafter(node *start) +{ + node *nn=new node(); + node *p= start,*pre=start; + int num,val; + cin>>num>>val; + nn->data=num; + while(pre->data!=val) + { + pre= p; + p=p->next; + } +nn->next=p; +pre->next=nn; + +return start; +} + +node *deletebeg(node *start) +{ + node *nn; + nn= start; + start=nn->next; + free(nn); + + return start; +} + +node *deleteend(node *start) +{ + node *nn=start,*p; + while(nn->next!=NULL) + { + p=nn; + nn=nn->next; + } + p->next=NULL; + free(nn); + + return start; +} + +node *deletenode(node *start) +{ + int val; + cin>>val; + node *p=start,*pre; + while(p->data==val) + { + start= deletebeg(start); + return start; +} +while(p->data!=val) +{ + pre=p; + p=p->next; +} +pre->next=p->next; +free(p); + +return start; + +} + +node *deleteafter(node *start) +{ + node *nn,*p=start; + nn=start; + int num; + cin>>num; + while(p->data!=num) + { + p=nn; + nn=nn->next; + } + p->next=nn->next; + free(nn); + + return start; +} + +node *deletelist(node *start) +{ + node *n=start; + while(n!=NULL) + { + start= deletebeg(start); + n=start; + } + + return start; +} + +node *sortll(node *start) +{ + node *p1,*p2; + int t; + p1=start; + while(p1->next!=NULL) + { + p2=p1->next; + while(p2!=NULL) + { + if(p1->data>p2->data) + { + t=p1->data; + p1->data=p2->data; + p2->data=t; + } + p2=p2->next; + } + p1=p1->next; + } + return start; +} + diff --git a/02. circular singly linked list.cpp b/02. circular singly linked list.cpp new file mode 100644 index 00000000..087b8b82 --- /dev/null +++ b/02. circular singly linked list.cpp @@ -0,0 +1,314 @@ +/* + hare krishna hare krishna krishna krishna hare hare + hare rama hare rama rama rama hare +*/ + +//// happy coding //// +//// chant hare krishna ///// +//// ritik dagar /////// + +#include +using namespace std; + +class node{ + public: + int data; + node *next; +}; + +node *start = NULL; +node *createll(node *); +node *display(node *); +node *insertbeg(node *); +node *insertend(node *); +node *insertbefore(node *); +node *insertafter(node *); +node *deletebeg(node *); +node *deleteend(node *); +node *deletenode(node *); +node *deleteafter(node *); +node *deletelist(node *); +node *sortll(node *); +int main() +{ + int option; + cout<<"hare krishna hare krishna\nkrishna krishna hare hare\nhare rama hare rama\nrama rama hare hare\n"; + cout<<"implementation of circular linked list\n"; + do{ + cout<<"\n\n *******MAIN MENU*******"; + cout<<"\n 1: Create a list "; + cout<<"\n 2: Display the list "; + cout<<"\n 3: Add a node at the begining"; + cout<<"\n 4: Add a node at the end"; + cout<<"\n 5: Add a node before a given node"; + cout<<"\n 6: Add a node after a given node"; + cout<<"\n 7: delete a node from the begining"; + cout<<"\n 8: delete a node from the end"; + cout<<"\n 9: delete a given node"; + cout<<"\n 10: delete a node after given node"; + cout<<"\n 11: delete entire list"; + cout<<"\n 12: sort the list"; + cout<<"\n 13: exit\n"; + cin>>option; + switch(option) + { + case 1: + start= createll(start); + cout<<"\ncircular linked list created"; + break; + case 2: + start= display(start); + break; + case 3: + start= insertbeg(start); + break; + case 4: + start= insertend(start); + break; + case 5: + start= insertbefore(start); + break; + case 6: + start= insertafter(start); + break; + case 7: + start= deletebeg(start); + break; + case 8: + start= deleteend(start); + break; + case 9: + start= deletenode(start); + break; + case 10: + start= deleteafter(start); + break; + case 11: + start= deletelist(start); + cout<<"\ncircular linked list deleted"; + break; + case 12: + start= sortll(start); + break; + + } + }while(option!=13); + + return 0; +} + +node* createll(node *start) +{ + node* nn,*p; + int num; + cout<<"\nenter -1 to end\n"; + cin>>num; + while(num!=-1) + { + nn=new node(); + nn->data=num; + if(start==NULL) + { + nn->next=nn; + start=nn; + } + else + { + p=start; + while(p->next!=start) + p=p->next; + p->next=nn; + nn->next=start; + } + cin>>num; + } + return start; +} +node *display(node* start) +{ + node *t=start; + while(t->next!=start) + { + cout<data<<" -> "; + t=t->next; + } + cout<data<<"\n"; + return start; +} + +node *insertbeg(node *start) +{ + node *p,*nn= new node(); + int num; + cin>>num; + p=start; + while(p->next!=start) + p=p->next; + p->next=nn; + nn->data=num; + nn->next=start; + start=nn; + + return start; +} + +node *insertend(node *start) +{ + node *nn=new node(); + node *p=start; + int num; + cin>>num; + nn->data=num; + nn->next=start; + while(p->next!=start) + { + p=p->next; + } + p->next=nn; + + + return start; + + +} +node *insertbefore(node *start) +{ + node *nn= new node(); + node *p=start; + node *preptr; + int num,val; + cin>>num>>val; + nn->data=num; + while(p->data!=val) + { + preptr=p; + p=p->next; + } + preptr->next=nn; + nn->next=p; + + return start; + +} + +node *insertafter(node *start) +{ + node *nn=new node(); + node *p= start,*pre=start; + int num,val; + cin>>num>>val; + nn->data=num; + while(pre->data!=val) + { + pre= p; + p=p->next; + } +nn->next=p; +pre->next=nn; + +return start; +} + +node *deletebeg(node *start) +{ + node *nn; + nn= start; + while(nn->next!=start) + nn= nn->next; + nn->next=start->next; + free(start); + start=nn->next; + + + return start; +} + +node *deleteend(node *start) +{ + node *nn=start,*p; + while(nn->next!=start) + { + p=nn; + nn=nn->next; + } + p->next=nn->next; + free(nn); + + return start; +} + +node *deletenode(node *start) +{ + int val; + cin>>val; + node *p=start,*pre; + while(p->data==val) + { + start= deletebeg(start); + return start; +} +while(p->data!=val) +{ + pre=p; + p=p->next; +} +pre->next=p->next; +free(p); + +return start; + +} + +node *deleteafter(node *start) +{ + node *nn,*p; + nn=start; + p=nn; + int num; + cin>>num; + while(p->data!=num) + { + p=nn; + nn=nn->next; + } + p->next=nn->next; + if(nn==start) + start=p->next; + free(nn); + + return start; +} + +node *deletelist(node *start) +{ + node *n=start; + while(n->next!=start) + { + start= deleteend(start); + } + free(start); + return start; +} + +node *sortll(node *start) +{ + node *p1,*p2; + int t; + p1=start; + while(p1->next!=start) + { + p2=p1->next; + while(p2!=start) + { + if(p1->data>p2->data) + { + t=p1->data; + p1->data=p2->data; + p2->data=t; + } + p2=p2->next; + } + p1=p1->next; + } + return start; +} diff --git a/03. doubly linked list.cpp b/03. doubly linked list.cpp new file mode 100644 index 00000000..d4501d08 --- /dev/null +++ b/03. doubly linked list.cpp @@ -0,0 +1,310 @@ +/* + hare krishna hare krishna krishna krishna hare hare + hare rama hare rama rama rama hare +*/ + +//// happy coding //// +//// chant hare krishna ///// +//// ritik dagar /////// + +#include +using namespace std; + +class node{ + public: + int data; + node *prev; + node *next; + node() + { + data=0; + prev=next=NULL; + } + node(int n) + { + data=n; + prev=NULL; + next=NULL; + } +}; +int num,val; +node *start = NULL; +node *createll(node *); +node *display(node *); +node *insertbeg(node *); +node *insertend(node *); +node *insertbefore(node *); +node *insertafter(node *); +node *deletebeg(node *); +node *deleteend(node *); +node *deletenode(node *); +node *deleteafter(node *); +node *deletebefore(node *); +node *deletelist(node *); +node *sortll(node *); +int main() +{ + int option; + cout<<"hare krishna hare krishna\nkrishna krishna hare hare\nhare rama hare rama\nrama rama hare hare\n"; + cout<<"representation of doubly linked list\n"; + do{ + cout<<"\n\n *******MAIN MENU*******"; + cout<<"\n 1: Create a list "; + cout<<"\n 2: Display the list "; + cout<<"\n 3: Add a node at the begining"; + cout<<"\n 4: Add a node at the end"; + cout<<"\n 5: Add a node before a given node"; + cout<<"\n 6: Add a node after a given node"; + cout<<"\n 7: delete a node from the begining"; + cout<<"\n 8: delete a node from the end"; + cout<<"\n 9: delete a given node"; + cout<<"\n 10: delete a node after given node"; + cout<<"\n 11: delete a node before given node"; + cout<<"\n 12: delete entire list"; + cout<<"\n 13: sort the list"; + cout<<"\n 14: exit\n"; + cin>>option; + switch(option) + { + case 1: + start= createll(start); + cout<<"\n linked list created"; + break; + case 2: + start= display(start); + break; + case 3: + start= insertbeg(start); + break; + case 4: + start= insertend(start); + break; + case 5: + start= insertbefore(start); + break; + case 6: + start= insertafter(start); + break; + case 7: + start= deletebeg(start); + break; + case 8: + start= deleteend(start); + break; + case 9: + start= deletenode(start); + break; + case 10: + start= deleteafter(start); + break; + case 11: + start=deletebefore(start); + break; + case 12: + start= deletelist(start); + cout<<"\n linked list deleted"; + break; + case 13: + start= sortll(start); + break; + + } + }while(option!=14); + + return 0; +} + +node* createll(node *start) +{ + node* nn,*p; + int num; + cout<<"\nenter -1 to end\n"; + cin>>num; + while(num!=-1) + { + nn=new node(num); + if(start==NULL) + { + start=nn; + } + else + { + p=start; + while(p->next!=NULL) + p=p->next; + p->next=nn; + nn->prev=p; + } + cin>>num; + } + return start; +} +node *display(node* start) +{ + node *t=start; + while(t!=NULL) + { + cout<data<<" -> "; + t=t->next; + } + cout<<"NULL\n"; + return start; +} + +node *insertbeg(node *start) +{ + cin>>num; + node *nn= new node(num); + nn->next=start; + start=nn; + return start; +} + +node *insertend(node *start) +{ + cin>>num; + node *nn=new node(num); + node *p=start; + while(p->next!=NULL) + { + p=p->next; + } + p->next=nn; + nn->prev=p; + + return start; +} +node *insertbefore(node *start) +{ + cin>>num>>val; + node *n= new node(num); + node *p=start; + while(p->data!=val) + p=p->next; + n->prev=p->prev; + n->next=p; + p->prev->next=n; + p->prev=n; + return start; +} + +node *insertafter(node *start) +{ + cin>>num>>val; + node *nn=new node(num); + node *p= start; + while(p->data!=val) + { + p=p->next; + } + nn->next=p->next; + nn->prev=p; + p->next->prev=nn; + p->next=nn; + +return start; +} + +node *deletebeg(node *start) +{ + node *nn; + nn= start; + start=nn->next; + start->prev=NULL; + free(nn); + + return start; +} + +node *deleteend(node *start) +{ + node *nn=start; + while(nn->next!=NULL) + { + nn=nn->next; + } + nn->prev->next=NULL; + free(nn); + return start; +} + +node *deletenode(node *start) +{ + cin>>val; + node *p=start; +while(p->data!=val) +{ + p=p->next; +} +p->prev->next= p->next; +free(p); + +return start; + +} + +node *deleteafter(node *start) +{ + node *p=start,*t; + int num; + cin>>num; + while(p->data!=num) + { + p=p->next; + } + t=p->next; + p->next=t->next; + t->next->prev=p; + free(t); + return start; +} +node *deletebefore(node *start) +{ + node *p=start; + cin>>num; + while(p->data!=num) + p=p->next; + node *t=p->prev; + if(t==start) + start=deletebeg(start); + else{ + p->prev=t->prev; + t->prev->next=p; +} + free(t); + return start; +} +node *deletelist(node *start) +{ + node *n=start; + while(n!=NULL) + { + start= deletebeg(start); + n=start; + } + + return start; +} + +node *sortll(node *start) +{ + node *p1,*p2; + int t; + p1=start; + while(p1->next!=NULL) + { + p2=p1->next; + while(p2!=NULL) + { + if(p1->data>p2->data) + { + t=p1->data; + p1->data=p2->data; + p2->data=t; + } + p2=p2->next; + } + p1=p1->next; + } + return start; +} diff --git a/04. circular linked list.cpp b/04. circular linked list.cpp new file mode 100644 index 00000000..d00ced8a --- /dev/null +++ b/04. circular linked list.cpp @@ -0,0 +1,317 @@ +/* + hare krishna hare krishna krishna krishna hare hare + hare rama hare rama rama rama hare +*/ + +//// happy coding //// +//// chant hare krishna ///// +//// ritik dagar /////// + +#include +using namespace std; + +class node{ + public: + int data; + node *prev; + node *next; + node() + { + data=0; + prev=next=NULL; + } + node(int n) + { + data=n; + prev=NULL; + next=NULL; + } +}; +int num,val; +node *start = NULL; +node *createll(node *); +node *display(node *); +node *insertbeg(node *); +node *insertend(node *); +node *insertbefore(node *); +node *insertafter(node *); +node *deletebeg(node *); +node *deleteend(node *); +node *deletenode(node *); +node *deleteafter(node *); +node *deletebefore(node *); +node *deletelist(node *); +node *sortll(node *); +int main() +{ + int option; + cout<<"hare krishna hare krishna\nkrishna krishna hare hare\nhare rama hare rama\nrama rama hare hare\n"; + cout<<"representation of circular doubly linked list\n"; + do{ + cout<<"\n\n *******MAIN MENU*******"; + cout<<"\n 1: Create a list "; + cout<<"\n 2: Display the list "; + cout<<"\n 3: Add a node at the begining"; + cout<<"\n 4: Add a node at the end"; + cout<<"\n 5: Add a node before a given node"; + cout<<"\n 6: Add a node after a given node"; + cout<<"\n 7: delete a node from the begining"; + cout<<"\n 8: delete a node from the end"; + cout<<"\n 9: delete a given node"; + cout<<"\n 10: delete a node after given node"; + cout<<"\n 11: delete a node before given node"; + cout<<"\n 12: delete entire list"; + cout<<"\n 13: sort the list"; + cout<<"\n 14: exit\n"; + cin>>option; + switch(option) + { + case 1: + start= createll(start); + cout<<"\n linked list created"; + break; + case 2: + start= display(start); + break; + case 3: + start= insertbeg(start); + break; + case 4: + start= insertend(start); + break; + case 5: + start= insertbefore(start); + break; + case 6: + start= insertafter(start); + break; + case 7: + start= deletebeg(start); + break; + case 8: + start= deleteend(start); + break; + case 9: + start= deletenode(start); + break; + case 10: + start= deleteafter(start); + break; + case 11: + start=deletebefore(start); + break; + case 12: + start= deletelist(start); + cout<<"\n linked list deleted"; + break; + case 13: + start= sortll(start); + break; + + } + }while(option!=14); + + return 0; +} + +node* createll(node *start) +{ + node* nn,*p; + int num; + cout<<"\nenter -1 to end\n"; + cin>>num; + while(num!=-1) + { + nn=new node(num); + if(start==NULL) + { + nn->next=start; + start=nn; + } + else + { + p=start; + while(p->next!=start) + p=p->next; + p->next=nn; + nn->prev=p; + nn->next=start; + start->prev=nn; + } + cin>>num; + } + return start; +} +node *display(node* start) +{ + node *t=start; + while(t->next!=start) + { + cout<data<<" -> "; + t=t->next; + } + cout<data; + return start; +} + +node *insertbeg(node *start) +{ + cin>>num; + node *nn= new node(num); + nn->next=start; + start->prev=nn; + start=nn; + return start; +} + +node *insertend(node *start) +{ + cin>>num; + node *nn=new node(num); + node *p=start; + while(p->next!=start) + { + p=p->next; + } + p->next=nn; + nn->prev=p; + nn->next=start; + start->prev=nn; + + return start; +} +node *insertbefore(node *start) +{ + cin>>num>>val; + node *n= new node(num); + node *p=start; + while(p->data!=val) + p=p->next; + n->prev=p->prev; + n->next=p; + p->prev->next=n; + p->prev=n; + return start; +} + +node *insertafter(node *start) +{ + cin>>num>>val; + node *nn=new node(num); + node *p= start; + while(p->data!=val) + { + p=p->next; + } + nn->next=p->next; + nn->prev=p; + p->next->prev=nn; + p->next=nn; + +return start; +} + +node *deletebeg(node *start) +{ + node *nn; + nn= start; + start=nn->next; + start->prev=NULL; + free(nn); + + return start; +} + +node *deleteend(node *start) +{ + node *nn=start; + while(nn->next!=start) + { + nn=nn->next; + } + nn->prev->next=start; + free(nn); + return start; +} + +node *deletenode(node *start) +{ + cin>>val; + node *p=start; +while(p->data!=val) +{ + p=p->next; +} +p->prev->next= p->next; +p->next->prev=p->prev; +free(p); + +return start; + +} + +node *deleteafter(node *start) +{ + node *p=start,*t; + int num; + cin>>num; + while(p->data!=num) + { + p=p->next; + } + t=p->next; + p->next=t->next; + t->next->prev=p; + free(t); + return start; +} +node *deletebefore(node *start) +{ + node *p=start; + cin>>num; + while(p->data!=num) + p=p->next; + node *t=p->prev; + if(t==start) + start=deletebeg(start); + else{ + p->prev=t->prev; + t->prev->next=p; +} + free(t); + return start; +} +node *deletelist(node *start) +{ + node *n=start; + while(n->next!=NULL) + { + start= deleteend(start); + } + free(start); + return start; +} + +node *sortll(node *start) +{ + node *p1,*p2; + int t; + p1=start; + while(p1->next!=NULL) + { + p2=p1->next; + while(p2!=NULL) + { + if(p1->data>p2->data) + { + t=p1->data; + p1->data=p2->data; + p2->data=t; + } + p2=p2->next; + } + p1=p1->next; + } + return start; +} + diff --git a/05. header linked list.cpp b/05. header linked list.cpp new file mode 100644 index 00000000..8a5d9c21 --- /dev/null +++ b/05. header linked list.cpp @@ -0,0 +1,93 @@ +/* + hare krishna hare krishna krishna krishna hare hare + hare rama hare rama rama rama hare +*/ + +//// happy coding //// +//// chant hare krishna ///// +//// rk dagar /////// + +#include +using namespace std; + +class node{ + public: + int data; + node *next; + node(int n) + { + data=n; + next=NULL; + } + node() + { + data=0; + next=NULL; + } +}; + +node *start = NULL; +node *createll(node *); +node *display(node *); +int main() +{ + int option; + do{ + cout<<"\n\n *******MAIN MENU*******"; + cout<<"\n 1: Create a header linked list "; + cout<<"\n 2: Display the header linked list "; + cout<<"\n 3: exit\n"; + cin>>option; + switch(option) + { + case 1: + start= createll(start); + cout<<"\nheader linked list created"; + break; + case 2: + start= display(start); + break; + + } + }while(option!=3); + + return 0; +} + +node* createll(node *start) +{ + node* n,*p; + int num; + cout<<"\nenter -1 to end\n"; + cin>>num; + while(num!=-1) + { + n=new node(num); + if(start==NULL) + { + start = new node(); + start->next=n; + } + else + { + p=start; + while(p->next!=NULL) + p=p->next; + p->next=n; + } + cin>>num; + } + return start; +} +node *display(node* start) +{ + node *t=start; + while(t!=NULL) + { + cout<data<<" -> "; + t=t->next; + } + cout<<"NULL\n"; + return start; +} + diff --git a/06. Addition and Subtraction on two polynomials.cpp b/06. Addition and Subtraction on two polynomials.cpp new file mode 100644 index 00000000..c6f0639c --- /dev/null +++ b/06. Addition and Subtraction on two polynomials.cpp @@ -0,0 +1,215 @@ +/* + hare krishna hare krishna krishna krishna hare hare + hare rama hare rama rama rama hare +*/ +#include +using namespace std; +int n,c,num,val; +class node{ + public: + int num,coeff; + node *next; + node(int n,int c) + { + num=n; + coeff=c; + next=NULL; + } + node() + { + num=0,coeff=0; + next=NULL; + } +}; + +node *start1 = NULL,*start2=NULL,*start3=NULL,*start4=NULL; + + +node* createll(node *start) +{ + node* n,*p; + cout<<"\nenter -1 to end\n"; + cin>>num>>c; + while(num!=-1) + { + n=new node(num,c); + if(start==NULL) + { + start=n; + } + else + { + p=start; + while(p->next!=NULL) + p=p->next; + p->next=n; + } + cin>>num; + if(num==-1) + break; + cin>>c; + } + return start; +} +node *display(node* start) +{ + node *t=start; + while(t!=NULL) + { + cout<num<<" x ^ "<coeff<<" -> "; + t=t->next; + } + cout<<"NULL\n"; + return start; +} +node *addnode(node *start,int num,int c) +{ + node *p,*n; + n=new node(num,c); + if(start==NULL) + { + start=n; + } + else + { + p=start; + while(p->next!=NULL) + p=p->next; + p->next=n; + } + return start; +} +node *addpoly(node *start3,node* a,node *b) +{ + node *p1=a,*p2=b; + int sum; + while(p1!=NULL&&p2!=NULL) + { + if(p1->coeff==p2->coeff) + { + sum= p1->num+p2->num; + start3 = addnode(start3,sum,p1->coeff); + p1=p1->next; + p2=p2->next; + } + else if(p1->coeff>p2->coeff) + { + start3= addnode(start3,p1->num,p1->coeff); + p1=p1->next; + } + else if(p2->coeff>p1->coeff) + { + start3= addnode(start3,p2->num,p2->coeff); + p2=p2->next; + } + } + if(p1==NULL) + { + while(p2!=NULL) + { + start3= addnode(start3,p2->num,p2->coeff); + p2=p2->next; + } + } + if(p2==NULL) + { + while(p1!=NULL) + { + start3= addnode(start3,p1->num,p1->coeff); + p1=p1->next; + } + } + return start3; +} +node *subpoly(node *s4,node *a,node *b) +{ + int diff; + node *p1=a, *p2=b; + while(p1!=NULL&&p2!=NULL) + { + if(p1->coeff==p2->coeff) + { + diff= p1->num - p2->num; + s4= addnode(s4,diff,p1->coeff); + p1=p1->next; + p2=p2->next; + } + else if(p1->coeff>p2->coeff) + { + s4= addnode(s4,p1->num,p1->coeff); + p1=p1->next; + } + else if(p2->coeff>p1->coeff) + { + s4= addnode(s4,p2->num,p2->coeff); + p2=p2->next; + } + } + if(p1==NULL) + { + while(p2!=NULL) + { + s4= addnode(s4,p2->num,p2->coeff); + p2=p2->next; + + } + } + if(p2==NULL) + { + while(p1!=NULL) + { + s4= addnode(s4,p1->num,p1->coeff); + p1=p1->next; + } + } + + return s4; +} +int main() +{ + int option; + do{ + cout<<"\n\n *******MAIN MENU*******"; + cout<<"\n 1: Enter First Polynomial "; + cout<<"\n 2: Display First Polynomial "; + cout<<"\n 3: Enter Second Polynomial"; + cout<<"\n 4: Display Second Polynomial"; + cout<<"\n 5: Add the Polynomials"; + cout<<"\n 6: Display the add result Polynomial"; + cout<<"\n 7: Subtract the polynomials"; + cout<<"\n 8: Display the subtract result polynomial"; + cout<<"\n 9: exit\n"; + cin>>option; + switch(option) + { + case 1: + start1= createll(start1); + break; + case 2: + display(start1); + break; + case 3: + start2= createll(start2); + break; + case 4: + display(start2); + break; + case 5: + start3= addpoly(start3,start1,start2); + break; + case 6: + display(start3); + break; + case 7: + start4= subpoly(start4,start1,start2); + break; + case 8: + display(start4); + break; + + } + }while(option!=9); + + return 0; +} +