1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Lynna [10]
3 years ago
10

Write a C program to insert and delete values from a stack(to perform pop and push operations) using an array data structure

Computers and Technology
1 answer:
Bumek [7]3 years ago
4 0
<h2>Answer:</h2>

#include<stdio.h>

// Method to insert a value into a stack

void push(char value, char myStack[], int *top, int sizeOfStack){

   if(*top == -1){

       myStack[sizeOfStack - 1] = value;

       *top = sizeOfStack - 1;

   }

   else if(*top == 0){

       printf("Stack is full. \n");

   }

   else{

       myStack[(*top) - 1] = value;

       (*top)--;

   }

}

//Method to remove a value from a stack

void pop(char myStack[], int *top, int sizeOfStack){

   if(*top == -1){

       printf("Stack is empty. \n");

   }

   else{

       printf("Value removed: %c \n", myStack[(*top)]);

       // Now if the value removed was the last value in the stack

       // you should set top to -1.  

       // This would show that the stack is now empty

       if((*top) == sizeOfStack - 1){

           (*top) = -1;

       }

       else{

           (*top)++;

       }

   }

}

//Method to test the pop and push methods

int main() {

 //variable to hold the size of stack

 int sizeOfStack = 6;

 //create the stack

 char myStack[sizeOfStack];

 

 //set the top to -1

 //this is to show that the stack is initially empty

 int top = -1;

 //insert a value tot the stack

 push('x', myStack, &top, sizeOfStack);

 //print the value on the top

 printf("Value on top of stack: %c\n", myStack[top]);

 //insert another value to the stack

 push('y',myStack, &top, sizeOfStack);

 //print the value on the top of the stack

 printf("Value on top of stack: %c\n", myStack[top]);

 //insert another value to the stack

 push('z',myStack, &top, sizeOfStack);

 //print the value on the top of the stack

 printf("Value on top of stack: %c\n", myStack[top]);

 //remove the last inserted value

 pop(myStack, &top, sizeOfStack);

 //print the value on the top of the stack

 printf("Value on top of stack: %c\n", myStack[top]);

 //remove another value

 pop(myStack, &top, sizeOfStack);

 //print the value on the top of the stack

 printf("Value on top of stack: %c\n", myStack[top]);

 return 0;

}

<h2>Sample Output:</h2>

Value on top of stack: x

Value on top of stack: y

Value on top of stack: z

Value removed: z  

Value on top of stack: y

Value removed: y  

Value on top of stack: x

<h2>Explanation:</h2>

The code above has been written in c and it contains comments explaining important parts of the code. Please go through the comments.

It also contains a sample output resulting from the run of the program.

You might be interested in
3. Run the C-LOOK algorithm (requests can only be serviced when the head is moving toward higher numbered tracks); the head is c
o-na [289]

Answer:

See explaination.

Explanation:

An algorithm is specifically defined as the step by step method or process of achieving any type of result.

Please kindly see the attached file for the C algorithm that fulfils the answer of the given problem.

3 0
3 years ago
Which option is an appropriate database function syntax?
kodGreya [7K]

Answer:

=DSUM(A4:D8, "Unit Cost", A1:B2)

Explanation:

From the list of given options, it's obvious that the question relates to Excel database sum function.

The correct answer among the list of options is:

=DSUM(A4:D8, "Unit Cost", A1:B2)

Where

DSUM represents the function itself

A4:D8 represents the range

"Unit cost" represents the field

and

A1:B2 represents the criteria of the function

Analysing other options:

Option 1: The equality sign (=) before excel formulas is missing

Option 2: Sum is used instead of DSUM and there's a missing bracket before the range

Option 3: There's a missing comma between the range, the field and the criteria

Hence, the last option (4) answers the question.

5 0
3 years ago
Read 2 more answers
Determine if the following identifiers are valid. If they are invalid, state the reason. For example, the identifier min value i
Natasha2012 [34]

Answer:

Explanation:

Determine if the following identifiers are valid. If they are invalid, state the reason. For example, the identifier min value is invalid as it uses a whitespace.

road

mid_point

8ball

break

_log_value

EVERY1

[email protected]

_log

NewNumber

$varA

class

4 0
3 years ago
What will the following code display? int numbers[4] = { 99, 87 }; cout &lt;&lt; numbers[3] &lt;&lt; endl; a. 87 b.0 d. 34. What
pishuonlain [190]

Answer:

1. The output is 0

2. Each element in the array is initialized to 0.0

Explanation:

Solving (a): The output of the code

We have:

<em>int numbers[4] = { 99, 87 }; </em>

<em>cout << numbers[3] << endl;</em>

<em />

The first line initializes the 0 and 1 index of the array to 99 and 87, respectively.

Other elements will be 0, by default.

So, the following code segment will output 0

<em>cout << numbers[3] << endl; </em>

<em />

Solving (b): What the given code will do?

The first and second line declares an array of 5 elements

However, the following iteration will only initialize the array with 0.0 starting from the second

<em>for(int i = 2; i <= SIZE; i++) { x[i] = 0.0; }</em>

<em />

Because, the counter is initialized to start from the second index.

7 0
3 years ago
If you want to create header, clicker the add a header button on the ___ tab
Finger [1]
On the insert tab
------------------------
6 0
3 years ago
Other questions:
  • Excel 2016 is primarily what type of program?
    15·1 answer
  • ____________ are used to store all the data in a database
    6·2 answers
  • How to fix the acount when it says "Uh oh, this account has been restricted because of an unusual amount of activity.
    8·1 answer
  • The TCP/IP ____ layer, sometimes called the Internet layer, is roughly equivalent to OSI's network layer.
    7·1 answer
  • What are the Components of a CPU?
    15·2 answers
  • Create ER Models &amp; Normalization for the following scenarios:
    5·1 answer
  • What are three metrics used to define consistent network application availability?
    5·1 answer
  • Henry is planning to visit Spain for a vacation. He wants to learn Spanish, but he prefers to do it in a fun way. He searches on
    8·2 answers
  • Your company's network topology diagrams aren't very detailed so you're helping to improve them. The new set will have separate
    9·1 answer
  • Cost, time, knowledge are examples of
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!