I got an idea when i started to learning linked lists. My idea was this. I want to insert 1,2,5 to a linked list. And in another field of linked list I want to add 2 digit numbers that starts with 1,2,5. You can see from image. ( C means combinations ) https://i.stack.imgur.com/w1QMj.jpg
int main() {
struct Node* head = NULL;
insertEnd(&head,1);
insertEnd(&head,2);
insertEnd(&head,5);
printLinkedList(head);
insertStartsWith(&head,5,51);
insertStartsWith(&head,5,52);
insertStartsWith(&head,5,53);
insertStartsWith(&head,5,54);
insertStartsWith(&head,1,11);
insertStartsWith(&head,1,12);
insertStartsWith(&head,1,13);
insertStartsWith(&head,1,14);
insertStartsWith(&head,2,21);
insertStartsWith(&head,2,22);
insertStartsWith(&head,2,23);
insertStartsWith(&head,2,24);
printLinkedListWithStartsWith(head);}
My node structure:
struct Node {
int data;
struct Node* next;
struct Node* startsWith; };
My code for inserting initial linked list 1,2 and 5:
void insertEnd(struct Node **pNode, int data){
struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
struct Node *lastNode=*pNode;
newNode->data=data;
newNode->next=NULL;
if (*pNode==NULL)
{
*pNode=newNode;
return;
}
while (lastNode->next!=NULL){
lastNode=lastNode->next;
}
lastNode->next=newNode;
return; }
This part is search number and insert 2 digit numbers to it's startsWith node.
void insertStartsWith(struct Node **pNode, int i, int i1) {
struct Node *tempNode=*pNode;
while (tempNode->next!=NULL){
if (tempNode->data==i){
struct Node *tempNode1=tempNode->startsWith;
while (tempNode1->next!=NULL){
tempNode1=tempNode1->next;
}
struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
newNode->data=i1;
newNode->next=NULL;
tempNode1->next=newNode;
return;
}
tempNode=tempNode->next;
} }
I add 1 2 and 5 to my linked list. But when I try to add combinations it fails. Where should I look for linked list inside a linked list?
edit 08.18.19
void printLinkedListWithStartsWith(struct Node *pNode) {
printf("Linked list with starts: ");
while (pNode!=NULL) {
printf("%d \n",pNode->data);
while(pNode->startsWith!=NULL){
printf("%d ",pNode->startsWith->data);
pNode->startsWith=pNode->startsWith->next;
}
pNode=pNode->next;
} }