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

Answers

Answer 1
Answer:

#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;

}

Sample Output:

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

Explanation:

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.


Related Questions

PI
s
ne
poem for the
During reading: Work
on the following questions:
suggests
I
a
1
b
What do you predict the poem will be about?
How does the shape support what the titles
How many stanzas does the poem have?
b How many sentences does it have?
3 Read the poem aloud in your group.
2
a
m
Until I saw the Sea by Lilian Moore
ou the Sea​

Answers

Explanation:

As the qn was copy pasted from another document...the qn seems incorrect... and incomplete..

pls see to it and attach the correct qn...

sorry

7. What is a slide transition?

Answers

Answer:

Is this multiple choice? If not, its a visual effect from one slide to another.

Explanation:

Answer:

A slide transition is the visual effect that occurs when you move from one slide to the next during a presentation. You can control the speed, add sound, and customize the look of transition effects

Explanation:

hope this helps:)

The decimal equivalent of the product of 1011 and 1100 is

Answers

Answer:

132

Explanation:

We convert each number to base 10 (decimal) and multiply.

So 1011₂ = 1 × 2³ + 0 × 2² + 1 × 2¹ + 1 × 2⁰

= 1 × 8 + 0 × 4 + 1 × 2 + 1 × 1

= 8 + 0 + 2 + 1

= 11₁₀

1100₂ = 1 × 2³ + 1 × 2² + 0 × 2¹ + 0 × 2⁰

= 1 × 8 + 1 × 4 + 0 × 2 + 0 × 1

= 8 + 4 + 0 + 0

= 12₁₀

So, 1011₂ × 1100₂ = 11₁₀ × 12₁₀ = 132₁₀

So, the decimal equivalent of the product of 1011 and 1100 is 132

does trend in computing important for organization management?​

Answers

Answer:

yes it is...................

The First National Bank debited $100.00 from your checking account into your savings account. What would the transaction descripton be?

Answers

Answer:

32 Day Interest Plus

Amount 1 - 32 days (per annum) 33 - 64 days (per annum)

R1 000 - R9 999 0.55% 0.65%

R10 000 - R24 999 0.80% 0.90%

R25 000 - R49 999 1.05% 1.15%

R50 000 - R99 999 1.55% 1.65%

Explanation:

List 5 general safety precautions that you can take when working with a computer equipment.

Answers

Answer:

Wear the right clothes, unplug all equipment, keep your work area clean, check for damaged parts, and do not force components into ports.

Explanation:

These are all general safety precautions when working with computer equipment.

Other Questions
7. What is a slide transition? Suppose a byte-addressable computer using set-associative cache has 2 16 bytes of main memory and a cache size of 32 blocks and each cache block contains 8 bytes. a) If this cache is 2-way set associative, what is the format of a memory address as seen by the cache; that is, what are the size of the tag, set, and offset fields Sonya randomly surveys 26 seventh graders to gather data about the amount of time spent each week using the Internet. Sonya records the data in the dot plot shown. What is the median of the data set? What is the mean and the median Explain THREE ways that you could be using your social media to help secure a position. List several mistakes people might make with their social media that could prevent them from getting a job they want. Which caused the US Army to take over the atomic research project being conducted at Columbia University in the early 1940s? the first atomic bomb test a letter from Albert Einstein Japan's research into atomic bombs the US entry into World War II Pls help I dont know hot to multiply the last part 8 2 please help me this is urgent Which line shown appears to have a Y-intercept of 3? A. IB. II C. III D. IV E. none of the above Rachel received a monthly raise of $105.25. If this represented a 5.5% increase, what is her monthly salary after the raise?A. $1,913.64B. $1,964.13. $2,018.89 D. $2.089.18 Let f(-x) = 5x 2 and g(x) = 2x + 3 and find f(x) f(0) 9(x) = 7x 1f(.2) 9(x) = 7x + 1f(x) 9(2) = 3x 5f(x) 9(x) = -3x +5 how many triangles can you make with two side lengths of 6 inches that meet at a 50 degree angle. How was life different from plantation to plantation? Please look at picture The authors of a paper concluded that more boys than girls listen to music at high volumes. This conclusion was based on data from independent random samples of 768 boys and 745 girls from a country, age 12 to 19. Of the boys, 397 reported that they almost always listen to music at a high volume setting. Of the girls, 331 reported listening to music at a high volume setting.Do the sample data support the authors' conclusion that the proportion of the country's boys who listen to music at high volume is greater than this proportion for the country's girls? Test the relevant hypotheses using a 0.01 significance level. (Use a statistical computer package to calculate the P-value. Use boys girls. Round your test statistic to two decimal places and your P-value to four decimal places.) Which resource is renewable? im almost done with this hw. plz help!!! give explanation if you actually know the answer so ik u dont just want points What is the intermediate step in the form (x+a)^2=b(x+a) 2 =b as a result of completing the square for the following equation?6x^2+48x+207=15 Explain what he is watching based on what it says in the poem star spangled bannerHeres the poem..The Star-Spangled BannerO say can you see, by the dawns early light,What so proudly we haild at the twilights last gleaming,Whose broad stripes and bright stars through the perilous fightOer the ramparts we watchd were so gallantly streaming?And the rockets red glare, the bombs bursting in air,Gave proof through the night that our flag was still there,O say does that star-spangled banner yet waveOer the land of the free and the home of the brave?On the shore dimly seen through the mists of the deepWhere the foes haughty host in dread silence reposes,What is that which the breeze, oer the towering steep,As it fitfully blows, half conceals, half discloses?Now it catches the gleam of the mornings first beam,In full glory reflected now shines in the stream,Tis the star-spangled banner - O long may it waveOer the land of the free and the home of the brave!And where is that band who so vauntingly swore,That the havoc of war and the battles confusionA home and a Country should leave us no more?Their blood has washd out their foul footsteps pollution.No refuge could save the hireling and slaveFrom the terror of flight or the gloom of the grave,And the star-spangled banner in triumph doth waveOer the land of the free and the home of the brave.O thus be it ever when freemen shall standBetween their lovd home and the wars desolation!Blest with victry and peace may the heavn rescued landPraise the power that hath made and preservd us a nation!Then conquer we must, when our cause it is just,And this be our motto - In God is our trust, And the star-spangled banner in triumph shall waveOer the land of the free and the home of the brave. can someone help my lil sis again. Fossil fuels were made from organisms that lived long ago. True or false______. Because ___________