Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 40121

ERROR IN LINKED LIST

$
0
0
#include<stdio.h>
#include<malloc.h>
struct data
{ int x;
  struct data *link;
}x;

typedef struct data NODE;
NODE*head=NULL;
NODE*curr=NULL;

void insert(int val);
void print();
void del();

int main()
{ int i,j,val;
  printf("How many nos you want to enter\n");
  scanf("%d",&j);

  for(i=0;i<j;i++)
  { scanf("%d",&val);
  insert(val);
  }

  print();
 // del();
  _getch();
  _getch();
}
void insert (int val)
{ NODE*ptr=(NODE*)malloc(sizeof(NODE));
  if(ptr==NULL)
  {printf("allocation failed\n");
  return;
  }
  ptr->x=val;
  if(curr==NULL)
  { head=ptr;
    curr=ptr;
    ptr->link=NULL;
  }
  else
      curr->link=ptr;
      curr=ptr;
      ptr->link=NULL;
      free(ptr);
}
void print()
{ NODE*ptr=(NODE*)malloc(sizeof(NODE));
  if(ptr==NULL)
  {printf("allocation failed\n");
  return;
  }
  int ex;
  ptr=head;
  while(ptr!=NULL)
  {   ex=ptr->x;
      printf("%d\n",ex);

      ptr=ptr->link;
  }

}
void del()
{ NODE*ptr=(NODE*)malloc(sizeof(NODE));
  if(ptr==NULL)
  {printf("allocation failed\n");
  return;
  }
  while(head!=curr)
  { printf("value to be deleted is %d \n",head->x);
    head=head->link;
  }
  if(head==curr)
  {printf("last data to be deleted is %d ",head->x);
   head=curr=NULL;
  }

}

Viewing all articles
Browse latest Browse all 40121

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>