0% found this document useful (0 votes)
3 views4 pages

Stack Program 1

The document contains two implementations of stack data structures in C. The first implementation demonstrates basic stack operations such as push, pop, and peek, while the second implementation reverses a string using a stack. Both implementations include error handling for full and empty stack conditions.

Uploaded by

Aaditya Singh
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)
3 views4 pages

Stack Program 1

The document contains two implementations of stack data structures in C. The first implementation demonstrates basic stack operations such as push, pop, and peek, while the second implementation reverses a string using a stack. Both implementations include error handling for full and empty stack conditions.

Uploaded by

Aaditya Singh
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

STACK

Implementation of stack using structure


#include <stdio.h>
struct sta
{
int max_size;
int stack[8];
int top;
} s;
int isEmpty()
{
return ([Link] == -1);
}
int isFull()
{
return ([Link] == s.max_size - 1);
}
int peek()
{
return [Link][[Link]];
}
int pop()
{
int data;
if (!isEmpty())
{
data = [Link][[Link]];
[Link] = [Link] - 1;
return data;
}
else
{
printf("Stack is empty. Could not retrieve data.\n");
return -1; // Return a sentinel value to indicate an error condition.
}
}
void push(int data)
{
if (!isFull())

Prof Swetanjali Maharana


{
[Link] = [Link] + 1;
[Link][[Link]] = data;
}
else
{
printf("Stack is full. Cannot insert data.\n");
}
}
int main()
{
s.max_size = 8;
[Link] = -1;
push(3);
push(5);
push(9);
push(1);
push(12);
push(15);
printf("Element at the top: %d\n", peek());
printf("Elements in the stack:\n");
while (!isEmpty())
{
int data = pop();
printf("%d\n", data);
}
printf("stack full:%s\n",isFull()?"true":"false");
printf("stack empty:%s\n",isEmpty()?"true":"false");
return 0;
}

OUTPUT
lement at the top: 15
Elements in the stack:
15
12
1
9
5
3

Prof Swetanjali Maharana


stack full:false
stack empty:true

String reverse in Stack


#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
struct Stack {
int top;
char items[MAX_SIZE];
};
void initialize(struct Stack* stack)
{
stack->top = -1;
}
int isFull(struct Stack* stack)
{
return stack->top == MAX_SIZE - 1;
}
int isEmpty(struct Stack* stack)
{
return stack->top == -1;
}
void push(struct Stack* stack, char item)
{
if (isFull(stack))
{
printf("Stack is full. Cannot push.\n");
return;
}
stack->items[++stack->top] = item;
}
char pop(struct Stack* stack)
{
if (isEmpty(stack))
{
printf("Stack is empty. Cannot pop.\n");
return '\0';
}

Prof Swetanjali Maharana


return stack->items[stack->top--];
}

void reverseString(char* str)


{
struct Stack stack;
initialize(&stack);
int len = strlen(str);
for (int i = 0; i < len; i++)
{
push(&stack, str[i]);
}
for (int i = 0; i < len; i++)
{
str[i] = pop(&stack);
}
}
int main() {
char str[] = "Hello, World!";
printf("Original String: %s\n", str);

reverseString(str);

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

return 0;
}
OUTPUT
Original String: Hello, World!
Reversed String: !dlroW ,olleH

Prof Swetanjali Maharana

You might also like