Thread: link list, segmentation error

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    1

    Question link list, segmentation error

    I'm having a segmentation error whenever I try to print the list I've created. I'm confused to why this is happening. It seems to be happening whenever I try to access node->data with printf. I'm doing this on my own to learn programming and creating abstract data types and familiarization with c, this is not a school assignment. Another thing, this compiles without any problem. It is only when I try to call printlist or delete node that I run into a segmentation fault.
    Code:
    List.h:
    
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef int node_data;
    
    struct node {
    
      struct node *next;
      struct node *prev;
      int data;
    
    };
    
    struct node *conductor = NULL;
    struct node *begin = NULL;
    
    void init() {
    
      /* Initializes this library,
         call before any other functions
         are called. */
    
      conductor = NULL;
    
    }
    
    int emptyList () {
    
      /* Checks to see if the list is empty,
         and returns true if so */
    
      if (conductor = NULL) {
        return 1;
      }else{
        return 0;
      }
    }
    
    void newNode () {
    
      /* Creates a new node */
      struct node *temp = malloc(sizeof(struct node));
    
      printf("Input data for the new node\n");
      temp->data = scanf("&#37;d", &(temp->data));
      if (emptyList()){
        begin = temp;
        temp->next = temp;
        temp->prev = temp;
    
        conductor = temp;
      }else{
        temp->prev = conductor;
        temp->next = begin;
      }
    }
    
    void printList() {
    
      /* Prints the list */
      int condition;
      struct node *temp;
    
      temp = conductor;
    
      do {
        int x = conductor->data;
        printf("%d\n", x);
        conductor = conductor->next;
    
        if (conductor = temp) {
          int condition = 0;
        }else{
          int condition = 1;
        }
      }while(condition != 0);
    
      conductor = temp;
    }
    
    void freeNode() {
    
      /* Deletes the node that the conductor is pointing to
         then re-links the broken list */
    
      struct node *temp;
      int choose;
    
      if (emptyList()){
        printf("The list is empty!\n");
      }else{
        do {
          printf("%d, is here, delete this node?\n", conductor->data);
          scanf("%d",&choose);
          if (choose){
            temp = conductor;
            conductor->prev = conductor->next;
            conductor = conductor->next;
            free(temp);
            printf("Deleting node...\n");
            if (emptyList()){
              printf("The list is empty! Re-initalise and star over!\n");
              break;
            }
          }else{
            conductor = conductor->next;
          }
        }
        while(choose != 1);
    }
    }
    Code:
    main.c:
    
    #include "list.h"
    #include <stdio.h>
    
    
    int main()
    {
      int menu;
      int loop = 0;
    
      /*printf("1. Make a new node\n"
             "2. Print the list\n"
             "3. Delete a node\n");
      //scanf("%d",&menu);
    
      printf("Initialising list\n");
      init();
      printf("List initialised!\n");
      */
    
      do {
        printf("1. Make a new node\n"
               "2. Print the list\n"
               "3. Delete a node\n"
               "4. Initialise List\n");
        //scanf("%d",&menu);
    
        /* printf("Initialising list\n");
           init();
        printf("List initialised!\n");
        */
        scanf("%d",&menu);
        switch (menu)
         {
         case 1:
          newNode();
          printf("newNode called\n");
          break;
    
         case 2:
          printList();
          printf("List printed!\n");
          break;
    
         case 3:
          freeNode();
          printf("Node deleted!\n");
          break;
    
         case 4:
          init();
          printf("List initialised!\n");
          break;
        }
    
        printf("Again?\n");
        scanf("%d",&loop);
      }while (loop);
    
      printf("Execution successful, terminating...\n");
      return 0;
    }
    Thanks for your help
    Last edited by bernhj; 10-16-2007 at 07:11 PM.

  2. #2
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Use a debugger and check your pointer values before you try to access them.
    The following line in you printList function "conductor" is null, so you can't access conductor->data.

    Code:
    int x = conductor->data;
    You have lots of other problems. The following link will go a long way in teaching you what you need to know.
    http://faq.cprogramming.com/cgi-bin/...&id=1073086407
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I haven't looked through your code yet but ...
    Code:
    >  if (conductor = NULL) {
    Use == for equality:
    Code:
      if (conductor == NULL) {
    Some compilers will warn you about this ... For example gcc, with the -Wall option.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >void printList() {
    >.
    >.
    >    if (conductor = temp) {
    Same problem here:
    Code:
        if (conductor == temp) {

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >void newNode () {
    >.
    >.
    >  }else{
    >    temp->prev = conductor;
    >    temp->next = begin;
    >  }
    Here I would think you'd want to set conductor equal to temp, otherwise the spot to add nodes is lost:
    Code:
      }else{
        temp->prev = conductor;
        temp->next = begin;
        conductor = temp;
      }
    And I could be wrong, but I would think printList() should set temp equal to the beginning of the list:
    Code:
    temp = begin;
    And then walk through the list until the conductor node is found. But first it should probably check for an empty list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM