How To Create Head Node In Linked List
Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at a contiguous location; the elements are linked using pointers.
Attention reader! Don't stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course .
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
Why Linked List?
Arrays can be used to store linear data of similar types, but arrays have the following limitations.
1) The size of the arrays is fixed: So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage.
2) Inserting a new element in an array of elements is expensive because the room has to be created for the new elements and to create room existing elements have to be shifted.
For example, in a system, if we maintain a sorted list of IDs in an array id[].
id[] = [1000, 1010, 1050, 2000, 2040].
And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the elements after 1000 (excluding 1000).
Deletion is also expensive with arrays until unless some special techniques are used. For example, to delete 1010 in id[], everything after 1010 has to be moved.
Advantages over arrays
1) Dynamic size
2) Ease of insertion/deletion
Drawbacks:
1) Random access is not allowed. We have to access elements sequentially starting from the first node. So we cannot do binary search with linked lists efficiently with its default implementation. Read about it here.
2) Extra memory space for a pointer is required with each element of the list.
3) Not cache friendly. Since array elements are contiguous locations, there is locality of reference which is not there in case of linked lists.
Representation:
A linked list is represented by a pointer to the first node of the linked list. The first node is called the head. If the linked list is empty, then the value of the head is NULL.
Each node in a list consists of at least two parts:
1) data
2) Pointer (Or Reference) to the next node
In C, we can represent a node using structures. Below is an example of a linked list node with integer data.
In Java or C#, LinkedList can be represented as a class and a Node as a separate class. The LinkedList class contains a reference of Node class type.
C
struct
Node {
int
data;
struct
Node* next;
};
C++
class
Node {
public
:
int
data;
Node* next;
};
Java
class
LinkedList {
Node head;
class
Node {
int
data;
Node next;
Node(
int
d) { data = d; }
}
}
Python
class
Node:
def
__init__(
self
, data):
self
.data
=
data
self
.
next
=
None
class
LinkedList:
def
__init__(
self
):
self
.head
=
None
C#
class
LinkedList {
Node head;
class
Node {
int
data;
Node next;
Node(
int
d) { data = d; }
}
}
Javascript
<script>
var
head;
class Node
{
constructor(val) {
this
.data = val;
this
.next =
null
;
}
}
</script>
First Simple Linked List in C Let us create a simple linked list with 3 nodes.
C++
#include <bits/stdc++.h>
using
namespace
std;
class
Node {
public
:
int
data;
Node* next;
};
int
main()
{
Node* head = NULL;
Node* second = NULL;
Node* third = NULL;
head =
new
Node();
second =
new
Node();
third =
new
Node();
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
return
0;
}
C
#include <stdio.h>
#include <stdlib.h>
struct
Node {
int
data;
struct
Node* next;
};
int
main()
{
struct
Node* head = NULL;
struct
Node* second = NULL;
struct
Node* third = NULL;
head = (
struct
Node*)
malloc
(
sizeof
(
struct
Node));
second = (
struct
Node*)
malloc
(
sizeof
(
struct
Node));
third = (
struct
Node*)
malloc
(
sizeof
(
struct
Node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
return
0;
}
Java
class
LinkedList {
Node head;
static
class
Node {
int
data;
Node next;
Node(
int
d)
{
data = d;
next =
null
;
}
}
public
static
void
main(String[] args)
{
LinkedList llist =
new
LinkedList();
llist.head =
new
Node(
1
);
Node second =
new
Node(
2
);
Node third =
new
Node(
3
);
llist.head.next = second;
second.next = third;
}
}
Python
class
Node:
def
__init__(
self
, data):
self
.data
=
data
self
.
next
=
None
class
LinkedList:
def
__init__(
self
):
self
.head
=
None
if
__name__
=
=
'__main__'
:
llist
=
LinkedList()
llist.head
=
Node(
1
)
second
=
Node(
2
)
third
=
Node(
3
)
llist.head.
next
=
second;
second.
next
=
third;
C#
using
System;
public
class
LinkedList {
Node head;
public
class
Node {
public
int
data;
public
Node next;
public
Node(
int
d)
{
data = d;
next =
null
;
}
}
public
static
void
Main(String[] args)
{
LinkedList llist =
new
LinkedList();
llist.head =
new
Node(1);
Node second =
new
Node(2);
Node third =
new
Node(3);
llist.head.next = second;
second.next = third;
}
}
Linked List Traversal
In the previous program, we have created a simple linked list with three nodes. Let us traverse the created list and print the data of each node. For traversal, let us write a general-purpose function printList() that prints any given list.
We strongly recommend that you click here and practice it, before moving on to the solution.
C++
#include <bits/stdc++.h>
using
namespace
std;
class
Node {
public
:
int
data;
Node* next;
};
void
printList(Node* n)
{
while
(n != NULL) {
cout << n->data <<
" "
;
n = n->next;
}
}
int
main()
{
Node* head = NULL;
Node* second = NULL;
Node* third = NULL;
head =
new
Node();
second =
new
Node();
third =
new
Node();
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
printList(head);
return
0;
}
C
#include <stdio.h>
#include <stdlib.h>
struct
Node {
int
data;
struct
Node* next;
};
void
printList(
struct
Node* n)
{
while
(n != NULL) {
printf
(
" %d "
, n->data);
n = n->next;
}
}
int
main()
{
struct
Node* head = NULL;
struct
Node* second = NULL;
struct
Node* third = NULL;
head = (
struct
Node*)
malloc
(
sizeof
(
struct
Node));
second = (
struct
Node*)
malloc
(
sizeof
(
struct
Node));
third = (
struct
Node*)
malloc
(
sizeof
(
struct
Node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
printList(head);
return
0;
}
Java
class
LinkedList {
Node head;
static
class
Node {
int
data;
Node next;
Node(
int
d)
{
this
.data = d;
next =
null
;
}
}
public
void
printList()
{
Node n = head;
while
(n !=
null
) {
System.out.print(n.data +
" "
);
n = n.next;
}
}
public
static
void
main(String[] args)
{
LinkedList llist =
new
LinkedList();
llist.head =
new
Node(
1
);
Node second =
new
Node(
2
);
Node third =
new
Node(
3
);
llist.head.next = second;
second.next = third;
llist.printList();
}
}
Python3
class
Node:
def
__init__(
self
, data):
self
.data
=
data
self
.
next
=
None
class
LinkedList:
def
__init__(
self
):
self
.head
=
None
def
printList(
self
):
temp
=
self
.head
while
(temp):
print
(temp.data)
temp
=
temp.
next
if
__name__
=
=
'__main__'
:
llist
=
LinkedList()
llist.head
=
Node(
1
)
second
=
Node(
2
)
third
=
Node(
3
)
llist.head.
next
=
second;
second.
next
=
third;
llist.printList()
C#
using
System;
public
class
LinkedList {
Node head;
public
class
Node {
public
int
data;
public
Node next;
public
Node(
int
d)
{
data = d;
next =
null
;
}
}
public
void
printList()
{
Node n = head;
while
(n !=
null
) {
Console.Write(n.data +
" "
);
n = n.next;
}
}
public
static
void
Main(String[] args)
{
LinkedList llist =
new
LinkedList();
llist.head =
new
Node(1);
Node second =
new
Node(2);
Node third =
new
Node(3);
llist.head.next = second;
second.next = third;
llist.printList();
}
}
Javascript
<script>
var
head;
class Node {
constructor(val) {
this
.data = val;
this
.next =
null
;
}
}
function
printList()
{
var
n = head;
while
(n !=
null
) {
document.write(n.data +
" "
);
n = n.next;
}
}
var
head =
new
Node(1);
var
second =
new
Node(2);
var
third =
new
Node(3);
head.next = second;
second.next = third;
printList();
</script>
Output:
1 2 3
Important Links :
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
How To Create Head Node In Linked List
Source: https://www.geeksforgeeks.org/linked-list-set-1-introduction/
Posted by: hangersaisuatecous1950.blogspot.com
0 Response to "How To Create Head Node In Linked List"
Post a Comment