简体   繁体   中英

Doubly Linked List with strings

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM