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

Im having some trouble understanding a required part to modify a piece of code (

ID: 3677619 • Letter: I

Question

Im having some trouble understanding a required part to modify a piece of code (c Programming). I've completed some steps but im stuck on the following.

- Write a function void push(int val, int *stacktop, int *stack), that pushes the val onto the stack; Write a function int pop(int *stacktop, int *stack), that pops the top value from the stack. Both functions must report error conditions (stack full or nothing to pop).

- in the Insert() funtion:

If the Idstack is empty, use the global variable IDgenerator to initialize the UserID and then increment the IDgenerator. If Idstack is not empty, call int pop(int *, int *) to obtain a number from the stack to initialize the UserID.

Currently, the new node is inserted in the first place of linked list. Modify the function to insert the new node in place so that the linked list is sorted by name in ascending order. Notice that you do not enter the UserID, which must be generated by the system in a systematic way, i.e., by using a global variable IDgenerator or reusing a deleted UserID. If an entry is deleted from the linked list, the UserID will be pushed into the stack for later reuse.

any insight in the right direction will help, im having trouble piecing everything together.

Here is the project.

#include <stdio.h>
#include <stdlib.h> // malloc is defined in this library
#include <string.h> // string operations are defined in this library
#define stacksize 4096

struct contact { //defines a node that can hold a persons details
   char name[30];
   char phone[20];
   char address[50];
   int UserID; //Added into struct contact
   struct contact *next; //Pointer to contact structure
}*head = NULL; // head is a global pointer to first entry

char *file_name; // Use a string for file name
//global variables
int IDgenerator = 0;
int top = 0;

// forward declarations
void menu();
/*
   This function displays a menu to the user of possible inputs to engage in the program. When the function is declared in main()
   the menu will print possible entries to execute program commands. Also this funtion has a void return type.
*/
void branching(char c);
/*
   This function passes through a parameter requiring a char input (char c) to be passed through. Once the paramater is obtained it will then
   execute a switch statement and run a switch depending on the char input value. The char value is inputed in main() upon users input.
*/
struct contact* find_node(char *str, int *position);
/*
  
*/
void display_node(struct contact *node, int index);
/*

*/
void push(int val, int *stacktop, int *stack);
/*

*/
void pop(int *stacktop, int *stack);
/*

*/
int insert();
/*

*/
int deletion();
/*

*/
int modify();
/*

*/
int search_name();
/*

*/
void display_all();
/*

*/
void load_file();
/*

*/
void save_file();
/*

*/

void menu() {
   fflush(stdin); // added this line to clear buffer. Otherwise it takes new line character as next input
   printf(" MENU ");
   printf("---- ");
   printf("i: Insert a new entry. ");
   printf("d: Delete an entry. ");
   printf("m: Modify an entry. ");
   printf("s: Search by name for an entry. ");
   printf("p: Print all entries. ");
   printf("q: Quit the program. ");
   printf("Please enter your choice (i, d, m, s, p, or q) --> ");
}
void branching(char c) {
   switch (c) {
   case 'i': if (insert() != 0)
       printf("INSERTION OPERATION FAILED. ");
       // elseprintf("INSERTED NODE IN THE LIST SUCCESSFULLY. ");

           else printf("INSERTED NODE IN THE LIST SUCCESSFULLY. ");
           break;
   case 'd': if (deletion() != 0)
       printf("DELETION OPERATION FAILED. ");
           else
               printf("DELETED THE ABOVE NODE SUCCESSFULLY. ");
       break;
   case 'm': if (modify() != 0)
       printf("MODIFY OPERATION FAILED. ");
           else
               printf("MODIFIED THE ABOVE NODE SUCCESSFULLY. ");
       break;
   case 's': if (search_name() != 0)
       printf("SEARCH FAILED. ");
           else
               printf("SEARCH FOR THE NODE SUCCESSFUL. ");
       break;
   case 'p': display_all();
       break;
   case 'q': save_file();
       break;
   default: printf("ERROR - Invalid input. ");
       printf("Try again..... ");
       break;
   }
   return;
}
int insert() { //inserts a new entry at the begging
   struct contact *node; //pointer to contact structure
   char sname[30]; // Macro Variabe;
   int index = 1;
   printf(" Insertion module............... ");
   printf("Enter the name of the person to be inserted: ");
   scanf("%s", sname);
   node = find_node(sname, &index); // find duplicates
   if (node != NULL) {
       printf("ERROR - Duplicate entry not allowed. ");
       printf("A entry is found in the list at index %d. ", index);
       display_node(node, index);
       return -1;
   }
   else {
       node = (struct contact*) malloc(sizeof(struct contact)); /*allocates the right amount of mememory for a variable of
                                                               contact type and sets adress to pointer variable (*node)*/
       if (node == NULL) {
           printf("ERROR - Could not allocate memory ! ");
           return -1;
       }
       strcpy(node->name, sname);
       printf("Enter telephone number: ");
       scanf("%s", node->phone);
       printf("Enter address: ");
       scanf("%s", node->address);
       node->next = head;
       head = node; //link new node
       return 0;
   }
}
int deletion() {
   char sname[30];
   struct contact *temp, *prev;
   int index = 1;
   printf(" Deletion module............... ");
   printf("Please enter the name of the person to be deleted: ");
   scanf("%s", sname);
   temp = head;
   while (temp != NULL)
   if (stricmp(sname, temp->name) != 0) { // case insensitive strcmp
       prev = temp;
       temp = temp->next;
       index++;
   }
   else {
       printf("Person to be deleted is found at index %d.", index);
       display_node(temp, index);
       if (temp != head)
           prev->next = temp->next;
       else
           head = head->next;
       free(temp);
       return 0;
   }
   printf("The person with name '%s' does not exist. ", sname);
   return -1;
}
int modify() {
   struct contact *node;
   char sname[30];
   int index = 1;
   printf(" Modification module............... "); printf("Enter the name whose record is to be modified in the ");
   printf("database: ");
   scanf("%s", sname);
   node = find_node(sname, &index);
   if (node != NULL) {
       printf("Person to be modified is found at index %d.", index);
       display_node(node, index);
       printf(" Enter the new telephone number of this person: ");
       scanf("%s", node->phone);
       printf("Enter the new address of this person: ");
       scanf("%s", node->address);
       return 0;
   }
   else {
       printf("The person with name '%s' does not exist ", sname);
       printf("database. ");
       return -1;
   }
}
int search_name() {
   struct contact *node;
   char sname[30];
   int index = 1;
   printf(" Search_name module............... ");
   printf("Please enter the name to be searched in the database: ");
   scanf("%s", sname);
   node = find_node(sname, &index);
   if (node != NULL) {
       printf("Person searched is found at index %d.", index);
       display_node(node, index);
       return 0;
   }
   else {
       printf("The person '%s' does not exist. ", sname);
       return -1;
   }
}
void display_all() {
   struct contact *node;
   int counter = 0;
   printf(" Display module...............");
   node = head;
   while (node != NULL) {
       display_node(node, counter++);
       node = node->next;
   }
   printf(" No more records. ");
}
void load_file() {
   FILE *file_descriptor;
   struct contact *node, *temp;
   char str[30];
   file_descriptor = fopen(file_name, "r");
   if (file_descriptor != NULL) {
       while (fread(str, 30, 1, file_descriptor) == 1) {
           node = (struct contact*) malloc(sizeof(struct contact));
           strcpy(node->name, str);
           fread(node->phone, 20, 1, file_descriptor);
           fread(node->address, 50, 1, file_descriptor);
           if (head != NULL)
               temp->next = node;
           else
               head = node;
           node->next = NULL;
           temp = node;
       }
       fclose(file_descriptor);
   }
}
void save_file() {
   FILE *file_descriptor;
   struct contact *node;
   file_descriptor = fopen(file_name, "w");
   if (file_descriptor != NULL) {
       node = head;
       while (node != NULL) {
           fwrite(node->name, 30, 1, file_descriptor);
           fwrite(node->phone, 20, 1, file_descriptor);
           fwrite(node->address, 50, 1, file_descriptor);
           node = node->next;
       }
   }
   else {
       printf(" ERROR - Could not open file for saving data ! ");
       getchar();
       exit(-1);
   }
}struct contact* find_node(char *str, int *position) {//When the method is called, it will pass in a string (sname) and an Int (index).
   struct contact *temp = head;
   while (temp != NULL) {
       if (stricmp(str, temp->name) != 0) { // case insensitive strcmp. Compares str and temp->name, depending on return value, will excute if != 0.
           temp = temp->next;
           (*position)++;
       }
       else
           return temp;
   }
   return NULL;
}


void push(int val, int *stacktop, int *stack){
   struct contact *temp;
  
   //node = (struct contact*) malloc(sizeof(struct contact));
   int IdStack = malloc(stacksize);// this is the stack

   if (temp == NULL){
       printf("stack is full");
   }


}
void display_node( struct contact *node, int index) {
   printf(" RECORD %d: ", index);
   printf(" Name: %s ", node->name);
   printf(" Telephone: %s ", node->phone);
   printf(" Address: %s ", node->address);
}

int main(int argc, char *argv[]) {

   char ch;

   //I dont feel any need to this block. So I have commented it out. You can uncomment it according to your need.
   if (argc != 2) { // Two command line parameters required
   printf("Command Line Parameters Required ! ");
   printf("Try again...... ");
   getchar(); // enter any character to return
   return -1;
   }
   printf("SINGLY LINKED LIST ");
   printf("******************");
   file_name = argv[1]; // assign person.dbms to file_name
   load_file();
   do {
       menu();
       fflush(stdin); // Flush the standard input buffer. // Uncommented this line
       ch = tolower(getchar()); // read a char, convert to lower case
       branching(ch);
   } while (ch != 'q');
   return 0;
}

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define stacksize 4096

struct contact {

char name[30];
char phone[20];
char address[50];
int UserID;
struct contact *next;
}*head = NULL;

char *file_name;

int IDgenerator = 0;
int top = 0;

void menu();

void branching(char c);

struct contact* find_node(char *str, int *position);

void display_node(struct contact *node, int index);

void push(int val, int *stacktop, int *stack);

void pop(int *stacktop, int *stack);

int insert();

int deletion();

int modify();

int search_name();

void display_all();

void load_file();

void save_file();

void menu() {
int ch;
fflush(stdin);
printf(" MENU ");
printf("---- ");
printf("i: Insert a new entry. ");
printf("d: Delete an entry. ");
printf("m: Modify an entry. ");
printf("s: Search by name for an entry. ");
printf("p: Print all entries. ");
printf("q: Quit the program. ");
printf("Please enter your choice (i, d, m, s, p, or q) --> ");
scanf(%d,ch);
branching(ch);
}
void branching(char c) {
switch (c) {
case 'i': if (insert() != 0)
  
printf("INSERTION OPERATION FAILED. ");

else {
insert();
printf("INSERTED NODE IN THE LIST SUCCESSFULLY. ");
}
  
break;
case 'd': if (deletion() != 0)

printf("DELETION OPERATION FAILED. ");
  
else printf("DELETED THE ABOVE NODE SUCCESSFULLY. ");
break;
case 'm': if (modify() != 0)

printf("MODIFY OPERATION FAILED. ");

else printf("MODIFIED THE ABOVE NODE SUCCESSFULLY. ");
break;
case 's': if (search_name() != 0)
  
printf("SEARCH FAILED. ");
  
else printf("SEARCH FOR THE NODE SUCCESSFUL. ");
break;
case 'p': display_all();
break;
case 'q': save_file();
break;
default: printf("ERROR - Invalid input. ");
printf("Try again..... ");
break;
}
return;
}
int insert() {
struct contact *node;
char sname[30];
int index = 1;
printf(" Insertion module............... ");
printf("Enter the name of the person to be inserted: ");
scanf("%s", sname);
node = find_node(sname, &index);
if (node != NULL) {
printf("ERROR - Duplicate entry not allowed. ");
printf("A entry is found in the list at index %d. ", index);
display_node(node, index);
return -1;
  
}
else {
node = (struct contact*) malloc(sizeof(struct contact));
  
if (node == NULL) {
printf("ERROR - Could not allocate memory ! ");
return -1;
}
int push(node);
strcpy(node->name, sname);
printf("Enter telephone number: ");
scanf("%s", node->phone);
printf("Enter address: ");
scanf("%s", node->address);
node->next = head;
head = node; //link new node
return 0;
}
}
int deletion() {
char sname[30];
struct contact *temp, *prev;
int index = 1;
printf(" Deletion module............... ");
printf("Please enter the name of the person to be deleted: ");
scanf("%s", sname);
temp = head;
while (temp != NULL)
if (stricmp(sname, temp->name) != 0) {
prev = temp;
temp = temp->next;
index++;
}
else {
printf("Person to be deleted is found at index %d.", index);
display_node(temp, index);
if (temp != head)
prev->next = temp->next;
else
head = head->next;
free(temp);
return 0;
}
printf("The person with name '%s' does not exist. ", sname);
return -1;
}
int modify() {
struct contact *node;
char sname[30];
int index = 1;
printf(" Modification module............... ");
printf("Enter the name whose record is to be modified in the ");
printf("database: ");
scanf("%s", sname);
node = find_node(sname, &index);
if (node != NULL) {
printf("Person to be modified is found at index %d.", index);
display_node(node, index);
printf(" Enter the new telephone number of this person: ");
scanf("%s", node->phone);
printf("Enter the new address of this person: ");
scanf("%s", node->address);
return 0;
}
else {
printf("The person with name '%s' does not exist ", sname);
printf("database. ");
return -1;
}
}
int search_name() {
struct contact *node;
char sname[30];
int index = 1;
printf(" Search_name module............... ");
printf("Please enter the name to be searched in the database: ");
scanf("%s", sname);
node = find_node(sname, &index);
if (node != NULL) {
printf("Person searched is found at index %d.", index);
display_node(node, index);
return 0;
}
else {
printf("The person '%s' does not exist. ", sname);
return -1;
}
}
void display_all() {
struct contact *node;
int counter = 0;
printf(" Display module...............");
node = head;
while (node != NULL) {
display_node(node, counter++);
node = node->next;
}
printf(" No more records. ");
}
void load_file() {
FILE *file_descriptor;
struct contact *node, *temp;
char str[30];
file_descriptor = fopen(file_name, "r");
if (file_descriptor != NULL) {
while (fread(str, 30, 1, file_descriptor) == 1) {
node = (struct contact*) malloc(sizeof(struct contact));
strcpy(node->name, str);
fread(node->phone, 20, 1, file_descriptor);
fread(node->address, 50, 1, file_descriptor);
if (head != NULL)
temp->next = node;
else
head = node;
node->next = NULL;
temp = node;
}
fclose(file_descriptor);
}
}
void save_file() {
FILE *file_descriptor;
struct contact *node;
file_descriptor = fopen(file_name, "w");
if (file_descriptor != NULL) {
node = head;
while (node != NULL) {
fwrite(node->name, 30, 1, file_descriptor);
fwrite(node->phone, 20, 1, file_descriptor);
fwrite(node->address, 50, 1, file_descriptor);
node = node->next;
}
}
else {
printf(" ERROR - Could not open file for saving data ! ");
getchar();
exit(-1);
}
}struct contact* find_node(char *str, int *position) {
struct contact *temp = head;
while (temp != NULL) {
if (stricmp(str, temp->name) != 0) {
temp = temp->next;
(*position)++;
}
else
return temp;
}
return NULL;
}

void push(int *stack){
struct contact *temp;
  
temp = (struct contact*) malloc(sizeof(struct contact));
int IdStack = malloc(stacksize);
  
if (IdStack != 0)
int pop(int *stack);
else{
temp -> userID = IDgenerator;
IDgenerator=IDgenerator+1;
}
  
if (temp == NULL){
printf("stack is full");
}

}
void pop(int *stack)
{
struct contact *temp;
temp = malloc(sizeof(struct contact));

if((*stack) == NULL){
printf("The stack is empty. Pop is not allowed ");
return 0;
}
else{
temp = *stack;
stack = *temp;
}
free(temp);
return;
}
void display_node( struct contact *node, int index) {
printf(" RECORD %d: ", index);
printf(" Name: %s ", node->name);
printf(" Telephone: %s ", node->phone);
printf(" Address: %s ", node->address);
}
int main(int argc, char *argv[]) {
  
char ch;

if (argc != 2) {
printf("Command Line Parameters Required ! ");
printf("Try again...... ");
getchar();
return -1;
}
printf("SINGLY LINKED LIST ");
printf("******************");
file_name = argv[1];
load_file();
do {
menu();
fflush(stdin);
ch = tolower(getchar());
branching(ch);
} while (ch != 'q');
return 0;
}