-4

I am writing a code on linklist in C programming language. When I am using an online compiler my code is working fine but when I am using Codeblock to run the code, the code is not working. I am posting the code kindly provide me solution. My code is to add node in a linklist on the last position.

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

struct Node
{
    int data;
    struct Node* next;
};

struct Node* Head;
void insert(int);
void print();

int main()
{
    Head = NULL;

    insert(2);
    insert(3);
    insert(4);

    print();

    return 0;
}

void insert(int a)
{
    struct Node* temp1=(struct Node*)malloc(sizeof(struct Node));

    temp1-> data = a;
    temp1-> next = NULL;

    if(Head == NULL)
    {
        Head = temp1;
        return;
    }

    struct Node* temp = Head;

    while(temp->next!= NULL)
    {
        temp = temp->next;
    }

    temp-> next = temp1;
}


void print()
{
    struct Node* temp2=Head;

    while(temp2 != NULL)
    {

        printf("%d \n", temp2->data);
        temp2 = temp2->next;
    }

    return;

}
ilim
  • 4,477
  • 7
  • 27
  • 46
  • 3
    "not working" is never a good description of the problem. Please describe the input, the expected behaviour and the actual behaviour. Also, best to use a debugger to help you trace the execution of your program rather than relying on others to debug for you. – kaylum Jan 27 '17 at 05:38
  • Please describe the failure you see. At a first glance your code seems fine. Could it be the `print` function that fails – Support Ukraine Jan 27 '17 at 05:38
  • 1
    Also try to properly format your code to make it as readable as possible for people – Gab Jan 27 '17 at 05:41
  • The temp2 variable is useless, every tmp variable is useless when using linked list as long as you DO NOT return the head/top. You can scroll the list just with head/top – Claudio Cortese Jan 27 '17 at 08:43
  • Also you may think to use a top-down approach and defining a function to insert on the head of the list, one for the queue insertion; I would also use a function that creates a new node. – Claudio Cortese Jan 27 '17 at 08:46
  • You should check if malloc returns NULL before accessing the pointer – Kami Kaze Jan 27 '17 at 08:56

1 Answers1

0

Here is how you can achieve what you want to do.

I suggest you to use a top-down approach when programming.

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

struct node
{
    int info;
    struct node *next;
};

struct node *Insert(struct node *, int);
void Print_List(struct node *);
void Remove_List(struct node *);

int main(int argc, char **argv)
{
    struct node *head;

    head = NULL;

    head = Insert(head, 10);
    head = Insert(head, 20);

    Print_List(head);

    Remove_List(head);

    head = NULL;

    return 0;
}

struct node *Create_New_Node(int);
struct node *Head_Insert(struct node *, int);
struct node *Queue_Insert(struct node *, int);
struct node *Insert(struct node *, int);
void Print_List(struct node *);
void Remove_List(struct node *);

struct node *Insert(struct node *top, int elem)
{
    if(top == NULL)
    {
        top = Head_Insert(top, elem);
    }
    else
    {
        top = Queue_Insert(top, elem);
    }

    return top;
}

struct node *Create_New_Node(int elem)
{
    struct node *new_node;

    new_node = (struct node *)malloc(sizeof(struct node));

    if(new_node != NULL)
    {
        new_node -> info = elem;
        new_node -> next = NULL;
    }

    return new_node;
}


struct node *Head_Insert(struct node *top, int elem)
{
    struct node *new_node = Create_New_Node(elem);

    if(new_node != NULL)
    {
        new_node -> next = top;
    }

    return new_node;
}

struct node *Queue_Insert(struct node *top, int elem)
{
    if(top != NULL)
    {
        if(top -> next != NULL)
        {
            top -> next = Queue_Insert(top -> next, elem);
        }
        else
        {
            struct node *new_node = Create_New_Node(elem);

            if(new_node != NULL)
            {
                 top -> next = new_node;
            }
        }
    }

    return top;
}

void Print_List(struct node *top)
{
    while(top != NULL)
    {
        printf("\nInfo : %d\tAddress : %u\tNext link address : %u\n", top -> info, top, top -> next);
        top = top -> next;
    }

    return;
}

void Remove_List(struct node *top)
{
    if(top != NULL)
    {
        Remove_List(top -> next);
        top -> next = NULL;
        free(top);
    }

    return;
}

Sample output :

Info : 10 Address : 39149584 Next link address : 39149616

Info : 20 Address : 39149616 Next link address : 0

Community
  • 1
  • 1
Claudio Cortese
  • 1,372
  • 2
  • 10
  • 21