2016-12-31 8 views
0

リンクリストをCで実装しようとしていますが、先頭とn番目に要素を挿入するはずの2つの異なる関数があります。 n番目の位置に挿入する関数の実行を開始する部分に行くと、プログラムがクラッシュします。誰かが私の間違いを指摘できますか?また、コンパイラは、最後のscanf文(コメントにマークされている)をスキップする傾向があります。n番目のリンク先リスト要素の挿入

/**************************************************************** 
*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; 
} 
+0

_skip最後のscanf文_なぜなら_Press qを終了するからです。入力した 'q'を削除する必要があります。 – BLUEPIXY

+0

'scanf(" q%d%d "、&x,&n);'または 'scanf("%* * Qq%d%d ")、&x,&n);、 – BLUEPIXY

+1

を試してみてください。\ n \メッセージの最初の**? – 0andriy

答えて

0

ptr-> nextがNULLでないことを確認する必要があります。

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

私はその変更を試みましたが、コメントにマークしたscanfステートメントはスキップします。 scanfの代わりにgetcharを使用しても、違いはありません。 – user2808264

1

は、私はいつも私が(私は0以上の通常のCの慣習とは異なり、あなたのコードに基づいて1以上でなければなりませんあなたの位置を仮定している)を理解するためにこのようなものが簡単であることがわかってきました
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も同じことをしています。ノードNを既存のノードABの間に挿入する鍵は、InsertElementAtTheBeginning(B, value)から返されるNを指すA->nextを指すことです。

もう1つの注意点は、NULLノードの前(つまり、空のリストの始めまたはリストの最後)に無条件に挿入することです.の値にかかわらず、位置にアイテムを挿入できないためですあなたが4つ以下のアイテムしか持っていない場合は10です。

関連する問題