C create linked list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sedaw
    New Member
    • Jan 2009
    • 36

    C create linked list

    i need to create a linked lists in size =N
    each struct pointing to the next sruct the user will enter and the first struct pointed by constant pointer head.

    TNX .............

    Code:
    #include <stdio.h>
    #define size=5
    void main()
    
    {
    	typedef struct
    	{
    		int x,y;
    		struct Strct *next;
    	}Coordinate;
    
    	for(i=0; i<size; i++)
    	{
    
    		Coordinate (?????);
    		printf("Enter name & value: <name,value>\n");
    		scanf("%d,%d",(????));
    	}
    
    
    
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    And what is your question?

    kind regards,

    Jos

    Comment

    • sedaw
      New Member
      • Jan 2009
      • 36

      #3
      how to do it , what i should add to the code ?

      tnx....

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Each node in a linked list consists of two parts: there are the links used to connect the nodes together; and there is the body or payload of the node. What goes in the body of your list?

        Please tell me a little of what you've already learned about linked lists?

        Comment

        • manjuks
          New Member
          • Dec 2007
          • 72

          #5
          If you go through books you will get functions to add some data to list. Just go through any book, try to understand, post again if you still come across any doubts.

          Thanks,
          Manjunath

          Comment

          • loonman
            New Member
            • Sep 2009
            • 7

            #6
            do you want help on how to learn how to create a linked list or are you trying to get it built for you?

            Comment

            • sedaw
              New Member
              • Jan 2009
              • 36

              #7
              Originally posted by loonman
              do you want help on how to learn how to create a linked list or are you trying to get it built for you?
              yes , i want to learn all bout linked lists , i do read books .
              _______________ _______________ _______________ _______________ ______

              ok.... , what about that ?

              Code:
              #include <stdio.h>
              
              	typedef struct 
              	{
              		int x,y;
              		char str[20];
              		struct Node* next;
              	}Node ;
              
              // i will add option to user initialize the field latter (thats not what important)
              void main()
              {
              	Node *head=NULL, *temp ;
              	int i, size;
              	printf("Enter the size of linked list\n");
              		scanf("%d",&size);
              
              		for(i=0; i<size; i++)
              		{
              			temp=(Node*)malloc(sizeof(Node));
              			temp->next=head;
              			head=temp;
              		}
              
              }
              the warning mesages:


              line 21 : warning C4133: '=' : incompatible types - from 'Node *' to 'Node *
              line 20 : warning C4013: 'malloc' undefined; assuming extern returning int


              tnx..

              Comment

              • manjuks
                New Member
                • Dec 2007
                • 72

                #8
                Hi,

                For first warning -- you have to define structure like below,

                struct node
                {
                int x,y;
                char str[20];
                struct node *next;
                };

                typedef struct node Node;

                For second warning -- malloc is defined in stdlib.h header file, so you have to include that header file.

                Thanks,
                Manjunath

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  Originally posted by sedaw
                  Code:
                  	typedef struct 
                  	{
                  		int x,y;
                  		char str[20];
                  		struct Node* next;
                  	}Node ;
                  the warning mesages:
                  line 21 : warning C4133: '=' : incompatible types - from 'Node *' to 'Node *
                  Originally posted by manjuks
                  For first warning -- you have to define structure like below,
                  Code:
                  struct node
                  {
                          int x,y;
                          char str[20];
                          struct node *next;
                  };
                  typedef struct node Node;
                  In your original code the first place the type name "Node" appears is in the definition of the "next" field. The compiler does not yet recognize that name as a type. Now look at Manjunath's alternative -- the compiler is aware there is a struct named "node" by the time it gets to the definition of the "next" field. That's why Manjunath's code makes the compiler warning go away.

                  Comment

                  • donbock
                    Recognized Expert Top Contributor
                    • Mar 2008
                    • 2427

                    #10
                    Divide and conquer.
                    You need a function that allocates a node and initializes its payload. Then you need another function that inserts a node into a linked list.

                    Does the assignment say whether you're to create a singly- or doubly-linked list?

                    Comment

                    • sedaw
                      New Member
                      • Jan 2009
                      • 36

                      #11
                      thank you all !


                      Originally posted by donbock
                      Divide and conquer.
                      You need a function that allocates a node and initializes its payload. Then you need another function that inserts a node into a linked list.

                      Does the assignment say whether you're to create a singly- or doubly-linked list?
                      what`s the difference of singly- or doubly-linked ?

                      that`s not assignment i just tryin to learn linked lists .
                      TNX .

                      Comment

                      • donbock
                        Recognized Expert Top Contributor
                        • Mar 2008
                        • 2427

                        #12
                        Originally posted by sedaw
                        what`s the difference of singly- or doubly-linked ?
                        that`s not assignment i just tryin to learn linked lists .
                        TNX .
                        Linked lists are described in any number of software engineering books.
                        The internet is your friend: there's Wikipedia; a link provided in an earlier forum reply; and there's Google.

                        This forum is pretty good at answering specific questions. An open-ended question like "tell me about linked lists" is best answered by an essay or book chapter, not a forum reply that I throw together in a couple of minutes.

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by sedaw
                          what`s the difference of singly- or doubly-linked ?
                          A singly linked list contains one pointer per node that points to another node; e.g.

                          A ---> B ---> C ---> D ---> E

                          this list contains five elements where node A points to node B and at the end node D points to node E which is the last node in the list (A is the first node). The pointer of node E doesn't point to another node and is null.

                          A doubly linked list has two pointers per node; one pointer points to a next node (same as a singly linked list) and the other pointer points to the previous nodes in the list: e.g.

                          A <==> B <==> C <==> D <==> E

                          The last node (E) doesn't pont to a next node and the first node (A) doesn't point to a previous node. There are variations on the scenario.

                          kind regards,

                          Jos

                          Comment

                          • sedaw
                            New Member
                            • Jan 2009
                            • 36

                            #14
                            tnx...

                            one more litlle thing.

                            what`s wrong in line 24:

                            scanf("%d,%d",h ead->x,head->y);

                            theres no debugging prob .

                            but the prog collapse in line 24 .


                            Code:
                            #include <stdio.h>
                            #include <stdlib.h>
                            
                            	struct node
                            	{
                            		int x,y;
                            		char str[20];
                            		struct Node* next;
                            	} ;
                            	typedef struct node Node;
                            
                            void main()
                            {
                            	Node *head=NULL, *temp, *currItem;
                            	int i,size;
                            	printf("Enter the size of linked list\n");
                            		scanf("%d",&size);
                            		for(i=0; i<size; i++)
                            		{
                            			temp=(Node*)malloc(sizeof(Node));
                            			temp->next=head;
                            			head=temp;
                            			printf("Enter coordinate, <x,y>\n");
                            			scanf("%d,%d",head->x,head->y);
                            		}
                            	    currItem=head;
                            		for(i=0; i<size; i++)
                            		{
                            			printf("value of cuurent item on linked list: %d\n",currItem->x);
                            			currItem=currItem->next;
                            		}
                            
                            }

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by sedaw
                              one more litlle thing.
                              what`s wrong in line 24:

                              scanf("%d,%d",h ead->x,head->y);

                              theres no debugging prob .
                              The scanf function needs to know where it has to store its parsed result; you have to pass it the address of something to tell that to the scanf function. You are passsing it two ints instead which are not addresses. Do this instead:

                              Code:
                              scanf("%d,%d",&(head->x),&(head->y));
                              Here you are passing the addresses of those two int slots so scanf knows where to put its results.

                              kind regards,

                              Jos

                              ps. don't forget to type a comma between those two ints; scanf wants to see it according to your format string "%d,%d"

                              Comment

                              Working...