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
Counting calculus students. About A university offers 3 calculus classes: Math 2A, 2B and 2C. In both parts, you are given data
OleMash [197]

Answer:

Part (a) n(A∩B∩C) = 4

Part (b) n(A∪B∪C) = 54

Explanation:

n(A) = no. of students who took Math 2A

n(B) = no. of students who took Math 2B

n(C) = no. of students who took Math 2C

n(A∩B) = no. of students who took both Math 2A and 2B

n(A∩C) = no. of students who took both Math 2A and 2C

n(B∩C) = no. of students who took both Math 2B and 2C

n(A∩B∩C) = no. of students who took all three Math 2A, 2B and 2C

n(A∪B∪C) = no. of total students in a group

∩ represents Intersection and ∪ represents Union

Part (a)

n(A∪B∪C) = n(A) + n(B) + n(C) - n(A∩B) - n(A∩C) - n(B∩C) + n(A∩B∩C)  

Where n(A∩B∩C) represents the number of students who took all three classes and n(A∪B∪C) represents the total number of students in group A

157 = 51 + 80 + 70 - 15 - 20 - 13 + n(A∩B∩C)

Re-arranging the equation to solve for n(A∩B∩C) since we want to find out those students who took all three classes

n(A∩B∩C) = 157 - 51 - 80 - 70 + 15 + 20 + 13

n(A∩B∩C) = 4

So there are 4 students in group A who took all three classes

Part (b)

n(A∪B∪C) = n(A) + n(B) + n(C) - n(A∩B) - n(A∩C) - n(B∩C) + n(A∩B∩C)

This time we are given n(A∩B∩C) students who took all three classes and want to find n(A∪B∪C) that is total number of students

n(A∪B∪C) = 28 + 28 + 25 - 11 - 9 - 10 + 3

n(A∪B∪C) = 54

So there are total 54 students in group B

8 0
3 years ago
Convert the following decimal number into octal number-147​
agasfer [191]

Answer:

The answer for your question is 223

3 0
3 years ago
Describe the positive and negative effects of Internet​
kvasek [131]

Answer:

pos-ugly

neg-gay

Explanation:

8 0
3 years ago
Read 2 more answers
Write a class named Movie that has fields to hold the following data: Title Studio Name Year produced Rating In the Movie class,
rewona [7]

Answer:

Explanation:

don't know

3 0
2 years ago
What movie would be greatly improved if it was made into a musical?
Lina20 [59]
Sixteen candles would be even better if it was a musical
3 0
3 years ago
Other questions:
  • Write a program that will calculate the following function:
    14·1 answer
  • ​​most code division multiple access (cdma) networks conform to ____________ , created by the telecommunications industry associ
    10·1 answer
  • List three examples of observations of earth by remote-sensing satellites
    6·1 answer
  • Let L be any r.e. language. We know that there is an unrestricted grammar for L. Show that L can be generated by an unrestricted
    10·1 answer
  • As the project manager for a software development project, you are helping to develop the project schedule. You decide that writ
    13·1 answer
  • 6) RAM, 5 main memory<br>which storage conbe used<br>as permanent storage<br>Rom<br>a) R<br>)​
    8·2 answers
  • You want to centrally back up the files users store in the Documents folder in their user profiles, but you don’t want users to
    7·1 answer
  • Five Star Retro Video rents VHS tapes and DVDs to the same connoisseurs who like to buy LP record albums. The store rents new vi
    13·1 answer
  • Difference between the four generations of computers in a tabular form​
    8·1 answer
  • Does anyone know how to do Python Essentials 5.7.1.6 because i am completely lost
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!