Stack ADT

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

#include<stdio.

h>
//Pre-processor macro
#define stackCapacity 5

int stack[stackCapacity], top=-1;


void push(int);
int pop(void);
int isFull(void);
int isEmpty(void);
void traverse(void);
void atTop(void);

//Main function of the program


void main(void)
{
int choice, stackItem;
//Always true While loop for continue iteration
while(1){
//Instructions to the user
printf("Stack Operation n");
printf("Enter `1` for Push Operation n");
printf("Enter `2` for Pop Operation n");
printf("Enter `3` for atTop Operation n");
printf("Enter `4` for Traverse Operation n");
printf("Enter `5` for Quit Operation n");
printf("Enter your choice : ");
scanf("%d",&choice);
//Switch Case to do the user specified task
switch(choice){
case 1:
printf("Enter a integer value : ");
scanf("%d",&stackItem);
push(stackItem);
break;
case 2:
stackItem = pop();
if(stackItem == 0){
printf("Your stack is underflow");
}else{
printf("Last popped item : %dn", stackItem);
}
break;
case 3:
atTop();
break;
case 4:
traverse();
break;
case 5:
return;
break;
default: printf("Please enter correct choice : ");
}
}
}

//Push Function to insert element into the stack


void push(int stackElement)
{
if(isFull()){
printf("Stack is full.It can't be overflowed. n");
}else{
top++;
stack[top] = stackElement;
printf("%d has been pushed n", stackElement);
}
}

//Function to check, Is stack full?


int isFull(){
if(top == stackCapacity-1){
return 1;
}else{
return 0;
}
}

//Function to check, Is stack empty?


int isEmpty(){
if(top == -1){
return 1;
}else{
return 0;
}
}

//Function to pop last element of the stack


int pop(){
if(isEmpty()){
return 0;
}else{
return stack[top--];
}
}

//function to check, Which element on the top?


void atTop()
{
if(isEmpty())
{
printf("Your Stack is empty n");
}else{
printf("Element at top is : %d n", stack[top]);
}
}

//Function to display the all the characters elements of stack


void traverse(){
if(isEmpty())
{
printf("Your Stack is empty n");
}else{
int i;
printf("Stack Elements are : n");
for(i=0; i <= top; i++){
printf("%d n", stack[i]);
}
}
}

Why use a stack to reverse a linked list?


Ans. Using a stack allows you to reverse the order of the nodes efficiently. The
stack keeps track of the nodes in reverse order, and by popping the nodes from the
stack and updating the pointers, you can reverse the linked list.
Q2: Are there any alternative methods to reverse a linked list?
Ans. Yes, there are other methods to reverse a linked list. Some common
approaches include using three-pointers to reverse the pointers of each node
iteratively or recursively, or by using a technique called "in-place" reversal where
you manipulate the pointers directly without using any additional data structure.

Q3: What are the advantages of using a stack for reversing a linked list?
Ans. Using a stack simplifies the process of reversing the linked list by taking
advantage of the LIFO property. It provides an intuitive and straightforward
solution that can be easily implemented.

Q4: Are there any drawbacks to using a stack for reversing a linked list?
Ans. Using a stack requires additional space to store the nodes, resulting in
increased space complexity. If the linked list is extremely large, it can lead to
memory limitations. In such cases, alternative methods like in-place reversal may
be more suitable.

Q5: Can a linked list with a cycle be reversed using a stack?


Ans. No, a linked list with a cycle cannot be reversed using a stack. The stack
approach relies on traversing the linked list from start to end, which is not possible
when there is a cycle in the list. It would lead to an infinite loop.

You might also like