Answer to Question #153557 in C for joy

Question #153557

store your name, ID, SSC Grade, HSC Grade using linked list and display it.


1
Expert's answer
2021-01-05T08:09:01-0500
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// The structure to hold the linked list node
typedef struct Node Node;

typedef struct Node {
  char text[30];
  Node* next;
} Node;


int main()
{
  // Allocating memory
  Node* name = (Node *) malloc(sizeof(Node));
  name->next = (Node *) malloc(sizeof(Node));
  name->next->next = (Node *) malloc(sizeof(Node));
  name->next->next->next = (Node *) malloc(sizeof(Node));
  // Store my name, ID, SSC Grade, HSC Grade using the linked list
  strcpy(name->text, "name");
  strcpy(name->next->text, "id");
  strcpy(name->next->next->text, "SSC grade");
  strcpy(name->next->next->next->text, "HSC grade");
  // Displaying my name, ID, SSC Grade, HSC Grade using the linked list
  printf("My name: %s\n", name->text);
  printf("ID: %s\n", name->next->text);
  printf("SSC grade: %s\n", name->next->next->text);
  printf("HSC grade: %s\n", name->next->next->next->text);
   
  // Freeing the allocated memory
  free(name->next->next->next);
  free(name->next->next);
  free(name->next);
  free(name);
   
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS