0

I'm trying to build a doubly linked list in C by inputting strings and I want to be clear if i'm right. It's hard for me to explain so I'll just post what I have. I'm new to C so please correct me and I hope i'm right and please let me know if it's best to post my whole code.

Okay so I have my struck which looks like this:

 struct node
 {


  char data[100];
  struct node *previous;  // Points to the previous node
  struct node *next;   // Points out to the next node
 }*head, *last;

And this is a function to insert at the beginning:

 void insert_beginning(char words[100])
 {
  struct node *var, *temp;
  var=(struct node *)malloc(sizeof(struct node));
  var->data[100]=words[100];

  if (head==NULL)
  {
    head=var;
    head->previous=NULL;
    head->next=NULL;
    last=head;
  }
   else
   {
    temp=var;
    temp->previous=NULL;
    temp->next=head;
    head->previous=temp;
    head=temp;
   }
 }

and this is my main:

 int main()
 {
char words[100];
int i;

head=NULL;

printf("Select the choice of operation on link list");
printf("\n1.) insert at begining\n2.) insert at at\n3.) insert at middle");
printf("\n4.) delete from end\n5.) reverse the link list\n6.) display list\n7.)exit");

while(1)
{
    printf("\n\n Enter the choice of operation you want to do ");
    scanf("%d",&i);

    switch(i)
    {
        case 1:
        {
            printf("Enter the value you want to insert in node ");
            scanf(" %s",&words);

            insert_beginning(words);
            display();
            break;
        }

I just want to be sure if these 3 parts are correct. Am I missing something? Appreciate the help. I hope i didn't complicate anything and asked the question as needed to be asked.

Yann Bohbot
  • 89
  • 3
  • 14
  • What are you hoping to achieve by `var->data[100]=words[100];`? For copying a standard C-string, use `strcpy` or its safer bro `strncpy`! – Till Dec 24 '13 at 22:53
  • i want to be able to insert a few names or even phrases if possible – Yann Bohbot Dec 24 '13 at 22:54
  • 1
    This is a code review. I think that these kind of questions are better of in http://programmers.stackexchange.com/ (not sure though) – Alkis Kalogeris Dec 24 '13 at 22:54
  • 2
    // Bad: `var->data[100]=words[100];`. // Better: strcpy (var->data, words);`. See ["man strcpy"](http://linux.die.net/man/3/strcpy) – paulsm4 Dec 24 '13 at 22:54
  • so basically i'm copying a string.. do I need to change my main as well? – Yann Bohbot Dec 24 '13 at 22:56
  • 1
    [Do ***NOT*** cast the return value of `malloc()`!](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858) –  Dec 24 '13 at 22:59
  • I'd advise that you still exercise caution when using `strncpy`. It isn't any safer than `strcpy`. See [stop using `strncpy` already](http://randomascii.wordpress.com/2013/04/03/stop-using-strncpy-already/) for more info. And as that page says, also beware Microsoft's `_snprintf` function if you try using it like the C99 `snprintf`. –  Dec 24 '13 at 23:24

1 Answers1

0

Your function insert_beginning has a problem :

  var=(struct node *)malloc(sizeof(struct node));
  var->data[100]=words[100]

By these two lines you want make a copy of words array and put it on the node of your list :) But you implementation is false, you just copy the 100th char (Which is out of bounds of the array cuz the first char begins at 0 and the last is at the 99 index) from the words array to the node data 100th char.

You have to copy all the words content, so you can use strncpy function to do it :

strncpy(var->data, words, 100);

so your function will seems like that :

 void insert_beginning(char words[100])
 {
  struct node *var, *temp;
  var=(struct node *)malloc(sizeof(struct node));
  strncpy(var->data, words, 100);

  if (head==NULL)
  {
    head=var;
    head->previous=NULL;
    head->next=NULL;
    last=head;
  }
   else
   {
    temp=var;
    temp->previous=NULL;
    temp->next=head;
    head->previous=temp;
    head=temp;
   }
 }

Don't use strcpy cuz isn't a safe function against buffer overflows, use strncpy.

It left to you just to fix the main which i didn't understand what do u want to do it.

La VloZ
  • 102
  • 10