Question: This is c programing. Need help for part 3 prof gave me this #include #include // structs struct node2d { struct node1d *first; char *name;

This is c programing. Need help for part 3 prof gave me this

#include  #include  // structs struct node2d { struct node1d *first; char *name; struct node2d *down; }; struct node1d { struct node1d *next; char *name; }; // funs in this file struct node1d *mk1dNode(char *name, struct node1d *next); struct node2d *mk2dNode(struct node1d *first, char *name, struct node2d *down); struct node2d *mkExConfig (); // external funs void printConfig(struct node2d *p2d); void print2d(struct node2d *p2d); void print1d(struct node1d *p1d); // main int main (int argc, char *argv[]) { struct node2d *p2d; p2d = mkExConfig(); printConfig(p2d); return 0; } struct node2d *mkExConfig() { struct node1d *p1d; struct node2d *p2d; // create rows from bottom to top // create bottom row from right to left p1d = mk1dNode("c3pO", NULL); p1d = mk1dNode("cso", p1d); p2d = mk2dNode(p1d, "R2D2", NULL); // create middle row p2d = mk2dNode(NULL, "alice", p2d); // create top row from right to left p1d = mk1dNode("e342", NULL); p1d = mk1dNode("sally", p1d); p1d = mk1dNode("xy2", p1d); p2d = mk2dNode(p1d, "joe", p2d); return p2d; } struct node1d *mk1dNode(char *name, struct node1d *next) { struct node1d *p1d; p1d = (struct node1d *)malloc(sizeof(struct node1d)); (*p1d).next = next; // (*p1d). is long form p1d->next = next; // common abbreviation p1d->name = name; return p1d; } struct node2d *mk2dNode(struct node1d *first, char *name, struct node2d *down) { struct node2d *p2d; p2d = (struct node2d *)malloc(sizeof(struct node2d)); p2d->first = first; p2d->name = name; p2d->down = down; return p2d; }

Part 1

Write printConfig(struct node2d *p2d), which prints the nodes top to bottom, left to right. The output for the sample configuration should be.

 2d node name=joe 1d node name=xy2 1d node name=sally 1d node name=e342 2d node name=alice 2d node name=R2D2 1d node name=cso 1d node name=c3pO All finished. 

Naturally the output from printConfig() depends on the structure generated. The above is the output for the configuration produced by mkExampleConfig.c

Part 2

Enhance the print2d routine as follows

Instead of always printing 2d node name, have the first line printed say The first 2d node has name and have the remaining lines say The next 2d node has name. Make the same improvement for 1d nodes as well. Do this using the C conditional expression (?:).

Make print2d compute the number of 2d nodes (these are the nodes in the left column, which have 2 pointers) and the number of 1d nodes (the remaining nodes, which have 1 pointer). Print these two values at the end of the run.

If you do part 2 correctly, you need not do part1.

Part 3

Implement insertion of 1d nodes onto a given sub-list. So

 Ins alice wife 

would create a 1d node with name wife and would insert it on the sub-list headed by the 2d node with name alice. Specifically:

Read commands from the console.

Each command is exactly one line.

The first four characters must be Ins . If they are not, print an error message and terminate the run. If any error is detected, the main() procedure must return a positive integer value less than 256 (any such value is fine) to indicate failure. If no errors are found in the run, main() must return 0, as the supplied code does. If you detect the error in one of your non-main functions and it is inconvenient to get back to main() to return non-zero, just execute exit(k); with 0

The next N>0 characters must be digits or letters (i.e., isalnum() is true). This string forms the name of the 2d node to which you are to add a 1d node. (Use the same treatment for an error as above.)

The next character must be a blank (same error treatment).

The next M>0 characters must be digits or letters (same error treatment). This string forms the name of the 1d node you are to add.

The next character must be a new line (same error treatment).

Keep processing such lines until an EOF is found in the first column. When you get the EOF, you should print the resulting configuration using the same print routine used in part 2.

You may set reasonable limits on M and N providing you mention the limits in your readme.

I promise an EOF will not occur in any column other that column one, you need not check for this. When the EOF occurs, free() any memory you have previously malloc()'ed.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!