C编程 - 在特定节点之后将值插入单链表中

时间:2015-07-13 18:39:21

标签: c linked-list

我有一个程序,我一直在冒汗,并继续在函数insertAfter上出现分段错误。我得到了基本代码,并被要求创建几个函数。我已经让他们中的大多数工作,但我不能让insertAfter在指定的节点后插入一个值。我还没有在insertBefore上做太多工作,但我假设我会遇到同样的问题

所以,这是我的代码:(我已经包含了创建新节点的头节点和函数)

struct lnode
{
    int data;
    struct lnode *next;
};


struct lheader
{
    struct lnode *start;
    int len;
};

struct lnode *makenode( int val )
{
    struct lnode *box;
    box = malloc( sizeof( struct lnode ) );
    box->data = val;
    box->next = NULL;
    return box;
}

这是功能:

void insertAfter( struct lheader *L, struct lnode *p )
{
    int pos, value;
    struct lnode *nn;
    struct lnode *temp;
    temp = p;
    printf( "What number do you want to insert? " );
    scanf( "%d", &value );
    printf( "Insert after which value: " );
    scanf( "%d", &pos );
    nn = makenode(value);
    if ( L->start == NULL )
    {
         L->start = nn;
    }
    else
    {
         temp = L->start;
         while( temp->next != NULL && temp->data != pos )
         {
             temp = temp->next;
         }
         if ( temp->data == pos )
         {
             nn->next = temp->next;
             temp->next = nn;
             printf("Value is %d: ", nn->data);
         }
         else
         {
             printf( "Value %d is not in list\n", pos );
         }
     }
}

我想在错误的地方添加了这个,我想!

感谢您的所有投入。我不得不接我的孩子,无法回到该计划。

这是主要功能,以及主要调用的打印功能。我评论了其他一些功能。

void printlist( struct lnode *front )
{
    struct lnode *mov;
    mov = front;
    while (mov != NULL)
    {
        printf("%d  ", mov->data);
        mov = mov->next;
    }
    printf("\n");
}


void printer( struct lheader *alist )
{
    struct lnode *mov;
    printf("--------------------------\n");
    printf("List print, len %d\n", alist->len);
    printlist( alist->start );
    printf("--------------------------\n");
}


int main()
{
    struct lheader *L;
    struct lnode  *head, *tmp;
    struct lnode  *mark;
    int i, x;

L = makelist();

for (i = 1; i <= 5; ++i)
{
    x = rand() % 25 + 1;
    printf("-- Adding -- %d\n", x);
    //insertFront( L, x );
    insertBack( L, x, i );
    printer( L );
}


printf(">>>>Value to search: ");
scanf("%d", &x);
i = isInList(L, x);
printf("I is %d\n", i);
tmp = findNode(L, x);
if (tmp == NULL)
{     
    printf("NOPE\n");
{
else
{ 
    printf("Found node %d\n", tmp->data);
{ 
insertAfter( L, mark );
// printer( L );
// insertBefore( L, mark );
// printer( L );
return 0;
}

我尝试了一个调试器(第一次),并且它在下面的代码片段中表示分段错误位于temp = temp-&gt;下一步:

else
{
     temp = L->start;
     while( temp->next != NULL && temp->data != pos )
     {
         temp = temp->next;
     }
     if ( temp->data == pos )
     {
         nn->next = temp->next;
         temp->next = nn;
         printf("Value is %d: ", nn->data);
     }
     else
     {
         printf( "Value %d is not in list\n", pos );
     }
 }

}

3 个答案:

答案 0 :(得分:1)

您可以检查以上条件:

  • value:要插入的数据。
  • loc:要插入数据的位置。
void insertAfter(int value,int loc)
{
    struct node* newNode;
    newNode=(struct node*)malloc(sizeof(struct node));
    newNode->data=value;

  if(head==NULL)
    {
      newNode->next=NULL;
      head=newNode;
    }
    else
    {
        struct node* temp=head;
        while(temp->next!=NULL)
        {
            if(temp->data==loc)
            {
                newNode->next=temp->next;
                temp->next=newNode;
                break;
            }
            else
            {
                temp=temp->next;
            }
        }
        if(temp->next==NULL)
            printf("Location not found");
    }
    printf("New Node inserted successfully after %d",loc);
}

答案 1 :(得分:1)

在列表中的特定位置插入(在节点之后):

- 我们可以使用以下步骤在单个链表中的节点之后插入新节点...

  1. 创建具有给定值的newNode。
  2. 检查列表是否为空(head == NULL)
  3. 如果为Empty,则设置newNode→next = NULL并且head = newNode。
  4. 如果它不是空的话,定义一个节点指针temp并用head初始化。
  5. 继续将temp移动到下一个节点,直到它到达我们想要插入newNode的节点(直到temp1→data等于location,这里的location是我们想要插入newNode的节点值)
  6. 每次检查是否到达最后一个节点的temp。如果到达最后一个节点,则显示在列表中找不到给定节点!插入不可能!!!&#39;并终止该功能。否则将temp移动到下一个节点。
  7. 最后,设置&#39; newNode→next = temp→next&#39;和&#39; temp→next = newNode&#39;

    void insertAfter(int value,int loc)
        {
            struct node* newNode;
            newNode=(struct node*)malloc(sizeof(struct node));
            newNode->data=value;
    
          if(head==NULL)
            {
              newNode->next=NULL;
              head=newNode;
            }
            else
            {
                struct node* temp=head;
                while(temp->next!=NULL)
                {
                    if(temp->data==loc)
                    {
                        newNode->next=temp->next;
                        temp->next=newNode;
                        break;
                    }
                    else
                    {
                        temp=temp->next;
                    }
                }
                if(temp->next==NULL)
                    printf("Location not found");
            }
            printf("New Node inserted successfully after %d",loc);
        }
    

答案 2 :(得分:0)

嗯,首先,你对我来说看起来有些偏差。

 while( temp->next != NULL && temp->data != pos )

如果我没弄错的话,你使用pos作为你想要添加新节点的元素的索引。但是,在这里,您将它与临时节点的值进行比较。在我看来,你应该将它与你迭代到下一个节点的次数进行比较。我认为你的问题源于此。

我可能会错误地解释你的变量命名。

相关问题