Thread: I need help with my code [linked list]

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    4

    I need help with my code [linked list]

    Hello everyone,
    this program is supposed to be quite simple. I tried to debug it for some time but couldn't realize what seems to be the problem. I'll be greatful for your help!

    The functioned is meant to get an array and its size, and returns the address of a linked list only with the elements that appear once. example:
    for this array:
    57 9 88 74 57 88 74 57 6 57


    the output will be:

    ---->6---->9

    the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define N 40
    typedef struct NODE node;
    typedef struct NODE{
        int data;
        node* next;
    };
    node* list(int ar[N], int size);
    void main(){
        int arr[] = { 57, 6, 57, 74, 88, 57, 74, 88, 9, 57 };
        node* new_list = list(arr[10], 10);
        node* tempi = new_list;
        while (tempi->next != NULL){
            printf("%d ->", tempi->data);
        }
        if (tempi->next == NULL) printf("%d", tempi->data);
    }
    
    node* list(int ar[N], int size){
        int i;
        node*head = (node*)malloc(sizeof(node));
        head->data = ar[0];
        head->next = NULL;
    
        for (i = 1; i < size; i++){
            node*temp = head;
            while((temp->data != ar[i]) && (temp->next != NULL)){
                temp = temp->next;
            }    
            if ((temp->next == NULL) && (temp->data != ar[i])){
            temp->next = (node*)malloc(sizeof(node));
            temp = temp->next;
            temp->data = ar[i];
            temp->next = NULL;
            }
        }
        
        return head;
        
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    There are three things that need fixing:
    Code:
    typedef struct NODE{
        int data;
        node* next;
    };
    Remove the typedef keyword here.

    Code:
    node* new_list = list(arr[10], 10);
    Remove the index on arr. You're passing an array, not an element in the array.

    Code:
    while (tempi->next != NULL){
        printf("%d ->", tempi->data);
    }
    The loop will execute infinitely because tempi is never changed. This fixes it:
    Code:
    while (tempi->next != NULL)
    {
        printf("%d ->", tempi->data);
        tempi = tempi->next;
    }
    Those are the only changes I made for the code as posted to function. And I'll let someone else .......... about void main and casting malloc.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    4
    about your second change - if I remove the number inside the brackets it says "expected an expression".

    didn't notice the third one... thanks
    it still encounters something endless and won't output anything though :/

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,377
    Remove the brackets too: they are part of the index notation, after all.
    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

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    if I remove the number inside the brackets it says "expected an expression".
    No, remove the brackets too:
    Code:
    node* new_list = list(arr, 10);
    it still encounters something endless and won't output anything though :/
    Post your new code after the above fix.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Dec 2014
    Posts
    4
    It's working!
    It didn't output the result it prints once all the numbers, rather the numbers that appears once.. but that's something I need solve

    Thank you all very much!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I have a problem in linked list code
    By QuantaPhysics in forum C Programming
    Replies: 2
    Last Post: 05-25-2014, 06:34 PM
  2. Help with debugging my Linked List code.
    By nslice22 in forum C Programming
    Replies: 8
    Last Post: 03-26-2014, 03:23 PM
  3. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  4. Linked list - why this bit of code?
    By chris1985 in forum C Programming
    Replies: 2
    Last Post: 10-04-2005, 06:17 AM
  5. Linked List Working Code
    By Linette in forum C++ Programming
    Replies: 9
    Last Post: 01-24-2002, 12:00 PM