简体   繁体   中英

try to find middel but program crash linked list in c with two pointers

i write function whom Traverse linked list using two pointers. Move one pointer by one and other pointer by two. When the fast pointer reaches end slow pointer will reach middle of the linked list. but my code crash when i try to move temp pointer by two

#include <stdio.h>
#include <stdlib.h>
#define MEM (struct node*) malloc(sizeof(struct node))

void addl(); //add elements at last
void print(); //print linked list
void addf(); //add element at first
void addm(); //add element at middel
struct node {

    int data;
    struct node* next;
};
struct node* head;

void addl()
{
    struct node* new, *temp;
    temp = head;

    new = MEM;

    printf("\n\t\tenter any number : ");
    scanf("%d", &new->data);
    new->next = 0;
    if (temp == 0)
        head = new;
    else {
        while ((temp->next != 0))
            temp = temp->next;
        temp->next = new;
    }
}
void print()
{
    struct node* temp = head; //
    printf(" \n Elements are : ");
    while (temp != 0) {
        printf(" %d ", temp->data);
        temp = temp->next;
    }
}
void addf()
{
    struct node* new;
    new = MEM;
    printf("\n\t\tenter any number : ");
    scanf("%d", &new->data);
    new->next = head;
    head = new;
}
void addm()
{
    struct node* new, *temp, *med;
    temp = head;
    med = head;
    new = MEM; //MEM #define for dynamic memory allocation

    printf("\n\t\tenter m any number : ");
    scanf("%d", &new->data);

    if (temp == 0)
        head = new;
    else {
        while ((temp = temp->next != 0)) {
            med = med->next;
            temp = temp->next; //fist move
            temp = temp->next; //2nd move when i add program crash
        }
        //  new->next=med;
        //med->next=new;
        printf("\n\t\tDATA : %d\n", med->data);
    }
}

int main()
{
    head = 0;
    int i = 5; //create linked list
    while (i) {

        system("cls");
        addf();
        addl();
        i--;
    }
    addm();
    print();
    return 0;
}

as of now addm not add anything in linked list because code crash while i try to found mid of linked list

The crash is due to these two line -

temp=temp->next;//for one move
temp=temp->next;//for second move when i add this program crash

Let's think of two situations -

1) List has one element. Then after the while check condition, dur to temp=temp->next line temp will point to NULL . In next temp=temp->next line you are trying to de-reference that NULL . That will crash

2) List have 2 elements. After while condition check temp will point to last element. And after next temp=temp->next line temp will point to NULL . Now in very next line you are trying to de-reference that NULL . Which is another point of crash

You need to remove one temp=temp->next from inside loop as it advances temp by 3 position in each loop iteration which is clearly a logical bug. Node that after that removing one of them will not eliminate the chance of crash.

Another thing is that the commented code is also wrong.

//  new->next=med;
//med->next=new;

You may have wanted to do -

new->next = med->next;
med->next = new;

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