What is singly linked list? Write an algorithm to add  a node at the beginning and end of singly linked list.

This answer is restricted. Please login to view the answer of this question.

Login Now

Singly linked list is type of list in which each node contains two fields one is info field which is used to store the data items and another is link field which is used to point the next node in the list. The last node has NULL pointer. The following example is a singly linked list that contains three elements 5,3,8.

Singly Linked List | Hamro CSIT

Algorithm to add node at the beginning:

1. Start
2. Create a new node using malloc function as,
      Newnode = (NodeType *)malloc(sizeof(NodeType));
3. Read the data item to be inserted say 'el'
4. Assign data to the info field of new node
      Newnode→info = el;
5. Set next of new node to first
     Newnode→next = first;
6. Set the first pointer to the new node
     first = Newnode;
7. End

Algorithm to add at the end:

1. Start
2. Create a new node using malloc function as,
      Newnode = (NodeType *)malloc(sizeof(NodeType));
3. Read the data item to be inserted say 'el'
4. Assign data to the info field of new node
      Newnode→info = el;
5. Set next of new node to null
     Newnode→next = null;
6. if(first == null)
     Set first = last = Newnode and exit;
7. else
     Set last→next = Newnode;
     Set last = Newnode;
8. End
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .