1

I'm trying to implement a Linked list using this code.This code complies successfully but results in Segmentation fault (core dumped) error.How can I resolve this?

#include<stdio.h>
#include<stdlib.h>
struct node{
    char ch;
    struct node *next;
};
struct node *head=(struct node *)malloc(sizeof(struct node));
struct node *p1=NULL;
void addnode(char ch) {
    if(head==NULL) {
        head->ch=ch;
        head->next=NULL;
    }   
    else {
        struct node *New=(struct node *) malloc (sizeof(struct node));
        for(p1=head;p1->next!=NULL;p1=p1->next);
            p1->next=New;
    }
}
void main() {
    char ch,frm,to;
    printf("\nEnter the string");
    while((ch=getchar())!='\0')
        addnode(ch);
    for(p1=head;p1!=NULL;p1=p1->next)
        printf("\n%c",p1->ch);
}
tommybee
  • 2,409
  • 1
  • 20
  • 23
Revaapriyan
  • 309
  • 2
  • 4
  • 20
  • 2
    When you allocate a new node, you never assign any values to it. So `ch` will be unknown, and `next` could point to anywhere. The fact that `addnode(ch)` does not actually use `ch` ought to be a warning sign... – jasonharper May 28 '17 at 17:31
  • Roger That,thank you; – Revaapriyan May 29 '17 at 04:23

3 Answers3

3

Simple Mistakes first: When you allocate memory in global you initiate a function call(malloc is also a function). Function calls can only be made inside main or other funcitons. So just declare head don't use malloc in global.

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

struct node{
char ch;
struct node *next;
};
struct node *head=NULL;

struct node *p1=NULL;
void addnode(char ch) {
if(head==NULL) {
    struct node *New=(struct node *) malloc (sizeof(struct node));
    head=New;
    New->ch=ch;
    New->next=NULL;
}

else {
    struct node *New=(struct node *) malloc (sizeof(struct node));
    New->ch=ch;
    New->next=NULL;
    for(p1=head;p1->next!=NULL;p1=p1->next);
        p1->next=New;
}
}

void main() {
char ch,frm,to;
printf("\nEnter the string");
while((ch=getchar())!='\n')
    addnode(ch);
for(p1=head;p1!=NULL;p1=p1->next)
    printf("\n%c",p1->ch);
}
  • Second mistake: Inside your addnode function when you chekk if head is null or not allocate some memory and assign it to head.

  • Third mistake: in your getchar() check until you find a new line not null character.

  • Fourth mistake: Assign ch to New and set New->next=null. You almost completely forgot that.

Pushan Gupta
  • 3,697
  • 5
  • 23
  • 39
1

This worked better and I over came the errors :). And i missed pointers clarity there, which were rectified here..

#include<stdio.h>
#include<stdlib.h>
struct Node{
    char ch;
    struct Node *next;
};
struct Node head={'\0',NULL};
struct Node *p1=NULL;
void add(char ch){
    if(head.ch=='\0')
        head.ch=ch;
    else{
    struct Node *new=(struct node *)malloc(sizeof(struct Node));
    new->ch=ch;
    for(p1=&head;p1->next!=NULL;p1=p1->next);
    p1->next=new;
    }
}
void main(){
    char c;
    while((c=getchar())!='\n')
        add(c);
    for(p1=&head;p1!=NULL;p1=p1->next)
        printf("%c\n",p1->ch);
}

But i still receive a warning saying,

initialization from incompatible pointer type [enabled by default]

struct Node *new=(struct node *)malloc(sizeof(struct Node));
               ^
Revaapriyan
  • 309
  • 2
  • 4
  • 20
  • 1
    Yepp! it will. Because of a typo: Make the n as Capital N in struct Node *new=(struct ///n///ode *)malloc(sizeof(struct Node)); Rest is good enough – Pushan Gupta May 29 '17 at 06:18
  • 1
    There is NO need to cast the return of `malloc`, it is unnecessary. See: [**Do I cast the result of malloc?**](http://stackoverflow.com/q/605845/995714) for thorough explanation. – David C. Rankin May 29 '17 at 07:04
0

I am not sure this is c way..but you have to think your code over how to release your allocated pointer...like free list function maybe..

Here is my way.

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

    struct node{
        char ch;
        struct node *next;
    };

    struct node * addnode(struct node *head, struct node *p1, char ch) {
        if(head==NULL) {
            printf("......return 2... \r\n");
            head=(struct node *)malloc(sizeof(struct node));
            head->ch=ch;
            head->next=NULL;

            return head;
        }
        else {
            struct node *New=NULL;
            printf("......return ... \r\n");

            New=(struct node *) malloc (sizeof(struct node));
            New->ch = ch;
            New->next=NULL;

            for(p1=head;p1->next!=NULL;p1=p1->next);

            p1->next=New;

            return head;

        }
    }

    void main() {

        char ch,frm,to;
        struct node *head=NULL, *p1=NULL;

        printf("\nEnter the string \n");


        while((ch=getchar())!='q')
            head = addnode(head, p1, ch);

        for(p1=head;p1!=NULL;p1=p1->next)
        {
            printf("\n%c",p1->ch);
        }

    }

Another one.

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

typedef struct node{
    char ch;
    struct node *next;
} *pNODE, NODE;


pNODE addnode2(pNODE head, pNODE p1, char ch) {
    if(head==NULL) {
        printf("......return 2... \r\n");
        head=(pNODE)malloc(sizeof(NODE));
        head->ch=ch;
        head->next=NULL;

        return head;
    }
    else {
        struct node *new=NULL;
        printf("......return ... \r\n");

        new=(pNODE) malloc (sizeof(NODE));
        new->ch = ch;
        new->next=NULL;

        for(p1=head;p1->next!=NULL;p1=p1->next);

        p1->next=new;

        return head;

    }
}

void main() {

    char ch,frm,to;
    pNODE head=NULL;
    pNODE p1=NULL;

    printf("\nEnter the string \n");


    while((ch=getchar())!='q')
        head = addnode2(head, p1, ch);

    for(p1=head;p1!=NULL;p1=p1->next)
    {
        printf("\n%c",p1->ch);
    }

}
tommybee
  • 2,409
  • 1
  • 20
  • 23
  • but this too resulted in same error,which i found occurred by casting the new node.Then I changed my code as, `struct Node *new=malloc(sizeof(struct Node));` But Thanks a lot for new **way** – Revaapriyan May 29 '17 at 06:05
  • 1
    well, new is some kind of c++ keyword pal. – tommybee May 29 '17 at 06:28
  • 1
    well, new is some kind of c++ keyword pal. it won't compile with your c++ compiler. I also have any warning message both microsoft or gcc compiler. I just added one more example. – tommybee May 29 '17 at 06:56