1 Download and run 2 Add the ability to push an integer arra


1) Download and run.
2) Add the ability to push an integer array containing [8][7][6][5][4][3][2][1]
the stack
at the ed of this operation.
2) Add the ability to push an integer array containing [12][11][10][9]
when option 4 is selected. Element [9] should be at the top of the stack
at the ed of this operation.

3) Add the ability to pop 6 elements off the stack
when option 6 is selected.
4) Demo for checkoff


/* Write a C program to implement stack. Stack is a LIFO data strcuture    *
* LIFO - Last in First Out                                                *
* Perform PUSH(insert operation), POP(Delete operation) and Display stack */

#include <stdio.h>

#define MAXSIZE 5
struct Stack /* Structure definition for stack */
{
int stk[MAXSIZE];
int top;
};


struct Stack s;

/* Function declaration/Prototype*/

void push (void);
int pop(void);
void display (void);

void main ()
{
int choice;
int option = 1;
s.top = -1;

printf (\"STACK OPERATION\ \");
while (option)
{
printf (\"------------------------------------------\ \");
printf (\" 1 --> PUSH \ \");
printf (\" 2 --> POP \ \");
printf (\" 3 --> DISPLAY \ \");
printf (\" 4 --> EXIT \ \");
printf (\"------------------------------------------\ \");

printf (\"Enter your choice\ \");
scanf (\"%d\", &choice);

switch (choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: return;
}


printf (\"Do you want to continue(Type 0 or 1)?\ \");
scanf (\"%d\", &option);
}
}

/*Function to add an element to the stack*/
void push ()
{
int num;
if (s.top == (MAXSIZE - 1))
{
printf (\"Stack is Full\ \");
return;
}
else
{
printf (\"Enter the element to be pushed\ \");
scanf (\"%d\", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
}
return;
}

/*Function to delete an element from the stack*/
int pop ()
{
int num;
if (s.top == - 1)
{
printf (\"Stack is Empty\ \");
return (s.top);
}
else
{
num = s.stk[s.top];
printf (\"poped element is = %d\ \", s.stk[s.top]);
s.top = s.top - 1;
}
return(num);
}

/*Function to display the status of the stack*/
void display ()
{
int i;
if (s.top == -1)
{
printf (\"Stack is empty\ \");
return;
}
else
{
printf (\"\ The status of the stack is\ \");
for (i = s.top; i >= 0; i--)
{
printf (\"%d\ \", s.stk[i]);
}
}
printf (\"\ \");
}

/*---------------------------------------------------------------------------
Output
STACK OPERATION
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
23
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
45
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
78
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
3

The status of the stack is
78
45
23

Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
2
poped element is = 78
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
1 --> PUSH
2 --> POP
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
3

The status of the stack is
45
23

Do you want to continue(Type 0 or 1)?
0
-----------------------------------------------------------------------------*/

Solution

#####Actual modified code stack.c ####

#include <stdio.h>
#define MAXSIZE 5
struct Stack /* Structure definition for stack */
{
int stk[MAXSIZE];
int top;

};

struct Stack s;
/* Function declaration/Prototype*/
void push (void);
int pop(void);
void display (void);

void main ()
{
   int choice;
   int option = 1;
   s.top = -1;
   printf (\"STACK OPERATION\ \");
   while (option)
   {
   printf (\"------------------------------------------\ \");
   printf (\" 1 --> PUSH \ \");
   printf (\" 2 --> POP \ \");
   printf (\" 3 --> DISPLAY \ \");
   printf (\" 4 --> EXIT \ \");
   printf (\"------------------------------------------\ \");
   printf (\"Enter your choice\ \");
   scanf (\"%d\", &choice);
   switch (choice)
   {
   case 1: push();
   break;
   case 2: pop();
   break;
   case 3: display();
   break;
   case 4: return;
   }

   printf (\"Do you want to continue(Type 0 or 1)?\ \");
   scanf (\"%d\", &option);
   }
}

/*Function to add an element to the stack*/
void push ()
{
   int num;
   if (s.top == (MAXSIZE - 1))
   {
   printf (\"Stack is Full\ \");
   return;
   }
   else
   {
   printf (\"Enter the element to be pushed\ \");
   scanf (\"%d\", &num);
   s.top = s.top + 1;
   s.stk[s.top] = num;
   }
   return;
}

/*Function to delete an element from the stack*/
int pop ()
{
   int num;
   if (s.top == - 1)
   {
   printf (\"Stack is Empty\ \");
   return (s.top);
   }
   else
   {
       num = s.stk[0];
       printf (\"poped element is = %d\ \", num);
       int j = 0;
       for(j = 0 ; j < s.top ; j++){
           s.stk[j] = s.stk[j+1];
       }
       s.top = s.top - 1;
   }
   return(num);
}

/*Function to display the status of the stack*/
void display ()
{
   int i;
   if (s.top == -1)
   {
   printf (\"Stack is empty\ \");
   return;
   }
   else
   {
   printf (\"\ The status of the stack is\ \");
   for (i = s.top; i >= 0; i--)
   {
   printf (\"%d\ \", s.stk[i]);
   }
   }
   printf (\"\ \");
}

Description - pop operation was not programmed correctly now it has been modified. once the element read from array with respect to pop oepration, we need to remove that element from array and shift all the elements one position upward. this logic was not working ealier. Please run the program.

Output ; output would be same as mentioned in actual quary, Please refer the actually question for result.

thanks,

 1) Download and run. 2) Add the ability to push an integer array containing [8][7][6][5][4][3][2][1] the stack at the ed of this operation. 2) Add the ability
 1) Download and run. 2) Add the ability to push an integer array containing [8][7][6][5][4][3][2][1] the stack at the ed of this operation. 2) Add the ability
 1) Download and run. 2) Add the ability to push an integer array containing [8][7][6][5][4][3][2][1] the stack at the ed of this operation. 2) Add the ability
 1) Download and run. 2) Add the ability to push an integer array containing [8][7][6][5][4][3][2][1] the stack at the ed of this operation. 2) Add the ability
 1) Download and run. 2) Add the ability to push an integer array containing [8][7][6][5][4][3][2][1] the stack at the ed of this operation. 2) Add the ability
 1) Download and run. 2) Add the ability to push an integer array containing [8][7][6][5][4][3][2][1] the stack at the ed of this operation. 2) Add the ability

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site