Page 528 - Computer science 868 Class 12
P. 528
Step 9: Create a new Node temp
Step 10: Accept any number in num
Step 11: Assign temp.data=num
Step 12: Assign temp.next=null
Step 13: Assign ptr.next=temp
Step 14: Set ptr=temp
Step 15: Increase i by 1
Step 16: Stop.
Java Code
void createList(int n)
{
System.out.println("enter value of the first node");
num=sc.nextInt();
Node start=new Node();
Node ptr= new Node()
ptr.data=num; // data initialised to num
ptr.next=null // address initialised to null
start=ptr; // initialising the start pointer
System.out.println("enter "+(n-1)+" elements");
for(int i=1;i<n ; i++)
{
num=sc.nextInt();
Node temp =new Node();
temp.data=num; // data initialised to num
temp.next=null // address initialised to null
ptr.next=temp; // linking the previous node with the new node
ptr=temp; // Assigning the next node as the previous node
}
}
2. Write an algorithm/Java code to insert a node at the beginning of an existing linked list using method:
void insertBegin(Node start, int item) where start refers to the start pointer and item is the data value.
start 1001 2145
29 1098 18 1156 35 NULL
1001 1098 1156
35 1001
ptr 2145
Ans. Algorithm
Step 1: Start.
Step 2: Create a new Node ptr
Step 3: Assign ptr.data=item
526526 Touchpad Computer Science-XII

