data structure non continuous linked list problem...

i have a code that i dont understand partially the code is as follows:-
#include<conio.h>
#include<stdio.h>
struct node
	{
		int info;
		struct node *next;
	};
	typedef struct node node;
int main()
{

	node n1,n2,n3,n4,*p;
	p=&n1;
	n1.info=10;
	n2.info=20;
	n3.info=25;
	n4.info=16;
	n1.next=&n2;
	n2.next=&n3;
	n3.next=&n4;
	n4.next=NULL;
	while(p!=NULL);
	{
		printf("%d",p->info);
		p=p->next;
	}
	getch();
}
it says no error but it did not ran......
things i dont understand is -> sign and concept of self refrential (node *next) structure i know how a linked list works but there are these few queries

Comments

  • It runs for me, but the command prompt is blank. As for the so called pointer to -> operator I have never understood it myself. Read about it a thousand time, and nothing sticks. Maybe if it wasn't so boring. Also this is most likely compiler specific I got a warning about getch();. It needed an underscore.
  • A few things:

    1. The [icode]->[/icode] operator replaces the [icode].[/icode] operator when the struct (or class in C++) is a pointer. That's really the only difference.
    2. [icode]while(p!=NULL);[/icode] -- This is the problem. Think on it for a bit.
    3. PUT YOUR DAMN CODE INSIDE CODE TAGS SO IT'S READABLE. Do you honestly think a programming forum wouldn't have some mechanism for posting code?
  • I figured while (p != NULL); had some sort of problem. I remember when I first came here I once thought a programming forum could-should format the code for you. At least code tag wise anyways. As for formatting I just had a strange reason as to why it was not needed. I think people end up with this problem from a lack of understanding code scope.
  • i will try out and post again i have a few more questions on linked list and some on stack i will try again those also and if i am unsuccessful pls gys help me on those also :):twisted:
  • it worked thanks gyus:twisted::twisted: but why was it not working with null???

    #include<conio.h>
    #include<stdio.h>
    struct node
    {
    int info;
    struct node *next;
    };
    typedef struct node node;
    int main()
    {

    node n1,n2,n3,n4,*p;
    p=&n1;
    n1.info=10;
    n2.info=20;
    n3.info=25;
    n4.info=16;
    n1.next=&n2;
    n2.next=&n3;
    n3.next=&n4;
    n4.next=0;
    while(p!=0);
    {
    printf("%d",p->info);
    p=p->next;
    }
    getch();
    }
  • @psychic When I run your code I still get a blank command prompt. I am wondering how is that considered working. Also where did you get this code from in the first place?
    Here is something for the use of code tags. http://www.programmingforums.org/thread14688.html
  • @psychic When I run your code I still get a blank command prompt. I am wondering how is that considered working. Also where did you get this code from in the first place?
    Here is something for the use of code tags. http://www.programmingforums.org/thread14688.html
    #include<conio.h>
    #include<stdio.h>
    int main()
    {
    	typedef struct node
    	{
    		int info;
    		struct node *next;
    	} node;
    	node n1,n2,n3,n4,*p;
    	p=&n1;
    	n1.info=10;
    	n2.info=20;
    	n3.info=30;
    	n4.info=40;
    	n1.next=&n2;
    	n2.next=&n3;
    	n3.next=&n4;
    	n4.next=NULL;
    	while(p!=NULL)
    	{
    		printf("%d\n",p->info);
    		p=p->next;
    	}
    }
    

    sory for the wrong code this works i have checked it .... i use cfree 5
  • Yes that does work, but you should have left your struct where it was.
    #include <stdio.h>
    
    typedef struct node
    {
    	int info;
    	struct node *next;
    } node;
    
    int main()
    {
    	node n1, n2, n3, n4, *p;
    	p = &n1;
    	n1.info = 10;
    	n2.info = 20;
    	n3.info = 30;
    	n4.info = 40;
    	n1.next = &n2;
    	n2.next = &n3;
    	n3.next = &n4;
    	n4.next = NULL;
    	while (p != NULL)
    	{
    		printf ("%d\n", p -> info);
    		p = p -> next;
    	}
    }// main.c
    
  • psychic wrote:
    #include<conio.h>
    #include<stdio.h>
    int main()
    {
    	typedef struct node
    	{
    		int info;
    		struct node *next;
    	} node;
    	node n1,n2,n3,n4,*p;
    	p=&n1;
    	n1.info=10;
    	n2.info=20;
    	n3.info=30;
    	n4.info=40;
    	n1.next=&n2;
    	n2.next=&n3;
    	n3.next=&n4;
    	n4.next=NULL;
    	while(p!=NULL)
    	{
    		printf("%d\n",p->info);
    		p=p->next;
    	}
    }
    

    sory for the wrong code this works i have checked it .... i use cfree 5
    Good. Now do you see why it works?
  • Padsall came back after long time here and happy after reading this thread because i am learning now new things again thanks for sharing your opinions.
  • peaceofpi wrote:
    Good. Now do you see why it works?

    yes thank you all....
Sign In or Register to comment.