0% found this document useful (0 votes)
4 views5 pages

Que 1 C

This document discusses the stack as an Abstract Data Type (ADT) in C, highlighting its Last In First Out (LIFO) principle and detailing operations such as push, pop, peek, isEmpty, and size. It includes C code implementations for stack operations and demonstrates the application of stacks in infix to postfix expression conversion and recursion. Additionally, it outlines future applications of stacks in memory management, undo/redo operations, expression evaluation, and more.

Uploaded by

xapidel741
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Que 1 C

This document discusses the stack as an Abstract Data Type (ADT) in C, highlighting its Last In First Out (LIFO) principle and detailing operations such as push, pop, peek, isEmpty, and size. It includes C code implementations for stack operations and demonstrates the application of stacks in infix to postfix expression conversion and recursion. Additionally, it outlines future applications of stacks in memory management, undo/redo operations, expression evaluation, and more.

Uploaded by

xapidel741
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Stack as an Abstract Data Type (ADT) in C

A stack is a linear data structure that follows the Last In First Out (LIFO) principle. The last
element pushed into the stack is the first one to be popped out.

Stack Operations (Procedure and Algorithms)


In the C language, a stack can be implemented using an array or a linked list. We will implement a
stack using an array for simplicity.
The stack will support the following operations:
• Push: Adds an element to the top of the stack.
• Pop: Removes the element from the top of the stack.
• Peek: Returns the top element without removing it.
• IsEmpty: Checks if the stack is empty.
• Size: Returns the current size of the stack.

Stack Implementation in C
Here is the C code to implement a stack using an array:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX 10 // Maximum size of the stack

// Stack structure
struct Stack {
int items[MAX];
int top;
};

// Function to initialize the stack


void initStack(struct Stack *stack) {
stack->top = -1; // Indicates that the stack is empty
}

// Function to check if the stack is empty


bool isEmpty(struct Stack *stack) {
return stack->top == -1;
}

// Function to check if the stack is full


bool isFull(struct Stack *stack) {
return stack->top == MAX - 1;
}

// Function to push an element onto the stack


void push(struct Stack *stack, int value) {
if (isFull(stack)) {
printf("Stack overflow\n");
return;
}
stack->items[++stack->top] = value;
printf("Pushed %d to stack\n", value);
}

// Function to pop an element from the stack


int pop(struct Stack *stack) {
if (isEmpty(stack)) {
printf("Stack underflow\n");
return -1; // Indicating empty stack
}
return stack->items[stack->top--];
}

// Function to peek at the top element


int peek(struct Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return -1; // Indicating empty stack
}
return stack->items[stack->top];
}

// Function to get the size of the stack


int size(struct Stack *stack) {
return stack->top + 1;
}

int main() {
struct Stack stack;
initStack(&stack);

push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
printf("Top element is %d\n", peek(&stack));
printf("Stack size is %d\n", size(&stack));

printf("Popped element is %d\n", pop(&stack));


printf("Top element is %d\n", peek(&stack));
printf("Stack size is %d\n", size(&stack));

return 0;
}

Explanation:
• initStack(): Initializes the stack by setting the top to -1, indicating that the stack is empty.
• push(): Adds an element to the stack. It checks if the stack is full before pushing.
• pop(): Removes and returns the top element of the stack. It checks if the stack is empty
before popping.
• peek(): Returns the top element of the stack without removing it.
• isEmpty(): Checks whether the stack is empty.
• isFull(): Checks whether the stack is full.
• size(): Returns the current size of the stack (i.e., the number of elements in the stack).

Application of Stack in Notation Conversion (Infix to Postfix)


Stacks are commonly used in expression notation conversion, such as converting infix expressions
(which humans generally use) into postfix or prefix expressions (which computers can process more
easily).
Infix to Postfix Conversion Algorithm:
1. Traverse the infix expression from left to right.
2. If the character is an operand, append it to the output.
3. If the character is an operator, pop operators from the stack to the output until the stack's top
operator has less precedence or the stack is empty, then push the current operator.
4. If the character is a parenthesis, push it onto the stack. For a closing parenthesis, pop from
the stack until an opening parenthesis is encountered.
Here’s an implementation of Infix to Postfix conversion in C using the stack:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define MAX 50

// Stack structure
struct Stack {
char items[MAX];
int top;
};

// Initialize the stack


void initStack(struct Stack *stack) {
stack->top = -1;
}

// Check if the stack is empty


int isEmpty(struct Stack *stack) {
return stack->top == -1;
}

// Push an item onto the stack


void push(struct Stack *stack, char c) {
if (stack->top == MAX - 1) {
printf("Stack overflow\n");
return;
}
stack->items[++stack->top] = c;
}

// Pop an item from the stack


char pop(struct Stack *stack) {
if (isEmpty(stack)) {
return -1;
}
return stack->items[stack->top--];
}

// Peek the top element


char peek(struct Stack *stack) {
if (isEmpty(stack)) {
return -1;
}
return stack->items[stack->top];
}

// Check if the character is an operator


int isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
}

// Check operator precedence


int precedence(char c) {
if (c == '+' || c == '-') return 1;
if (c == '*' || c == '/') return 2;
return 0;
}

// Infix to Postfix conversion


void infixToPostfix(char *infix) {
struct Stack stack;
initStack(&stack);
int i = 0, j = 0;
char postfix[MAX];

while (infix[i] != '\0') {


if (isalnum(infix[i])) { // If the character is an operand
postfix[j++] = infix[i];
}
else if (infix[i] == '(') { // Left parenthesis
push(&stack, infix[i]);
}
else if (infix[i] == ')') { // Right parenthesis
while (!isEmpty(&stack) && peek(&stack) != '(') {
postfix[j++] = pop(&stack);
}
pop(&stack); // Pop '('
}
else if (isOperator(infix[i])) { // Operator
while (!isEmpty(&stack) && precedence(peek(&stack)) >=
precedence(infix[i])) {
postfix[j++] = pop(&stack);
}
push(&stack, infix[i]);
}
i++;
}

while (!isEmpty(&stack)) { // Pop remaining operators


postfix[j++] = pop(&stack);
}
postfix[j] = '\0';
printf("Postfix Expression: %s\n", postfix);
}

int main() {
char infix[MAX];
printf("Enter infix expression: ");
scanf("%s", infix);

infixToPostfix(infix);

return 0;
}

Explanation of Infix to Postfix Conversion:


1. The infixToPostfix() function reads each character from the infix expression.
2. If it's an operand, it's directly added to the output.
3. If it's an operator, the stack is checked for operators with higher or equal precedence, and
they are popped onto the output.
4. Parentheses are handled appropriately with the stack.
5. Finally, the remaining operators in the stack are popped into the output.
Application of Stack in Recursion
In recursion, the function calls are managed using the call stack. The stack keeps track of the
function calls, the local variables, and the return address. Each recursive call adds a new frame to
the stack, and when the base case is reached, the stack begins to unwind.
For example, in a factorial calculation using recursion:
#include <stdio.h>

int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1); // Recursive call
}

int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}

Here, each recursive call adds a new frame to the stack, and when the base case (n == 0) is
reached, the function calls start to return, unwinding the stack.

Future Applications of Stack in Computer Systems


1. Memory Management: Stacks are used in memory management to handle function calls,
local variables, and return addresses. Each function call is pushed onto the call stack.
2. Undo/Redo Operations: In applications like text editors, a stack is used to store previous
states to enable undo and redo functionalities.
3. Expression Evaluation: Stacks are crucial for evaluating expressions (like postfix or prefix
expressions) in calculators or interpreters.
4. Balanced Parentheses Checking: A stack can be used to verify whether an expression has
balanced parentheses or other delimiters.
5. Backtracking Algorithms: Stacks are used in backtracking algorithms, such as solving
mazes or puzzles (like the N-Queens problem), where the system needs to backtrack to a
previous state.
6. Depth-First Search (DFS): In graph traversal algorithms like DFS, a stack can be used to
keep track of the nodes to visit.

Conclusion
Stacks are fundamental data structures used in a variety of applications in computer systems.
Understanding their implementation, along with their application in notations conversion, recursion,
and other real-world problems, is essential for building efficient software systems.

You might also like