Question

In: Computer Science

You have already implemented a Queue before that works on First Come First served basis. In...

You have already implemented a Queue before that works on First Come First served basis.
In this assignment you are to implement a class that works on Last In First Out. Such a structure is called a Stack. It only allows two operations add (named push) and remove (named pop).
Push only allows adding any data item to the last entry place.
Pop only allows you to remove the last added entry from the stack.

For example if your stack has items (means values where added in the order 12 then 30 then 15)
12 30 15

After Push(45) the stack will look like
12 30 15 45

After call to Pop() the stack must look like
12 30 15

After another call to Pop() the stack must look like
12 30

You must implement a Stack as a class.
This should have
three methods
constructor: That will declare an empty array of 5 elements.
Pop: This function will just remove a value from the last of the Stack and return it to the user.
Push: This method will take an item as argument to be added to the stack.
If the space in the stack is not full the item will be added to the end of the stack
If the stack is full with items then a new array must be declared that is double the size of the last array and all values must be copied to the new array before the new item is added to the last of these values.

NOTE: The code must be in "C LANGUAGE"

THANK YOU

Solutions

Expert Solution

As C doesnt supports class(oop) i have written the code with structure. If you need the code with class we can implement it with c++. Also the below code could be modified to work with class.

Code :

#include <stdio.h>

typedef struct
{
   int *a,tos,n;
}stack;

void init(stack *,int);
int isempty(stack *);
int isfull(stack *);
void push(stack *,int);
int pop(stack *);
void copy(stack *,int);
void display(stack *);

void init(stack *s,int x)
{
   s->tos=-1;
   s->n=x;
   s->a=(int *)malloc(s->n*sizeof(int));
}

int isempty(stack *s)
{
   if(s->tos==-1)
       return 1;
   else
       return 0;
}

int isfull(stack *s)
{
   if(s->n-1==s->tos)
       return 1;
   else
       return 0;
}

void push(stack *s,int info)
{
   if(isfull(s))
   {
       copy(s,2*(s->n));
   }
   s->tos++;
   s->a[s->tos]=info;
}

int pop(stack *s)
{
   int info;
   if(isempty(s))
   {
       printf("\n\tStack Is Empty");
       return 0;
   }
   else
   {
       info=s->a[s->tos];
       s->tos--;
       return info;
   }
}
  
void display(stack *s)
{
   int i;
   for (i=0; i <= s->tos; i++)
       printf("%d ",s->a[i]);
   printf("\n");
}

void copy(stack *s,int x)
{
   stack s1;
   init(&s1,x);
   int i;
   for (i=0; i <= s->tos; i++)
       s1.a[s1.tos++] = s->a[i];
   s = &s1;
   return;
}

int main()
{
   stack s;
   init(&s,5);
   push(&s,1);
   push(&s,5);
   push(&s,4);
   push(&s,3);
   display(&s);
   push(&s,1);
   push(&s,5);
   push(&s,4);
   push(&s,3);
   display(&s);
   printf("Element popped : %d\n",pop(&s));
   printf("Element popped : %d\n",pop(&s));
   display(&s);
   return 0;
}

Output :

C++

#include <stdio.h>

class stack
{
int *a,tos,n;

   public :
   stack(int s)
   {
       tos = -1;
       n = s;
       a = new int[s];
   };

   int isempty()
   {
   if(tos==-1)
       return 1;
   else
       return 0;
   }

   int isfull()
   {
   if(tos<n)
       return 0;
   else
       return 1;
   }

   void push(int info)
   {
   stack *s1 = this;
   if (isfull())
       copy(s1,2*n);
   tos++;
   a[tos]=info;
   }

   int pop()
   {
   int info;
   if(isempty())
   {
       printf("\n\tStack Is Empty");
       return 0;
   }
   else
   {
       info=a[tos];
       tos--;
       return info;
   }
   }

   void display()
   {
   for (int i=0; i <= tos; i++)
       printf("%d ",a[i]);
   printf("\n");
   }

   void copy(stack *stk,int s)
   {
       stack s1(s);
       for(int i=0; i <= tos; i++)
s1.a[s1.tos++] = a[i];
stk = &s1;
   }
};

int main()
{
stack s(5);
s.push(1);
s.push(5);
s.push(4);
s.push(3);
s.display();
s.push(1);
s.push(5);
s.push(4);
s.push(3);
s.display();
printf("Element popped : %d\n",s.pop());
printf("Element popped : %d\n",s.pop());
s.display();
return 0;
}


Related Solutions

Tire Kingdom installs automobile tires on a first-come-first-served basis. A random sample of 40 customers experienced...
Tire Kingdom installs automobile tires on a first-come-first-served basis. A random sample of 40 customers experienced an average wait time of 90.5 minutes. Assume that the standard deviation of the total wait time for all customers is 20.6 minutes. Determine the 90% confidence interval for this sample.
The < and == operators for the class Record have already been implemented for you.
The < and == operators for the class Record have already been implemented for you. Write the code necessary to complete the >, <=,>= and != operators. (hint: you do not need to know anything about the Record class to complete)
Assume you have a stack and a queue implemented using an array of size 4. show...
Assume you have a stack and a queue implemented using an array of size 4. show the content of the array for the stack (then the queue) for the following operations: (for the queue replace push by add and pop by remove; keep in mind that the queue uses a circular array): push(-3), push(-5), push(-9), push(-10), pop(), pop(), push(-13), pop(), push( -15), push(-17). java code
Identify three conditions that would need to be implemented (or have already been implemented) in your...
Identify three conditions that would need to be implemented (or have already been implemented) in your organization to create a culture of innovation and change.
Identify three conditions that would need to be implemented (or have already been implemented) in your...
Identify three conditions that would need to be implemented (or have already been implemented) in your organization to create a culture of innovation and change.
For First Come First Served (FCFS), -What is the arrival time, completion time, burst time, turnaround...
For First Come First Served (FCFS), -What is the arrival time, completion time, burst time, turnaround time, and the waiting time? -discuss these terms further as they pertain to the efficiency of the algorithm as well as properties such as starvation. -Pros and Cons of FCFS
in C++ We are going to implement the following scheduling algorithms 1. First-Come First-Served (FCFS) 2....
in C++ We are going to implement the following scheduling algorithms 1. First-Come First-Served (FCFS) 2. Shortest Remaining Time First (SRTF) 3. Highest Response Ratio Next (HRRN) 4. Round Robin, with di_erent quantum values (RR) We are interested to compute the following metrics, for each experiment: _ The average turnaround time _ The total throughput (number of processes done per unit time) _ The CPU utilization _ The average number of processes in the ready queue The simulator needs to...
An operating system uses the First-Come, First-Served (FCFS) CPU scheduling algorithm. Consider the following set of...
An operating system uses the First-Come, First-Served (FCFS) CPU scheduling algorithm. Consider the following set of processes in this OS, with the length of the CPU burst time given in milliseconds, and the shown priority. A larger priority number implies a higher priority. There is no pre-emption. The processes are assumed to have arrived in the order P1, P2, P3, P4, P5, all at time 0. Process Burst Time Priority P1 2 2 P2 1 5 P3 4 1 P4...
PLEASE READ CAREFULLY BEFORE STARTING THE ASSIGNMENT!!! I ALREADY HAVE ANSWERS FOR THE FIRST JOURNAL TABLE...
PLEASE READ CAREFULLY BEFORE STARTING THE ASSIGNMENT!!! I ALREADY HAVE ANSWERS FOR THE FIRST JOURNAL TABLE AS YOU CAN SEE BELOW, BUT I DON'T HAVE THE ANSWER FOR THE SECOND JOURNAL TABLE. I FOUND MANY ANSWERS FOR THE SECOND TABLE HERE ON CHEGG. BUT NOT ONE HAS A CORRECT ANSWER. FOR SOME OF THEM IT'S COMPLETELY DIFFERENT ANSWERS ALTHOUGH THE QUESTION IS THE SAME. PLEASE ANSWER THAT TABLE CAREFULLY AND CORRECTLY. USE THE SAME EXACT TABLE THAT I PROVIDED BELOW...
You will design a program to keep track of a restaurants waitlist using a queue implemented...
You will design a program to keep track of a restaurants waitlist using a queue implemented with a linked list. Make sure to read pages 1215-1217 and 1227-1251 1. Create a class named waitList that can store a name and number of guests. Use constructors to automatically initialize the member variables. 2. Add the following operations to your program: a. Return the first person in the queue b. Return the last person in the queue c. Add a person to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT