0

There's nothing wrong when i built the program in both VS & codeblock. While, when i ran it, it either broke after i typed in the index number or just show letters infinitely...

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


// list_node structure

typedef struct list_node{
    int item;
    struct list_node *next;
}ListNode;
//call functions that will be used 

void printNode(ListNode *head);
int removeNode(ListNode **ptrhead, int index);
ListNode *findNode(ListNode *head, int index);

int main(){
    int index,value;
    ListNode *head=NULL;
    ListNode *temp;
    //build the list  

    printf("Enter a value:");
    scanf("%d",&value);
    do{
              temp->item=value;
        if(head==NULL){
            head=(struct list_node *)malloc(sizeof(ListNode));
            temp=head;
        }
        else{
            temp->next=(struct list_node *)malloc(sizeof(ListNode));
            temp=temp->next;
        }
        printf("Enter a value:");
        scanf("%d",&value);

    }while(value!=-1);

    printf("Enter the index: ");
    scanf("%d",&index);
    // remove the node at the position indexed

    // when I used debugger, I saw it didn't execute this step. Maybe there's something wrong with it....

    removeNode(&head,index);

    printNode(head);

    return 0;
}


void printNode(ListNode *head){
    if (head==NULL)
        exit(0);
    while(head!=NULL){
        printf("%d",head->item);
        head=head->next;
    }
    printf("\n");
}

ListNode *findNode(ListNode *head,int index){
    if(head==NULL||index<0)
        return NULL;
    while(index>0){
        head=head->next;
        index--;
    }
    return head;
}


int removeNode(ListNode **ptrhead,int index){
    ListNode *pre,*cur,*temphead;

    temphead=*ptrhead;

    if(findNode(temphead,index)!=NULL){
        pre=findNode(temphead,index);
        cur=pre->next;
        temphead->next=cur;
        return 0;
    }
    else
        return -1;
}

4 Answers 4

0
prev=NULL;
cur=head;
/* traverse the list until you find your target */
while (cur != NULL && cur->id != search_id) {
  prev=cur;
  cur=cur->next;
}
/* if a result is found */
if (cur != NULL) {
  /* check for the head of the list */
  if (prev == NULL)
    head=cur->next;
  else
    prev->next=cur->next;  
  /* release the old memory structure */
  free(cur);
}

Whats up dude....I hope u will get it... :)

0
0

temp->item=value; - you're dereferencing temp when it's uninitialized. Undefined behavior, crash and burn.

1
  • should i initialize [temp] or [temp->next]? I thought I already initialized [temp] with [ListNode *temp=NULL;].
    – Baller
    Mar 24, 2013 at 6:42
0

In your loop,

    temp->item=value;

No space has been allocated to temp. Hence, this will crash !!

    if(head==NULL){
        head=(struct list_node *)malloc(sizeof(ListNode));
        temp=head;

Typically, you woud allocate a temp node and assign it to head as head = temp. Another point which is missing is when a new node is created, the next should be initialized to NULL as temp->next = NULL

In removeNode,

    if(findNode(temphead,index)!=NULL){
        pre=findNode(temphead,index);

pre will be pointing to the node to be removed, but temphead will still be pointing to head of the list. findNode will not modify the temphead. So tmphead->next = cur will modify the head always

2
  • @user2201968 glad to be of help. Hope my answer was sufficient.
    – Ganesh
    Mar 24, 2013 at 7:48
  • int removeNode(ListNode **ptrhead,int index){ ListNode *pre,*cur; if (index==-1) return 1; else if(findNode((*ptrhead),(index))!=NULL){ pre=findNode((*ptrhead),(index-1)); cur=pre->next; pre->next=cur->next; return 0; } else return 1; }
    – Baller
    Mar 24, 2013 at 8:07
0
int removeNode(ListNode **ptrhead,int index){
ListNode *pre,*cur;
if (index==-1)
    return 1;
else if(findNode((*ptrhead),(index))!=NULL){
    pre=findNode((*ptrhead),(index-1));
    cur=pre->next;
    pre->next=cur->next;
    return 0;
}
else
    return 1;
}

I modified the last part and addedtemp->next=NULLin the main function. this program can ran well now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.