Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Week 1

The document provides two implementations of a stack data structure: one using arrays and the other using linked lists. Each implementation includes functions for initializing the stack, pushing and popping elements, checking if the stack is empty or full, and displaying the stack contents. The main function demonstrates how to interact with the stack through a menu-driven interface.

Uploaded by

md sameer
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 views7 pages

Week 1

The document provides two implementations of a stack data structure: one using arrays and the other using linked lists. Each implementation includes functions for initializing the stack, pushing and popping elements, checking if the stack is empty or full, and displaying the stack contents. The main function demonstrates how to interact with the stack through a menu-driven interface.

Uploaded by

md sameer
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/ 7

Week 1:

Menu-Driven Stack Implementation using Arrays:


#include <stdio.h>

#define MAX_SIZE 100

typedef struct {
int arr[MAX_SIZE];
int top;
int maxSize;
} Stack;

void initStack(Stack *s, int size) {


s->top = -1;
s->maxSize = size;
}

int isEmpty(Stack *s) {


return s->top == -1;
}

int isFull(Stack *s) {


return s->top == s->maxSize - 1;
}

void push(Stack *s, int element) {


if (isFull(s)) {
printf("Stack Overflow\n");
return;
}
s->arr[++s->top] = element;
printf("Pushed: %d\n", element);
}

int pop(Stack *s) {


if (isEmpty(s)) {
printf("Stack Underflow\n");
return -1;
}
int element = s->arr[s->top--];
printf("Popped: %d\n", element);
return element;
}

void display(Stack *s) {


if (isEmpty(s)) {
printf("Stack is empty\n");
return;
}
printf("Stack elements: ");
for (int i = s->top; i >= 0; i--) {
printf("%d", s->arr[i]);
if (i > 0) printf(" ");
}
printf("\n");
}

void checkEmpty(Stack *s) {


if (isEmpty(s)) {
printf("Stack is empty\n");
} else {
printf("Stack is not empty\n");
}
}

void checkFull(Stack *s) {


if (isFull(s)) {
printf("Stack is full\n");
} else {
printf("Stack is not full\n");
}
}

void topElement(Stack *s) {


if (isEmpty(s)) {
printf("Stack is empty\n");
return;
}
printf("Top element: %d\n", s->arr[s->top]);
}

void elementCount(Stack *s) {


printf("Number of elements: %d\n", s->top + 1);
}
int main() {
Stack stack;
int size, choice, element;

scanf("%d", &size);
if (size <= 0 || size > MAX_SIZE) {
return 1;
}

initStack(&stack, size);

while (1) {
scanf("%d", &choice);

switch (choice) {
case 1: // Push
scanf("%d", &element);
push(&stack, element);
break;

case 2: // Pop
pop(&stack);
break;

case 3: // Display
display(&stack);
break;

case 4: // IsEmpty
checkEmpty(&stack);
break;

case 5: // IsFull
checkFull(&stack);
break;

case 6: // TopElement
topElement(&stack);
break;

case 7: // ElementCount
elementCount(&stack);
break;

case 8: // Exit
return 0;

default:
continue;
}
}

return 0;
}

Stack Implementation using Linked List

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

struct Node {
int data;
struct Node* next;
};

struct Stack {
struct Node* top;
};

// Initialize stack
struct Stack* createStack() {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->top = NULL;
return stack;
}

// Check if stack is empty


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

// Push element onto stack


void push(struct Stack* stack, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = stack->top;
stack->top = newNode;
printf("Pushed %d\n", data);
}

// Pop element from stack


void pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack Underflow\n");
return;
}
struct Node* temp = stack->top;
int popped = temp->data;
stack->top = temp->next;
free(temp);
printf("%d\n", popped);
}

// Peek at top element


void peek(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack Empty\n");
return;
}
printf("%d\n", stack->top->data);
}

// Display stack
void display(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack Empty\n");
return;
}
struct Node* current = stack->top;
while (current->next != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("%d\n", current->data);
}

// Free stack memory


void freeStack(struct Stack* stack) {
struct Node* current = stack->top;
while (current != NULL) {
struct Node* temp = current;
current = current->next;
free(temp);
}
free(stack);
}

int main() {
int n, operation, data;
scanf("%d", &n);

struct Stack* stack = createStack();

while (n--) {
scanf("%d", &operation);
switch (operation) {
case 1: // Push
scanf("%d", &data);
push(stack, data);
break;

case 2: // Pop
pop(stack);
break;

case 3: // Peek
peek(stack);
break;

case 4: // Display
display(stack);
break;

case 5: // IsEmpty
printf("%s\n", isEmpty(stack) ? "Yes" : "No");
break;
}
}

freeStack(stack);
return 0;
}

You might also like