簡體   English   中英

功能后插入雙鏈表

[英]Doubly Linked List Insert After Function

我之前已經發布過類似的內容,但這是我遇到的問題的另一部分,如果您能幫助我解決錯誤,請多多關照。

如我的標題所述,我正在構建一個雙向鏈接列表,我想知道我的函數insert_after是否有效。 當我運行完整代碼時,我無法在選擇的值之后插入,這就是為什么要發布此代碼。 如果我不清楚或這個問題是否被錯誤提出,請告訴我。 我是C語言的新手,只需要您的幫助即可理解。

這是我的結構:

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

這是我要在選擇的單詞之后插入的功能:

 char insert_after(char words[99], char loc[99])
 {
  struct node *temp, *var, *temp1;
  var=(struct node *)malloc(sizeof(struct node));
  strncpy(var->data, words,100);

  if (head==NULL)
  {
    head=var;
    head->previous=NULL;
    head->next=NULL;

  }
  else
  {
    temp=head;
    while ((temp!=NULL) && (temp->data!=loc))
    {
        temp=temp->next;
    }
    if (temp==NULL)
    {
        printf("\n %s not presented at list\n", loc);
    }
    else
    {
        temp1=temp->next;
        temp->next=var;
        var->previous=temp;
        var->next=temp1;
        temp1->previous=var;
    }
  }
  last=head;
  while (last->next!=NULL)
  {
    last=last->next;
  }
  return 0;// take it out after
 }

這是我的主要任務:->情況3是我的問題

 int main()
 {
char loc[99];
char words[99];
int i, dat;

head=NULL;

printf("Select the choice of operation on link list");
printf("\n1.) Insert At Begning\n2.) Insert At End\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 a word you want to insert in the 1st node ");
            scanf(" %s",words);

            insert_beginning(words);
            display();
            break;
        }
        case 2:
        {
            printf("Enter a word you want to insert in the last node ");
            scanf(" %s",words);
            insert_end(words);
            display();
            break;
        }
        case 3:
        {
            printf("After which data you want to insert your new data ");
            scanf(" %s",words);

            printf("Enter the data you want to insert in list ");
            scanf(" %s",loc);

            insert_after(words, loc);
            display();
            break;
        }

我沒有寫完整的代碼,因為它很長,所以我發布了重要的內容。 如果我的問題不清楚,請告訴我。

謝謝

這是個問題:

while ((temp!=NULL) && (temp->data!=loc))

temp-> data!= loc不是比較字符串,它只是比較指針地址。 您可以使用strcmp或類似的字符串比較函數。 如果兩個參數不同,strcmp將返回非零值。

while ((temp!=NULL) && (strcmp(temp->data, words))

請注意,將words與列表中的值進行比較,因為wordsloc要插入的值。

如果使用char數組作為insert_after函數的參數,則需要注意確保數組的整個長度正確初始化。

請注意,如果列表中只有一個節點,則必須檢查下一個節點(在這種情況下,下一個節點將為NULL)。

char insert_after(char *words, char *loc)
 {
  struct node *temp, *var;
  var=(struct node *)malloc(sizeof(struct node));
  strcpy(var->data, loc);

  if (head==NULL)
  {
    head=var;
    head->previous=NULL;
    head->next=NULL;

  }
  else
  {
    temp=head;
    while ((temp!=NULL) && (strcmp(temp->data, words)))
    {
        temp=temp->next;
    }
    if (temp==NULL)
    {
        printf("\n %s not presented at list\n", words);
    }
    else
    {
        struct node * followingNode = temp->next;
        temp->next=var;
        var->previous=temp;
        var->next= followingNode;
        if (followingNode)
        followingNode->previous = var;
    }
  }
}

您必須使用memcmp或更好的是strcmp進行字符串比較,而不是!=

問題

  1. char words[99]有99個元素,但您最多允許100個, strncpy(var->data, words,100); 如果words未以'\\0'結尾,則可能會導致問題。 您可以非故意地將word[99]寫到var->data ,這是不允許的。 將語句更改為此:

     strncpy(var->data, words, 99); var->data[99] = '\\0'; // In case words wasn't a string 
  2. 您無法比較類似temp->data!=loc這樣的字符串。 您實際上是在比較地址。 將此行更改為以下內容:

     while((temp != NULL) && (strcmp(temp->data, loc) != 0)); 
  3. var->next=temp1不正確。 我們在列表的末尾,因此此行應重寫如下:

     var->next = NULL; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM