Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Length of Linked List #20

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions insertionInLinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Alternate method to declare the class
// in order to minimize the
// memory allocation work

#include <bits/stdc++.h>
using namespace std;

class node {
public:
int data;
node* next;

// A constructor is called here
node(int value)
{

// It automatic assigns the
// value to the data
data = value;

// Next pointer is pointed to NULL
next = NULL;
}
};

// Function to insert an element
// at head position
void insertathead(node*& head, int val)
{
node* n = new node(val);
n->next = head;
head = n;
}

// Function to insert a element
// at a specified position
void insertafter(node* head, int key, int val)
{
node* n = new node(val);
if (key == head->data) {
n->next = head->next;
head->next = n;
return;
}

node* temp = head;
while (temp->data != key) {
temp = temp->next;
if (temp == NULL) {
return;
}
}
n->next = temp->next;
temp->next = n;
}

// Function to insert an
// element at the end
void insertattail(node*& head, int val)
{
node* n = new node(val);
if (head == NULL) {
head = n;
return;
}

node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = n;
}

// Function to print the
// singly linked list
void print(node*& head)
{
node* temp = head;

while (temp != NULL) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}

// Main function
int main()
{

// Declaring an empty linked list
node* head = NULL;

insertathead(head, 1);
insertathead(head, 2);
cout << "After insertion at head: ";
print(head);
cout << endl;

insertattail(head, 4);
insertattail(head, 5);
cout << "After insertion at tail: ";
print(head);
cout << endl;

insertafter(head, 1, 2);
insertafter(head, 5, 6);
cout << "After insertion at a given position: ";
print(head);
cout << endl;

return 0;
}
// contributed by divyanshmishra101010
61 changes: 61 additions & 0 deletions lengthOfLinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Tail Recursive C++ program to find length
// or count of nodes in a linked list
#include <bits/stdc++.h>
using namespace std;

/* Link list node */
class Node {
public:
int data;
Node* next;
};

/* Given a reference (pointer to pointer) to the head
of a list and an int, push a new node on the front
of the list. */
void push(Node** head_ref, int new_data)
{
/* allocate node */
Node* new_node = new Node();

/* put in the data */
new_node->data = new_data;

/* 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 tail recursive function to count the nodes of a linked
// list
// default value of the count is used as zero
int getCount(Node* head, int count = 0)
{
// base case
if (head == NULL)
return count;
// move the pointer to next node and increase the count
return getCount(head->next, count + 1);
}

/* Driver code*/
int main()
{
/* Start with the empty list */
Node* head = NULL;

/* Use push() to construct below list
1->2->1->3->1 */
push(&head, 1);
push(&head, 3);
push(&head, 1);
push(&head, 2);
push(&head, 1);

// Function call
cout << "Count of nodes is " << getCount(head);
return 0;
}

// This is code is contributed by Abhijeet Kumar