A singly linked list is a linked list where each node points to the next node in the list. It does not have a pointer to the previous node. It is the simplest type of linked list and is useful for implementation of stack and queue.
- with struct
- with classes
struct ListNode {
int data; // element stored in the node
struct ListNode* next; // pointer to the next node
};
In this example, the struct ListNode has two elements: an integer data and a pointer next to another ListNode struct. This allows us to create a ``linked listofnodes`, where each node contains an element and a pointer to the next node in the list.
class LinkList
{
private:
class ListNode
{
public:
int data;
ListNode* next;
ListNode(int data) : data(data)
{
next = NULL;
}
};
ListNode* head;
ListNode* tail;
public:
LinkList()
{
head = NULL;
tail = NULL;
}
~LinkList()
{
ListNode* traversingList = head;
while (traversingList != NULL)
{
ListNode* deleteNode = traversingList;
traversingList = traversingList->next;
delete deleteNode;
}
}
};
The Above code represents a singly linked list with classes. The class LinkList has a private inner class ListNode that represents a single node in the list, which has a constructor that initializes the next pointer to NULL. The outer class LinkList has three private members: a pointer head to the first node in the list, a pointer tail to the last node in the list and a constructor that initializes the head and tail pointers to NULL. The class also has a destructor that iterates through the list, starting at the head, and deletes each node, releasing memory.
we can do Following operations with singly list.