Thread: Help with execution of program.

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    17

    Help with execution of program.

    I am new at this, so probably a lot of mistakes, I tried to make a header file and pasted all functions, Here:

    Code:
    /*linkedlist.h contains methods to delete, push, display Linked List*/struct Node{
        int data;
        struct Node *next;
    }*head;
    struct Node* push(struct Node *current_head, int new_data);
    void displayList(struct Node* current_head);
    struct Node* deleteList(struct Node* current_head);
    struct Node* dynamicList(struct Node* current_head)
    {   int num;
        puts("Enter the elements:(-1 to end)");
        while(num != -1){
            scanf("%d", &num);
            if(num != -1)
                current_head = push(current_head, num);
            else
                break;
        }
        return current_head;
      }
    struct Node* deleteList(struct Node* current_head){
        struct Node *next;
        while(current_head != NULL){
            next = current_head->next;
            free(current_head);
            current_head = next;
        }
        printf("The list is deleted.");
        return current_head;
    }
    void displayList(struct Node* current_head)
    {
      while(current_head != NULL){
          printf("%d\n", current_head->data);
          current_head = current_head->next;
      }
    }
    struct Node* push(struct Node *current_head, int new_data)
    {
        struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
        new_node->data = new_data;
        new_node->next = current_head;
        return new_node;
    }
    Then I created another file and imported those functions:

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include "linkedlist.h"
    
    
    int main(){
        struct Node* head = (struct Node*)malloc(sizeof(struct Node));
        head = dynamicList(head);
        displayList(head);
        head = deleteList(head);
    }
    When I am running this code after the input, the list is not displayed but a series of numbers(addresses?) are displayed like in integer overflows.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You're almost there.
    The problem is you list wasn't terminated properly with a valid NULL pointer.

    Also, code doesn't go in header files.

    Code:
    ===== linkedlist.h
    struct Node {
        int data;
        struct Node *next;
    };
    
    struct Node* dynamicList(struct Node* current_head);
    struct Node* push(struct Node *current_head, int new_data);
    void displayList(struct Node* current_head);
    struct Node* deleteList(struct Node* current_head);
    ===== linkedlist.c
    #include <stdio.h>
    #include "linkedlist.h"
    #include <stdlib.h>
    
    struct Node* dynamicList(struct Node* current_head)
    {
        int num;
        puts("Enter the elements:(-1 to end)");
        while(num != -1){
            scanf("%d", &num);
            if(num != -1)
                current_head = push(current_head, num);
            else
                break;
        }
        return current_head;
    }
    
    struct Node* deleteList(struct Node* current_head)
    {
        struct Node *next;
        while(current_head != NULL){
            next = current_head->next;
            free(current_head);
            current_head = next;
        }
        printf("The list is deleted.");
        return current_head;
    }
    
    void displayList(struct Node* current_head)
    {
      while(current_head != NULL){
          printf("%d\n", current_head->data);
          current_head = current_head->next;
      }
    }
    
    struct Node* push(struct Node *current_head, int new_data)
    {
        struct Node* new_node = malloc(sizeof(struct Node));  // no casting necessary here (see the FAQ)
        new_node->data = new_data;
        new_node->next = current_head;
        return new_node;
    }
    ===== main.c
    #include <stdio.h>
    #include <stdlib.h>
    #include "linkedlist.h"
    
    int main(){
        struct Node* head = NULL;
        head = dynamicList(head);
        displayList(head);
        head = deleteList(head);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your dynamicList function is actually more of a helper function for the main function than part of the interface of your struct Node, so I would put it in the same source file as the main function and leave it out of the header. I might even declare it static.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2019
    Posts
    17
    I did what you suggested, moved the code to a .c file and the prototypes to header file, and it totally makes sense now(library and header files are different), but I'm getting linker error. Here you go:

    Code:
     C: \Users \ DELL \ AppData \ Local \ Temp \ ccD4Kh6o.o: test.c: (.text + 0x1e): undefined reference to ` dynamicList '
    C:\Users\DELL\AppData\Local\Temp\ccD4Kh6o.o:test.c:(.text+0x2e):
     undefined reference to `displayList' C: \Users \ DELL \ AppData \ Local \ Temp \ ccD4Kh6o.o: test.c: (.text + 0x3a):
    undefined reference to ` deleteList
        '
    collect2.exe: error: ld returned 1 exit status
    Last edited by Salem; 03-17-2019 at 09:57 AM. Reason: Removed crayola

  5. #5
    Registered User
    Join Date
    Jan 2019
    Posts
    17
    I searched the term helper function and what I'm getting is that it is a function that performs part of the computation of another function and are used to make your programs easier to read by giving descriptive names to computations. Leaving the second part of the definition I don't see a solid reason to include it in main. Why should I declare it static? What happens when I declare it static?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your linker error looks like it is because you didn't compile and link both source files.

    dynamicList belongs with the main function because it is very specific to your particular program: it performs interactive I/O to support the main function. It doesn't generically create a linked list such that say, you could call it from within a GUI program. That's why it is a helper function for your main function.

    Similiarly, your deleteList function should not print "the list is deleted". This should happen in the function that calls deleteList. In other words, write your library functions such that they can be easily used in multiple programs with different user interfaces. You don't really have much choice in displayList as it is intended to print to standard output, but to make it more generic I might provide a FILE* parameter.

    Declaring a function static gives its identifier static linkage, so the name can be used in a different source file without conflict. This is useful for helper functions.

    By the way, stop using fancy colours for your text: it just makes it difficult to read.
    Last edited by laserlight; 03-17-2019 at 08:57 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2019
    Posts
    17
    Thank You for your thorough input. I copied and pasted the definition of helper function that's why fancy colour.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting pathname from program in execution
    By nicauron in forum C Programming
    Replies: 12
    Last Post: 08-11-2011, 02:15 PM
  2. program execution
    By kris.c in forum Tech Board
    Replies: 14
    Last Post: 05-16-2007, 01:54 PM
  3. execution of a C program
    By kris.c in forum C Programming
    Replies: 7
    Last Post: 07-27-2006, 11:14 AM
  4. program execution
    By xlordt in forum C Programming
    Replies: 4
    Last Post: 04-11-2003, 05:47 AM
  5. Program Execution
    By UnclePunker in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2003, 10:07 AM

Tags for this Thread