Question: C Languange : Can someone help to modify this? Modify the displayList(), so the phone number display data with dash after second position: Example: ------Welcome------
C Languange : Can someone help to modify this?
Modify the displayList(), so the phone number display data with dash after second position:
Example:
------Welcome------ [1] Create [2] Display [3] Delete [4] Quit
Enter your choice: 2 FULL NAME | PHONE NUMBER ----------------------------------------------------- ALEX | 98-87654321
Below is the code that needs to modify :
// Demonstration of example of implement Stack with Single Linked List #include#include #include struct node { char name[20] ;// Data of node as name int data; //Data of the node struct node *nextptr; //Address of the next node }*HeadNode; const int MAXSIZE = 8; int Stack[8], top = -1; // Remark Refer follows link for more details on "?: ternary operator; // https://www.geeksforgeeks.org/conditional-or-ternary-operator-in-c-c/ int isempty() { return (top == -1)? 1 : 0; } int isfull() { return (top == MAXSIZE)? 1 : 0; } int peek() { return HeadNode->data; } int push(char* name, int data); void displayList(); int pop(); // Remark Refer follows link for more details on adding Color to printf() Output; // https://www.theurbanpenguin.com/4184-2/ void print_green() { printf("\033[1;32m"); } void print_green1() { printf("\033[1;32m"); } void print_cyan() {printf("\033[1;36m");} void print_reset() {printf("\033[0m");} int main() { int opt, data; char name[20]; while(1){ printf(" ------Welcome------ "); printf(" [1] Create "); printf(" [2] Display "); printf(" [3] Delete "); printf(" [4] Quit "); printf(" Enter your choice: "); scanf("%d", &opt); switch (opt){ case 1: printf(" Enter first name: "); scanf("%s", name); printf("Enter number: "); scanf("%d", &data); push(name ,data); displayList(); break; case 2: displayList(); break; case 3: if (!isempty()) printf(">: Data \"%d\" pop out Stack: ", data = pop()); else printf(":> Stack is empty. "); displayList(); break; case 4: goto exitloop; default: printf (">: Error! No action being taken! "); } printf(" Do you want to quit? "); printf("1 for no / 0 for yes: "); scanf("%d", &opt); if(opt == 0 ) goto exitloop; } exitloop: ; return 0; } int push(char *name, int data) { struct node *stNode; if(!isfull()){ stNode = (struct node*)malloc(sizeof(struct node)); if(stNode == NULL) printf(" Memory can not be allocated."); else { top++; strcpy(stNode->name,name); stNode->data = data; //Links the data part stNode->nextptr = NULL; if(HeadNode != NULL) stNode->nextptr = HeadNode; //Links the address part HeadNode = stNode; } } else printf(">: Could not insert data, Stack is full. "); } int pop() { int data; struct node *delNode; if(!isempty()){ delNode = HeadNode; data = delNode->data; HeadNode = HeadNode->nextptr; free(delNode); top--; return data; } else printf(">: Could not retrieve data, Stack is empty. "); } void displayList(){ struct node *stNode; print_green(); printf("FULL NAME\t|\tPHONE NUMBER "); printf("--------------------------------------------- "); if(HeadNode == NULL) printf(" Stack is empty. "); else { stNode = HeadNode; while(stNode != NULL){ printf("%s\t\t|\t%d ", stNode->name,stNode->data); // prints the data of current node stNode = stNode->nextptr; // advances the position of current node } } print_reset(); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
