Convert a String to a Singly Linked List | C ++ program to Convert a String to a Singly Linked List ; Structure for a Singly Linked List ; Function to add a new node to the Linked List ; Function to convert the string to Linked List . ; curr pointer points to the current node where the insertion should take place ; Function to print the data present in all the nodes ; Driver code
#include <iostream> NEW_LINE using namespace std ; struct node { char data ; node * next ; } ; node * add ( char data ) { node * newnode = new node ; newnode -> data = data ; newnode -> next = NULL ; return newnode ; } node * string_to_SLL ( string text , node * head ) { head = add ( text [ 0 ] ) ; node * curr = head ; for ( int i = 1 ; i < text . size ( ) ; i ++ ) { curr -> next = add ( text [ i ] ) ; curr = curr -> next ; } return head ; } void print ( node * head ) { node * curr = head ; while ( curr != NULL ) { cout << curr -> data << " ▁ - > ▁ " ; curr = curr -> next ; } } int main ( ) { string text = " GEEKS " ; node * head = NULL ; head = string_to_SLL ( text , head ) ; print ( head ) ; return 0 ; }