A limitedsized Stack ADT is an abstract data type that has a

A limited-sized Stack ADT is an abstract data type that has a limit on the height of the stack. It can be created with a static array of size N, where N is the maximum height of the array such that all (except print) operations below are O(1) operations A hint on this will be given in class. The operations for this ADT are: print (T) - prints the stack from top to bottom - push (T, x) - Pushes the integer x into stack T - pop (T) - Pops the integer from top of the stack - isEmpty (T) - returns 1 (true) or 0 (false) depending on emptiness of the stack T height (T) - returns the number of items currently in the stack - peek(T) - returns the data of the top of the stack T without popping You are to create a limited-sized Stack datatype with an array of integers (among other attributes). Using the operations available for a C array, implement each one of the functions described above (some are more straight forward than others). Here is the definition of your structure. typedef struct {int data(N);//array of at most size N//N should be a constant declared globally int top; int bottom;} stack_t; In your main function, include calls to each of these functions. In an attempt increase test coverage, your test cases should include: Pushing into an array successfully Pushing into an array unsuccessfully (already full) Popping an array successfully Popping an array unsuccessfully (already empty) Showing the height Peeking at the top element (both empty and non-empty stack) In your hardcopy, please provide a screenshot of the output of your code. You may include all test cases in one execution of your program. However, please utilize the print function after each of the first four test cases above to show the progress of your program.

Solution

#include<stdio.h>
#include<stdlib.h>
const int N =10;
typedef struct
{
   int data[10];//array of at most size n
   int top;
   int bottom;
}stack_t;

void print(stack_t t)//prints the stack from top to bottom
{
  
   printf(\"The stack:\ \");
   int i=t.top;
   printf(\"%d\ \",t.data[t.top]);
   while(i>=t.bottom)
   {
       printf(\"%d\ \",t.data[i]);
       i--;  
   }
  
}
void push(stack_t *t,int k)//pushes the integer k to stack
{
   if(t->top+1<N){
  
   t->top++;
  
  
   t->data[t->top]=k;
   }
   else
   {
       printf(\"\ Error stack is full\ \");  
   }
}
int pop(stack_t *t)//pops top element from stack
{
   if(t->top==-1){
       printf(\"\ Error : stack is empty\ \");
   return -1;}
   int k =t->data[t->top];
   t->top--;
   return k;   

}
int isEmpty(stack_t t)//checks whether stack is empty or not
{
   if(t.top==-1)return 1;
   return 0;
}
int height(stack_t t)//returns height of the stack
{
   return t.top+1;
}

int peek(stack_t t)//returns top element of the stack
{
   return t.data[t.top];
  
}

int main()
{
  
   stack_t T;
   T.top=-1;
   T.bottom=0;
   push(&T,2);//pushing to array
   push(&T,3);//pushing to array
   push(&T,1);//pushing to array
   print(T);//printing stack
   pop(&T);//POPING from stack successfull
   printf(\"\ Height of the stack:%d\ \",height(T));//finding height of stack
   printf(\"Top element of the stack:%d\ \",peek(T));//printing top element
   pop(&T);//successfull
   pop(&T);//successfull
   pop(&T);//unsuccessfull*/
   return 0;
}

output:-

The stack:
1
3
2

Height of the stack:2
Top element of the stack:3

Error : stack is empty


Process exited normally.
Press any key to continue . . .

 A limited-sized Stack ADT is an abstract data type that has a limit on the height of the stack. It can be created with a static array of size N, where N is the
 A limited-sized Stack ADT is an abstract data type that has a limit on the height of the stack. It can be created with a static array of size N, where N is the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site