Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Languange : Can someone fix this? After I put 0 to number of student input. Then when using options 2, 3, and 4, the

C Languange : Can someone fix this?

 

After I put 0 to number of student input. Then when using options 2, 3, and 4, the DisplayList code does not detect the name input and programme input properly. Help me to fix this.

 

Below is the code that needs to fix :

#include  #include  #include  #define FALSE 0 #define TRUE 1 unsigned short dispFwd = TRUE; struct node { char name[50]; int id; char programme[50]; struct node *nextptr, *prevptr; } *HeadNode, *RearNode; // some parramters have been changed in some functions in order to fill the node of struvts in proper way with name id and programe void createNodeList(int n); void displayList(); void insertNode( char name[], int id, char programme[], int pos); void insertRear(char name[], int id, char programme[]); void createHead(char name[], int id, char programme[]); void insertHead(char name[], int id, char programme[]); void print_green() {printf("\033[1;32m");} void print_cyan() {printf("\033[1;36m");} void print_reset() {printf("\033[0m");} void print_boldred() {printf("\033[1;31m");} int main() { unsigned short i, opt, optDisp; int n, pos, id; printf("  STUDENT INFORMATION MANAGEMENT SYSTEM IN DOUBLE LINKED LIST : "); printf(" -------------------------------------------------------------- "); print_cyan(); printf(" Input the number of student :> "); scanf("%d", &n); createNodeList(n); print_green(); printf("  >: Student Record :  "); displayList(); while (1) { print_reset(); printf(" +++++++++++++++++++++++++++++++++++++++++++++ "); printf("WELCOME TO STUDENT INFORMATION MANAGEMENT SYSTEM "); printf("+++++++++++++++++++++++++++++++++++++++++++++ "); printf("  MAIN MENU "); printf("  [1] Display data order "); printf("  [2] Insert new node at any position "); printf("  [3] Insert new node at the begining of list "); printf("  [4] Insert new node at the end of list "); printf("  [5] Exit "); printf("  Select Operation to the list :> "); scanf("%d", &opt); switch (opt) { case 1: print_reset(); printf("  *Display Data Order*"); printf("  [1] Forward "); printf("  [2] Reverse "); printf("  Select display data order to the list :> "); scanf("%d", &optDisp); switch (optDisp) { case 1: dispFwd = TRUE; break; case 2: dispFwd = FALSE; break; default: print_boldred(); printf(">: Invalid Input! No action being taken!  "); } print_green(); printf("  >: Student Record :  "); displayList(); break; case 2: printf("  *Insert data request*   Add new data to the list (any position):  "); print_cyan(); printf("  Enter the student name : "); char name[50]; scanf("%s", name); printf(" Enter ID of %s (8 Last Digit Number): ", name); scanf("%d", &id); printf(" Programme of %s (e.g BME) : ", name); char programme[50]; scanf("%49s", programme); if (HeadNode == NULL) createHead(name, id, programme); else { printf("  Enter the insert position:> "); scanf("%d", &pos); insertNode(name, id, programme, pos); } printf("  >: Data after inserted in the list are :  "); displayList(); break; case 3: printf("  *Insert data request*   Add new data to the list (at begining) :  "); printf("  Enter the name of student : "); scanf("%s", name); printf(" Enter student ID of %s : ", name); scanf("%d", &id); printf(" Programme of %s (e.g BME) : ", name); scanf("%s", programme); if (HeadNode == NULL) createHead(name, id, programme); else insertHead(name, id, programme); printf("  >: Data after inserted in the list are :  "); displayList(); break; case 4: printf("  *Insert data request*   Add new data to the list (end position):  "); printf("  Enter the student name : "); scanf("%s", name); printf(" Enter ID of %s (8 Last Digit Number) : ", name); scanf("%d", &id); printf(" Programme of %s (e.g BME): ", name); scanf("%s", programme); if (HeadNode == NULL) createHead(name, id, programme); else insertRear(name, id, programme); printf("  >: Data after inserted in the list are :  "); displayList(); break; case 5: goto exitloop; default: print_boldred(); printf(" >: Invalid Input! No action being taken!  "); } } exitloop:; return 0; } void createNodeList(int n) { struct node *CurrentNode, *PrevNode; int i, id; char name[50]; char programme[50]; for (i = 1; i <= n; i++) { CurrentNode = (struct node *)malloc(sizeof(struct node)); if (CurrentNode == NULL) { print_boldred(); printf(" Memory can not be allocated."); break; } else { print_cyan(); printf("  Input student name for node %d : ", i); scanf("%49s", CurrentNode->name); printf(" Student ID of %s (8 Last Digit Number) : ", CurrentNode->name); scanf("%d", &id); printf(" Programme of %s (e.g BME) : ", CurrentNode->name); scanf("%49s", CurrentNode->programme); CurrentNode->id = id; if (HeadNode == NULL){ CurrentNode->prevptr = NULL; // links the address field to NULL CurrentNode->nextptr = NULL; // links the address field to NULL HeadNode = RearNode = CurrentNode; } else{ CurrentNode->nextptr = NULL; CurrentNode->prevptr = RearNode; RearNode->nextptr = CurrentNode; RearNode = CurrentNode;} } } } void createHead(char name[], int id, char programme[]) { struct node *stNode; stNode = (struct node *)malloc(sizeof(struct node)); if(stNode == NULL) printf(" Memory can not be allocated."); else{ stNode->id = id; stNode->prevptr = NULL; // links the address field to NULL stNode->nextptr = NULL; // links the address field to NULL HeadNode = RearNode = stNode; } } void insertHead(char name[], int id, char programme[]) { struct node *stNode; stNode = (struct node*)malloc(sizeof(struct node)); if(stNode == NULL) printf(" Memory can not be allocated."); else { stNode->id = id; stNode->prevptr = NULL; stNode->nextptr = HeadNode; //Links the address part HeadNode->prevptr = stNode; HeadNode = stNode; //Makes stnode as first node } } void insertRear(char name[], int id, char programme[]) { struct node *stNode; stNode = (struct node*)malloc(sizeof(struct node)); if(stNode == NULL) printf(" Memory can not be allocated."); else { stNode->id = id; stNode->prevptr = RearNode; stNode->nextptr = NULL; //Links the address part RearNode->nextptr = stNode; RearNode = stNode; //Makes stnode as first node } } void insertNode(char name[], int id, char programme[], int pos) { struct node *stNode, *PreNode; int i, tpos = 1; stNode = (struct node*)malloc(sizeof(struct node)); if(stNode == NULL) printf(" Memory can not be allocated."); else { stNode->id = id; //Links the data part //Case A: When the insert location is at the first Node (i.e., Headnode) if (pos==1){ stNode->prevptr = NULL; stNode->nextptr = HeadNode; //Links the address part HeadNode->prevptr = stNode; HeadNode = stNode; //Makes stnode as first node } //Case B: Insert a new node at the middle of Singly Linked List else{ PreNode = HeadNode; for(i=2; inextptr; if(PreNode == NULL) break; } if(PreNode != NULL){ stNode->nextptr = PreNode->nextptr; // PreNode->nextptr pointed the current Node at "pos" stNode->prevptr = PreNode; PreNode->nextptr = stNode; if (stNode->nextptr == NULL) RearNode = stNode; } else printf(" Insert is not possible to the given position. "); } } } void displayList() { struct node *stNode; if (HeadNode == NULL) { print_boldred(); printf(" The Student Record List Is Empty"); } else { print_green(); stNode = HeadNode; printf("\t%10s | %10s | %10s ", "Students", "ID numbers", "Programme"); while (stNode != NULL) { printf("\t%10s | %10d | %10s ", stNode->name, stNode->id, stNode->programme); stNode = stNode->nextptr; } print_reset(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Demystified

Authors: Andrew Oppel

1st Edition

0072253649, 9780072253641

Students also viewed these Databases questions

Question

Explain the market segmentation.

Answered: 1 week ago

Question

Mention the bases on which consumer market can be segmented.

Answered: 1 week ago