Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Below is an example of a struct that has a member that is a pointer to the same

ID: 3574838 • Letter: B

Question

Below is an example of a struct that has a member that is a pointer to the same type of struct as is currently being defined. It is an example of a linked list Each struct is a node that contains some data (in this case a letter), and a pointer to the next Node. The last node is special, as its pointer holds the value 0 (zero or NULL) instead of an address like the rest. One way to depict such a data structure is below: A rightarrow B rightarrow C There are three structures each points at the next (the last struct" s pointer holds a 0). I could make the above structure like so: struct Node a, b, c; c.letter = 'c'; c.next = 0; b.letter = 'b'; b.next = &c; a.letter = 'a'; a.next = &b; You need to write a function (named *get_node") that takes two arguments, a struct Node and an int n. You need to return the character held by the n'th node. In the example above the 0th Node is A, 1th Node is B and the 2th node is C. If the number is greater than the number of nodes in the list, return the character associated with the last node. get_node(a, 1)//Should return 'b' include struct Node {char letter; struct Node * next;}; char get_node(struct Node node, int n); int main(void) {char letters[100]; int number; scant("%s %d", letters, inumber); struct Node nodes(100); int i; for (i = 0; letters[i] ! = ''; ++i) {nodes[i].letter = letters[i]; nodes[i].next = inodes(i + 1);} nodes[i - 1].next = 0; printf("%c", get_node(nodes(0), number)); return 0;} char get_node(struct Node node, int n) {

Explanation / Answer

PROGRAM CODE:

/*
* main.cpp
*
* Created on: 11-Nov-2016
* Author: kasturi
*/


#include "stdlib.h"
#include "stdio.h"
#include "string.h"
struct Node{
   char letter;
   struct Node* next;
};

char get_node(struct Node node, int n);

int main()
{
char letters[100];
int number;
scanf("%s %d", letters, &number);
struct Node nodes[100];

int i;
for(i=0; letters[i] != ''; i++)
{
   nodes[i].letter = letters[i];
   nodes[i].next = &nodes[i+1];
}
nodes[i-1].next = 0;

printf("%c", get_node(nodes[0], number));
return 0;
}

char get_node(struct Node node, int n)
{
   struct Node *ptr = &node;
   int counter = 0;
   char letter;
   while(ptr->next != 0)
   {
       letter = node.letter;
       if(n == counter)
       {
           break;
       }
       else
       {
           memcpy(ptr, ptr->next, sizeof *ptr);
           counter++;
       }

   }
   return letter;
}

OUTPUT:

abc 1

b

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote