Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 1 A stack is LIFO linear data structure with the following operations:

ID: 3865456 • Letter: Q

Question

Question 1 A stack is LIFO linear data structure with the following operations: 1. Push : adds key to the top of the stack int push(int value) if (Ifull)) top-top1 tack[top]-value 2. Top 0:returns the key at the top of the stack but does not remove key from the stack int Top) return stack[top]: 3. Pop () : removes and returns the key at the top of the stack int pop)( int value; f (lemptyO) value stack[top] top-top-1 return value 4. Empty : Determines whether the stack is empty int enptyO f (top-1) return 1 else return 8; 5. Full : Determines whether the stack is already full.

Explanation / Answer

Answer for the given Question:

See the below three c programs based on given functions in the problem statement.

stack implementation using given functions

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#define size 5
struct stack {
int s[size];
int top;
} st;

int full() {
if (st.top >= size - 1)
return 1;
else
return 0;
}

void push(int item) {
st.top++;
st.s[st.top] = item;
}

int empty() {
if (st.top == -1)
return 1;
else
return 0;
}

int pop() {
int item;
item = st.s[st.top];
st.top--;
return (item);
}

void display() {
int i;
if (empty())
printf(" Stack Is Empty!");
else {
for (i = st.top; i >= 0; i--)
printf(" %d", st.s[i]);
}
}

int main() {
int item, choice;
char ans;
st.top = -1;

printf(" Implementation Of Stack");
do {
printf(" Main Menu");
printf(" 1.Push 2.Pop 3.Display 4.exit");
printf(" Enter Your Choice");
scanf("%d", &choice);
switch (choice) {
case 1:
printf(" Enter The item to be pushed");
scanf("%d", &item);
if (full())
printf(" Stack is Full!");
else
push(item);
break;
case 2:
if (empty())
printf(" Empty stack!Underflow !!");
else {
item = pop();
printf(" The popped element is %d", item);
}
break;
case 3:
display();
break;
case 4:
exit(0);
}
printf(" Do You want To Continue?");
ans = getche();
} while (ans == 'Y' || ans == 'y');

return 0;
}

Reverse of string using stack implementation

/*C program to Reverse String using STACK*/
#include <stdio.h>
#include <string.h>

#define MAX 100 /*maximum no. of characters*/

/*stack variables*/
int top=-1;
int item;
/***************/

/*string declaration*/
char stack_string[MAX];

/*function to push character (item)*/
void push(char item);

/*function to pop character (item)*/
char pop(void);

/*function to check stack is empty or not*/
int empty(void);

/*function to check stack is full or not*/
int full(void);

int main()
{
char str[MAX];

int i;

printf("Input a string: ");
scanf("%[^ ]s",str); /*read string with spaces*/
/*gets(str);-can be used to read string with spaces*/

for(i=0;i<strlen(str);i++)
push(str[i]);

for(i=0;i<strlen(str);i++)
str[i]=pop();

printf("Reversed String is: %s ",str);

return 0;
}

/*function definition of push*/
void push(char item)
{
/*check for full*/
if(full())
{
printf(" Stack is FULL !!! ");
return;
}

/*increase top and push item in stack*/
top=top+1;
stack_string[top]=item;
}

/*function definition of pop*/
char pop()
{
/*check for empty*/
if(empty())
{
printf(" Stack is EMPTY!!! ");
return 0;
}

/*pop item and decrease top*/
item = stack_string[top];
top=top-1;
return item;
}

/*function definition of empty*/
int empty()
{
if(top==-1)
return 1;
else
return 0;
}

/*function definition of full*/
int full()
{
if(top==MAX-1)
return 1;
else
return 0;
}

Stack implementation using linkedlist fashion

/*
* C Program to Implement a Stack using Linked List
*/
#include <stdio.h>
#include <stdlib.h>

struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;

int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();

int count = 0;

int main()
{
int no, ch, e;

printf(" 1 - Push");
printf(" 2 - Pop");
printf(" 3 - Top");
printf(" 4 - Empty");
printf(" 5 - Exit");
printf(" 6 - Dipslay");
printf(" 7 - Stack Count");
printf(" 8 - Destroy stack");

create();

while (1)
{
printf(" Enter choice : ");
scanf("%d", &ch);

switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf(" Top element : %d", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}

/* Create empty stack */
void create()
{
top = NULL;
}

/* Count stack elements */
void stack_count()
{
printf(" No. of elements in stack : %d", count);
}

/* Push data into stack */
void push(int data)
{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}

/* Display stack elements */
void display()
{
top1 = top;

if (top1 == NULL)
{
printf("Stack is empty");
return;
}

while (top1 != NULL)
{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}

/* Pop Operation on stack */
void pop()
{
top1 = top;

if (top1 == NULL)
{
printf(" Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf(" Popped value : %d", top->info);
free(top);
top = top1;
count--;
}

/* Return top element */
int topelement()
{
return(top->info);
}

/* Check if stack is empty or not */
void empty()
{
if (top == NULL)
printf(" Stack is empty");
else
printf(" Stack is not empty with %d elements", count);
}

/* Destroy entire stack */
void destroy()
{
top1 = top;

while (top1 != NULL)
{
top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
}
free(top1);
top = NULL;

printf(" All stack elements destroyed");
count = 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote