From ec8ab715b19951754f3f9b2fba05b4db3e585416 Mon Sep 17 00:00:00 2001 From: niteshY <72301137+niteshpg@users.noreply.github.com> Date: Sat, 3 Oct 2020 16:08:20 +0530 Subject: [PATCH 1/3] Update sortarrary.cpp Update search.cpp comment remover --- array_examples/search.cpp | 39 +++++++++++++++-------------------- array_examples/sortarrary.cpp | 29 ++++++++++++-------------- doubly_linkedlist.cpp | 8 ++++--- encapsulation.cpp | 23 ++------------------- 4 files changed, 37 insertions(+), 62 deletions(-) diff --git a/array_examples/search.cpp b/array_examples/search.cpp index 230bb9d..d564c4c 100644 --- a/array_examples/search.cpp +++ b/array_examples/search.cpp @@ -1,35 +1,30 @@ #include using namespace std; -int main() -{ +int main(){ int n,a[n],m; cout<<"enter size of array"; cin>>n; cout<<"ente element in array"; - for(int i=0;i>a[i]; } + cout<<"enter element you want to search"<<"\n"; cin>>m; - int flag=0; - for(int i=0;i>n; cout<<"Enter the array elements: "; - for(int i=0;i>a[i]; + } - for(int i=1;ia[j+1]) - { - temp=a[j]; - a[j]=a[j+1]; - a[j+1]=temp; - } + for(int i=1;ia[j+1]){ + swap(a[j],a[j+1]); + } } cout<<"Array after sort:"; - for(i=0;i data = v; newNode -> next = NULL; if(head == NULL) diff --git a/encapsulation.cpp b/encapsulation.cpp index d76be57..c9956f9 100644 --- a/encapsulation.cpp +++ b/encapsulation.cpp @@ -1,37 +1,18 @@ -/* -How Encapsulation is achieved in a class -To do this: -1) Make all the data members private. -2) Create public setter and getter functions for each data member in such a way that -the set function set the value of data member and get function get the value of data member.*/ - #include using namespace std; class ExampleEncap{ private: - /* Since we have marked these data members private, - * any entity outside this class cannot access these - * data members directly, they have to use getter and - * setter functions. - */ int num; char ch; public: - /* Getter functions to get the value of data members. - * Since these functions are public, they can be accessed - * outside the class, thus provide the access to data members - * through them - */ int getNum() const { return num; } char getCh() const { return ch; } - /* Setter functions, they are called for assigning the values - * to the private data members. - */ + void setNum(int num) { this->num = num; } @@ -46,4 +27,4 @@ int main(){ cout< Date: Fri, 18 Dec 2020 10:56:43 +0530 Subject: [PATCH 2/3] fix style issue --- DFS.cpp | 148 ++++++------ DFS.cpp.orig | 80 +++++++ dataabstruction.cpp | 41 ++-- dataabstruction.cpp.orig | 20 ++ doubly_linkedlist.cpp | 452 +++++++++++++++++++------------------ doubly_linkedlist.cpp.orig | 264 ++++++++++++++++++++++ encapsulation.cpp | 44 ++-- encapsulation.cpp.orig | 30 +++ singly_linkedlist.cpp | 360 ++++++++++++++--------------- singly_linkedlist.cpp.orig | 230 +++++++++++++++++++ statickeyword.cpp | 42 ++-- statickeyword.cpp.orig | 28 +++ thispointer.cpp | 30 +-- thispointer.cpp.orig | 22 ++ 14 files changed, 1234 insertions(+), 557 deletions(-) create mode 100644 DFS.cpp.orig create mode 100644 dataabstruction.cpp.orig create mode 100644 doubly_linkedlist.cpp.orig create mode 100644 encapsulation.cpp.orig create mode 100644 singly_linkedlist.cpp.orig create mode 100644 statickeyword.cpp.orig create mode 100644 thispointer.cpp.orig diff --git a/DFS.cpp b/DFS.cpp index 187bdc5..46a8de2 100644 --- a/DFS.cpp +++ b/DFS.cpp @@ -1,80 +1,80 @@ -#include -using namespace std; - -// Graph class represents a directed graph -// using adjacency list representation -class Graph { - int V; // No. of vertices - - // Pointer to an array containing - // adjacency lists - list *adj; - - // A recursive function used by DFS - void DFSUtil(int v, bool visited[]); - -public: - Graph(int V); // Constructor - - // function to add an edge to graph - void addEdge(int v, int w); - - // DFS traversal of the vertices - // reachable from v - void DFS(int v); - -}; - -Graph::Graph(int V) { - this->V = V; - adj = new list[V]; -} - -void Graph::addEdge(int v, int w) { - adj[v].push_back(w); // Add w to v’s list. -} - -void Graph::DFSUtil(int v, bool visited[]) { - // Mark the current node as visited and - // print it - visited[v] = true; - cout << v << " "; - +#include +using namespace std; + +// Graph class represents a directed graph +// using adjacency list representation +class Graph { + int V; // No. of vertices + + // Pointer to an array containing + // adjacency lists + list *adj; + + // A recursive function used by DFS + void DFSUtil(int v, bool visited[]); + +public: + Graph(int V); // Constructor + + // function to add an edge to graph + void addEdge(int v, int w); + + // DFS traversal of the vertices + // reachable from v + void DFS(int v); + +}; + +Graph::Graph(int V) { + this->V = V; + adj = new list[V]; +} + +void Graph::addEdge(int v, int w) { + adj[v].push_back(w); // Add w to v’s list. +} + +void Graph::DFSUtil(int v, bool visited[]) { + // Mark the current node as visited and + // print it + visited[v] = true; + cout << v << " "; + // Recur for all the vertices adjacent neighbors - // to this vertex - for ( int neighbor : adj[v]){ - if (!visited[neighbor]){ + // to this vertex + for ( int neighbor : adj[v]) { + if (!visited[neighbor]) { DFSUtil(neighbor, visited); } } -} - -// DFS traversal of the vertices reachable from v. -// It uses recursive DFSUtil() -void Graph::DFS(int v) { - // Mark all the vertices as not visited - bool visited[V]; - +} + +// DFS traversal of the vertices reachable from v. +// It uses recursive DFSUtil() +void Graph::DFS(int v) { + // Mark all the vertices as not visited + bool visited[V]; + memset(visited, false, sizeof(visited)); - - // Call the recursive helper function - // to print DFS traversal - DFSUtil(v, visited); -} - -// Driver code -int main() { - // Create a graph given in the above diagram - Graph g(4); - g.addEdge(0, 1); - g.addEdge(0, 2); - g.addEdge(1, 2); - g.addEdge(2, 0); - g.addEdge(2, 3); - g.addEdge(3, 3); - + + // Call the recursive helper function + // to print DFS traversal + DFSUtil(v, visited); +} + +// Driver code +int main() { + // Create a graph given in the above diagram + Graph g(4); + g.addEdge(0, 1); + g.addEdge(0, 2); + g.addEdge(1, 2); + g.addEdge(2, 0); + g.addEdge(2, 3); + g.addEdge(3, 3); + cout << "Following is Depth First Traversal" - " (starting from vertex 2) \n"; - g.DFS(2); - - return 0; + " (starting from vertex 2) \n"; + g.DFS(2); + + return 0; diff --git a/DFS.cpp.orig b/DFS.cpp.orig new file mode 100644 index 0000000..187bdc5 --- /dev/null +++ b/DFS.cpp.orig @@ -0,0 +1,80 @@ +#include +using namespace std; + +// Graph class represents a directed graph +// using adjacency list representation +class Graph { + int V; // No. of vertices + + // Pointer to an array containing + // adjacency lists + list *adj; + + // A recursive function used by DFS + void DFSUtil(int v, bool visited[]); + +public: + Graph(int V); // Constructor + + // function to add an edge to graph + void addEdge(int v, int w); + + // DFS traversal of the vertices + // reachable from v + void DFS(int v); + +}; + +Graph::Graph(int V) { + this->V = V; + adj = new list[V]; +} + +void Graph::addEdge(int v, int w) { + adj[v].push_back(w); // Add w to v’s list. +} + +void Graph::DFSUtil(int v, bool visited[]) { + // Mark the current node as visited and + // print it + visited[v] = true; + cout << v << " "; + + // Recur for all the vertices adjacent neighbors + // to this vertex + for ( int neighbor : adj[v]){ + if (!visited[neighbor]){ + DFSUtil(neighbor, visited); + } + } +} + +// DFS traversal of the vertices reachable from v. +// It uses recursive DFSUtil() +void Graph::DFS(int v) { + // Mark all the vertices as not visited + bool visited[V]; + + memset(visited, false, sizeof(visited)); + + // Call the recursive helper function + // to print DFS traversal + DFSUtil(v, visited); +} + +// Driver code +int main() { + // Create a graph given in the above diagram + Graph g(4); + g.addEdge(0, 1); + g.addEdge(0, 2); + g.addEdge(1, 2); + g.addEdge(2, 0); + g.addEdge(2, 3); + g.addEdge(3, 3); + + cout << "Following is Depth First Traversal" + " (starting from vertex 2) \n"; + g.DFS(2); + + return 0; diff --git a/dataabstruction.cpp b/dataabstruction.cpp index 02a41c3..cafcb9a 100644 --- a/dataabstruction.cpp +++ b/dataabstruction.cpp @@ -1,20 +1,21 @@ -#include -using namespace std; - class Sum -{ -private: int x, y, z; // private variables are providing data abstraction in this program -public: -void add() -{ -cout<<"Enter two numbers: "; -cin>>x>>y; -z= x+y; -cout<<"Sum of two number is: "< +using namespace std; +class Sum +{ +private: + int x, y, z; // private variables are providing data abstraction in this program +public: + void add() + { + cout<<"Enter two numbers: "; + cin>>x>>y; + z= x+y; + cout<<"Sum of two number is: "< +using namespace std; + class Sum +{ +private: int x, y, z; // private variables are providing data abstraction in this program +public: +void add() +{ +cout<<"Enter two numbers: "; +cin>>x>>y; +z= x+y; +cout<<"Sum of two number is: "< data = value; - newNode -> prev = NULL; - if(head == NULL) + doublylist() { - newNode -> next = NULL; - head = newNode; + head=NULL; + tail=NULL; } - else + void addtohead(int value) { - newNode -> next = head; - head = newNode; + node *newNode=new node; + newNode -> data = value; + newNode -> prev = NULL; + if(head == NULL) + { + newNode -> next = NULL; + head = newNode; + } + else + { + newNode -> next = head; + head = newNode; + } + cout<<"\nInsertion successful at the head"< data = v; - newNode -> next = NULL; - if(head == NULL) - { - newNode -> prev= NULL; - head = newNode; - } - else - { node *temp = head; - while(temp -> next != NULL) - temp = temp -> next; - temp -> next = newNode; - newNode -> prev = temp; - } - cout<<"\nInsertion success!!! at the end"<prev== temp->next) - { - head = NULL; - delete temp; - } - else{ - head = temp->next; - head->prev = NULL; - delete temp; - } - cout<<"Deletion success at the front"<prev == temp->next) - { - head = NULL; - delete temp; - } - else{ - while(temp->next != NULL) - temp = temp->next; - temp->prev ->next = NULL; - delete temp; - } - cout<<"Deletion successful at the end "<>loc; - temp=head; - for(int i=0;inext; - } - ptr->data = item; - ptr->next = temp->next; - ptr -> prev = temp; - temp->next = ptr; - temp->next->prev=ptr; - cout<<"Node Inserted at the desired position \n"; - } -} -void search_element(int data) -{ - node * temp = new node; - temp = head; - int pos = 0; - int flag=0; - while(temp != NULL) + void insertAtEnd(int v) { - if(temp -> data == data) // Element is found + node *newNode=new node; + newNode -> data = v; + newNode -> next = NULL; + if(head == NULL) { - - flag=1;//If found, print and exit - break; - + newNode -> prev= NULL; + head = newNode; } else - { - temp = temp ->next; //If not found, traverse the list - pos++; + { node *temp = head; + while(temp -> next != NULL) + temp = temp -> next; + temp -> next = newNode; + newNode -> prev = temp; } + cout<<"\nInsertion success!!! at the end"<prev== temp->next) + { + head = NULL; + delete temp; + } + else { + head = temp->next; + head->prev = NULL; + delete temp; + } + cout<<"Deletion success at the front"<next; + if(head == NULL) + cout<<"List is Empty!!! Deletion not possible"<prev == temp->next) + { + head = NULL; + delete temp; + } + else { + while(temp->next != NULL) + temp = temp->next; + temp->prev ->next = NULL; + delete temp; + } + cout<<"Deletion successful at the end "<>loc; + temp=head; + for(int i=0; inext; + } + ptr->data = item; + ptr->next = temp->next; + ptr -> prev = temp; + temp->next = ptr; + temp->next->prev=ptr; + cout<<"Node Inserted at the desired position \n"; + } + } + void search_element(int data) { - deleteBeginning(); + node * temp = new node; + temp = head; + int pos = 0; + int flag=0; + while(temp != NULL) + { + if(temp -> data == data) // Element is found + { + + flag=1;//If found, print and exit + break; + + } + else + { + temp = temp ->next; //If not found, traverse the list + pos++; + } + } + if(flag==1) + { + cout<<"\nThe element is found in the list"; + } + else + { + cout<<"\nThe element is not found in the list"; + } } - else if(current != NULL) + void deleteFromN(int position) { - current->prev->next = current->next; - current->next->prev = current->prev; - delete current; - cout<<"SUCCESSFULLY DELETED NODE FROM POSITION.\n"; + node *current; + current = head; + for(int i=1; inext; + } + if(position == 0) + { + deleteBeginning(); + } + else if(current != NULL) + { + current->prev->next = current->next; + current->next->prev = current->prev; + delete current; + cout<<"SUCCESSFULLY DELETED NODE FROM POSITION.\n"; + } + else + { + cout<<"Invalid position!\n"; + } } - else + void reverse() { - cout<<"Invalid position!\n"; + node *current = head; + node *temp = NULL; + while(current!=NULL) + { + temp = current->prev; //swap the next and prev pointer + current->prev = current->next; + current->next = temp; + current = current->prev; + } + temp->prev; + cout<<"LIST REVERSED SUCCESSFULLY.\n"; } -} -void reverse() -{ - node *current = head; - node *temp = NULL; - while(current!=NULL) + void display() { - temp = current->prev; //swap the next and prev pointer - current->prev = current->next; - current->next = temp; - current = current->prev; + node *ptr; + cout<<"printing values"<data<<"->"<<" "; + ptr=ptr->next; + } } - temp->prev; - cout<<"LIST REVERSED SUCCESSFULLY.\n"; -} - void display() -{ - node *ptr; - cout<<"printing values"<data<<"->"<<" "; - ptr=ptr->next; - } -} }; int main() -{doublylist d; - while(1) - { int n; - cout<>n; - switch(n) - { - case 1: - int m; - cout<<"enter item to add to head"<>m; - d.addtohead(m); - break; - case 2: - int item; - cout<<"enter item to add to head"<>item; - d.insertAtEnd(item); - break; + cin>>n; + switch(n) + { + case 1: + int m; + cout<<"enter item to add to head"<>m; + d.addtohead(m); + break; + case 2: + int item; + cout<<"enter item to add to head"<>item; + d.insertAtEnd(item); + break; case 3: - d.deleteBeginning(); - break; - case 4: - d.deleteEnd(); - break; - case 5: - d.display(); - break; - case 6: - int newvalue; - cout<<"enter value for desired position\n"; - cin>>newvalue; - d.insert_specified(newvalue); - break; - case 7: - int newvalue1; - cout<<"enter value for searching position\n"; - cin>>newvalue1; - d.search_element(newvalue1); - break; - case 8: - int newvalue2; - cout<<"enter position desired for deletion \n"; - cin>>newvalue2; - d.deleteFromN(newvalue2); - break; - case 9: - d.reverse(); - break; - default: - cout<<"exit successfull";}} + d.deleteBeginning(); + break; + case 4: + d.deleteEnd(); + break; + case 5: + d.display(); + break; + case 6: + int newvalue; + cout<<"enter value for desired position\n"; + cin>>newvalue; + d.insert_specified(newvalue); + break; + case 7: + int newvalue1; + cout<<"enter value for searching position\n"; + cin>>newvalue1; + d.search_element(newvalue1); + break; + case 8: + int newvalue2; + cout<<"enter position desired for deletion \n"; + cin>>newvalue2; + d.deleteFromN(newvalue2); + break; + case 9: + d.reverse(); + break; + default: + cout<<"exit successfull"; + } + } } diff --git a/doubly_linkedlist.cpp.orig b/doubly_linkedlist.cpp.orig new file mode 100644 index 0000000..b5193c3 --- /dev/null +++ b/doubly_linkedlist.cpp.orig @@ -0,0 +1,264 @@ +#include +using namespace std; +struct node +{ + int data; + node *prev; + node *next; +}; + +class doublylist{ +private: + node *head,*tail; +public: + doublylist() + { + head=NULL; + tail=NULL; + } + void addtohead(int value) + { + node *newNode=new node; + newNode -> data = value; + newNode -> prev = NULL; + if(head == NULL) + { + newNode -> next = NULL; + head = newNode; + } + else + { + newNode -> next = head; + head = newNode; + } + cout<<"\nInsertion successful at the head"< data = v; + newNode -> next = NULL; + if(head == NULL) + { + newNode -> prev= NULL; + head = newNode; + } + else + { node *temp = head; + while(temp -> next != NULL) + temp = temp -> next; + temp -> next = newNode; + newNode -> prev = temp; + } + cout<<"\nInsertion success!!! at the end"<prev== temp->next) + { + head = NULL; + delete temp; + } + else{ + head = temp->next; + head->prev = NULL; + delete temp; + } + cout<<"Deletion success at the front"<prev == temp->next) + { + head = NULL; + delete temp; + } + else{ + while(temp->next != NULL) + temp = temp->next; + temp->prev ->next = NULL; + delete temp; + } + cout<<"Deletion successful at the end "<>loc; + temp=head; + for(int i=0;inext; + } + ptr->data = item; + ptr->next = temp->next; + ptr -> prev = temp; + temp->next = ptr; + temp->next->prev=ptr; + cout<<"Node Inserted at the desired position \n"; + } +} +void search_element(int data) +{ + node * temp = new node; + temp = head; + int pos = 0; + int flag=0; + while(temp != NULL) + { + if(temp -> data == data) // Element is found + { + + flag=1;//If found, print and exit + break; + + } + else + { + temp = temp ->next; //If not found, traverse the list + pos++; + } + } + if(flag==1) + { + cout<<"\nThe element is found in the list"; + } + else + { + cout<<"\nThe element is not found in the list"; + } +} +void deleteFromN(int position) +{ + node *current; + current = head; + for(int i=1; inext; + } + if(position == 0) + { + deleteBeginning(); + } + else if(current != NULL) + { + current->prev->next = current->next; + current->next->prev = current->prev; + delete current; + cout<<"SUCCESSFULLY DELETED NODE FROM POSITION.\n"; + } + else + { + cout<<"Invalid position!\n"; + } +} +void reverse() +{ + node *current = head; + node *temp = NULL; + while(current!=NULL) + { + temp = current->prev; //swap the next and prev pointer + current->prev = current->next; + current->next = temp; + current = current->prev; + } + temp->prev; + cout<<"LIST REVERSED SUCCESSFULLY.\n"; +} + void display() +{ + node *ptr; + cout<<"printing values"<data<<"->"<<" "; + ptr=ptr->next; + } +} +}; +int main() +{doublylist d; + while(1) + { int n; + cout<>n; + switch(n) + { + case 1: + int m; + cout<<"enter item to add to head"<>m; + d.addtohead(m); + break; + case 2: + int item; + cout<<"enter item to add to head"<>item; + d.insertAtEnd(item); + break; + case 3: + d.deleteBeginning(); + break; + case 4: + d.deleteEnd(); + break; + case 5: + d.display(); + break; + case 6: + int newvalue; + cout<<"enter value for desired position\n"; + cin>>newvalue; + d.insert_specified(newvalue); + break; + case 7: + int newvalue1; + cout<<"enter value for searching position\n"; + cin>>newvalue1; + d.search_element(newvalue1); + break; + case 8: + int newvalue2; + cout<<"enter position desired for deletion \n"; + cin>>newvalue2; + d.deleteFromN(newvalue2); + break; + case 9: + d.reverse(); + break; + default: + cout<<"exit successfull";}} +} diff --git a/encapsulation.cpp b/encapsulation.cpp index c9956f9..cae4cc0 100644 --- a/encapsulation.cpp +++ b/encapsulation.cpp @@ -1,30 +1,30 @@ #include using namespace std; -class ExampleEncap{ +class ExampleEncap { private: - int num; - char ch; + int num; + char ch; public: - int getNum() const { - return num; - } - char getCh() const { - return ch; - } + int getNum() const { + return num; + } + char getCh() const { + return ch; + } - void setNum(int num) { - this->num = num; - } - void setCh(char ch) { - this->ch = ch; - } + void setNum(int num) { + this->num = num; + } + void setCh(char ch) { + this->ch = ch; + } }; -int main(){ - ExampleEncap obj; - obj.setNum(100); - obj.setCh('A'); - cout< +using namespace std; +class ExampleEncap{ +private: + int num; + char ch; +public: + int getNum() const { + return num; + } + char getCh() const { + return ch; + } + + void setNum(int num) { + this->num = num; + } + void setCh(char ch) { + this->ch = ch; + } +}; +int main(){ + ExampleEncap obj; + obj.setNum(100); + obj.setCh('A'); + cout<next = tmp; tail = tail->next; } cout<<"succesfully added to tail"; } - void addtohead(int val) - { node *new_node = new node; - new_node->data = val; - new_node->next = NULL; - if (head == NULL) - { - head = new_node; - } else - { - new_node->next = head; - head= new_node; - } - cout<<"succesfully added to head"; + void addtohead(int val) + { node *new_node = new node; + new_node->data = val; + new_node->next = NULL; + if (head == NULL) + { + head = new_node; + } else + { + new_node->next = head; + head= new_node; + } + cout<<"succesfully added to head"; } - void randominsert(int itemn) - { + void randominsert(int itemn) + { node *ptr = new node; - struct node *temp; - int loc; - cout<<"Enter the location"; - cin>>loc; - - ptr->data = itemn; - temp=head; - for(int i=0;inext; - if(temp == NULL) - { - cout<<"\ncan't insert\n"; - return; - } - } - ptr ->next = temp ->next; - temp ->next = ptr; - cout<<"\nNode inserted at desired position"; - } - void begdelete() - { - node *ptr; - if(head == NULL) - { - cout<<"\nList is empty"; - } - else - { - ptr = head; - head = ptr->next; - delete ptr; - cout<<"\n Node deleted from the begining ..."; - } - } -void end_delete() - { - struct node *ptr,*ptr1; - if(head == NULL) - { - cout<<"\nlist is empty"; - } - else if(head -> next == NULL) - { - head = NULL; - delete head; - cout<<"\nOnly node of the list deleted ..."; - } - else - { ptr = head; - while(ptr->next != NULL) - { - ptr1 = ptr; - ptr = ptr ->next; - } - ptr1->next = NULL; - delete ptr; - cout<<"\n Deleted Node from the last ..."; - } - } -void search(int key) -{ - struct node *search; - search=head; - while (search->next!= NULL) + struct node *temp; + int loc; + cout<<"Enter the location"; + cin>>loc; + + ptr->data = itemn; + temp=head; + for(int i=0; inext; + if(temp == NULL) + { + cout<<"\ncan't insert\n"; + return; + } + } + ptr ->next = temp ->next; + temp ->next = ptr; + cout<<"\nNode inserted at desired position"; + } + void begdelete() { - if (search->data == key) + node *ptr; + if(head == NULL) { - cout<<"element is found in list\n"; - return; + cout<<"\nList is empty"; + } + else + { + ptr = head; + head = ptr->next; + delete ptr; + cout<<"\n Node deleted from the begining ..."; } - search = search->next; - } - cout<<"item not found in list\n"; -} -void Delete(int anypos) -{ node* temp1 = head; - if(anypos == 1){ - head = temp1->next; - delete temp1; - cout<<"succesfully deleted from desired positon in list\n"< next == NULL) + { + head = NULL; + delete head; + cout<<"\nOnly node of the list deleted ..."; + } + else + { ptr = head; + while(ptr->next != NULL) + { + ptr1 = ptr; + ptr = ptr ->next; + } + ptr1->next = NULL; + delete ptr; + cout<<"\n Deleted Node from the last ..."; + } + } + void search(int key) { - temp1 = temp1->next; + struct node *search; + search=head; + while (search->next!= NULL) + { + if (search->data == key) + { + cout<<"element is found in list\n"; + return; + } + search = search->next; + } + cout<<"item not found in list\n"; } - node* temp2 = temp1->next; - temp1->next = temp2->next; - delete temp2; - cout<<"succesfully deleted from desired positon in list\n"<next; - curNode->next = head; - head = curNode; - curNode = nxtNode; + void Delete(int anypos) + { node* temp1 = head; + if(anypos == 1) { + head = temp1->next; + delete temp1; + cout<<"succesfully deleted from desired positon in list\n"<next; + } + node* temp2 = temp1->next; + temp1->next = temp2->next; + delete temp2; + cout<<"succesfully deleted from desired positon in list\n"<next; + curNode->next = head; + head = curNode; + curNode = nxtNode; + } + } + void display() { node *tmp; tmp = head; @@ -162,69 +162,69 @@ void display() } }; int main() -{ singlelist l; - int n; - while(1) - { cout<>n; - switch(n) -{ - case 1: - int item; - cout<<"enter item to tail"<>item; - l.addtotail(item); - break; - case 2: - int item1; - cout<<"enter item to tail"<>item1; - l.addtohead(item1); - break; - case 3: - int iteminsert; - cout<<"enter item to desired position"<>iteminsert; - l.randominsert(iteminsert); - break; - case 4: - l.begdelete(); - break; - case 5: - l.end_delete(); - break; - case 6: - int value; - cout<<"enter item you want to search"<>value; - l.search(value); - break; - case 7: - int delpos; - cout<<"enter position you want to delete"<>delpos; - l.Delete(delpos); - break; + cout<<"8. display"<>n; + switch(n) + { + case 1: + int item; + cout<<"enter item to tail"<>item; + l.addtotail(item); + break; + case 2: + int item1; + cout<<"enter item to tail"<>item1; + l.addtohead(item1); + break; + case 3: + int iteminsert; + cout<<"enter item to desired position"<>iteminsert; + l.randominsert(iteminsert); + break; + case 4: + l.begdelete(); + break; + case 5: + l.end_delete(); + break; + case 6: + int value; + cout<<"enter item you want to search"<>value; + l.search(value); + break; + case 7: + int delpos; + cout<<"enter position you want to delete"<>delpos; + l.Delete(delpos); + break; - case 8: - l.display(); - break; + case 8: + l.display(); + break; - case 9: - l.reverse(); - break; - default: - cout<<"nothing"< +using namespace std; +struct node +{ + int data; + node *next; +}; +class singlelist +{ + private: + node *tail,*head; + public: + singlelist() + { head=NULL; + tail=NULL; + } + void addtotail(int n) + { + node *tmp = new node; + tmp->data = n; + tmp->next = NULL; + if(head == NULL) + { + head = tmp; + tail = tmp; + }else + { + tail->next = tmp; + tail = tail->next; + } + cout<<"succesfully added to tail"; + } + void addtohead(int val) + { node *new_node = new node; + new_node->data = val; + new_node->next = NULL; + if (head == NULL) + { + head = new_node; + } else + { + new_node->next = head; + head= new_node; + } + cout<<"succesfully added to head"; + } + void randominsert(int itemn) + { + node *ptr = new node; + struct node *temp; + int loc; + cout<<"Enter the location"; + cin>>loc; + + ptr->data = itemn; + temp=head; + for(int i=0;inext; + if(temp == NULL) + { + cout<<"\ncan't insert\n"; + return; + } + } + ptr ->next = temp ->next; + temp ->next = ptr; + cout<<"\nNode inserted at desired position"; + } + void begdelete() + { + node *ptr; + if(head == NULL) + { + cout<<"\nList is empty"; + } + else + { + ptr = head; + head = ptr->next; + delete ptr; + cout<<"\n Node deleted from the begining ..."; + } + } +void end_delete() + { + struct node *ptr,*ptr1; + if(head == NULL) + { + cout<<"\nlist is empty"; + } + else if(head -> next == NULL) + { + head = NULL; + delete head; + cout<<"\nOnly node of the list deleted ..."; + } + else + { ptr = head; + while(ptr->next != NULL) + { + ptr1 = ptr; + ptr = ptr ->next; + } + ptr1->next = NULL; + delete ptr; + cout<<"\n Deleted Node from the last ..."; + } + } +void search(int key) +{ + struct node *search; + search=head; + while (search->next!= NULL) + { + if (search->data == key) + { + cout<<"element is found in list\n"; + return; + } + search = search->next; + } + cout<<"item not found in list\n"; +} +void Delete(int anypos) +{ node* temp1 = head; + if(anypos == 1){ + head = temp1->next; + delete temp1; + cout<<"succesfully deleted from desired positon in list\n"<next; + } + node* temp2 = temp1->next; + temp1->next = temp2->next; + delete temp2; + cout<<"succesfully deleted from desired positon in list\n"<next; + curNode->next = head; + head = curNode; + curNode = nxtNode; + } +} +void display() + { + node *tmp; + tmp = head; + while (tmp != NULL) + { + cout << tmp->data <<"->"<<" "; + tmp = tmp->next; + } + } +}; +int main() +{ singlelist l; + int n; + while(1) + { cout<>n; + switch(n) +{ + case 1: + int item; + cout<<"enter item to tail"<>item; + l.addtotail(item); + break; + case 2: + int item1; + cout<<"enter item to tail"<>item1; + l.addtohead(item1); + break; + case 3: + int iteminsert; + cout<<"enter item to desired position"<>iteminsert; + l.randominsert(iteminsert); + break; + case 4: + l.begdelete(); + break; + case 5: + l.end_delete(); + break; + case 6: + int value; + cout<<"enter item you want to search"<>value; + l.search(value); + break; + case 7: + int delpos; + cout<<"enter position you want to delete"<>delpos; + l.Delete(delpos); + break; + + case 8: + l.display(); + break; + + case 9: + l.reverse(); + break; + default: + cout<<"nothing"< using namespace std; - class mayank - { +class mayank +{ - public: - static int count; - mayank() - { - count++; - } - static int display() - { - cout<<"no of objects"< +using namespace std; + class mayank + { + + public: + static int count; + mayank() + { + count++; + } + static int display() + { + cout<<"no of objects"<num =num; - this->ch=ch; - } - void displayMyValues(){ - cout<num =num; + this->ch=ch; + } + void displayMyValues() { + cout< +using namespace std; +class Demo { +private: + int num; + char ch; +public: + void setMyValues(int num, char ch){ + this->num =num; + this->ch=ch; + } + void displayMyValues(){ + cout< Date: Sat, 2 Oct 2021 21:43:50 +0530 Subject: [PATCH 3/3] Update mergesort.cpp --- sorting/mergesort.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting/mergesort.cpp b/sorting/mergesort.cpp index 53627a4..1c503ef 100644 --- a/sorting/mergesort.cpp +++ b/sorting/mergesort.cpp @@ -1,4 +1,4 @@ -#include +#include using namespace std; void merge(int *a,int s,int e){