链接列表元素在第n个位置插入

时间:2016-12-31 12:25:09

标签: c data-structures linked-list

我正在尝试在C中实现链表,我有两个不同的函数,应该在开头和第n个位置插入元素。当程序进入开始执行插入第n个位置的功能的部分时,我的程序崩溃了。有人可以指出我的错误。此外,编译器倾向于跳过最后一个scanf语句(在注释中标记)。

/****************************************************************
*Date: 12/30/2016
*This program adds an element to the beginning  and nth position
*of a linked list and then displays the elements in the list
*****************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>



struct node
{
    int data;//Refers to the data part of the linked list
    struct node* next;//Refers to the pointer which points to the next element
};


/*************************************************************************************
Description   : This function is used to print the elements in the linked list
Input         : Pointer to the beginning of the linked list
Output        : N/A
*************************************************************************************/


void PrintElements(struct node *start)
{
    if(start==NULL)//If the list is empty
        printf("List is empty");
    else
    {
        while(start!=NULL)
        {
            printf("%d ",start->data);
            start=start->next;
        }
    }
}

/*************************************************************************************
Description   : This function is used to insert elements in the beginning of the
                linked list
Input         : Element to be inserted
Output        : N/A
*************************************************************************************/

struct node* InsertElementInTheBeginning(struct node *start, int x)
{
    struct node *new_node=(struct node*)malloc(sizeof(struct node));
    if(new_node==NULL)
    {
        printf("Memory allocation failed");
        return start;
    }
    new_node->data=x;
    new_node->next=start;
    start=new_node;
    return new_node;

}

/*************************************************************************************
Description   : This function is used to insert elements in the nth position of the
                linked list
Input         : Element and position to be inserted, pointer to beginning of linked
                list
Output        : N/A
*************************************************************************************/

struct node* InsertElementAtN(struct node *start,int x, int n)//Here the starting position for the linked list is assumed to be 1
{
    int i;
    struct node* new_node=(struct node*)malloc(sizeof(struct node));
    if(new_node==NULL)
    {
        printf("Memory allocation failed");
        return start;
    }
    new_node->data=x;
    new_node->next=NULL;
    if(n==1)
    {
        new_node->next=start;
        start=new_node;
        return start;
    }
    struct node *ptr;
    ptr=start;
    for(i=0;i<n-2;i++)
    {
        ptr=ptr->next;
    }

    new_node->next=ptr->next;
    ptr->next=new_node;
    return start;
}

int main()
{

    int x, n;
    struct node *HEAD;
    struct node *ptr;
    HEAD=NULL; //Assigning HEAD to null when there are no elements in the list
    ptr=NULL;
    printf("\n\rEnter numbers to be inserted into the list\n Press q to quit\n");
    while(scanf("%d",&x)==1)
    {
        HEAD=InsertElementInTheBeginning(HEAD,x);
        PrintElements(HEAD);
        printf("\n\rEnter numbers to be inserted into the list\n Press q to quit\n");
    }
        printf("\n\rEnter the number and position to be inserted");
        scanf("%d %d",&x,&n);//The compiler always skips this scanf no clue why
        HEAD=InsertElementAtN(HEAD, x, n);
        PrintElements(HEAD);

     //Freeing dynamic memory

    while(HEAD!=NULL)
    {
        ptr=HEAD;
        HEAD=ptr->next;
        free(ptr);

    }

    return 0;
}

3 个答案:

答案 0 :(得分:1)

我总是发现这样的东西对我来说更容易理解(我假设你的位置必须是1或更多基于你的代码,不像通常的C约定为0或更多):

struct node *insertValueAt(struct node *head, int value, int position) {
    struct node *current;

    if (head == NULL || position == 1)
         return insertBefore(head, value);

    for (current = head; position > 1 && current->next != NULL; position--)
        current = current->next;

    current->next = insertBefore(current->next, value);

    return head;
}

请注意,我使用了名称insertBefore,但您的InsertElementAtTheBeginning做了同样的事情。在现有节点NA之间插入节点B的关键是使A->next指向从N返回的InsertElementAtTheBeginning(B, value)

需要注意的另一件事是在NULL节点之前(即在空列表的开头或列表的末尾)无条件插入,而不管position的值是什么,因为你可以如果只有少于4个项目,请在第10位插入一个项目。

答案 1 :(得分:0)

你应该检查ptr-&gt; next是不是NULL。

 for(i=0;i<n-2;i++)
 {
    if (ptr->next == NULL) return NULL;
    ptr=ptr->next;
 }

答案 2 :(得分:0)

在main()中的第一个while循环之后添加一个getchar()。有关说明,请查看C: Multiple scanf's, when I enter in a value for one scanf it skips the second scanf

相关问题