For my program, the goal is to read a string and use the input toward my code. However, we must note how some of my commands require only one input and some require two. For example: the "ina", "inb", "del", "rep" commands require 2 inputs, where as prn requires only 1. Is there another way I can apply this to my code, other than the way I have been using (scanf twice). Because when I want to use prn, I have to include an unnecessary int.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
int data;
char *item;
struct node* next;
};
//Global Variables
struct node* root = NULL;
//Prototypes
void ina();
void inb();
int length();
void prn();
void del();
void rep();
//Main
void main () {
char command[4];
int num;
char str[255];
while(1) {
printf("Command? ");
fflush(stdout);
scanf("%s", &command); //This is where I want to generalize
scanf("%d", &num); //and clean up
//Reads input and selects which command to execute
if (strcmp(command, "ina")==0) {
ina(num);
} else
if(strcmp(command, "inb")==0) {
inb(num);
} else
if (strcmp(command, "prn")==0) {
prn();
} else
if (strcmp(command, "del")==0) {
del(num);
} else
if (strcmp(command, "rep")==0) {
rep(num);
} else
if (strcmp(command, "end")==0) {
exit(1);
}
else {
return;
}
}
}
//Command Insert After
void ina(int n) {
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->data = n;
temp->next = NULL;
if(root==NULL) {
root = temp;
printf("Text inserted at beginning\n");
}
else {
struct node* p;
p = root;
while(p->next != NULL) {
p = p->next;
}
p->next = temp;
printf("Ok\n");
}
}
//Command Insert Before
void inb(int n) {
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->data = n;
temp->next=NULL;
if (root == NULL) {
root = temp;
printf("Text inserted at beginning\n");
fflush(stdout);
}
else {
temp->next=root;
root = temp;
printf("Ok\n");
fflush(stdout) ;
}
}
//Command Length, not necessary but use for delete command
int length() {
struct node* temp;
int count = 0;
temp = root;
while(temp != NULL) {
count++;
temp = temp->next;
}
return count;
}
//Command Print
void prn() {
struct node* temp;
temp = root;
if(temp == NULL) {
printf("List is empty\n");
fflush(stdout);
}
else {
while(temp != NULL) {
printf("%d\n",temp->data);
temp = temp->next;
}
printf("\n");
}
}
//Command Delete
void del(int n) {
struct node* temp;
if(n > length()) {
printf("No Such Index\n");
fflush(stdout);
return;
}
else if(n==1) {
temp = root;
root = temp->next;
temp->next = NULL;
free(temp);
}
else {
struct node* p = root, *q;
int i = 1;
while(i<n-1) {
p = p->next;
i++;
}
q = p->next;
p->next = q->next;
q->next = NULL;
free(q);
}
printf("Deleted\n");
fflush(stdout);
}
//Command Replace FIX
void rep(int i) {
int old, n;
struct node* temp;
temp = root;
old = i;
printf("\nEnter what you want to replace with: ");
scanf("%d", &n);
if(temp == NULL) {
printf("No such index\n");
return NULL;
}
while (temp != NULL) {
if(temp->data == old) {
temp->data = n;
printf("Replaced\n");
}
temp = temp->next;
}
}
Output Notice how "prn" command I had to add an unnecessary 1 and for the "end" command I didn't add anything at first and nothing processed. Output image