Question
Using C
2.A Stack data structure with at least following functions pushO - Insert an element at one end of the stack called top. )-Remove and return the element at the top of the stack, if it is not empty. peek0- Return the element at the top of the stack without removing it, if the stack is not empty. size0 -Return the number of elements in the stack. isEmpty0-Return true if the stack is empty, otherwise return false isFullO-Return true if the stack is full, otherwise return false. You can also write your own c++ program to implement the same., I
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code: (HomeworkLib submission portal automatically removes backslash n , Hence i have added the screenshot of code too)

#include<stdio.h>
#define MAX 5 // Maximum size of array
int top = -1;
int stack_arr[MAX];
int isFull()
{
if(top==MAX-1)
return 1;
else
return 0;
}
int isEmpty()
{
if(top==-1)
return 1;
else
return 0;
  
}
void push()
{
   int x;
   if(isFull()==1)
       printf("Stack Overflow ");
   else
   {
       printf("Enter the item to be pushed in stack : ");
       scanf("%d",&x);
       top=top+1;
       stack_arr[top] = x;
   }
}

int pop()
{
int x=-1000; // It is garbage value
   if(isEmpty()==1)
       printf("Stack Underflow ");
   else
   {
   x=stack_arr[top];
       top=top-1;
   }
   return x;
}
int size()
{
return (top+1);
}
int peek()
{
int x=-1000;
   if(isEmpty()==1)
       printf("Stack Underflow ");
   else
   {
   x=stack_arr[top];
   }
   return x;
}
int main()
{
   int choice,x;

   do{
       printf("1.Push 2.Pop 3.Peek 4.Size 5.Quit ");
       printf("Enter your choice : ");
       scanf("%d",&choice);
       switch(choice)
       {
       case 1 :
           push();
           break;
       case 2:
       x=pop();
           printf("Popped element is : %d ",x);
           break;
       case 3:
       x=peek();
           printf("Top element is : %d ",x);
           break;
       case 4:
       x=size();
       printf("Size of the stack is: %d ",x);
       case 5:
break;
       default:
           printf("Wrong choice ");
       }}while(choice!=5);
       return 0;
}

Output:

1.Push 2.Pop 3.Peek 4.Size 5.Quit Enter your choice: 1 Enter the item to be pushed in stack: 10 1.Push 2.Pop 3.Peek 4.Size 5.Quit Enter your choice : 1 Enter the item to be pushed in stack: 20 1.Push 2.Pop 3.Peek 4.Size 5.Quit Enter your choice 1 Enter the item to be pushed in stack : 30 1.Push 2.Pop 3.Peek 4.Size 5.Quit Enter your choice : 4 Size of the stack is: 3 1.Push 2.Pop 3.Peek 4.Size 5.Quit Enter your choice : 3 Top element is : 30 1.Push 2.Pop 3.Peek 4.Size 5.Quit Enter your choice : 2 Popped element is : 30 1.Push 2.Pop 3.Peek 4.Size 5.Quit Enter your choice: 2 Popped element is : 20 1.Push 2.Pop 3.Peek 4.Size 5.Quit Enter your choice : 2 Popped element is : 10 1.Push 2.Pop 3.Peek 4.Size 5.Quit Enter your choice : 2 Stack Underflow Popped element is :-1000 1.Push 2.Pop 3.Peek 4.Size 5.Quit Enter your choice: 5 . . .Program finished with exit code 0 ess ENTER to exi

Screenshot:

8 9 #include<stdio.h> 10 #define MAX 5 // Maximum size of array 11 int top-1; 12 int stack_arr[MAX]; 13 int isFull() 14 15 16aih.c 42 int pop() 43 int x--1000;1/ It is garbage value if(isEmpty()-1) 45 46 47 48 49 50 51 52 53 54 int size() printf(Stack Underflow ); else X-stack arrl top top-top-1; return x; 56 57 58 int peek() 59 68 61 62 63 64 65 return (top+1); int x--1000 if(isEmpty()-1) printf(Stack Underflow ); else X-stack arrl top 67 68 return x;69 int main 70 71 72 73 74 75 76 int choice,x; printf(1.Push 2.Pop 3.Peek 4.Size printf(Enter your choice : ); scanf(%d , & choice); switch(choice) 5.Quit n); 78 79 80 81 82 83 84 85 86 87 case 1 push(); break case 2: x-pop(); printf(Popped break element is : %d\n,x); case 3: x-peek): printf(Top element break is : %d\n,x); 89 90 91 92 93 94 95 96 97 98 case 4: x-size(); printf(Size of the stack is: %d\n,x); case 5: break default: printf(Wrong choice\n); ))while (choice ! ); return e; 100

Add a comment
Know the answer?
Add Answer to:
Using C 2.A Stack data structure with at least following functions pushO - Insert an element...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a C++ program. Include comment, input and output file. STACK IMPLEMENTATION USING AN link list IMPLEMENT THE FOLLOWING STACK OPERATIONS using  a TEMPLATED CLASS. WRITE ALL THE FULL-FUNCTION DE...

    Create a C++ program. Include comment, input and output file. STACK IMPLEMENTATION USING AN link list IMPLEMENT THE FOLLOWING STACK OPERATIONS using  a TEMPLATED CLASS. WRITE ALL THE FULL-FUNCTION DEFINITIONS NEEDED for the OPERATIONS. OUTPUT: PRINT ALL THE ELEMENTS ON THE STACK. Stack Operations initializestack: Initializes the stack to an empty state. isEmptyStack: Determines whether the stack is empty. If the stack is empty, it returns the value true; otherwise, it returns the value false. isFul1stack: Determines whether the stack...

  • There is a data structure called a drop-out stack that behaves like a stack in every...

    There is a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, then when the n+1element is pushed, the bottom element is lost. Implement a drop-out stack using links, by modifying the LinkedStack code. (size, n, is provided by the constructor. Request: Please create a separate driver class, in a different file, that tests on different types of entries and show result of the tests done on...

  • Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns...

    Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The method signature for sizeIS is public int sizeIs() a.) Write the code for sizeIs for the ArrayStack class b.) Write the code for sizeIs for the LinkedStack class (do not add any instance variables to the class; each time sizeIs is called you must "walk" through the stack...

  • (C++) Two stacks of the same type are the same if they have the same number...

    (C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...

  • In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

    In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...

  • In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> {...

    In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element);    /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T...

  • In C, Implement each of the functions to create a working stack 6 II-Implement each of...

    In C, Implement each of the functions to create a working stack 6 II-Implement each of the functions to create a working stack. 7 II -Do not change any of the function declarations I-(i.e. stack t* create stack) should not have additional arguments) II-You should not have any 'printf' statements in your stack functions 10 II(You may consider using these printf statements to debug, but they should be removed from your final version) 12 #ifndefMYSTACK_A 13 #define MYSTACKH 14 15...

  • C++ myStack.lh stackADT.h Instructions main.cpp 1 #include«iostream» 2 #include<stdlib.h> 3 #include«conio.h> 4 #include«ostream» 5 using namespace std; 6 const int SIZE-5; //Stack size 7 //...

    C++ myStack.lh stackADT.h Instructions main.cpp 1 #include«iostream» 2 #include<stdlib.h> 3 #include«conio.h> 4 #include«ostream» 5 using namespace std; 6 const int SIZE-5; //Stack size 7 //class declaration 8 class stack Instructions Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same Overload the relational operatorfor the class stackType that returns true if two stacks of the same type are the same; it returns false...

  • I was told I need three seperate files for these classes is there anyway to tie...

    I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts {       public static void main(String[] args) {             Scanner input = new Scanner(System.in);             String sentence;             String again;             do {                   System.out                               .println("Enter a sentence, I will tell you if it is a palindrome: ");...

  • Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of...

    Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of the peek, isEmpty, size, and toString methods. See Base_A06Q1.java for a starting place and a description of these methods. Here is the base given: /** * Write a description of the program here. * * @author Lewis et al., (your name) * @version (program version) */ import java.util.Iterator; public class Base_A06Q1 { /** * Program entry point for stack testing. * @param args Argument...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT