0% found this document useful (0 votes)
18 views

Implementing Stack in C

Stack

Uploaded by

kranshu60416041
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Implementing Stack in C

Stack

Uploaded by

kranshu60416041
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Implementing Stack in C

1. Implement stacks using arrays in c

#include <stdio.h>
#include <stdbool.h>

#define MAX 100 // Define the maximum size of the stack

// Global variables for stack


int stack[MAX];
int top = -1; // Initial state of the stack

// Function prototypes
void initStack();
bool isEmpty();
bool isFull();
void push(int value);
int pop();
int peek();
void printStack();
void menu();

void initStack() {
top = -1; // Set top to -1 indicating an empty stack
}

bool isEmpty() {
return top == -1;
}

bool isFull() {
return top == MAX - 1;
}

void push(int value) {


if (isFull()) {
printf("Stack overflow\n");
return;
}
stack[++top] = value; // Increment top and add value to stack
printf("%d pushed to stack\n", value);
}

int pop() {
if (isEmpty()) {
printf("Stack underflow\n");
return -1; // Return -1 to indicate stack is empty
}
return stack[top--]; // Return the top element and decrement top
}

int peek() {
if (isEmpty()) {
printf("Stack is empty\n");
return -1; // Return -1 to indicate stack is empty
}
return stack[top]; // Return the top element
}

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

void menu() {
int choice, value;

while (1) {
printf("\nStack Operations Menu:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
value = pop();
if (value != -1) {
printf("%d popped from stack\n", value);
}
break;
case 3:
value = peek();
if (value != -1) {
printf("Top element is %d\n", value);
}
break;
case 4:
printStack();
break;
case 5:
printf("Exiting...\n");
return;
default:
printf("Invalid choice, please try again.\n");
}
}
}

int main() {
initStack();
menu();
return 0;
}

2. Implement stacks using Linked List in c


#include <stdio.h>
#include <stdlib.h>
// Structure for a node in the linked list
struct Node {
int data;
struct Node *next;
};

// Structure for the stack


struct Stack {
struct Node *top;
};

// Function to create a new node


struct Node *createNode(int data) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to initialize an empty stack


void initialize(struct Stack *stack) {
stack->top = NULL;
}

// Function to check if the stack is empty


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

// Function to push an element onto the stack


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

// Function to pop an element from the stack


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

// Function to peek at the top element of the stack


int peek(struct Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
exit(1);
}
return stack->top->data;
}

// Function to display the contents of the stack


void display(struct Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return;
}
struct Node *temp = stack->top;
printf("Stack elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

int main() {
struct Stack stack;
initialize(&stack);
int choice, data;

while (1) {
printf("\n1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter data to push: ");
scanf("%d", &data);
push(&stack, data);
break;
case 2:
data = pop(&stack);
printf("%d popped from stack\n", data);
break;
case 3:
data = peek(&stack);
printf("Top element: %d\n", data);
break;
case 4:
display(&stack);
break;
case 5:
exit(0);
default:
printf("Invalid choice\n");
}
}

return 0;
}

You might also like