Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. need help with linked structs at C language

need help with linked structs at C language

Scheduled Pinned Locked Moved C / C++ / MFC
comhelp
13 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A a random user

    i recently studied about this subject and i tried to code a program that does some stuff connected to it but i ran into troubles while tryign to add more structs with numbers i cutted mose of my program (to your convinient) please help me to udnerstand my mistakes http://pastebin.com/nyj3wtXT[^]

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    Please add some proper detail to your question and include your code here rather than offsite.

    A 1 Reply Last reply
    0
    • L Lost User

      Please add some proper detail to your question and include your code here rather than offsite.

      A Offline
      A Offline
      a random user
      wrote on last edited by
      #3

      well i first created the code to work on the main but somehow somethign went wrong when i tried putting the codes in functions < every time i go to the function insertAtEnd but at the second loop of the while it just says error #include #include #include struct numberNode { int number; struct numberNode* Next; }; typedef struct numberNode numStruct; numStruct* createSong(int numbers) { numStruct* newSong = (numStruct*)malloc(sizeof(numStruct)); if (newSong) { newSong->number = numbers; newSong->Next = NULL; } return newSong; } void insertAtEnd(numStruct** firstNode, numStruct* newNode) { numStruct* currNode = *firstNode; // if the linked list is empty // should put the new node as the first if (!currNode) { *firstNode = newNode; newNode->Next = NULL; } else { while (currNode->Next) // problem at the second loop { currNode = currNode->Next; } currNode->Next = newNode; newNode->Next = NULL; } printf("\n\n"); } numStruct* AddSongs(numStruct* anchorNode, numStruct* newNode) { int count = 0; int numbers; printf("\nPlease enter numbers and at the end enter -999\n"); do { count++; _flushall(); numbers = 0; printf("\n\n%d.\n\nnumber: ", count); scanf("%d",&numbers); if (numbers != -999) { newNode = createSong(numbers); insertAtEnd(&anchorNode, newNode); } } while (numbers != -999); printf("\n\n"); } int main(void) { numStruct* anchorNode = NULL; numStruct* newNode; AddSongs(&anchorNode, &newNode); system("PAUSE"); }

      F 1 Reply Last reply
      0
      • A a random user

        well i first created the code to work on the main but somehow somethign went wrong when i tried putting the codes in functions < every time i go to the function insertAtEnd but at the second loop of the while it just says error #include #include #include struct numberNode { int number; struct numberNode* Next; }; typedef struct numberNode numStruct; numStruct* createSong(int numbers) { numStruct* newSong = (numStruct*)malloc(sizeof(numStruct)); if (newSong) { newSong->number = numbers; newSong->Next = NULL; } return newSong; } void insertAtEnd(numStruct** firstNode, numStruct* newNode) { numStruct* currNode = *firstNode; // if the linked list is empty // should put the new node as the first if (!currNode) { *firstNode = newNode; newNode->Next = NULL; } else { while (currNode->Next) // problem at the second loop { currNode = currNode->Next; } currNode->Next = newNode; newNode->Next = NULL; } printf("\n\n"); } numStruct* AddSongs(numStruct* anchorNode, numStruct* newNode) { int count = 0; int numbers; printf("\nPlease enter numbers and at the end enter -999\n"); do { count++; _flushall(); numbers = 0; printf("\n\n%d.\n\nnumber: ", count); scanf("%d",&numbers); if (numbers != -999) { newNode = createSong(numbers); insertAtEnd(&anchorNode, newNode); } } while (numbers != -999); printf("\n\n"); } int main(void) { numStruct* anchorNode = NULL; numStruct* newNode; AddSongs(&anchorNode, &newNode); system("PAUSE"); }

        F Offline
        F Offline
        Frankie C
        wrote on last edited by
        #4

        Your code is almost correct, just a couple of errors in the main function. The variable newNode have to be initialized to NULL to avoid errors and compiler complainings. anchorNode and NewNode are already pointer to structures of type numStruct, so you have to pass them directly to AddSongs(), but you're passing their address (even if this sounds strange to me because you should have got a compiler error). The correct code is this:

        int main(void)
        {
        numStruct* anchorNode = NULL;
        numStruct* newNode = NULL;
        AddSongs(anchorNode, newNode);

        system("PAUSE");
        

        }

        A 1 Reply Last reply
        0
        • F Frankie C

          Your code is almost correct, just a couple of errors in the main function. The variable newNode have to be initialized to NULL to avoid errors and compiler complainings. anchorNode and NewNode are already pointer to structures of type numStruct, so you have to pass them directly to AddSongs(), but you're passing their address (even if this sounds strange to me because you should have got a compiler error). The correct code is this:

          int main(void)
          {
          numStruct* anchorNode = NULL;
          numStruct* newNode = NULL;
          AddSongs(anchorNode, newNode);

          system("PAUSE");
          

          }

          A Offline
          A Offline
          a random user
          wrote on last edited by
          #5

          ok then if thats the case why does this next function doesnt work after making the linked list void printList(numStruct* firstNode) { numStruct* currSong = firstNode; printf("\n\n-------------------------------------\n"); while (currSong) { printf("number= %d \n", currSong->number); currSong = currSong->Next; } printf("-------------------------------------\n\n"); printf("\n\n"); } which in main it will look like this int main(void) { numStruct* anchorNode = NULL; numStruct* newNode = NULL; AddSongs(anchorNode, newNode); printList(anchorNode); system("PAUSE"); }

          F 1 Reply Last reply
          0
          • A a random user

            ok then if thats the case why does this next function doesnt work after making the linked list void printList(numStruct* firstNode) { numStruct* currSong = firstNode; printf("\n\n-------------------------------------\n"); while (currSong) { printf("number= %d \n", currSong->number); currSong = currSong->Next; } printf("-------------------------------------\n\n"); printf("\n\n"); } which in main it will look like this int main(void) { numStruct* anchorNode = NULL; numStruct* newNode = NULL; AddSongs(anchorNode, newNode); printList(anchorNode); system("PAUSE"); }

            F Offline
            F Offline
            Frankie C
            wrote on last edited by
            #6

            Because just the first variable anchorNode must be passed as pointer to struct pointer to have it assigned. Also AddSongs must pass it directly to insertAtEnd(). I report the whole program here:

            #include #include #include struct numberNode {
            int number;
            struct numberNode *Next;
            };

            typedef struct numberNode numStruct;

            numStruct *createSong(int numbers)
            {
            numStruct *newSong = (numStruct *)malloc(sizeof(numStruct));
            if (newSong)
            {
            newSong->number = numbers;
            newSong->Next = NULL;
            }
            return newSong;
            }

            void insertAtEnd(numStruct **firstNode, numStruct *newNode)
            {
            numStruct *currNode = *firstNode;

            // if the linked list is empty
            // should put the new node as the first
            if (!currNode)
            {
            	\*firstNode = newNode;
            	newNode->Next = NULL;
            }
            else
            {
            	while (currNode->Next)	// problem at the second loop
            	{
            		currNode = currNode->Next;
            	}
            
            	currNode->Next = newNode;
            	newNode->Next = NULL;
            }
            printf("\\n\\n");
            

            }

            numStruct *AddSongs(numStruct **anchorNode, numStruct *newNode)
            {
            int count = 0;
            int numbers;

            printf("\\nPlease enter numbers and at the end enter -999\\n");
            
            do
            {
            	count++;
            	//flushall();
            	fflush(stdin);
            	numbers = 0;
            
            	printf("\\n\\n%d.\\n\\nnumber: ", count);
            	scanf("%d", &numbers);
            
            	if (numbers != -999)
            	{
            		newNode = createSong(numbers);
            		insertAtEnd(anchorNode, newNode);
            	}
            }
            while (numbers != -999);
            printf("\\n\\n");
            return NULL;
            

            }

            void printList(numStruct *firstNode)
            {
            numStruct *currSong = firstNode;
            printf("\n\n-------------------------------------\n");
            while (currSong)
            {
            printf("number= %d \n", currSong->number);
            currSong = currSong->Next;
            }
            printf("-------------------------------------\n\n");
            printf("\n\n");
            }

            int main(void)
            {
            numStruct *anchorNode = NULL;
            numStruct *newNode = NULL;
            AddSongs(&anchorNode, newNode);
            printList(anchorNode);

            system("PAUSE");
            

            }

            Check it against your code. We pass a pointer to a variable when we want access the variable from the called function, in your case to assign to anchorNode the beginning of the memory list.

            A 2 Replies Last reply
            0
            • F Frankie C

              Because just the first variable anchorNode must be passed as pointer to struct pointer to have it assigned. Also AddSongs must pass it directly to insertAtEnd(). I report the whole program here:

              #include #include #include struct numberNode {
              int number;
              struct numberNode *Next;
              };

              typedef struct numberNode numStruct;

              numStruct *createSong(int numbers)
              {
              numStruct *newSong = (numStruct *)malloc(sizeof(numStruct));
              if (newSong)
              {
              newSong->number = numbers;
              newSong->Next = NULL;
              }
              return newSong;
              }

              void insertAtEnd(numStruct **firstNode, numStruct *newNode)
              {
              numStruct *currNode = *firstNode;

              // if the linked list is empty
              // should put the new node as the first
              if (!currNode)
              {
              	\*firstNode = newNode;
              	newNode->Next = NULL;
              }
              else
              {
              	while (currNode->Next)	// problem at the second loop
              	{
              		currNode = currNode->Next;
              	}
              
              	currNode->Next = newNode;
              	newNode->Next = NULL;
              }
              printf("\\n\\n");
              

              }

              numStruct *AddSongs(numStruct **anchorNode, numStruct *newNode)
              {
              int count = 0;
              int numbers;

              printf("\\nPlease enter numbers and at the end enter -999\\n");
              
              do
              {
              	count++;
              	//flushall();
              	fflush(stdin);
              	numbers = 0;
              
              	printf("\\n\\n%d.\\n\\nnumber: ", count);
              	scanf("%d", &numbers);
              
              	if (numbers != -999)
              	{
              		newNode = createSong(numbers);
              		insertAtEnd(anchorNode, newNode);
              	}
              }
              while (numbers != -999);
              printf("\\n\\n");
              return NULL;
              

              }

              void printList(numStruct *firstNode)
              {
              numStruct *currSong = firstNode;
              printf("\n\n-------------------------------------\n");
              while (currSong)
              {
              printf("number= %d \n", currSong->number);
              currSong = currSong->Next;
              }
              printf("-------------------------------------\n\n");
              printf("\n\n");
              }

              int main(void)
              {
              numStruct *anchorNode = NULL;
              numStruct *newNode = NULL;
              AddSongs(&anchorNode, newNode);
              printList(anchorNode);

              system("PAUSE");
              

              }

              Check it against your code. We pass a pointer to a variable when we want access the variable from the called function, in your case to assign to anchorNode the beginning of the memory list.

              A Offline
              A Offline
              a random user
              wrote on last edited by
              #7

              that worked perfectly thank you very much

              1 Reply Last reply
              0
              • F Frankie C

                Because just the first variable anchorNode must be passed as pointer to struct pointer to have it assigned. Also AddSongs must pass it directly to insertAtEnd(). I report the whole program here:

                #include #include #include struct numberNode {
                int number;
                struct numberNode *Next;
                };

                typedef struct numberNode numStruct;

                numStruct *createSong(int numbers)
                {
                numStruct *newSong = (numStruct *)malloc(sizeof(numStruct));
                if (newSong)
                {
                newSong->number = numbers;
                newSong->Next = NULL;
                }
                return newSong;
                }

                void insertAtEnd(numStruct **firstNode, numStruct *newNode)
                {
                numStruct *currNode = *firstNode;

                // if the linked list is empty
                // should put the new node as the first
                if (!currNode)
                {
                	\*firstNode = newNode;
                	newNode->Next = NULL;
                }
                else
                {
                	while (currNode->Next)	// problem at the second loop
                	{
                		currNode = currNode->Next;
                	}
                
                	currNode->Next = newNode;
                	newNode->Next = NULL;
                }
                printf("\\n\\n");
                

                }

                numStruct *AddSongs(numStruct **anchorNode, numStruct *newNode)
                {
                int count = 0;
                int numbers;

                printf("\\nPlease enter numbers and at the end enter -999\\n");
                
                do
                {
                	count++;
                	//flushall();
                	fflush(stdin);
                	numbers = 0;
                
                	printf("\\n\\n%d.\\n\\nnumber: ", count);
                	scanf("%d", &numbers);
                
                	if (numbers != -999)
                	{
                		newNode = createSong(numbers);
                		insertAtEnd(anchorNode, newNode);
                	}
                }
                while (numbers != -999);
                printf("\\n\\n");
                return NULL;
                

                }

                void printList(numStruct *firstNode)
                {
                numStruct *currSong = firstNode;
                printf("\n\n-------------------------------------\n");
                while (currSong)
                {
                printf("number= %d \n", currSong->number);
                currSong = currSong->Next;
                }
                printf("-------------------------------------\n\n");
                printf("\n\n");
                }

                int main(void)
                {
                numStruct *anchorNode = NULL;
                numStruct *newNode = NULL;
                AddSongs(&anchorNode, newNode);
                printList(anchorNode);

                system("PAUSE");
                

                }

                Check it against your code. We pass a pointer to a variable when we want access the variable from the called function, in your case to assign to anchorNode the beginning of the memory list.

                A Offline
                A Offline
                a random user
                wrote on last edited by
                #8

                there is one more thing that bothers me 1 more problem im having in my full program is to "delete" a part of the linked list and reconnect it to the others by an input i tried to do this next thing: void deleteNode(numStruct** firstNode, numStruct* nodeToDelete) { numStruct* currNode = *firstNode; numStruct* temp; // if the list is not empty if (*firstNode) { // the first node should be deleted if (currNode == nodeToDelete) { *firstNode = (*firstNode)->Next; free(currNode); } else { while (nodeToDelete != currNode->Next && currNode->Next) { currNode = currNode->Next; } if (nodeToDelete == currNode->Next && nodeToDelete) { temp = currNode->Next; currNode->Next = temp->Next; free(temp); } } } inside the main: numStruct* t = anchorNode; int arr[50] = { 261363 }, spot = 0; printf("Please enter all the numbers you want to delete and end it with - 999 \n"); while ((arr[spot] != -999) && (spot != 50)) { scanf("%d", &arr[spot]); if (arr[spot] != -999) { spot++; } } if (arr[spot] == -999) { arr[spot] = 261363; spot--; } for (int i = 0; i < spot; i++) { int flag = 0; while ((t->number != arr[i]) && (t->number != NULL)) { t = t->Next; if (t->number == arr[i]) { flag = 1; } } if (flag == 1) { deleteNode(anchorNode, t); t = anchorNode; } } the problem is that it doesnt really delete it when i go beck to the menu and do the print function again it appears the same

                F D 2 Replies Last reply
                0
                • A a random user

                  there is one more thing that bothers me 1 more problem im having in my full program is to "delete" a part of the linked list and reconnect it to the others by an input i tried to do this next thing: void deleteNode(numStruct** firstNode, numStruct* nodeToDelete) { numStruct* currNode = *firstNode; numStruct* temp; // if the list is not empty if (*firstNode) { // the first node should be deleted if (currNode == nodeToDelete) { *firstNode = (*firstNode)->Next; free(currNode); } else { while (nodeToDelete != currNode->Next && currNode->Next) { currNode = currNode->Next; } if (nodeToDelete == currNode->Next && nodeToDelete) { temp = currNode->Next; currNode->Next = temp->Next; free(temp); } } } inside the main: numStruct* t = anchorNode; int arr[50] = { 261363 }, spot = 0; printf("Please enter all the numbers you want to delete and end it with - 999 \n"); while ((arr[spot] != -999) && (spot != 50)) { scanf("%d", &arr[spot]); if (arr[spot] != -999) { spot++; } } if (arr[spot] == -999) { arr[spot] = 261363; spot--; } for (int i = 0; i < spot; i++) { int flag = 0; while ((t->number != arr[i]) && (t->number != NULL)) { t = t->Next; if (t->number == arr[i]) { flag = 1; } } if (flag == 1) { deleteNode(anchorNode, t); t = anchorNode; } } the problem is that it doesnt really delete it when i go beck to the menu and do the print function again it appears the same

                  F Offline
                  F Offline
                  Frankie C
                  wrote on last edited by
                  #9

                  There are a lot of errors and redundant code. See the following solution, study it and go on on yourself now.

                  void deleteNode(numStruct **firstNode, numStruct *nodeToDelete)
                  {
                  numStruct *temp = NULL;

                  if (!\*firstNode)
                  	return;	//Empty list
                  
                  // Special case the node we must delete is the first one
                  if (\*firstNode == nodeToDelete)
                  {
                  	temp = \*firstNode;
                  	\*firstNode = (\*firstNode)->Next;
                  	free(temp);
                  	return;
                  }
                  
                  for (numStruct \* currNode = \*firstNode; currNode->Next; currNode = currNode->Next)
                  {
                  	if (nodeToDelete == currNode->Next)
                  	{
                  		temp = currNode->Next;
                  		currNode->Next = (currNode->Next)->Next;
                  		free(temp);
                  		return;
                  	}
                  }
                  

                  }

                  void RemoveNodes(numStruct **anchorNode)
                  {
                  numStruct *t = *anchorNode;
                  int arr[50] = {0};
                  int spot = 0;

                  printf("Please enter all the numbers you want to delete and end it with - 999 \\n");
                  
                  for (spot=0; spot < 50; spot++)
                  {
                  	fflush (stdin);
                  	scanf("%d", &arr\[spot\]);
                  	if (arr\[spot\] == -999)
                  		break;
                  }
                  
                  if (spot >= 50)
                  	spot--;
                  
                  for (int i = 0; i < spot; i++, t = \*anchorNode)
                  {
                  	while (t != NULL)
                  	{
                  		if (t->number == arr\[i\])
                  		{
                  			deleteNode(anchorNode, t);
                  			break;
                  		}
                  		t = t->Next;
                  	}
                  }
                  

                  }

                  int main(void)
                  {
                  numStruct *anchorNode = NULL;
                  numStruct *newNode = NULL;
                  AddSongs(&anchorNode, newNode);
                  printList(anchorNode);

                  RemoveNodes(&anchorNode);
                  printList(anchorNode);
                  
                  system("PAUSE");
                  

                  }

                  A 1 Reply Last reply
                  0
                  • F Frankie C

                    There are a lot of errors and redundant code. See the following solution, study it and go on on yourself now.

                    void deleteNode(numStruct **firstNode, numStruct *nodeToDelete)
                    {
                    numStruct *temp = NULL;

                    if (!\*firstNode)
                    	return;	//Empty list
                    
                    // Special case the node we must delete is the first one
                    if (\*firstNode == nodeToDelete)
                    {
                    	temp = \*firstNode;
                    	\*firstNode = (\*firstNode)->Next;
                    	free(temp);
                    	return;
                    }
                    
                    for (numStruct \* currNode = \*firstNode; currNode->Next; currNode = currNode->Next)
                    {
                    	if (nodeToDelete == currNode->Next)
                    	{
                    		temp = currNode->Next;
                    		currNode->Next = (currNode->Next)->Next;
                    		free(temp);
                    		return;
                    	}
                    }
                    

                    }

                    void RemoveNodes(numStruct **anchorNode)
                    {
                    numStruct *t = *anchorNode;
                    int arr[50] = {0};
                    int spot = 0;

                    printf("Please enter all the numbers you want to delete and end it with - 999 \\n");
                    
                    for (spot=0; spot < 50; spot++)
                    {
                    	fflush (stdin);
                    	scanf("%d", &arr\[spot\]);
                    	if (arr\[spot\] == -999)
                    		break;
                    }
                    
                    if (spot >= 50)
                    	spot--;
                    
                    for (int i = 0; i < spot; i++, t = \*anchorNode)
                    {
                    	while (t != NULL)
                    	{
                    		if (t->number == arr\[i\])
                    		{
                    			deleteNode(anchorNode, t);
                    			break;
                    		}
                    		t = t->Next;
                    	}
                    }
                    

                    }

                    int main(void)
                    {
                    numStruct *anchorNode = NULL;
                    numStruct *newNode = NULL;
                    AddSongs(&anchorNode, newNode);
                    printList(anchorNode);

                    RemoveNodes(&anchorNode);
                    printList(anchorNode);
                    
                    system("PAUSE");
                    

                    }

                    A Offline
                    A Offline
                    a random user
                    wrote on last edited by
                    #10

                    thats brilliant thank you very very much!!

                    F 1 Reply Last reply
                    0
                    • A a random user

                      thats brilliant thank you very very much!!

                      F Offline
                      F Offline
                      Frankie C
                      wrote on last edited by
                      #11

                      Thanks.

                      1 Reply Last reply
                      0
                      • A a random user

                        there is one more thing that bothers me 1 more problem im having in my full program is to "delete" a part of the linked list and reconnect it to the others by an input i tried to do this next thing: void deleteNode(numStruct** firstNode, numStruct* nodeToDelete) { numStruct* currNode = *firstNode; numStruct* temp; // if the list is not empty if (*firstNode) { // the first node should be deleted if (currNode == nodeToDelete) { *firstNode = (*firstNode)->Next; free(currNode); } else { while (nodeToDelete != currNode->Next && currNode->Next) { currNode = currNode->Next; } if (nodeToDelete == currNode->Next && nodeToDelete) { temp = currNode->Next; currNode->Next = temp->Next; free(temp); } } } inside the main: numStruct* t = anchorNode; int arr[50] = { 261363 }, spot = 0; printf("Please enter all the numbers you want to delete and end it with - 999 \n"); while ((arr[spot] != -999) && (spot != 50)) { scanf("%d", &arr[spot]); if (arr[spot] != -999) { spot++; } } if (arr[spot] == -999) { arr[spot] = 261363; spot--; } for (int i = 0; i < spot; i++) { int flag = 0; while ((t->number != arr[i]) && (t->number != NULL)) { t = t->Next; if (t->number == arr[i]) { flag = 1; } } if (flag == 1) { deleteNode(anchorNode, t); t = anchorNode; } } the problem is that it doesnt really delete it when i go beck to the menu and do the print function again it appears the same

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #12

                        a random user wrote:

                        the problem is that it doesnt really delete it

                        So have you stepped through each line of code using the debugger? Short of doing that, asking questions and pasting others' code snippets into your project are just a waste of time.

                        "One man's wage rise is another man's price increase." - Harold Wilson

                        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                        A 1 Reply Last reply
                        0
                        • D David Crow

                          a random user wrote:

                          the problem is that it doesnt really delete it

                          So have you stepped through each line of code using the debugger? Short of doing that, asking questions and pasting others' code snippets into your project are just a waste of time.

                          "One man's wage rise is another man's price increase." - Harold Wilson

                          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                          A Offline
                          A Offline
                          a random user
                          wrote on last edited by
                          #13

                          esecuse me kind sir but thats not really true after each and every answer i get i dont just copy it, i also try to udnerstand where i went wrong and try to udnerstand it , I am learning from my mistakes after all i only started with the programming in this year

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups