0

I have created a linked list, which prints the output just fine except only when the string name contains a single digit 3 .I also tried fflush() on stdin and stdout, without success.

The function:

  void disp(node *head)
     {
      while(head->next!=NULL)
      {
      printf("\n%d\n%d\n",head->roll,head->marks);
      puts(head->name);puts(head->add);
      head=head->next;
      }
      printf("\nlast::::%d\n%d\n",head->roll,head->marks);
      puts(head->name);puts(head->add);
     }

Also, why there is a need of extra printing statements, after the while loop? Why the loop while(head->next!=NULL) terminates on the second last node?

The code :

#include<stdio.h>
#include<conio.h>

  typedef struct NODE
 {
 char *name,*add;
 int roll,marks;
 struct NODE *next;
 }node;

 void disp();

 int main()
 {
  node *head,*temp,*cur; int ch;

  head=(node *)malloc(sizeof(node));
  printf("enter roll marks name add\n");
  scanf("%d%d",&(head->roll),&(head->marks));
  fflush(stdin);fflush(stdout);
  gets(head->name);gets(head->add);
  head->next=NULL;
  temp=cur=head;

  while(1)
 {
  printf("enter more?y/n\n");
  ch=getche();
  if(ch=='n')break;
  temp=(node *)malloc(sizeof(node));
  printf("enter roll marks name add\n");
  scanf("%d%d",&(temp->roll),&(temp->marks));
  fflush(stdin);fflush(stdout);
  gets(temp->name);gets(temp->add);
  temp->next=NULL;
  cur->next=temp;
  cur=temp;
  }

  disp(head);
  getch();
  return 66;
 }

  void disp(node *head)
 {
  while(head->next!=NULL)
  {
  printf("\n%d\n%d\n",head->roll,head->marks);
  puts(head->name);puts(head->add);
  head=head->next;
  }
  printf("\nlast::::%d\n%d\n",head->roll,head->marks);
  puts(head->name);puts(head->add);
 }

Sorry for an old compiler : Turboc (sorry its a compulsion)

UPDATE:

here is the output sample as requested :

enter roll marks name add
1
2
3
4
enter more?y/n
yenter roll marks name add
5
6
7
8
enter more?y/n
n
1
2

4

last::::5
6
7
8

As we can see, number 3 is not displayed.

joey rohan
  • 3,505
  • 5
  • 33
  • 70

1 Answers1

0

Pay attention:

  1. "Also, why there is a need of extra printing statements, after the while loop? Why the loop while(head->next!=NULL) terminates on the second last node?"

--> Answer: Look at your while() condition: head->next!=NULL: means that you won't enter the while if the next node equals NULL. So, for the last node you'll won't enter the while, and won't print your things. You can change your logic in order to do it more nice.

  1. Your main question:

--> Answer: Before I know really what's happening, you must pay attention: you do your malloc thing and allocates memory for each node of yours, BUT! you didn't allocate your strings. for each node you allocate, you need to allocate also char *name,*add. if you don't, when you do gets(temp->name);gets(temp->add); you're just messing with the memory - these pointers doesn't point on anything - it works magically and might don't work at other time/ other compiler. You must malloc these two chars* as well. If you don't want to mess with it, you can start with declaring them as

char name[128], add[128]; - This way you don't have to allocate them - cause they are already allocated. You can start like this.

hudac
  • 2,584
  • 6
  • 34
  • 57
  • for head->next!=NULL, why last node will equal null? are not we talking about the address part of the node, but not the node itself? – joey rohan Feb 02 '14 at 17:37
  • 2
    reserving 128 bytes in advance would not save him from failing in the future, `gets` is a dangerous function and should not be used in newer programs, `fgets` should be used instead. – yeyo Feb 02 '14 at 17:43
  • @joeyrohan, Look at your code, for each node, next will be equal `NULL`: `temp->next=NULL;`. (It's a good thing) – hudac Feb 02 '14 at 17:45
  • Even I get warnings its dangerous, but `fgets` works same as `gets` just provided the `stdout` thing? – joey rohan Feb 02 '14 at 17:48
  • 1
    @joeyrohan That's incorrect. While it is true that `fgets()` works similarly to `gets()`, the most important distinction between the two is that `fgets()` allows you to specify the maximum amount of input to accept. Since the `gets()` function does not provide this ability, it is always possible for someone to provide more input than your program is prepared to accept, this is called a "buffer overflow" and is a far-too-common cause of program failures and security breaches. It is for this reason that the `gets()` function is usually regarded as unsafe for any use, anywhere, ever. – This isn't my real name May 08 '14 at 16:24