Skip to content

Commit

Permalink
Merge pull request #1040 from Kavs-123/master
Browse files Browse the repository at this point in the history
 C++Programs added on Arrays and LinkedList
  • Loading branch information
fineanmol committed Oct 20, 2021
2 parents aaa5fe7 + 5362abe commit a4057da
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 0 deletions.
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;
}
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;
}
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;
}
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";
}
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;
}
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;
}

0 comments on commit a4057da

Please sign in to comment.