Question

how do i return and array of string (2d array) from a single linkedlist in c?...

how do i return and array of string (2d array) from a single linkedlist in c?
for example i have a linkedlist that contains
cat->dog->dish
how do i return them to main as an array
(cat,dog,dish)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Remember in C Language you can directly return the array from a function . If you need to return the array of values then you need to use pointers and double pointers.

CODE:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//template for node
struct node
{
char *data; //char pointer to store string
struct node *next; //next pointer to hold reference of next node
};
char** getArrayFromLinkedlist(struct node *head) //method which returns array when linked list head is given
{
struct node * temp=head; //assigning head to temp;
int length=0;
while(temp!=NULL) //finding length of linked list;
{
length++; //incrementing length
temp=temp->next; //moving to next node
}
char ** array = malloc(length * sizeof(char*)); //declaring dynamic memory for out double pointer to store srray of strings
  
temp=head;
int i=0;
while(temp!=NULL) //iteratin over linked list
{
array[i]=malloc(20 * sizeof(char)); //allocating memory for out string(here max length is 20)
array[i]=temp->data; //setting linked list data to array
i=i+1; //incrementing array index
temp=temp->next; //moving to next node;
}
array[i]=NULL; //setting last element of array to NULL
return array; //returning array
  
  
}

int main()
{
struct node *head=NULL; //head node of linked list
//allocating memory
head = (struct node*)malloc(sizeof(struct node));
struct node *newnode1=(struct node*)malloc(sizeof(struct node));
struct node *newnode2=(struct node*)malloc(sizeof(struct node));

//Setting temporary data
char *word1="cat";
head->data = malloc(strlen(word1) + 1); //allocating memory
head->data=word1;
head->next=newnode1;
char * word2="dog";
newnode1->data = malloc(strlen(word2) + 1); newnode1->next=newnode2;
newnode1->data=word2;
char *word3="dish";
newnode2->data = malloc(strlen(word3) + 1); newnode2->next=NULL;
newnode2->data=word3;
char** mystr=getArrayFromLinkedlist(head); //callin our method which return double pointer
while(*mystr) //iteratig over array of strings
{
printf("%s ",*mystr);
mystr++;
}
return 0;
}

OUTPUT:

Please rate my answer if you liked it.

Add a comment
Know the answer?
Add Answer to:
how do i return and array of string (2d array) from a single linkedlist in c?...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.LinkedList; public class testprintOut { private static LinkedList[] array; public static void main(String[] args) {...

    import java.util.LinkedList; public class testprintOut { private static LinkedList[] array; public static void main(String[] args) { int nelems = 5; array = new LinkedList[nelems]; for (int i = 0; i < nelems; i++) { array[i] = new LinkedList<String>(); } array[0]=["ab"]; System.out.println(array[0]); } } //I want to create array of linked lists so how do I create them and print them out efficiently? //Syntax error on token "=", Expression expected after this token Also, is this how I can put them...

  • I have a question in how to create a method. For example there is a LinkedList...

    I have a question in how to create a method. For example there is a LinkedList of Objects, say and Object is public class Dog; private String name; private int age I want my method to take a dog age, and return the position (int) of the dog with that age has in the list example; //takes a dog age and returns position in the list, if no age in the list of dogs with age, method returns -1 public...

  • Java programming: I need to create a method that is given a 2D char array and...

    Java programming: I need to create a method that is given a 2D char array and a String that returns a part of the original 2D array. The input string will tell the method which rows from the input array need to be returned. For example, a 5x5 char array input along with a string "0 4" would return rows 1 and 5 of the input array. The String input will always be numbers divided by spaces and will not...

  • C Programming Exercise: Write a simple program in C so that takes a four charachter string...

    C Programming Exercise: Write a simple program in C so that takes a four charachter string from a file and puts it into a two dimensional array. Make sure your program satisfies the following: (i) uses a 2D Array(rows and columns) (ii) get and put functions (iii) use multiple functions/methods : -In your Main function take input from file using the get functions. In your Second function, call it print2DArray, which takes the string from the file in main method...

  • PLEASE HELP IN JAVA: This is an exercise in using the java LinkedList class. Using input...

    PLEASE HELP IN JAVA: This is an exercise in using the java LinkedList class. Using input file words.txt , create a LinkedList BUT you must NOT add a word that is already in the list and you MUST add them in the order received to the end of the list. For example, if your input was cat, cat, dog, mouse, dog, horse your list would now contain cat, dog, mouse, horse. And the head of the list would be cat,...

  • C++ Linux Compiler Letter Frequency Write a function that will take a string and return a...

    C++ Linux Compiler Letter Frequency Write a function that will take a string and return a count of each letter in the string. For example, "my dog ate my homework" contains 3 m's, 3 o's, 2 e's, 2 y's and one each of d, g, a, t, h, w, r and k. Your function should take a single string argument and return a dynamically allocated array of 26 integers representing the count of each of the letters a .. z...

  • How to deal with string manipulation in c++ without using built-in functions. how to return the...

    How to deal with string manipulation in c++ without using built-in functions. how to return the array (1d or 2d) from functions in c++)?

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

  • How do I write a C program that finds how many times a word is used...

    How do I write a C program that finds how many times a word is used in a string and prints the result. The string is hardcoded. How do I also make it print the hashes? For instance: String: " Dog Cat Owl Ferret Rabbit Cat Dolphin Penguin Cat" Word 1: Owl Word 2: Cat Desired Output: Owl- 1 time(s) Cat: 3 time(s) Owl- # Cat-###

  • If i have a node list type string, how do i put the data from each...

    If i have a node list type string, how do i put the data from each node into a string array and print the results? I am creating the node list myself so i just need generic code how to print the data from each node into a String array. 1670 * 168 * Returns an array of the words in the list, in the order that the client * would expect. * * 169 170 171 172 * *...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT