Link of CPP list
preprocessor :-
https://www.tutorialspoint.com/cprogramming/c_preprocessors.htm
IPC :-
// A complete working C++ program to demonstrate
// all insertion methods on Linked List
#include <bits/stdc++.h>
using namespace std;
// A linked list node
class Node
{
public:
int data;
Node *next;
};
// inserts
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node(); /* 1. allocate node */
new_node->data = new_data; /* 2. put in the data */
new_node->next = (*head_ref); /* 3. Make next of new node as head */
(*head_ref) = new_node; /* 4. move the head to point to the new node */
}
void insertAfter(Node* prev_node, int new_data) ///* Given a node prev_node, insert a new node after the given prev_node */
{
if (prev_node == NULL) /*1. check if the given prev_node is NULL */
{
cout<<"the given previous node cannot be NULL";
return;
}
Node* new_node = new Node(); /* 2. allocate new node */
new_node->data = new_data; /* 3. put in the data */
new_node->next = prev_node->next; /* 4. Make next of new node as next of prev_node */
prev_node->next = new_node; /* 5. move the next of prev_node as new_node */
}
/* Given a reference (pointer to pointer) to the head
of a list and an int, appends a new node at the end */
void append(Node** head_ref, int new_data)
{
Node* new_node = new Node(); /* 1. allocate node */
Node *last = *head_ref; /* used in step 5*/
new_node->data = new_data; /* 2. put in the data */
new_node->next = NULL; /* 3. This new node is going to be the last node, so make next of it as NULL*/
if (*head_ref == NULL) /* 4. If the Linked List is empty, then make the new node as head */
{
*head_ref = new_node;
return;
}
while (last->next != NULL) /* 5. Else traverse till the last node */
last = last->next;
last->next = new_node; /* 6. Change the next of last node */
return;
}
// This function prints contents of
// linked list starting from head
void printList(Node *node)
{
while (node != NULL)
{
cout<<" "<<node->data;
node = node->next;
}
}
/* Driver code*/
int main()
{
Node* head = NULL; /* Start with the empty list */
append(&head, 6); // Insert 6. So linked list becomes 6->NULL
// Insert 7 at the beginning.
// So linked list becomes 7->6->NULL
push(&head, 7);
// Insert 1 at the beginning.
// So linked list becomes 1->7->6->NULL
push(&head, 1);
// Insert 4 at the end. So
// linked list becomes 1->7->6->4->NULL
append(&head, 4);
// Insert 8, after 7. So linked
// list becomes 1->7->8->6->4->NULL
insertAfter(head->next, 8);
cout<<"Created Linked list is: ";
printList(head);
return 0;
}
// This code is contributed by rathbhupendra
Output:
Created Linked list is: 1 7 8 6 4
===============================================================================
// C++ program to count number of nodes
// in loop in a linked list if loop is
// present
#include<bits/stdc++.h>
using namespace std;
/* Link list node */
struct Node
{
int data;
struct Node* next;
};
// Returns count of nodes present in loop.
int countNodes(struct Node *n)
{
int res = 1;
struct Node *temp = n;
while (temp->next != n)
{
res++;
temp = temp->next;
}
return res;
}
/* This function detects and counts loop
nodes in the list. If loop is not there
in then returns 0 */
int countNodesinLoop(struct Node *list)
{
struct Node *slow_p = list, *fast_p = list;
while (slow_p && fast_p &&
fast_p->next)
{
slow_p = slow_p->next;
fast_p = fast_p->next->next;
/* If slow_p and fast_p meet at
some point then there is a loop */
if (slow_p == fast_p)
return countNodes(slow_p);
}
/* Return 0 to indeciate that
their is no loop*/
return 0;
}
struct Node *newNode(int key)
{
struct Node *temp =
(struct Node*)malloc(sizeof(struct Node));
temp->data = key;
temp->next = NULL;
return temp;
}
// Driver Code
int main()
{
struct Node *head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(4);
head->next->next->next->next = newNode(5);
/* Create a loop for testing */
head->next->next->next->next->next = head->next;
cout << countNodesinLoop(head) << endl;
return 0;
}
// This code is contributed by SHUBHAMSINGH10
Comments
Post a Comment