Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create convert a string to a singly linkedlist.cpp #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <iostream>
using namespace std;

// Structure for a Singly Linked List
struct node {
char data;
node* next;
};

// Function to add a new node to the Linked List
node* add(char data)
{
node* newnode = new node;
newnode->data = data;
newnode->next = NULL;
return newnode;
}

// Function to convert the string to Linked List.
node* string_to_SLL(string text, node* head)
{
head = add(text[0]);
node* curr = head;

// curr pointer points to the current node
// where the insertion should take place
for (int i = 1; i < text.size(); i++) {
curr->next = add(text[i]);
curr = curr->next;
}
return head;
}

// Function to print the data present in all the nodes
void print(node* head)
{
node* curr = head;
while (curr != NULL) {
cout << curr->data << " -> ";
curr = curr->next;
}
}

// Driver code
int main()
{

string text = "GEEKS";

node* head = NULL;
head = string_to_SLL(text, head);

print(head);
return 0;
}