-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1040 from Kavs-123/master
C++Programs added on Arrays and LinkedList
- Loading branch information
Showing
6 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
Program's_Contributed_By_Contributors/C++_Programs/Arrays/Duplicate_Elements.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include<iostream> | ||
|
||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int i,j,a[50],size; | ||
cout<<"Enter array size( Max:50 ) :: "; | ||
cin>>size; | ||
cout<<"\nEnter array elements :: \n"; | ||
for(i=0; i<size; i++) | ||
{ | ||
cout<<"\nEnter arr["<<i<<"] Element :: "; | ||
cin>>a[i]; | ||
} | ||
|
||
cout<<"\nStored Data in Array :: \n\n"; | ||
|
||
for(i=0;i<size;i++) | ||
{ | ||
cout<<" "<<a[i]<<" "; | ||
} | ||
|
||
cout<<"\n\nDuplicate Values in Given Array are :: \n\n"; | ||
for(i=0; i<size; i++) | ||
{ | ||
for(j=i+1;j<size;j++) | ||
{ | ||
if(a[i]==a[j]) | ||
{ | ||
cout<<" "<<a[i]<<" "; | ||
} | ||
} | ||
} | ||
|
||
cout<<"\n"; | ||
|
||
return 0; | ||
} |
35 changes: 35 additions & 0 deletions
35
Program's_Contributed_By_Contributors/C++_Programs/Arrays/Largest_Element.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include<iostream> | ||
|
||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int i, a[50] ,size; | ||
cout<<"Enter array size( Max:50 ) :: "; | ||
cin>>size; | ||
cout<<"\nEnter array elements :: \n"; | ||
for(i=0; i<size; i++) | ||
{ | ||
cout<<"\nEnter arr["<<i<<"] Element :: "; | ||
cin>>a[i]; | ||
} | ||
|
||
cout<<"\nStored Data in Array :: \n\n"; | ||
|
||
for(i=0;i<size;i++) | ||
{ | ||
cout<<" "<<a[i]<<" "; | ||
} | ||
|
||
int largest=a[0]; | ||
|
||
for (i=0;i<size;i++) | ||
{ | ||
if(a[i]>largest) | ||
{ | ||
largest=a[i]; | ||
} | ||
} | ||
cout<<"\n\nLargest Element in an Array :: "<<largest<<endl; | ||
return 0; | ||
} |
40 changes: 40 additions & 0 deletions
40
Program's_Contributed_By_Contributors/C++_Programs/Arrays/Reverse_Elements.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include<iostream> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int a[20],b[20],i,j,size; | ||
|
||
cout<<"Enter array size( Max:50 ) :: "; | ||
cin>>size; | ||
|
||
cout<<"\nEnter array elements :: \n"; | ||
for(i=0; i<size; i++) | ||
{ | ||
cout<<"\nEnter arr["<<i<<"] Element :: "; | ||
cin>>a[i]; | ||
} | ||
|
||
cout<<"\nThe Entered Array is :: \n\n"; | ||
|
||
for(i=0;i<size;i++) | ||
{ | ||
cout<<" "<<a[i]<<" "; | ||
} | ||
|
||
cout<<"\n\nReverse of Given Array is :: \n\n"; | ||
|
||
for(i=size-1,j=0; i>=0;i--,j++) | ||
{ | ||
b[i]=a[j]; | ||
} | ||
|
||
for(i=0;i<size;i++) | ||
{ | ||
cout<<" "<<b[i]<<" "; | ||
} | ||
|
||
cout<<"\n"; | ||
|
||
return 0; | ||
} |
59 changes: 59 additions & 0 deletions
59
Program's_Contributed_By_Contributors/C++_Programs/Linked List/Delete.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Node { | ||
public: | ||
int data; | ||
Node* next; | ||
}; | ||
|
||
/*delete entire linked list */ | ||
void deleteList(Node** head_ref) | ||
{ | ||
|
||
|
||
Node* current = *head_ref; | ||
Node* next = NULL; | ||
|
||
while (current != NULL) | ||
{ | ||
next = current->next; | ||
free(current); | ||
current = next; | ||
} | ||
|
||
*head_ref = NULL; | ||
} | ||
|
||
// push a new node on the front of the list. | ||
|
||
void push(Node** head_ref, int new_data) | ||
{ | ||
|
||
Node* new_node = new Node(); | ||
|
||
|
||
new_node->data = new_data; | ||
new_node->next = (*head_ref); | ||
|
||
(*head_ref) = new_node; | ||
} | ||
|
||
/* Driver code*/ | ||
int main() | ||
{ | ||
|
||
Node* head = NULL; | ||
|
||
|
||
push(&head, 1); | ||
push(&head, 4); | ||
push(&head, 1); | ||
push(&head, 12); | ||
push(&head, 1); | ||
|
||
cout << "Deleting linked list"; | ||
deleteList(&head); | ||
|
||
cout << "\nLinked list deleted"; | ||
} |
60 changes: 60 additions & 0 deletions
60
Program's_Contributed_By_Contributors/C++_Programs/Linked List/Detect_Loop.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using namespace std; | ||
|
||
|
||
struct Node { | ||
int data; | ||
struct Node* next; | ||
int flag; | ||
}; | ||
|
||
void push(struct Node** head_ref, int new_data) | ||
{ | ||
/* allocate node */ | ||
struct Node* new_node = new Node; | ||
|
||
|
||
new_node->data = new_data; | ||
new_node->flag = 0; | ||
|
||
/* linking the old list off the new node */ | ||
new_node->next = (*head_ref); | ||
|
||
(*head_ref) = new_node; | ||
} | ||
|
||
bool detectLoop(struct Node* h) | ||
{ | ||
while (h != NULL) | ||
{ | ||
|
||
if (h->flag == 1) | ||
return true; | ||
|
||
h->flag = 1; | ||
|
||
h = h->next; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/* Driver program */ | ||
int main() | ||
{ | ||
/* Start with the empty list */ | ||
struct Node* head = NULL; | ||
|
||
push(&head, 20); | ||
push(&head, 4); | ||
push(&head, 15); | ||
push(&head, 10); | ||
|
||
head->next->next->next->next = head; | ||
|
||
if (detectLoop(head)) | ||
cout << "Loop found"; | ||
else | ||
cout << "No Loop"; | ||
|
||
return 0; | ||
} |
66 changes: 66 additions & 0 deletions
66
Program's_Contributed_By_Contributors/C++_Programs/Linked List/Find_Middle.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
|
||
class NodeOperation{ | ||
public: | ||
|
||
// add a new node | ||
void pushNode(class Node** head_ref,int data_val) | ||
{ | ||
|
||
|
||
class Node *new_node = new Node(); | ||
|
||
new_node->data = data_val; | ||
new_node->next = *head_ref; | ||
|
||
*head_ref = new_node; | ||
} | ||
|
||
// print a given linked list | ||
void printNode(class Node *head) | ||
{ | ||
while(head != NULL) | ||
{ | ||
cout <<head->data << "->"; | ||
head = head->next; | ||
} | ||
cout << "NULL" << endl; | ||
} | ||
|
||
void printMiddle(class Node *head) | ||
{ | ||
struct Node *slow_ptr = head; | ||
struct Node *fast_ptr = head; | ||
|
||
if (head!=NULL) | ||
{ | ||
while (fast_ptr != NULL && fast_ptr->next != NULL) | ||
{ | ||
fast_ptr = fast_ptr->next->next; | ||
slow_ptr = slow_ptr->next; | ||
} | ||
cout << "The middle element is [" << slow_ptr->data << "]" << endl; | ||
} | ||
} | ||
}; | ||
|
||
// Driver Code | ||
int main() | ||
{ | ||
class Node *head = NULL; | ||
class NodeOperation *temp = new NodeOperation(); | ||
for(int i=5; i>0; i--) | ||
{ | ||
temp->pushNode(&head, i); | ||
temp->printNode(head); | ||
temp->printMiddle(head); | ||
} | ||
return 0; | ||
} |