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)
![](/static/compass_v2/shared-icons/check-mark.png)
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
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)