strings in linkedlists

Hi,

I have created a linkedlist with data as string datatype . The build is sucess but while execution of the program a window with the following message " linkedlistexaample.exe has stopped working " message is displaying.

my declaration is as follows

#include <iostream.h>

#include <string.h>

using namespace std;

struct list

{

string data;

list* next;

}

int main()

{ list *list1 ;

cin>>list1.data;

cout<<list1.data;

return 0;

}

how does strings are stored in the memory ,how their operations will be done in linkedlist .

thanks.

the problem is not related to the string, you just haven't allocated any memory for your list here list* list1;

do you know how to dynamcally allocate with new?
 
list* list1 = new list();

I would also say that your list class doesn't really represent a list, it is more like a node, and a list should be made up of many nodes
Last edited on
Lists and nodes are pretty much the same in this context. There is little difference in having a list of objects and simply a pointer to the first element, which carries a pointer to the next. A node is just a more general term as it can be used in many ways (e.g. trees, single/double linked lists, etc).
quirkyusername ,


Actually i declared a single node with the name list and i allocated the memory dynamically as following,

list1=(*list)malloc(sizeof(struct list))

and the build is successful , but my problem not yet solved.
Topic archived. No new replies allowed.