how do i create a method that adds instances from a class into a singly linked list(java)
how do i create a method that adds instances from a class into a singly linked list(java)
Answer :
Inserting a new element into a simply linked list at the beginning is fairly simple. We just need to make a few adjustments to the linking of the nodes. To insert a new node in the list at the beginning, the following steps must be performed.
Allocate space for the new node and store the data in the data part of the node. This will be done using the following statements.
ptr = (struct node *) malloc(sizeof(struct node *));
ptr → data = item
Make the link as part of the new node point to the existing first node of the list. This is done using the following command.
ptr->next = head;
Finally, we need to create a new node as the first node of the list, which we do with the following command.
head = ptr;
Step by step
Solved in 2 steps