My execution's name is test4.
Input:
$ ./test4 cccc zz abcdef0123456789 aaabbbcccddd
I expect to create a linked list of type char* as follow:
---> 12:aaabbbcccddd ---> 12:abcdef0123456789 ---> 2:zz ---> 4:cccc
each node has the form of "n:a" (n is the length of string a, n <= 12, if the length of a is more than 12 then n = 12)
Below is my code:
struct Node *prepend(struct Node *list, char *s)
{
struct Node *node = (struct Node *)malloc(sizeof(struct Node));
if (node == NULL)
return NULL;
int length_s = strlen(s);
if (length_s > 12)
length_s = 12;
char* temp = NULL;
sprintf(temp,"%d:%s", length_s, s);
strcpy(node->data, temp);
node->next = list;
return node;
}
prepend function links a new node to list
struct Node {
struct Node *next;
char *data;
};
int main(int argc, char **argv)
{
struct Node *list = NULL;
argv++;
while (*argv)
{
list = prepend(list,*argv);
argv++;
}
return 0;
}
Assume all necessary libraries and struct are included, I keep getting a segmentation error when running the code. How do I fix it? I believe the problem is in sprintf but can't figure out why.
You don't allocate memory for temp here:
char* temp = NULL;
sprintf(temp,"%d:%s", length_s, s);
You could use either a static array of chars or dynamically allocate the memory for it.
In order to copy what you want, you should so this:
If data of Node is a char*,
node->data = malloc((length_s + 1) + 1 + digits_of_length);
sprintf(node->data,"%d:%s", length_s, s);
If data of Node is an array of chars, you should do this:
sprintf(node->data,"%d:%s", ((length_s + 1) + 1 + digits_of_length), s);
As you see this gets a bit nasty, because you have to find the digits of the length too, in order to allocate memory.
Why not augmenting your Node with an extra field called str_length, which will keep track of the length of the string the current node holds. That way, you could modify your function to this:
struct Node *prepend(struct Node *list, char *s)
{
struct Node *node = (struct Node *)malloc(sizeof(struct Node));
if (node == NULL)
return NULL;
int length_s = strlen(s);
node->data = malloc(length_s + 1);
strcpy(node->data, temp);
node->str_length = length_s + 1;
node->next = list;
return node;
}
When you go trhough this, don't forget to write a free_list(), which will de-allocates the whole list.
Also, don't cast what malloc returns. Why?
The problem is in these statements
char* temp = NULL;
sprintf(temp,"%d:%s", length_s, s);
You did not allocate memory that will be pointed to by poointer temp. Sp the program has undefined begaviour.
You could make your life simpler if instead of the pointer in your structure you would use an array of size 12 + 1 because the length of copied strings is limited by 12 characters.
For example
enum { size = 13 };
struct Node
{
char data[size];
Node *next;
};
//...
struct Node *prepend( struct Node *list, const char *s )
{
struct Node *node = ( struct Node * )malloc( sizeof( struct Node ) );
if ( node == NULL ) return list;
strnspy( node->data, s, size );
node->data[size - 1] = '\0';
node->next = list;
return node;
}
If you need to use a pointer instead of a character array and sprintft instead of strncpy hen you can write
struct Node *prepend( struct Node *list, const char *s )
{
const size_t N = 12;
struct Node *node = ( struct Node * )malloc( sizeof( struct Node ) );
if ( node == NULL ) return list;
node->data = malloc( ( N + 1 ) * sizeof( char ) );
sprintf( node->data, "%*.*s", N, N, s );
node->next = list;
return node;
}
When the list will not be needed any more you have to delete it. For example
void delete( struct Node *list )
{
while ( list )
{
Node *tmp = list;
list = list->next;
free( tmp->data );
free( tmp );
}
}
Related
my assignment is to create a linked list and then write a bool function attempts to add a student with given id and name into the given list; if a student with that id is already in the list then return false, otherwise the list is modified and true is returned. Im a beginner and I rarely understand why segmentation faults occur so any help will be appreciated.
here are my structure definitions(provided by prof)
struct snode{
int id;
char * name;
struct snode * next;
};
struct slist{
struct snode * front;
};
here is my bool function
bool insert_student(int id, char name[], struct slist * lst) {
struct snode *head = malloc(sizeof(struct snode));
head = lst->front;
// check if list is empty
if (head != NULL) {
struct snode *node = malloc(sizeof(struct snode));
while (node != NULL) {
// traverse the list to see if student exists in list
if (node->id = id) {
return 0;
}
else {
// if it doesnt exist, add it
struct snode *ins_std = malloc(sizeof(struct snode));
ins_std = node->next;
ins_std->id = id;
ins_std->name = name;
ins_std->next = lst->front;
lst->front = ins_std;
return 1;
}
node = node->next;
}
} // if list is empty
else {
head->next = NULL;
head->name = name;
head->id = id;
return 1;
}
}
main function
int main() {
struct slist *head = create_list();
int id1 = 11001;
int id2 = 11002;
int id3 = 11003;
int id4 = 11004;
int id5 = 11005;
char name1[] = "Dave";
char name2[] = "Ali";
char name3[] = "John";
char name4[] = "Randall";
char name5[] = "Kelly";
assert(insert_student(id1, name1, head) == 1);
insert_student(id2, name2, head);
insert_student(id3, name3, head);
insert_student(id4, name4, head);
insert_student(id5, name5, head);
}
Im a beginner and I rarely understand why segmentation faults occur I suspect you never do at this stage.
struct snode *head = malloc(sizeof(struct snode));
head = lst->front;
Here you allocated some space and save the reference to head, only to be overwritten by lst->front (which could be NULL). These two lines already cause segfault. I think what you are trying to do is to save the reference to the allocated space to lst->front, namely lst->front = head; instead of the other way around.
if(node->id = id){
Common error, use == for equality check.
struct snode *ins_std =malloc(sizeof(struct snode));
ins_std = node->next;
Similar issue to the first code snippet.
Solving the above issues should fix the segfaults and assertion errors. There are also a few logical loopholes in the code, but that is for another story.
As a beginner, it helps to have "working code" to study. Here's a 'stripped down' version of your assignment. Make sure you understand what it does, then gradually add elaborations to build-up toward the entire project.
Programs aren't "typed-in", straight from brain to keyboard. You start off with something simple, then SLOWLY embellish that. Compile (with warnings turned up to the max) often, and test each step along the way.
Best wishes.
struct snode{
int id;
struct snode * next;
};
bool addNode( struct snode **pList, int id ) {
for( struct snode *pSrch = *pList; pSrch; pSrch = pSrch->next )
if( pSrch->id == id )
return false; // already on list...
struct snode *pNew = malloc( sizeof( *pNew ) );
// check of malloc() success omitted
pNew->id = id;
pNew->next = *pList; // PREpending to existing list
*pList = pNew;
return true;
}
int main() {
int ids[] = { 11001, 11002, 11003, 11004, 11005, 11002, 11006, 11007, }; // Notice blooper
const int nIDs = sizeof ids/sizeof ids[0];
struct snode *pList = NULL; // This will be 'buried' as "front"...
for( int i = 0; i < nIDs; i++ ) {
printf( "ID %d ... ", ids[i] );
if( addNode( &pList, ids[i] ) )
printf( "Success\n" );
else
printf( "Failed\n" );
}
for( struct snode *pWalk = pList; pWalk; pWalk = pWalk->next )
printf( "Walking... ID %d\n", pWalk->id );
return 0;
}
There are several mistakes in the code shown.
Here is a cleaned up example.
Within insert_student we:
Immediately check that lst is not NULL. If it is, we immediately return false. Every point in the function afterward we can assume lst is not NULL.
Create a pointer to a node that will point to the last node in the list.
If the front node in the list is not NULL we proceed to check if the node is the last. If it is, store a pointer to it in last. And if the id is already taken, we immediately return false.
Now we allocate a new node, making sure to allocate space for the name (and the null terminator character), and using strcpy to copy the name argument into the memory we've just allocated.
If last was assigned something other than NULL, have its next pointer point to the new node.
If last is still NULL it stands to reason lst->front was NULL. Assign the new node pointer to that pointer.
If execution has reached this point, the insertion has happened. Return true.
A further exercise would be to write a function to free the allocated memory.
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
struct snode {
int id;
char *name;
struct snode *next;
};
struct slist {
struct snode *front;
};
bool insert_student(struct slist *lst, int id, const char *name);
int main() {
struct slist *lst = malloc(sizeof(struct slist));
insert_student(lst, 1, "Bob");
insert_student(lst, 2, "Dave");
for (struct snode *cur = lst->front; cur; cur = cur->next) {
printf("%2d, %s\n", cur->id, cur->name);
}
return 0;
}
bool insert_student(struct slist *lst, int id, const char *name) {
if (!lst) return 0;
struct snode *last = NULL;
if (lst->front) {
for (struct snode *cur = lst->front; cur; cur = cur->next) {
if (cur->next == NULL) last = cur;
if (cur->id == id) return 0;
}
}
struct snode *new_node = malloc(sizeof(struct snode));
new_node->id = id;
new_node->name = malloc(strlen(name) + 1);
strcpy(new_node->name, name);
if (last) {
last->next = new_node;
}
else {
lst->front = new_node;
}
return 1;
}
Output:
1, Bob
2, Dave
I get this error when I run the code. Could someone explain why?
Line 70: Char 15: runtime error: member access within misaligned address 0xbebebebebebebebe for type 'struct ListNode', which requires 8 byte alignment [ListNode.c]
0xbebebebebebebebe: note: pointer points here
<memory cannot be printed>
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
struct ListNode * head1=l1;
struct ListNode * head2=l2;
struct ListNode *toReturn=malloc(sizeof(struct ListNode));
//it is complainig about this temp
struct ListNode *temp = NULL;
int carry,total=0;
while(head1 !=NULL || head2!=NULL)
{
temp = malloc(sizeof(struct ListNode));
if(carry==0)
{
total = head1->val + head2->val;
}
else if(carry!=0)
{
total = head1->val + head2->val+carry;
}
if(total ==10)
{
total =0;
carry = 1;
}
else if(total>10)
{
total = total -10;
carry =1;
}
toReturn-> val = total;
toReturn->next = temp;
head1= head1->next;
head2=head2->next;
}
return toReturn;
}
Warning, doing
while(head1 !=NULL || head2!=NULL)
head1 or head2 can be NULL when you dereference them in the body of the loop to get val
you probably wanted :
while(head1 !=NULL && head2!=NULL)
Line 70: Char 15: runtime error: member access within misaligned address 0xbebebebebebebebe for type 'struct ListNode', which requires 8 byte alignment [ListNode.c]
0xbebebebebebebebe: note: pointer points here
An other problem in your function is your function returns a non initialized cell :
if both head1 and head2 are NULL before the loop you return a newly allocated ListNode where both val and (worst) next are non initialized
if by chance the lists received in argument have the same length you return a list of two cells where the second cell has both val and (worst) next are non initialized. Out of that all the cells allocated through temp = malloc(sizeof(struct ListNode)); except the last one are lost (memory leak)
If you call again the function with a list returned by itself you will try to dereference a non initialized field next with an undefined behavior explaining your error
Out of that
Why are you doing
struct ListNode * head1=l1;
struct ListNode * head2=l2;
for nothing because you do not use l1 and l2 later, just work with these variables without introducing the new ones.
Why that complicated code :
if(carry==0)
{
total = head1->val + head2->val;
}
else if(carry!=0)
{
total = head1->val + head2->val+carry;
}
rather than just
total = head1->val + head2->val + carry;
Visibly you do decimal addition and val values between 0 and 9, so why that complicated code :
if(total ==10)
{
total =0;
carry = 1;
}
else if(total>10)
{
total = total -10;
carry =1;
}
rather than just
if (total>10)
{
total = total - 10;
carry = 1;
}
and you also missed an else to reset carry to 0 :
if (total>10)
{
total = total - 10;
carry = 1;
}
else
carry = 0;
Your algorithm also have to manage the case the two lists do not have the same length, that can be done in several ways, to manage the case one of the two lists is NULL inside the while or adding code after the while
Your function has several serious problems.
For example if the both passed to the function pointers are null pointers then the function returns a pointer to an uninitialized node
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
struct ListNode * head1=l1;
struct ListNode * head2=l2;
struct ListNode *toReturn=malloc(sizeof(struct ListNode));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//it is complainig about this temp
struct ListNode *temp = NULL;
int carry,total=0;
while(head1 !=NULL || head2!=NULL)
{
//...
}
return toReturn;
}
So this memory allocation in the beginning of the function
struct ListNode *toReturn=malloc(sizeof(struct ListNode));
does not make a sense.
If one of the pointers head1 or head2 is equal to NULL then this while loop
while(head1 !=NULL || head2!=NULL)
{
//...
}
invokes undefined behavior because within the loop you are accessing memory using a null pointer as for example in this statement
total = head1->val + head2->val;
You should write the condition in the while loop like
while(head1 !=NULL && head2!=NULL)
{
//...
}
Also the last node of the created list also stays uninitialized
while(head1 !=NULL || head2!=NULL)
{
temp = malloc(sizeof(struct ListNode));
//...
toReturn-> val = total;
toReturn->next = temp;
head1= head1->next;
head2=head2->next;
}
Also you are always changing the same node pointed to by the pointer toReturn
toReturn-> val = total;
toReturn->next = temp;
So the function produces memory leaks and the list is not being built.
Numerous if statements are redundant. For example instead of
if(carry==0)
{
total = head1->val + head2->val;
}
else if(carry!=0)
{
total = head1->val + head2->val+carry;
}
you could just write one statement
total = head1->val + head2->val+carry;
You need to append the function with a code that processes one of the lists when other list is empty that is when either head1 or head2 is equal to NULL.
Also the data member val of the structure ListNode should have the type unsigned int if you suppose that lists will contain unsigned numbers, Otherwise you need to store also the sign of the stored number.
Here is a demonstrative program that shows how the function addTwoNumbers can be written.
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
unsigned int val;
struct ListNode *next;
};
int set( struct ListNode **head, unsigned long long n )
{
const unsigned int Base = 10;
while ( *head )
{
struct ListNode *tmp = *head;
head = &( *head )->next;
free( tmp );
}
int success = 1;
struct ListNode **current = head;
do
{
*current = malloc( sizeof( struct ListNode ) );
success = *current != NULL;
if ( success )
{
( *current )->val = n % Base;
( *current )->next = NULL;
current = &( *current )->next;
}
} while ( success && ( n /= Base ) );
if ( !success )
{
while ( *head )
{
struct ListNode *tmp = *head;
head = &( *head )->next;
free( tmp );
}
}
return success;
}
FILE * display( const struct ListNode *head, FILE *fp )
{
if ( head )
{
display( head->next, fp );
fprintf( fp, "%u", head->val );
}
return fp;
}
struct ListNode * addTwoNumbers( const struct ListNode *head1,
const struct ListNode *head2 )
{
const unsigned Base = 10;
struct ListNode *result = NULL;
struct ListNode **current = &result;
unsigned int carry = 0;
int success = 1;
while ( success && ( head1 || head2 ) )
{
*current = malloc( sizeof( struct ListNode ) );
success = *current != NULL;
if ( success )
{
( *current )->next = NULL;
unsigned int sum = carry + ( head1 ? head1->val : 0 ) +
( head2 ? head2->val : 0 );
( *current )->val = sum % Base;
carry = !( sum < Base );
if ( head1 ) head1 = head1->next;
if ( head2 ) head2 = head2->next;
current = &( *current )->next;
}
}
if ( !success )
{
while ( result )
{
struct ListNode *tmp = result;
result = result->next;
free( tmp );
}
}
return result;
}
int main(void)
{
struct ListNode *head1 = NULL;
set( &head1, 123456789 );
fputc( '\n', display( head1, stdout ) );
struct ListNode *head2 = NULL;
set( &head2, 123456789 );
fputc( '\n', display( head2, stdout ) );
struct ListNode *sum = addTwoNumbers( head1, head2 );
fputc( '\n', display( sum, stdout ) );
return 0;
}
The program output is
123456789
123456789
246913578
If you want that the list could contain also negative numbers then the first node of the list could contain the sign: 0 - the number is non-negative, 1 - the number is negative. Another approach is to store signed digits in nodes of the list.
This is part of a larger bit of code but I will include what I think is important. There are actually two types of linked lists I am working with. As you will see the first struct just links to the first node of the list.
Here's the first:
typedef struct mlist {
Node *headFirstName;
Node *headLastName;
} MultiLinkedList;
Here's the second:
typedef struct node {
char *first;
char *last;
long number;
struct node *nextFirst;
struct node *nextLast;
} Node;
Here is how names and numbers are currently added to the list:
MultiLinkedList *add(MultiLinkedList *list, char *first, char *last, long num) {
// allocate a new node
Node *newNode = malloc ( sizeof(Node) );
newNode->first = malloc ( strlen(first) + 1 );
strcpy(newNode->first, first);
newNode->last = malloc ( strlen(last) + 1 );
strcpy(newNode->last, last);
newNode->number = num;
// add this new node at the head of the "byFirst" list
newNode->nextFirst = list->headFirstName;
list->headFirstName = newNode;
// add this new node at the head of the "byLast" list
newNode->nextLast = list->headLastName;
list->headLastName = newNode;
// return the multi-list object with updated head pointers
return list;
}
And here is how I am currently attempting to count the names in the list:
int size(MultiLinkedList *list) {
int count = 0;
Node *newNode = malloc ( sizeof(Node) );
newNode->nextFirst = list->headFirstName;
newNode->nextLast = list->headLastName;
while (newNode->nextFirst!=NULL) {
count++;
}
// return the number of names in the list
return count;
}
If there is a specific name for traversing multiple lists like this then could someone just direct me to that?
You should use size_t for size
Your malloc() is useless
If you don't do somewhere something like x = x->next how do you want your loop finish ?
size_t size(MultiLinkedList *list) {
size_t count = 0;
for (Node *i = list->headFirstName; i; i = i->next) {
count++;
}
// return the number of names in the list
return count;
}
so i am trying to create a linked list, that has a function to add a new node at the beggining of the list, and then print the list out. i am a novice coder and am still learning how to do this, when i execute the code, nothing prints ..
#include <stdio.h>
typedef char DATA;
struct node
{
DATA d;
struct node *next;
};
int main()
{
struct node *header = NULL;
struct node *second = NULL;
struct node *third = NULL;
header->d = 'a';
header->next = second;
second->d = 'b';
second->next = third;
third->d = 'c';
third->next = NULL;
struct addnew;
printList(header);
}
struct node *addnew(node, d, header)
{
struct node *addnew = header;
addnew->d = 'k';
addnew->next = header;
return addnew;
}
int printList(node, next, header)
{
struct node *current = header;
while (next != 'NULL')
{
printf('/c', current->d);
current = current->next;
}
return current->d;
}
The program can look the following way (without testing)
#include <stdio.h>
#include <stdlib.h>
typedef char Data ;
struct node
{
Data d ;
struct node *next ;
};
struct node * addNew( struct node *head, Data value )
{
struct node *new_node = malloc( sizeof( struct node ) );
if ( new_node != NULL )
{
new_node->d = value;
new_node->next = head;
head = new_node;
}
return head;
}
void printList( struct node *head )
{
for ( ; head; head = head->next ) printf( "%c ", head->d );
}
struct node * freeList( struct node *head )
{
while ( head != NULL )
{
struct node *tmp = head;
head = head->next;
free( tmp );
}
return head;
}
int main( void )
{
struct node *head = NULL;
head = addNew( head, 'a' );
head = addNew( head, 'b' );
head = addNew( head, 'c' );
printList( head );
printf( "\n" );
head = freeList( head );
}
As for your code then it contains many errors. For example you did not allocate memory for elements of the list. You only defined pointers to elements that were not allocated and assigned NULL to these pointers.
struct node *header = NULL;
struct node *second = NULL;
struct node *third = NULL;
So next statements
header->d = 'a';
header->next = second;
//..
are wrong.
Also this declaration
struct addnew;
makes no sense.
And you did not correctly define the functions.
# jazuze lots of errors. Even I'm learning linked lists, its okay. I've commented below the areas you need to correct. I have the compiled your code with the corrections and it works properly but I'm not giving you the solution easily. I suggest you work on the below::
#include <stdio.h>
typedef char DATA ; //why do you need to typedef char
struct node {
DATA d ;
struct node * next ;
};
int main (){
struct node* header = NULL;
struct node* second = NULL;
struct node* third = NULL;
header->d = 'a';
header->next = second;
second->d = 'b';
second->next = third;
third->d = 'c';
third->next = NULL;
struct addnew;
printList(header);
}
// place all functions before main
struct node* addnew (node, d, header){ //should be struct node* addnew(struct node* header). This whole function needs to be corrected
struct node* addnew = header; //incorrect logic. You need to malloc addnew first so that you get a new node in memory
addnew->d = 'k';
addnew->next = header;
return addnew; //you lose the header
}
int printList(node, next, header){ //(node, next, header) what is this?? It should be printList(struct node* header)
struct node *current = header;
while (next != 'NULL'){ // incorrect logic. What is next. Should be (current != NULL)
printf('/c', current->d); //should be printf(ā%cā, current->d);
current = current->next;
}
return current->d; //why return current->d
}
My list head always points to tail. What's the problem?
My linked_list.h:
#ifndef LINKED_LIST
#define LINKED_LIST
struct node
{
char *data;
struct node *nextElement;
struct node *prevElement;
};
void createList(struct node **head, struct node **tail);
void fill_list (char *word, struct node **head, struct node **tail);
#endif
main.c:
#include <stdio.h>
#include <stdlib.h>
#include "linked_list.h"
#include <string.h>
int main()
{
FILE *dataFile;
char *word = (char *) calloc ( 255, sizeof(char) );
/* Create empty list */
struct node *head, *tail;
createList (&head, &tail);
/*------------------------*/
/* Data file open*/
dataFile = fopen("data.txt" ,"r");
if( dataFile == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
/* Data reading */
while (( fscanf(dataFile, "%s", word) ) != EOF )
{
int i = 0;
int wordsCount = 0;
for (i = 0; i <= strlen(word); i++)
{
if ( (word[i] >= 'a') && (word[i] <= 'z') )
wordsCount = wordsCount + 1;
}
if ( wordsCount == strlen(word) )
{
fill_list ( word, &head, &tail );
}
}
fclose(dataFile);
return 0;
};
and linked_list.c:
#include <stdio.h>
#include <stdlib.h>
#include "linked_list.h"
void createList(struct node **head, struct node **tail)
{
*head = NULL;
*tail = NULL;
}
void fill_list ( char *word, struct node **head, struct node **tail )
{
struct node *elem, *temp;
if ( (*head) == NULL )
{
// printf("HEAD = NULL\n");
elem = (struct node *) malloc ( sizeof (struct node) );
elem -> data = word;
elem -> nextElement = NULL;
elem -> prevElement = NULL;
(*head) = elem;
*tail = elem;
// printf("%s\n", (*head) -> data );
}
else
{
// printf("HEAD != NULL\n");
elem = (struct node *) malloc ( sizeof (struct node) );
elem -> data = word;
elem -> nextElement = NULL;
elem -> prevElement = *tail;
*tail = elem;
// printf("%s\n", (*head) -> data );
}
}
My data file: qw erty b cc.
Firstly, head == NULL, so head -> data = 'qw' and it should be as head all the time, but it changes to erty, then b and cc after every loop step.
What I'm doing wrong?
The problem is that you use the same string for all input, and you use that for all nodes. This means all nodes will have its data member point to the same string. This string will of course only contain what you last read into it.
You may want to keep the string buffer in main as a normal array (instead of allocating it on the heap) and use e.g. strdup to duplicate the string for the nodes. Don't forget to free them later though.
A pointer is exactly what it sounds like, it's a variable that points to some other place in memory. You can have many pointers all pointing to the same memory.
In your case you pass the pointer word in the function main to all our calls to fill_list. This means that all nodes you create in fill_list will use the exactly same pointer, and they will all point to the exact same memory.
This means that all nodes in your list will have the data member seem to be the same value and it will always be the last string read into word in the main function.
If you use a function such as strdup it will duplicate the string. I.e. it will allocate completely new memory for the string and copy from the old area into the newly allocate area, and return a pointer to the newly allocated memory.
try this
void fill_list ( char *word, struct node **head, struct node **tail )
{
struct node *elem, *temp;
/* you need to create node first ! */
elem = (struct node *) malloc ( sizeof (struct node));
elem -> data = NULL ;
elem -> data = (char*) malloc( sizeof(char)*255));
if ( elem -> data == NULL )
{
perror("alloc data failed ");
}
if ( (*head) == NULL )
{
// printf("HEAD = NULL\n");
/* call memcpy() function need to #include <string.h> */
elem -> data = memcpy((void*)elem -> data,(void*)word,strlen(word));
elem -> nextElement = NULL;
elem -> prevElement = NULL;
(*head) = elem;
*tail = elem;
// printf("%s\n", (*head) -> data );
}
else
{
// printf("HEAD != NULL\n");
elem -> data = memcpy((void*)elem -> data,(void*)word,strlen(word));
elem -> nextElement = NULL;
elem -> prevElement = *tail;
*tail = elem;
// printf("%s\n", (*head) -> data );
}