Implementation of Linked List
Below is the implementation of Linked List in different languages:
// C++ program to show inserting a node
// at front of given Linked List
#include <bits/stdc++.h>
using namespace std;
// A linked list node
class Node {
public:
int data;
Node* next;
};
// Given a reference (pointer to pointer)
// to the head of a list and an int, inserts
// a new node on the front of the list.
void insertAtFront(Node** head_ref, int new_data)
{
// 1. allocate node
Node* new_node = new Node();
// 2. put in the data
new_node->data = new_data;
// 3. Make next of new node as head
new_node->next = (*head_ref);
// 4. move the head to point
// to the new node
(*head_ref) = new_node;
}
/* Function to remove the first node
of the linked list */
Node* removeFirstNode(Node* head)
{
if (head == NULL)
return NULL;
// Move the head pointer to the next node
Node* temp = head;
head = head->next;
delete temp;
return head;
}
// This function prints contents of
// linked list starting from head
void printList(Node* node)
{
while (node != NULL) {
cout << " " << node->data;
node = node->next;
}
cout << "\n";
}
// Driver code
int main()
{
// Start with the empty list
Node* head = NULL;
// Insert 1 at the beginning.
insertAtFront(&head, 6);
insertAtFront(&head, 5);
insertAtFront(&head, 4);
insertAtFront(&head, 3);
insertAtFront(&head, 2);
insertAtFront(&head, 1);
cout << "After inserting nodes at the front:";
printList(head);
head = removeFirstNode(head);
cout << "After removing first node:";
printList(head);
return 0;
}
// Java program to show inserting a node
// at front of given Linked List
import java.io.*;
// A linked list node
class Node {
int data;
Node next;
}
// Class to insert element in LL
class LinkedList {
Node head; // head of list
// Given a reference (pointer to pointer)
// to the head of a list and an int, inserts
// a new node on the front of the list.
void insertAtFront(int new_data)
{
// 1. allocate node
Node new_node = new Node();
// 2. put in the data
new_node.data = new_data;
// 3. Make next of new node as head
new_node.next = head;
// 4. move the head to point
// to the new node
head = new_node;
}
// Function to remove the first node
// of the linked list /
void removeFirstNode()
{
if (head == null)
return;
// Move the head pointer to the next node
Node temp = head;
head = head.next;
}
// This function prints contents of
// linked list starting from head
void printList()
{
Node node = head;
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
System.out.println();
}
// Driver code
public static void main(String[] args)
{
// Start with the empty list
LinkedList list = new LinkedList();
list.insertAtFront(6);
list.insertAtFront(5);
list.insertAtFront(4);
list.insertAtFront(3);
list.insertAtFront(2);
list.insertAtFront(1);
System.out.print("After inserting nodes at the front: ");
list.printList();
list.removeFirstNode();
System.out.print("After removing first node: ");
list.printList();
}
}
# Python3 program to show inserting a node
# at front of given Linked List
# A linked list node
class Node:
def __init__(self):
self.data = None
self.next = None
# Given a reference (pointer to pointer)
# to the head of a list and an int, inserts
# a new node on the front of the list.
def insertAtFront(head_ref, new_data):
# 1. allocate node
new_node = Node()
# 2. put in the data
new_node.data = new_data
# 3. Make next of new node as head
new_node.next = head_ref
# 4. move the head to point
# to the new node
head_ref = new_node
return head_ref
# Function to remove the first node
# of the linked list
def removeFirstNode(head):
if not head:
return None
temp = head
# Move the head pointer to the next node
head = head.next
temp = None
return head
# This function prints contents of
# linked list starting from head
def printList(node):
while (node != None):
print(node.data, end=" ")
node = node.next
print()
# Driver code
if __name__ == '__main__':
# Start with the empty list
head = None
head = insertAtFront(head, 6)
head = insertAtFront(head, 5)
head = insertAtFront(head, 4)
head = insertAtFront(head, 3)
head = insertAtFront(head, 2)
head = insertAtFront(head, 1)
print("After inserting nodes at the front: ", end = "")
printList(head)
head = removeFirstNode(head)
print("After removing first node: ", end = "")
printList(head)
// C# program to show inserting a node
// at front of given Linked List
using System;
class GFG {
public Node head; // head of list
/* Linked list Node*/
public class Node {
public int data;
public Node next;
public Node(int d)
{
data = d;
next = null;
}
}
/* Inserts a new Node at front of the list. */
public void insertAtFront(int new_data)
{
/* 1 & 2: Allocate the Node &
Put in the data*/
Node new_node = new Node(new_data);
/* 3. Make next of new Node as head */
new_node.next = head;
/* 4. Move the head to point to new Node */
head = new_node;
}
// Function to remove the first node
// of the linked list /
public void removeFirstNode()
{
if (head == null)
return ;
// Move the head pointer to the next node
head = head.next;
}
/* This function prints contents of linked list
starting from the given node */
public void printList()
{
Node tnode = head;
while (tnode != null) {
Console.Write(tnode.data + " ");
tnode = tnode.next;
}
Console.WriteLine();
}
// Driver Code
public static void Main(String[] args)
{
/* Start with the empty list */
GFG llist = new GFG();
llist.insertAtFront(6);
llist.insertAtFront(5);
llist.insertAtFront(4);
llist.insertAtFront(3);
llist.insertAtFront(2);
llist.insertAtFront(1);
Console.Write("After inserting nodes at the front: ");
llist.printList();
llist.removeFirstNode();
Console.Write("After removing first node: ");
llist.printList();
}
}
// A linked list node
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Given a reference (pointer to pointer)
// to the head of a list and an int, inserts
// a new node on the front of the list.
function insertAtFront(head_ref, new_data) {
// 1. Create a new node
const new_node = new Node(new_data);
// 2. Make the new node point to the current head
new_node.next = head_ref[0];
// 3. Update the head to the new node
head_ref[0] = new_node;
}
// Function to remove the first node
// of the linked list /
function removeFirstNode( head) {
if (head == null)
return null;
// Move the head pointer to the next node
temp = head;
head = head.next;
return head;
}
// This function prints contents of
// linked list starting from head
function printList(node) {
let current = node;
while (current !== null) {
console.log(" " + current.data);
current = current.next;
}
console.log("\n");
}
// Driver code
function main() {
// Start with an empty list
let head = [null]; // Use an array to simulate a pointer to pointer
// Insert elements at the beginning
insertAtFront(head, 1);
insertAtFront(head, 2);
insertAtFront(head, 3);
insertAtFront(head, 4);
insertAtFront(head, 5);
insertAtFront(head, 6);
console.log("After inserting Nodes at the front:");
// The nodes will be: 6 5 4 3 2 1
printList(head[0]);
head[0] = removeFirstNode(head[0]);
console.log("After removing first node:");
// The nodes will be: 5 4 3 2 1
printList(head[0]);
}
main();
Output
After inserting nodes at the front: 1 2 3 4 5 6 After removing first node: 2 3 4 5 6
Introduction to Linked List – Data Structure and Algorithm Tutorials
Linked List is basically chains of nodes where each node contains information such as data and a pointer to the next node in the chain. It is a popular data structure with a wide range of real-world applications. In this article, we will provide a complete introduction of Linked List, which will help you tackle any problem based on Linked List.
Table of Content
- What is a Linked List?
- Basic Terminologies of Linked List
- Importance of Linked List
- Types of Linked List
- Singly Linked List
- Doubly Linked List
- Circular Linked List
- Implementation of Linked List
- Linked List vs. Array
- Advantages of Linked List
- Disadvantages of Linked List
- Applications of Linked List
- Frequently Asked Questions (FAQs) about Linked list
Contact Us