Create print-middle-element-of-linked-list.CPP

pull/861/head
Yashika Gupta 2022-10-01 01:53:25 +05:30 committed by GitHub
parent b67f5e1786
commit 0d8a5e101d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,84 @@
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class NodeOperation {
public:
// Function to add a new node
void pushNode(class Node** head_ref, int data_val)
{
// Allocate node
class Node* new_node = new Node();
// Put in the data
new_node->data = data_val;
// Link the old list off the new node
new_node->next = *head_ref;
// move the head to point to the new node
*head_ref = new_node;
}
// A utility function to print a given linked list
void printNode(class Node* head)
{
while (head != NULL) {
cout << head->data << "->";
head = head->next;
}
cout << "NULL" << endl;
}
/* Utility Function to find length of linked list */
int getLen(class Node* head)
{
int len = 0;
class Node* temp = head;
while (temp) {
len++;
temp = temp->next;
}
return len;
}
void printMiddle(class Node* head)
{
if (head) {
// find length
int len = getLen(head);
class Node* temp = head;
// trvaers till we reached half of length
int midIdx = len / 2;
while (midIdx--) {
temp = temp->next;
}
// temp will be storing middle element
cout << "The middle element is [" << temp->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;
}