Question
Using C programming. How can I use argv and/or argc to take user input for setting the initial stack by user input from the command
Using C programming.
How can I use argv and/or argc to take user input for setting the initial stack by user input from the command line when starting the program, then running through the menu I have created to add more elements, pop elements, and so on (Using the functions I have made, below)
Main Function:
#include
#include
#include "Linked_Stack.h"
int main(){
node input;
int choice, k=1, s=1, d=1;
constr();
while (k==1){
printf(" Enter 1 to push Enter 2 to pop Enter 3 to peek Enter 4 to check if stack is empty. Enter 5 to print stack. ");
scanf(" %d", &choice);
switch(choice){
case 1:
s=1;
while (s==1){
printf(" Enter element to push: ");
scanf("%d", &input.data);
push(input.data);
printf(" Enter 1 to push another element, or any other integer to exit back to main menu. ");
scanf("%d", &s);
}
break;
case 2:
d=1;
while (d==1){
pop();
printf(" Enter 1 to pop another element, or any other integer to exit back to main menu. ");
scanf("%d", &d);
}
break;
case 3:
peek();
break;
case 4:
empty();
break;
case 5:
printf(" Stack is currently defined as: { ");
print(head);
printf(" }");
printf(" Enter 1 to return to main menu, or any other integer to exit program: ");
scanf("%d", &k);
}
}
/*push(56);
push(5);
push(72);
push(73);
peek();
printf(" Final stack output below: ");
printf("{ " );
print(head);
printf(" }");
empty();*/
}
Linked_Stack.h
-----------------------------------------------------------------------------------------------------------------------
#include
#include
struct node{
int data;
struct node *next;
};
typedef struct node node;
// All we need to construct the stack is to set the first element to NULL
node* head;
void constr(){
head = NULL;
}
int push(int datum){
node* tmp; // Creates temporary node
tmp = malloc(sizeof(node)); // Sets it to size of node
tmp->data = datum; // Assigns current value to input
tmp->next = head; // Pushes the NULL value to end
head = tmp; // Sets new head to assigned value
}
int pop(){
if (head == NULL){
printf(" Stack is empty! Cannot delete! ");
return 0;
}
else {
node* tmp;
tmp = head;
head = tmp->next;
}
}
int peek(){
if (head == NULL){
printf(" Stack is Null, nothing to return ");
}
else {
printf(" Last element to enter stack is: %d ", *head);
}
}
void print(node *head){ // Takes first memory location as input
if (head==NULL){ // For an endpoint and to ensure there isn't an arrow pointing into nothing - aesthetic
printf("NULL");
}
else{
printf("%d->", head->data);
print(head->next); // Reassigns input value of print function to next value. Continues untill it hits first if statement
}
}
void empty(){
if (head==NULL){
printf(" Stack is empty. ");
}
else{
printf(" Stack is not empty. ");
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started