Karan Gajjar
Karan Gajjar

Reputation: 1

Singly linked list in c

below is my code for singly linked list in c. Can anyone help me with this?

this is my main c file:

#include <stdio.h>
#include <stdlib.h>
#include "myclib.c"


struct mydata
{
    int num;
    char name;
    struct mydata *next;
};

int main()
{
    struct mydata *head, *newnode, *temp;

    head = (struct mydata*)malloc(sizeof(struct mydata));
    newnode = (struct mydata*)malloc(sizeof(struct mydata));
    temp = (struct mydata*)malloc(sizeof(struct mydata));

    head -> num = 123;
    head -> name = 'k';
    head -> next = NULL;

    newnode -> num = 456;
    newnode -> name = 'd';
    newnode -> next = NULL;

    printf("before.app.head = %p\n",head);
    printf("before.app.newnode = %p\n",newnode);
    printf("before.app.head->next = %p\n",head -> next);    
    printf("before.app.newnode->next = %p\n",newnode -> next);

    head = (struct mydata*)addNodeAtHead(head, newnode, (newnode -> next));

    printf("after.app.head = %p\n",head);
    printf("after.app.newnode = %p\n",newnode);
    printf("after.app.head->next = %p\n",head -> next); 
    printf("after.app.node->next = %p\n",newnode -> next);

    temp = head;

    while(temp != NULL)
    {
        printf("num : %d\n",temp -> num);
        printf("name : %c\n",temp -> name);
        temp = temp -> next;
    }

    free(temp);
    free(head);

    return 0;
}

this is myclib.c file:

#include <stdio.h>


    void * addNodeAtHead(void *head, void *node, void *nodenext)
    {
        printf("\nbefore.head = %p\n",head);
        printf("before.node = %p\n",node);
        printf("before.nodenext = %p\n",nodenext);
        nodenext = head;
        head = node;
        printf("after.head = %p\n",head);
        printf("after.node = %p\n",node);
        printf("after.nodenext = %p\n\n",nodenext);

        return head;

    }

i am trying to add newnode in front of head and than changing head pointer to newnode.

Upvotes: 0

Views: 707

Answers (6)

ArgyrisSofroniou
ArgyrisSofroniou

Reputation: 122

#include <stdio.h>

void addNodeAtHead(void **head, void *node){
        void *prH=*head;

        *head=node;
        node->next=prH;
}

Upvotes: 0

Ravikiran
Ravikiran

Reputation: 600

//------ SINGLY LINKED LIST PROGRAM ( CREATION AND TRAVERSING ) -------X 
#include<stdio.h>
#include<conio.h>
struct node
{
  int info;
  struct node *link;
};
struct node *first;
int main()
{
  void create();
  void traverse();
  create();
  traverse();
  return 0;
}
void create()
{
  struct node *cpt,*ptr;
  char ch;
  ptr=(struct node*)malloc(sizeof(struct node));
  printf("enter the data \n");
  scanf("%d",&ptr->info);
  first=ptr;
  do
  {
    cpt=(struct node*)malloc(sizeof(struct node));
    printf("enter the data \n");
    scanf("%d",&cpt->info);
    ptr->link=cpt;
    ptr=cpt;
    printf("Do you want to create a new node <Y/N> :\n");
    ch=getch();
  }
  while(ch=='y');
  ptr->link=NULL;
}
void traverse()
{
  struct node *ptr;
  ptr=first;
  while(ptr!=NULL)
  {
    printf("The entered nodes are:%d \n",ptr->info);
    ptr=ptr->link;
  }
}

Upvotes: 0

Ravikiran
Ravikiran

Reputation: 600

**PROGRAM ON SINGLY LINKER LIST ( CREATION AND TRAVERSING WITH COMMENTS )**
#include<stdio.h> //Header file for input and output operations.
#include<conio.h>  
struct node   // structure declaration .
{
int info;            // stores information.
struct node *link;   // stores the address of next link.
};
struct node *first; // used to point to the first node.
void create();      // function call |NOTE| This line is optional.
void traverse();    // function call |NOTE| This line is optional.
int main()          // main function.
{
create();      // function call for creation.
traverse();    // function call for traversing i.e nothing but display.
return 0;
}
void create()      // declaration of create function , creation of nodes.
{
char ch;       // variable to take an input from the user for decision.
struct node *ptr,*cpt; // these are used to create a node and referred to as 
//previous pointer and current pointer.
ptr=(struct node*)malloc(sizeof(struct node)); //dynamic declaration 
printf("Enter data into the first node \n");
scanf("%d",&ptr->info); // taking the input from the user.
first=ptr; //assigning the information taken from previous pointer to first for 
//future identification.
do  // loop which continuously generates and links new nodes to previous nodes.
{
   cpt=(struct node*)malloc(sizeof(struct node)); //dynamic declaration of current 
   //pointer.
   printf("Enter the data for another node \n");
   scanf("%d",&cpt->info); // getting input from the user of next node 
  // information.
   ptr->link=cpt;  // linking the previous pointer to the new pointer.
   ptr=cpt;   // transferring the previous node information to the current node 
   //i.e previous pointer to the current pointer.
   printf("Do you want to create another node <Y/N>:\n\n");
   ch=getch();  // getting the users decision.
   }
   while(ch=='y');  // checking the users decision.
   ptr->link=NULL;  // assigning the previous pointer link to null;
   }
   void traverse()  // function declaration of display or traversing.
   {
   int count=1;   // counting variable for naming the nodes.
   struct node *ptr;   // pointer variable used for traversing.
   ptr=first;  // assigning ptr to the first for starting traversing from the 
   //first node.
   printf("TRAVERSING OF A LINKED LIST \n");
   while(ptr!=NULL)  // checking whether the ptr is null or not, And if not 
   //executes the statements written in the body.
   {
    printf("The data stored in node %d is:%d \n",count,ptr->info);  //prints the 
   // node id and information present in the node.
    ptr=ptr->link;  // This statement is used to traverse to the next node.
    count++;  // count incrementation.
    } 
    }

    // f☺ll☺w ☺n instagram ♥ ---> @cyber_saviour 

Upvotes: 0

Sazzadur Rahaman
Sazzadur Rahaman

Reputation: 7116

When you are passing (newnode -> next) to the function addNodeAtHead. The value of (newnode -> next) is copied to the node variable in the function. And you are updating that node variable with new value head. After the execution of the function node variable get destroyed and has no relation with (newnode -> next). And so (newnode -> next) remains unchanged.

To over come it, just change your addNodeAtHead like bellow:

void * addNodeAtHead(void *head, void *node)
{
    printf("\nbefore.head = %p\n",head);
    printf("before.node = %p\n",node);
    ((mydata *)node)-> next = (mydata *) head;
    printf("after.head = %p\n",head);
    printf("after.node = %p\n",node);

    return node;

}

And call it simply like:

  head = (struct mydata*)addNodeAtHead(head, newnode);

And Everything should be okay now.

Upvotes: 1

BLUEPIXY
BLUEPIXY

Reputation: 40145

#include <stdio.h>
#include <stdlib.h>
//#include "myclib.c"

struct mydata
{
    int num;
    char name;
    struct mydata *next;
};

struct mydata* addNodeAtHead(struct mydata* head, struct mydata* node)
{
#ifdef DEBUG
    printf("\nbefore.head = %p\n",head);
    printf("before.node = %p\n",node);
//  printf("before.nodenext = %p\n",nodenext);
#endif
    if(node){
        node->next = head;
        head = node;
    }
#ifdef DEBUG
    printf("after.head = %p\n",head);
    printf("after.node = %p\n",node);
//  printf("after.nodenext = %p\n\n",nodenext);
#endif

    return head;

}

int main()
{
    struct mydata *head, *newnode, *temp;

    head = (struct mydata*)malloc(sizeof(struct mydata));
    newnode = (struct mydata*)malloc(sizeof(struct mydata));
    //temp = (struct mydata*)malloc(sizeof(struct mydata));//unused and rewrite to other pointer

    head -> num = 123;
    head -> name = 'k';
    head -> next = NULL;

    newnode -> num = 456;
    newnode -> name = 'd';
    newnode -> next = NULL;

#ifdef DEBUG
    printf("before.app.head = %p\n",head);
    printf("before.app.newnode = %p\n",newnode);
    printf("before.app.head->next = %p\n",head -> next);    
    printf("before.app.newnode->next = %p\n",newnode -> next);
#endif

    head = (struct mydata*)addNodeAtHead(head, newnode);

#ifdef DEBUG
    printf("after.app.head = %p\n",head);
    printf("after.app.newnode = %p\n",newnode);
    printf("after.app.head->next = %p\n",head -> next); 
    printf("after.app.node->next = %p\n",newnode -> next);
#endif

    temp = head;

    while(temp != NULL)
    {
        printf("num : %d\n",temp -> num);
        printf("name : %c\n",temp -> name);
        temp = temp -> next;
    }
/*
    free(temp);//NULL
    free(newnode);//...
    free(head);//already changed
*/
    temp=head;
    while(temp != NULL){
        struct mydata *prev = temp;
        temp=temp->next;
        free(prev);
    }
    return 0;
}

Upvotes: 1

Treb
Treb

Reputation: 20271

You need to set the next pointer on the added node to point to the original head node. I changed the signature of addNodeAtHead: You should not pass void * when you are alyway passing pointers of the type mydata *. I also change the variable names to be more clear (IMO) about their purpose

mydata * addNodeAtHead(mydata * original_head, mydata * new_node)
{
    new_node -> next = original_head;
    return new_node; // new_node is now the head of the list!
}

Upvotes: 0

Related Questions