Linked List: Method To Insert (Add) A Node in The Beginning of A Linked List
Linked List: Method To Insert (Add) A Node in The Beginning of A Linked List
If you use a list shown above represented by an object ‘ptr‘ of a class ‘Node‘, then to handle its contents
the following statements can be applied:
Single linked lists is a connection of various lists in such a way that each list has the address of the next
list. In this system,control can only proceed in the forward direction but not in backward direction. Lin k
of the last node is NULL which indicates end of the linked list structure.
int c = 1;
while(c <= n) //'ptr' will access all nodes up to the nth node
{
ptr = ptr.link;
c++;
}
temp.link = ptr.link;
ptr.link = temp;
}
while(ptr.link != null) //'ptr' will access all nodes up to the last node
{
ptr = ptr.link;
}
temp.link = null;
ptr.link = temp;
}
int c = 0;
while(c <= n)
{
ptr1 = ptr;
ptr = ptr.link;
c++;
}
ptr1.link = ptr.link;
ptr.link = null;
}
Method to Traverse a Single Linked List :
If the class definition of a linked list is:
class Node
{
int data;
Node link;
}
ptr = ptr.link;
}
}
Method to compute and return the sum of all integer items stored in the
linked list.
class ListNodes
{
int item;
ListNodes next;
}