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

struct node {
    int data;
    int key;
    struct node *next,*ptr;
};
struct node *head = NULL;
void input(){
    ptr=(struct node *)malloc(sizeof(struct node));
    for(int i=0;i<5;i++){
        printf("enter the elements");
        scanf("%d",&ptr->data);
        ptr->key=i;
        ptr=ptr->next;
    }

    void display(){
        ptr=head;
        while(ptr!=NULL){
            printf("the linkedlist is --\n");
            printf("%d",ptr->data);
            ptr=ptr->next;
        }
    }

}

void main(){
    input();
    display(); 
}

This gives two errors-

  1. error: 'ptr' undeclared
  2. warning: implicit declaration of function 'display'

I have declared ptr ,then why is it giving this error? And what about display function? And is my code logic correct to input the elements in a linkedlist and display them?

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
sugandh goyal
  • 325
  • 2
  • 4
  • 14
  • 2
    I formatted your code so the improper indentation will hopefully stand out like a sore thumb. Regrdless, the only `ptr` is a *member* of `struct node`, so all those `ptr = ...` are wrong. If you don't have one, [get a good book on C](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list?s=1|5.9545). – WhozCraig Jun 12 '17 at 04:57

2 Answers2

1

The first error ptr undeclared is because, you have declared it as part of the structure. So you cannot access the structure member directly. You need to create a struct variable to access it . See http://www.c4learn.com/c-programming/c-accessing-structure-members/. Coming to the second error, the display function is within the input. So the scope of that function is only within the input(). So you might have brace mismatch. So do this,

void input(){
    ptr=(struct node *)malloc(sizeof(struct node));
    for(int i=0;i<5;i++){
        printf("enter the elements");
        scanf("%d",&ptr->data);
        ptr->key=i;
        ptr=ptr->next;
    }
}
void display(){
        ptr=head;
        while(ptr!=NULL){
            printf("the linkedlist is --\n");
            printf("%d",ptr->data);
            ptr=ptr->next;
        }
}

So coming to logic, you got it wrong as well.

deepakchethan
  • 5,240
  • 1
  • 23
  • 33
0
#include<stdio.h>
#include<stdlib.h>

struct node {
    int data;
    int key;
    struct node *next;
};
struct node * createNode(int data){
    struct node *temp =(struct node *)malloc(sizeof(struct node));
    temp->data = data;
    temp->next = NULL;

   return temp;
}
struct node * input(){
    int num;
    struct node * head = NULL, *ptr, *list;

   for(int i=0;i<5;i++){
      scanf("%d",&num);
      ptr = createNode(num);
      ptr->key=i;  // Why are you using this ?

      if(!head){
        list = head = ptr;
      }
      else {
        list = list->next = ptr;
      }
  }

  return head;

}
void display(struct node * head){
  struct node * ptr=head;
  while(ptr!=NULL){
    printf("%d ",ptr->data);
    ptr=ptr->next;
  }
}

int main(){
    struct node * head = input();
    display(head); 
    return 0;
}

I have tried to refactor your code. Hope this helps. Also, you can get a good book on C.

Ajinkya
  • 104
  • 11