Im having some trouble understanding a required part to modify a piece of code (
ID: 3677660 • 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.
Modify the int deletion() function, so that the UserID of the deleted node is pushed into the IdStack for reuse. You must call void push(int, int *, int *) to complete the operation.
Modify the display_all function to display the new member UserID of the contact structure.
Modify the load_file and save_file functions to include the following functions:
Save and retrieve variable IDgenerator.
Save and retrieve the new member UserID of the contact structure.
any insight in the right direction will help, im having trouble piecing everything together.
Here is the project.
#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;
}
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)
pop(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;
}
Explanation / Answer
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 = next;
}
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;
}