Add the given digit to a number stored in a linked list
Here given code implementation process.
// C Program
// Add the given digit to a number stored in a linked list
#include <stdio.h>
#include <stdlib.h> //for malloc function
// Linked List LinkNode
struct LinkNode
{
int data;
struct LinkNode *next;
};
// Singly linked list
struct SingleLL
{
struct LinkNode *head;
struct LinkNode *tail;
};
// Returns the new linked list
struct SingleLL *newLinkedList()
{
// Create memory of head and tail Nodes
struct SingleLL *sll = (struct SingleLL *) malloc(sizeof(struct SingleLL));
if (sll == NULL)
{
printf("Memory overflow\n");
}
else
{
sll->head = NULL;
}
return sll;
}
// Returns a new Node of linked list
struct LinkNode *createLinkNode(int data)
{
// Create dynamic node
struct LinkNode *node = (struct LinkNode *) malloc(sizeof(struct LinkNode));
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
// Set initial node value
node->data = data;
node->next = NULL;
}
return node;
}
// Add new Node at end of linked list
void addNode(struct SingleLL *sll, int data)
{
if (data < 0)
{
printf("\n Number is negative : %d \n", data);
return;
}
else if (data > 9)
{
printf("\n Two digit number is not allowed : %d \n", data);
return;
}
struct LinkNode *node = createLinkNode(data);
if (sll->head == NULL)
{
sll->head = node;
}
else
{
struct LinkNode *temp = sll->head;
// Find last node
while (temp->next != NULL)
{
temp = temp->next;
}
// Append the node at last position
temp->next = node;
}
}
// Display linked list element
void display(struct SingleLL *sll)
{
if (sll->head == NULL)
{
printf("\n Empty linked list\n");
return;
}
struct LinkNode *temp = sll->head;
// iterating linked list elements
while (temp != NULL)
{
if (temp != sll->head)
{
printf("→");
}
printf(" %d ", temp->data);
// Visit to next node
temp = temp->next;
}
printf("→ NULL\n");
}
// Add a single digit, in number represent of linked list
void addDigit(struct SingleLL *sll, int digit)
{
if (digit < 0 || digit > 9)
{
printf("\n Invalid digit : %d\n", digit);
return;
}
else if (sll->head == NULL)
{
printf("\n Empty linked list\n");
return;
}
struct LinkNode *temp = sll->head;
struct LinkNode *auxiliary = NULL;
// Find last node and
// Detect last node which is contain value of less than 9,
// If it is exist
while (temp->next != NULL)
{
if (temp->data < 9)
{
auxiliary = temp;
}
// Visit to next node
temp = temp->next;
}
temp->data = temp->data + digit;
if (temp->data > 9)
{
// When need to update linked list
if (auxiliary == NULL)
{
// When auxiliary is NULL then assign first node to this
auxiliary = sll->head;
// Create a new node
struct LinkNode *node = createLinkNode(1);
// Add node at begining
node->next = sll->head;
// Make new head
sll->head = node;
}
// Manage last node
temp->data = temp->data % 10;
// Update the node value from auxiliary to second last node
while (auxiliary->next != NULL)
{
auxiliary->data = (auxiliary->data + 1) % 10;
// Visit to next node
auxiliary = auxiliary->next;
}
}
}
int main()
{
// Create a empty linked list
struct SingleLL *sll = newLinkedList();
// Constructed linked list
// 9 → 9 → 9 → 8 → 9 → NULL
addNode(sll, 9);
addNode(sll, 9);
addNode(sll, 9);
addNode(sll, 8);
addNode(sll, 9);
display(sll);
int digit = 9;
printf(" Add digit : %d \n", digit);
addDigit(sll, digit);
display(sll);
digit = 6;
printf(" Add digit : %d \n", digit);
addDigit(sll, digit);
display(sll);
digit = 5;
printf(" Add digit : %d \n", digit);
addDigit(sll, digit);
display(sll);
return 0;
}
Output
9 → 9 → 9 → 8 → 9 → NULL
Add digit : 9
9 → 9 → 9 → 9 → 8 → NULL
Add digit : 6
1 → 0 → 0 → 0 → 0 → 4 → NULL
Add digit : 5
1 → 0 → 0 → 0 → 0 → 9 → NULL
/*
Java Program for
Add the given digit to a number stored in a linked list
*/
// Linked list node
class LinkNode
{
public int data;
public LinkNode next;
public LinkNode(int data)
{
this.data = data;
this.next = null;
}
};
public class SingleLL
{
public LinkNode head;
public SingleLL()
{
this.head = null;
}
//Add new Node at end of linked list
public void addNode(int data)
{
if (data < 0)
{
System.out.print("\n Number is negative : " + data + " \n");
return;
}
else if (data > 9)
{
System.out.print("\n Two digit number is not allowed : " + data + " \n");
return;
}
LinkNode node = new LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
LinkNode temp = this.head;
//Find last node
while (temp.next != null)
{
temp = temp.next;
}
// Append the node at last position
temp.next = node;
}
}
// Display linked list element
public void display()
{
if (this.head == null)
{
System.out.print("\n Empty linked list\n");
return;
}
LinkNode temp = this.head;
//iterating linked list elements
while (temp != null)
{
if (temp != this.head)
{
System.out.print(" →");
}
System.out.print(" " + temp.data);
// Visit to next node
temp = temp.next;
}
System.out.print(" → NULL\n");
}
// Add a single digit, in number represent of linked list
public void addDigit(int digit)
{
if (digit < 0 || digit > 9)
{
System.out.print("\n Invalid digit : " + digit + "\n");
return;
}
else if (this.head == null)
{
System.out.print("\n Empty linked list\n");
return;
}
LinkNode temp = this.head;
LinkNode auxiliary = null;
// Find last node and
// Detect last node which is contain value of less than 9,
// If it is exist
while (temp.next != null)
{
if (temp.data < 9)
{
auxiliary = temp;
}
// Visit to next node
temp = temp.next;
}
temp.data = temp.data + digit;
if (temp.data > 9)
{
// When need to update linked list
if (auxiliary == null)
{
// When auxiliary is NULL then assign first node to this
auxiliary = this.head;
// Create a new node
LinkNode node = new LinkNode(1);
// Add node at begining
node.next = this.head;
// Make new head
this.head = node;
}
// Manage last node
temp.data = temp.data % 10;
// Update the node value from auxiliary to second last node
while (auxiliary.next != null)
{
auxiliary.data = (auxiliary.data + 1) % 10;
// Visit to next node
auxiliary = auxiliary.next;
}
}
}
public static void main(String[] args)
{
// Create a empty linked list
SingleLL sll = new SingleLL();
// Constructed linked list
// 9 → 9 → 9 → 8 → 9 → NULL
sll.addNode(9);
sll.addNode(9);
sll.addNode(9);
sll.addNode(8);
sll.addNode(9);
sll.display();
int digit = 9;
System.out.print(" Add digit : " + digit + " \n");
sll.addDigit(digit);
sll.display();
digit = 6;
System.out.print(" Add digit : " + digit + " \n");
sll.addDigit(digit);
sll.display();
digit = 5;
System.out.print(" Add digit : " + digit + " \n");
sll.addDigit(digit);
sll.display();
}
}
Output
9 → 9 → 9 → 8 → 9 → NULL
Add digit : 9
9 → 9 → 9 → 9 → 8 → NULL
Add digit : 6
1 → 0 → 0 → 0 → 0 → 4 → NULL
Add digit : 5
1 → 0 → 0 → 0 → 0 → 9 → NULL
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program for
Add the given digit to a number stored in a linked list
*/
// Linked list node
class LinkNode
{
public:
int data;
LinkNode *next;
LinkNode(int data)
{
this->data = data;
this->next = NULL;
}
};;
class SingleLL
{
public:
LinkNode *head;
SingleLL()
{
this->head = NULL;
}
//Add new Node at end of linked list
void addNode(int data)
{
if (data < 0)
{
cout << "\n Number is negative : " << data << " \n";
return;
}
else if (data > 9)
{
cout << "\n Two digit number is not allowed : " << data << " \n";
return;
}
LinkNode *node = new LinkNode(data);
if (this->head == NULL)
{
this->head = node;
}
else
{
LinkNode *temp = this->head;
//Find last node
while (temp->next != NULL)
{
temp = temp->next;
}
// Append the node at last position
temp->next = node;
}
}
// Display linked list element
void display()
{
if (this->head == NULL)
{
cout << "\n Empty linked list\n";
return;
}
LinkNode *temp = this->head;
//iterating linked list elements
while (temp != NULL)
{
if (temp != this->head)
{
cout << " →";
}
cout << " " << temp->data;
// Visit to next node
temp = temp->next;
}
cout << " → NULL\n";
}
// Add a single digit, in number represent of linked list
void addDigit(int digit)
{
if (digit < 0 || digit > 9)
{
cout << "\n Invalid digit : " << digit << "\n";
return;
}
else if (this->head == NULL)
{
cout << "\n Empty linked list\n";
return;
}
LinkNode *temp = this->head;
LinkNode *auxiliary = NULL;
// Find last node and
// Detect last node which is contain value of less than 9 , // If it is exist
while (temp->next != NULL)
{
if (temp->data < 9)
{
auxiliary = temp;
}
// Visit to next node
temp = temp->next;
}
temp->data = temp->data + digit;
if (temp->data > 9)
{
// When need to update linked list
if (auxiliary == NULL)
{
// When auxiliary is NULL then assign first node to this
auxiliary = this->head;
// Create a new node
LinkNode *node = new LinkNode(1);
// Add node at begining
node->next = this->head;
// Make new head
this->head = node;
}
// Manage last node
temp->data = temp->data % 10;
// Update the node value from auxiliary to second last node
while (auxiliary->next != NULL)
{
auxiliary->data = (auxiliary->data + 1) % 10;
// Visit to next node
auxiliary = auxiliary->next;
}
}
}
};
int main()
{
// Create a empty linked list
SingleLL sll = SingleLL();
// Constructed linked list
// 9 → 9 → 9 → 8 → 9 → NULL
sll.addNode(9);
sll.addNode(9);
sll.addNode(9);
sll.addNode(8);
sll.addNode(9);
sll.display();
int digit = 9;
cout << " Add digit : " << digit << " \n";
sll.addDigit(digit);
sll.display();
digit = 6;
cout << " Add digit : " << digit << " \n";
sll.addDigit(digit);
sll.display();
digit = 5;
cout << " Add digit : " << digit << " \n";
sll.addDigit(digit);
sll.display();
return 0;
}
Output
9 → 9 → 9 → 8 → 9 → NULL
Add digit : 9
9 → 9 → 9 → 9 → 8 → NULL
Add digit : 6
1 → 0 → 0 → 0 → 0 → 4 → NULL
Add digit : 5
1 → 0 → 0 → 0 → 0 → 9 → NULL
// Include namespace system
using System;
/*
C# Program for
Add the given digit to a number stored in a linked list
*/
// Linked list node
public class LinkNode
{
public int data;
public LinkNode next;
public LinkNode(int data)
{
this.data = data;
this.next = null;
}
};
public class SingleLL
{
public LinkNode head;
public SingleLL()
{
this.head = null;
}
//Add new Node at end of linked list
public void addNode(int data)
{
if (data < 0)
{
Console.Write("\n Number is negative : " + data + " \n");
return;
}
else if (data > 9)
{
Console.Write("\n Two digit number is not allowed : " + data + " \n");
return;
}
LinkNode node = new LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
LinkNode temp = this.head;
//Find last node
while (temp.next != null)
{
temp = temp.next;
}
// Append the node at last position
temp.next = node;
}
}
// Display linked list element
public void display()
{
if (this.head == null)
{
Console.Write("\n Empty linked list\n");
return;
}
LinkNode temp = this.head;
//iterating linked list elements
while (temp != null)
{
if (temp != this.head)
{
Console.Write(" →");
}
Console.Write(" " + temp.data);
// Visit to next node
temp = temp.next;
}
Console.Write(" → NULL\n");
}
// Add a single digit, in number represent of linked list
public void addDigit(int digit)
{
if (digit < 0 || digit > 9)
{
Console.Write("\n Invalid digit : " + digit + "\n");
return;
}
else if (this.head == null)
{
Console.Write("\n Empty linked list\n");
return;
}
LinkNode temp = this.head;
LinkNode auxiliary = null;
// Find last node and
// Detect last node which is contain value of less than 9 , // If it is exist
while (temp.next != null)
{
if (temp.data < 9)
{
auxiliary = temp;
}
// Visit to next node
temp = temp.next;
}
temp.data = temp.data + digit;
if (temp.data > 9)
{
// When need to update linked list
if (auxiliary == null)
{
// When auxiliary is NULL then assign first node to this
auxiliary = this.head;
// Create a new node
LinkNode node = new LinkNode(1);
// Add node at begining
node.next = this.head;
// Make new head
this.head = node;
}
// Manage last node
temp.data = temp.data % 10;
// Update the node value from auxiliary to second last node
while (auxiliary.next != null)
{
auxiliary.data = (auxiliary.data + 1) % 10;
// Visit to next node
auxiliary = auxiliary.next;
}
}
}
public static void Main(String[] args)
{
// Create a empty linked list
SingleLL sll = new SingleLL();
// Constructed linked list
// 9 → 9 → 9 → 8 → 9 → NULL
sll.addNode(9);
sll.addNode(9);
sll.addNode(9);
sll.addNode(8);
sll.addNode(9);
sll.display();
int digit = 9;
Console.Write(" Add digit : " + digit + " \n");
sll.addDigit(digit);
sll.display();
digit = 6;
Console.Write(" Add digit : " + digit + " \n");
sll.addDigit(digit);
sll.display();
digit = 5;
Console.Write(" Add digit : " + digit + " \n");
sll.addDigit(digit);
sll.display();
}
}
Output
9 → 9 → 9 → 8 → 9 → NULL
Add digit : 9
9 → 9 → 9 → 9 → 8 → NULL
Add digit : 6
1 → 0 → 0 → 0 → 0 → 4 → NULL
Add digit : 5
1 → 0 → 0 → 0 → 0 → 9 → NULL
<?php
/*
Php Program for
Add the given digit to a number stored in a linked list
*/
// Linked list node
class LinkNode
{
public $data;
public $next;
function __construct($data)
{
$this->data = $data;
$this->next = null;
}
};
class SingleLL
{
public $head;
function __construct()
{
$this->head = null;
}
//Add new Node at end of linked list
public function addNode($data)
{
if ($data < 0)
{
echo "\n Number is negative : ". $data ." \n";
return;
}
else if ($data > 9)
{
echo "\n Two digit number is not allowed : ". $data ." \n";
return;
}
$node = new LinkNode($data);
if ($this->head == null)
{
$this->head = $node;
}
else
{
$temp = $this->head;
//Find last node
while ($temp->next != null)
{
$temp = $temp->next;
}
// Append the node at last position
$temp->next = $node;
}
}
// Display linked list element
public function display()
{
if ($this->head == null)
{
echo "\n Empty linked list\n";
return;
}
$temp = $this->head;
//iterating linked list elements
while ($temp != null)
{
if ($temp != $this->head)
{
echo " →";
}
echo " ". $temp->data;
// Visit to next node
$temp = $temp->next;
}
echo " → NULL\n";
}
// Add a single digit, in number represent of linked list
public function addDigit($digit)
{
if ($digit < 0 || $digit > 9)
{
echo "\n Invalid digit : ". $digit ."\n";
return;
}
else if ($this->head == null)
{
echo "\n Empty linked list\n";
return;
}
$temp = $this->head;
$auxiliary = null;
// Find last node and
// Detect last node which is contain value of less than 9 , // If it is exist
while ($temp->next != null)
{
if ($temp->data < 9)
{
$auxiliary = $temp;
}
// Visit to next node
$temp = $temp->next;
}
$temp->data = $temp->data + $digit;
if ($temp->data > 9)
{
// When need to update linked list
if ($auxiliary == null)
{
// When auxiliary is NULL then assign first node to this
$auxiliary = $this->head;
// Create a new node
$node = new LinkNode(1);
// Add node at begining
$node->next = $this->head;
// Make new head
$this->head = $node;
}
// Manage last node
$temp->data = $temp->data % 10;
// Update the node value from auxiliary to second last node
while ($auxiliary->next != null)
{
$auxiliary->data = ($auxiliary->data + 1) % 10;
// Visit to next node
$auxiliary = $auxiliary->next;
}
}
}
}
function main()
{
// Create a empty linked list
$sll = new SingleLL();
// Constructed linked list
// 9 → 9 → 9 → 8 → 9 → NULL
$sll->addNode(9);
$sll->addNode(9);
$sll->addNode(9);
$sll->addNode(8);
$sll->addNode(9);
$sll->display();
$digit = 9;
echo " Add digit : ". $digit ." \n";
$sll->addDigit($digit);
$sll->display();
$digit = 6;
echo " Add digit : ". $digit ." \n";
$sll->addDigit($digit);
$sll->display();
$digit = 5;
echo " Add digit : ". $digit ." \n";
$sll->addDigit($digit);
$sll->display();
}
main();
Output
9 → 9 → 9 → 8 → 9 → NULL
Add digit : 9
9 → 9 → 9 → 9 → 8 → NULL
Add digit : 6
1 → 0 → 0 → 0 → 0 → 4 → NULL
Add digit : 5
1 → 0 → 0 → 0 → 0 → 9 → NULL
/*
Node Js Program for
Add the given digit to a number stored in a linked list
*/
// Linked list node
class LinkNode
{
constructor(data)
{
this.data = data;
this.next = null;
}
};
class SingleLL
{
constructor()
{
this.head = null;
}
//Add new Node at end of linked list
addNode(data)
{
if (data < 0)
{
process.stdout.write("\n Number is negative : " + data + " \n");
return;
}
else if (data > 9)
{
process.stdout.write("\n Two digit number is not allowed : " + data + " \n");
return;
}
var node = new LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
var temp = this.head;
//Find last node
while (temp.next != null)
{
temp = temp.next;
}
// Append the node at last position
temp.next = node;
}
}
// Display linked list element
display()
{
if (this.head == null)
{
process.stdout.write("\n Empty linked list\n");
return;
}
var temp = this.head;
//iterating linked list elements
while (temp != null)
{
if (temp != this.head)
{
process.stdout.write(" →");
}
process.stdout.write(" " + temp.data);
// Visit to next node
temp = temp.next;
}
process.stdout.write(" → NULL\n");
}
// Add a single digit, in number represent of linked list
addDigit(digit)
{
if (digit < 0 || digit > 9)
{
process.stdout.write("\n Invalid digit : " + digit + "\n");
return;
}
else if (this.head == null)
{
process.stdout.write("\n Empty linked list\n");
return;
}
var temp = this.head;
var auxiliary = null;
// Find last node and
// Detect last node which is contain value of less than 9 , // If it is exist
while (temp.next != null)
{
if (temp.data < 9)
{
auxiliary = temp;
}
// Visit to next node
temp = temp.next;
}
temp.data = temp.data + digit;
if (temp.data > 9)
{
// When need to update linked list
if (auxiliary == null)
{
// When auxiliary is NULL then assign first node to this
auxiliary = this.head;
// Create a new node
var node = new LinkNode(1);
// Add node at begining
node.next = this.head;
// Make new head
this.head = node;
}
// Manage last node
temp.data = temp.data % 10;
// Update the node value from auxiliary to second last node
while (auxiliary.next != null)
{
auxiliary.data = (auxiliary.data + 1) % 10;
// Visit to next node
auxiliary = auxiliary.next;
}
}
}
}
function main()
{
// Create a empty linked list
var sll = new SingleLL();
// Constructed linked list
// 9 → 9 → 9 → 8 → 9 → NULL
sll.addNode(9);
sll.addNode(9);
sll.addNode(9);
sll.addNode(8);
sll.addNode(9);
sll.display();
var digit = 9;
process.stdout.write(" Add digit : " + digit + " \n");
sll.addDigit(digit);
sll.display();
digit = 6;
process.stdout.write(" Add digit : " + digit + " \n");
sll.addDigit(digit);
sll.display();
digit = 5;
process.stdout.write(" Add digit : " + digit + " \n");
sll.addDigit(digit);
sll.display();
}
main();
Output
9 → 9 → 9 → 8 → 9 → NULL
Add digit : 9
9 → 9 → 9 → 9 → 8 → NULL
Add digit : 6
1 → 0 → 0 → 0 → 0 → 4 → NULL
Add digit : 5
1 → 0 → 0 → 0 → 0 → 9 → NULL
# Python 3 Program for
# Add the given digit to a number stored in a linked list
# Linked list node
class LinkNode :
def __init__(self, data) :
self.data = data
self.next = None
class SingleLL :
def __init__(self) :
self.head = None
# Add new Node at end of linked list
def addNode(self, data) :
if (data < 0) :
print("\n Number is negative : ", data ," ")
return
elif(data > 9) :
print("\n Two digit number is not allowed : ", data ," ")
return
node = LinkNode(data)
if (self.head == None) :
self.head = node
else :
temp = self.head
# Find last node
while (temp.next != None) :
temp = temp.next
# Append the node at last position
temp.next = node
# Display linked list element
def display(self) :
if (self.head == None) :
print("\n Empty linked list")
return
temp = self.head
# iterating linked list elements
while (temp != None) :
if (temp != self.head) :
print(" →", end = "")
print("", temp.data, end = "")
# Visit to next node
temp = temp.next
print(" → NULL")
# Add a single digit, in number represent of linked list
def addDigit(self, digit) :
if (digit < 0 or digit > 9) :
print("\n Invalid digit : ", digit )
return
elif(self.head == None) :
print("\n Empty linked list")
return
temp = self.head
auxiliary = None
# Find last node and
# Detect last node which is contain value of less than 9,
# If it is exist
while (temp.next != None) :
if (temp.data < 9) :
auxiliary = temp
# Visit to next node
temp = temp.next
temp.data = temp.data + digit
if (temp.data > 9) :
# When need to update linked list
if (auxiliary == None) :
# When auxiliary is NULL then assign first node to this
auxiliary = self.head
# Create a new node
node = LinkNode(1)
# Add node at begining
node.next = self.head
# Make new head
self.head = node
# Manage last node
temp.data = temp.data % 10
# Update the node value from auxiliary to second last node
while (auxiliary.next != None) :
auxiliary.data = (auxiliary.data + 1) % 10
# Visit to next node
auxiliary = auxiliary.next
def main() :
# Create a empty linked list
sll = SingleLL()
# Constructed linked list
# 9 → 9 → 9 → 8 → 9 → NULL
sll.addNode(9)
sll.addNode(9)
sll.addNode(9)
sll.addNode(8)
sll.addNode(9)
sll.display()
digit = 9
print(" Add digit : ", digit ," ")
sll.addDigit(digit)
sll.display()
digit = 6
print(" Add digit : ", digit ," ")
sll.addDigit(digit)
sll.display()
digit = 5
print(" Add digit : ", digit ," ")
sll.addDigit(digit)
sll.display()
if __name__ == "__main__": main()
Output
9 → 9 → 9 → 8 → 9 → NULL
Add digit : 9
9 → 9 → 9 → 9 → 8 → NULL
Add digit : 6
1 → 0 → 0 → 0 → 0 → 4 → NULL
Add digit : 5
1 → 0 → 0 → 0 → 0 → 9 → NULL
# Ruby Program for
# Add the given digit to a number stored in a linked list
# Linked list node
class LinkNode
# Define the accessor and reader of class LinkNode
attr_reader :data, :next
attr_accessor :data, :next
def initialize(data)
self.data = data
self.next = nil
end
end
class SingleLL
# Define the accessor and reader of class SingleLL
attr_reader :head
attr_accessor :head
def initialize()
self.head = nil
end
# Add new Node at end of linked list
def addNode(data)
if (data < 0)
print("\n Number is negative : ", data ," \n")
return
elsif(data > 9)
print("\n Two digit number is not allowed : ", data ," \n")
return
end
node = LinkNode.new(data)
if (self.head == nil)
self.head = node
else
temp = self.head
# Find last node
while (temp.next != nil)
temp = temp.next
end
# Append the node at last position
temp.next = node
end
end
# Display linked list element
def display()
if (self.head == nil)
print("\n Empty linked list\n")
return
end
temp = self.head
# iterating linked list elements
while (temp != nil)
if (temp != self.head)
print(" →")
end
print(" ", temp.data)
# Visit to next node
temp = temp.next
end
print(" → NULL\n")
end
# Add a single digit, in number represent of linked list
def addDigit(digit)
if (digit < 0 || digit > 9)
print("\n Invalid digit : ", digit ,"\n")
return
elsif(self.head == nil)
print("\n Empty linked list\n")
return
end
temp = self.head
auxiliary = nil
# Find last node and
# Detect last node which is contain value of less than 9,
# If it is exist
while (temp.next != nil)
if (temp.data < 9)
auxiliary = temp
end
# Visit to next node
temp = temp.next
end
temp.data = temp.data + digit
if (temp.data > 9)
# When need to update linked list
if (auxiliary == nil)
# When auxiliary is NULL then assign first node to this
auxiliary = self.head
# Create a new node
node = LinkNode.new(1)
# Add node at begining
node.next = self.head
# Make new head
self.head = node
end
# Manage last node
temp.data = temp.data % 10
# Update the node value from auxiliary to second last node
while (auxiliary.next != nil)
auxiliary.data = (auxiliary.data + 1) % 10
# Visit to next node
auxiliary = auxiliary.next
end
end
end
end
def main()
# Create a empty linked list
sll = SingleLL.new()
# Constructed linked list
# 9 → 9 → 9 → 8 → 9 → NULL
sll.addNode(9)
sll.addNode(9)
sll.addNode(9)
sll.addNode(8)
sll.addNode(9)
sll.display()
digit = 9
print(" Add digit : ", digit ," \n")
sll.addDigit(digit)
sll.display()
digit = 6
print(" Add digit : ", digit ," \n")
sll.addDigit(digit)
sll.display()
digit = 5
print(" Add digit : ", digit ," \n")
sll.addDigit(digit)
sll.display()
end
main()
Output
9 → 9 → 9 → 8 → 9 → NULL
Add digit : 9
9 → 9 → 9 → 9 → 8 → NULL
Add digit : 6
1 → 0 → 0 → 0 → 0 → 4 → NULL
Add digit : 5
1 → 0 → 0 → 0 → 0 → 9 → NULL
/*
Scala Program for
Add the given digit to a number stored in a linked list
*/
// Linked list node
class LinkNode(var data: Int , var next: LinkNode)
{
def this(data: Int)
{
this(data, null);
}
};
class SingleLL(var head: LinkNode)
{
def this()
{
this(null);
}
//Add new Node at end of linked list
def addNode(data: Int): Unit = {
if (data < 0)
{
print("\n Number is negative : " + data + " \n");
return;
}
else if (data > 9)
{
print("\n Two digit number is not allowed : " + data + " \n");
return;
}
var node: LinkNode = new LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
var temp: LinkNode = this.head;
//Find last node
while (temp.next != null)
{
temp = temp.next;
}
// Append the node at last position
temp.next = node;
}
}
// Display linked list element
def display(): Unit = {
if (this.head == null)
{
print("\n Empty linked list\n");
return;
}
var temp: LinkNode = this.head;
//iterating linked list elements
while (temp != null)
{
if (temp != this.head)
{
print(" →");
}
print(" " + temp.data);
// Visit to next node
temp = temp.next;
}
print(" → NULL\n");
}
// Add a single digit, in number represent of linked list
def addDigit(digit: Int): Unit = {
if (digit < 0 || digit > 9)
{
print("\n Invalid digit : " + digit + "\n");
return;
}
else if (this.head == null)
{
print("\n Empty linked list\n");
return;
}
var temp: LinkNode = this.head;
var auxiliary: LinkNode = null;
// Find last node and
// Detect last node which is contain value of less than 9 , // If it is exist
while (temp.next != null)
{
if (temp.data < 9)
{
auxiliary = temp;
}
// Visit to next node
temp = temp.next;
}
temp.data = temp.data + digit;
if (temp.data > 9)
{
// When need to update linked list
if (auxiliary == null)
{
// When auxiliary is NULL then assign first node to this
auxiliary = this.head;
// Create a new node
var node: LinkNode = new LinkNode(1);
// Add node at begining
node.next = this.head;
// Make new head
this.head = node;
}
// Manage last node
temp.data = temp.data % 10;
// Update the node value from auxiliary to second last node
while (auxiliary.next != null)
{
auxiliary.data = (auxiliary.data + 1) % 10;
// Visit to next node
auxiliary = auxiliary.next;
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
// Create a empty linked list
var sll: SingleLL = new SingleLL();
// Constructed linked list
// 9 → 9 → 9 → 8 → 9 → NULL
sll.addNode(9);
sll.addNode(9);
sll.addNode(9);
sll.addNode(8);
sll.addNode(9);
sll.display();
var digit: Int = 9;
print(" Add digit : " + digit + " \n");
sll.addDigit(digit);
sll.display();
digit = 6;
print(" Add digit : " + digit + " \n");
sll.addDigit(digit);
sll.display();
digit = 5;
print(" Add digit : " + digit + " \n");
sll.addDigit(digit);
sll.display();
}
}
Output
9 → 9 → 9 → 8 → 9 → NULL
Add digit : 9
9 → 9 → 9 → 9 → 8 → NULL
Add digit : 6
1 → 0 → 0 → 0 → 0 → 4 → NULL
Add digit : 5
1 → 0 → 0 → 0 → 0 → 9 → NULL
/*
Swift 4 Program for
Add the given digit to a number stored in a linked list
*/
// Linked list node
class LinkNode
{
var data: Int;
var next: LinkNode? ;
init(_ data: Int)
{
self.data = data;
self.next = nil;
}
};
class SingleLL
{
var head: LinkNode? ;
init()
{
self.head = nil;
}
//Add new Node at end of linked list
func addNode(_ data: Int)
{
if (data < 0)
{
print("\n Number is negative : ", data ," ");
return;
}
else if (data > 9)
{
print("\n Two digit number is not allowed : ", data ," ");
return;
}
let node: LinkNode? = LinkNode(data);
if (self.head == nil)
{
self.head = node;
}
else
{
var temp: LinkNode? = self.head;
//Find last node
while (temp!.next != nil)
{
temp = temp!.next;
}
// Append the node at last position
temp!.next = node;
}
}
// Display linked list element
func display()
{
if (self.head == nil)
{
print("\n Empty linked list");
return;
}
var temp: LinkNode? = self.head;
//iterating linked list elements
while (temp != nil)
{
if (!(temp === self.head))
{
print(" →", terminator: "");
}
print("", temp!.data, terminator: "");
// Visit to next node
temp = temp!.next;
}
print(" → NULL");
}
// Add a single digit, in number represent of linked list
func addDigit(_ digit: Int)
{
if (digit < 0 || digit > 9)
{
print("\n Invalid digit : ", digit );
return;
}
else if (self.head == nil)
{
print("\n Empty linked list");
return;
}
var temp: LinkNode? = self.head;
var auxiliary: LinkNode? = nil;
// Find last node and
// Detect last node which is contain value of less than 9 , // If it is exist
while (temp!.next != nil)
{
if (temp!.data < 9)
{
auxiliary = temp;
}
// Visit to next node
temp = temp!.next;
}
temp!.data = temp!.data + digit;
if (temp!.data > 9)
{
// When need to update linked list
if (auxiliary == nil)
{
// When auxiliary is NULL then assign first node to this
auxiliary = self.head;
// Create a new node
let node: LinkNode? = LinkNode(1);
// Add node at begining
node!.next = self.head;
// Make new head
self.head = node;
}
// Manage last node
temp!.data = temp!.data % 10;
// Update the node value from auxiliary to second last node
while (auxiliary!.next != nil)
{
auxiliary!.data = (auxiliary!.data + 1) % 10;
// Visit to next node
auxiliary = auxiliary!.next;
}
}
}
}
func main()
{
// Create a empty linked list
let sll: SingleLL = SingleLL();
// Constructed linked list
// 9 → 9 → 9 → 8 → 9 → NULL
sll.addNode(9);
sll.addNode(9);
sll.addNode(9);
sll.addNode(8);
sll.addNode(9);
sll.display();
var digit: Int = 9;
print(" Add digit : ", digit ," ");
sll.addDigit(digit);
sll.display();
digit = 6;
print(" Add digit : ", digit ," ");
sll.addDigit(digit);
sll.display();
digit = 5;
print(" Add digit : ", digit ," ");
sll.addDigit(digit);
sll.display();
}
main();
Output
9 → 9 → 9 → 8 → 9 → NULL
Add digit : 9
9 → 9 → 9 → 9 → 8 → NULL
Add digit : 6
1 → 0 → 0 → 0 → 0 → 4 → NULL
Add digit : 5
1 → 0 → 0 → 0 → 0 → 9 → NULL
/*
Kotlin Program for
Add the given digit to a number stored in a linked list
*/
// Linked list node
class LinkNode
{
var data: Int;
var next: LinkNode ? ;
constructor(data: Int)
{
this.data = data;
this.next = null;
}
};
class SingleLL
{
var head: LinkNode ? ;
constructor()
{
this.head = null;
}
//Add new Node at end of linked list
fun addNode(data: Int): Unit
{
if (data < 0)
{
print("\n Number is negative : " + data + " \n");
return;
}
else if (data > 9)
{
print("\n Two digit number is not allowed : " + data + " \n");
return;
}
var node: LinkNode ? = LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
var temp: LinkNode ? = this.head;
//Find last node
while (temp?.next != null)
{
temp = temp.next;
}
// Append the node at last position
temp?.next = node;
}
}
// Display linked list element
fun display(): Unit
{
if (this.head == null)
{
print("\n Empty linked list\n");
return;
}
var temp: LinkNode ? = this.head;
//iterating linked list elements
while (temp != null)
{
if (temp != this.head)
{
print(" →");
}
print(" " + temp.data);
// Visit to next node
temp = temp.next;
}
print(" → NULL\n");
}
// Add a single digit, in number represent of linked list
fun addDigit(digit: Int): Unit
{
if (digit < 0 || digit > 9)
{
print("\n Invalid digit : " + digit + "\n");
return;
}
else if (this.head == null)
{
print("\n Empty linked list\n");
return;
}
var temp: LinkNode ? = this.head;
var auxiliary: LinkNode ? = null;
// Find last node and
// Detect last node which is contain value of less than 9 , // If it is exist
while (temp?.next != null)
{
if (temp.data < 9)
{
auxiliary = temp;
}
// Visit to next node
temp = temp.next;
}
temp?.data = temp!!.data + digit;
if (temp.data > 9)
{
// When need to update linked list
if (auxiliary == null)
{
// When auxiliary is NULL then assign first node to this
auxiliary = this.head;
// Create a new node
var node: LinkNode ? = LinkNode(1);
// Add node at begining
node?.next = this.head;
// Make new head
this.head = node;
}
// Manage last node
temp.data = temp.data % 10;
// Update the node value from auxiliary to second last node
while (auxiliary?.next != null)
{
auxiliary.data = (auxiliary.data + 1) % 10;
// Visit to next node
auxiliary = auxiliary.next;
}
}
}
}
fun main(args: Array < String > ): Unit
{
// Create a empty linked list
var sll: SingleLL = SingleLL();
// Constructed linked list
// 9 → 9 → 9 → 8 → 9 → NULL
sll.addNode(9);
sll.addNode(9);
sll.addNode(9);
sll.addNode(8);
sll.addNode(9);
sll.display();
var digit: Int = 9;
print(" Add digit : " + digit + " \n");
sll.addDigit(digit);
sll.display();
digit = 6;
print(" Add digit : " + digit + " \n");
sll.addDigit(digit);
sll.display();
digit = 5;
print(" Add digit : " + digit + " \n");
sll.addDigit(digit);
sll.display();
}
Output
9 → 9 → 9 → 8 → 9 → NULL
Add digit : 9
9 → 9 → 9 → 9 → 8 → NULL
Add digit : 6
1 → 0 → 0 → 0 → 0 → 4 → NULL
Add digit : 5
1 → 0 → 0 → 0 → 0 → 9 → NULL
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment