Structure within structure and pointer

MrSoftware

Joined Oct 29, 2013
2,273
Understand what I changed in your code below. Here are your exercises: (1) Draw out what the code below does on paper and understand it. (2) extend the code below to add additional nodes. (3) Move the code for adding a new node into a function such as AddNode() below, then use that to simplify your code.

C:
List* AddNode(char* name, int marks)
{
  //  your code here to make a new node and return the ref
}
I hacked your code up a bit, it's a little ugly but hopefully it will help you understand linked lists:
C:
#include<stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct record        
   {
   char     Name[20];
   int      Marks;
   struct record   *next;       // your declaration is wrong
   }List;

int main()
   {
   List   *head = NULL;      // Top of the List
   List   *current = NULL;   // The node we're working on right now

   head = (List*)malloc(sizeof(List));
   printf("Allocated %lu bytes at address 0x%p\n", sizeof(List), head);

   // DO NOT MODIFY HEAD!  It will always point to the top of your List
   current = head;

   current->Marks = 20;
   strcpy(current->Name,"Vajra");
   current->next = NULL;

   // Lets add one more node to the end of the list
   current->next = (List*)malloc(sizeof(List));
   printf("Allocated %lu bytes at address 0x%p\n", sizeof(List), current->next);
   current = current->next;

   strcpy(current->Name, "djsfantasi");
   current->Marks = 49;
   current->next = NULL;

   // Print the whole list, starting at the top
   current = head;

   while(current)
   {
      printf(" %d ",current->Marks);
      printf(" %s \n",current->Name);

      current = current->next;
   };

   printf("\n");
   // Memory for the nodes was allocated one chunk at a time, it must be cleaned up one chunk at a time
   while(head)
   {
       current = head;
       head = head->next;
       printf("Freeing memory at address 0x%p\n", current);
       free(current);
   }

   return(0);
   }


1572269460034.png
 
Last edited:

djsfantasi

Joined Apr 11, 2010
9,237
I was not fully ready for linked. my first post was related to structure and I saw linked list in the second post so I thought I should try once then wrote a small program for a list



I think you have posted code without checking on compiler I have fixed some error in your code

C:
//GCC compiler
#include<stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct record          
   {
   char     Name[20];
   int      Marks;
   struct record   *next;       // your declaration is wrong
   }List;
 
int main()
   {
   List   *head = NULL;

   head = (List*)malloc(sizeof(List));

   head->Marks = 20;
   strcpy(head->Name,"Vajra\n");
   head->next = NULL;

   while(head)   //  this condition should be head
      {
      printf(" %d ",head->Marks);
      printf(" %s ",head->Name);

      head = head->next;
      };

   free(head);               // ALWAYS clean up after yourself.  Release the allocation, and zero the pointer.
   head = NULL;

   return(0);
   }
20 Vajra

I do not understand how to extend this program to store marks of students

output of link listed should be following

42 BobTPH
45 BobaMosfet
49 djsfantasi
10 vajra
ok, you have a structure that defined a student record. You want to extend this program to be able to have many records.

Correct?

One last time... Learn how to use a linked list.

Linked lists and structures are not alternatives to each other. To implement multiple records in a structure, one uses linked lists. You define a structure with the fields of your record, and then add the pointers required for a linked list.

If you don’t understand the concept of a linked list, you won’t be able to store multiple records of a structure.
 
Top