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

Stack

The document presents two implementations of a stack data structure: one using arrays and the other using linked lists. Each implementation includes functions for basic stack operations such as push, pop, peep, and display, along with a main function to interact with the user. The array-based implementation checks for overflow and underflow conditions, while the linked list implementation manages memory dynamically.

Uploaded by

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

Stack

The document presents two implementations of a stack data structure: one using arrays and the other using linked lists. Each implementation includes functions for basic stack operations such as push, pop, peep, and display, along with a main function to interact with the user. The array-based implementation checks for overflow and underflow conditions, while the linked list implementation manages memory dynamically.

Uploaded by

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

Stack implementation using arrays.

#include <stdio.h>
#define SIZE 10
int top = -1, arr[SIZE];

void push();
void pop();
void peep();
void display();

int main()
{
int choice;
printf("Operations of Stack:\n");
printf("1.Push\n2.Pop\n3.Peep\n4.Display\n5.Exit\n");
while(1)
{
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1: push(); break;
case 2: pop(); break;
case 3: peep(); break;
case 4: display(); break;
case 5: return 0;
default: printf("\nInvalid choice, try again.\n");
}
}
}

void push()
{
int n;
if(top == SIZE - 1)
{
printf("\nStack overflow\n");
}
else
{
printf("Enter the element to be added onto the stack: ");
scanf("%d", &n);
top = top + 1;
arr[top] = n;
}
}

void pop()
{
if(top == -1) //w.s.enmpty
{
printf("\nStack underflow\n");
}
else
{
printf("Popped element: %d\n", arr[top]);
top = top - 1;
}
}

void peep()
{
if(top == -1)
{
printf("\nStack is empty, nothing to peep.\n");
}
else
{
printf("Top element of the stack: %d\n", arr[top]);
}
}

void display()
{
int i;
if(top == -1)
{
printf("\nStack is empty (underflow)\n");
}
else
{
printf("\nElements present in the stack:\n");
for(i = top; i >= 0; i--)
{
printf("%d\t", arr[i]);
}
}
}
Stack implementation using linked list.

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

struct Node
{
int data;
struct Node* next;
};
struct Node* top = NULL;

void push(int item);


void pop();
void peep();
void display();

int main()
{
int choice, item;

printf("Operations of Stack:\n");
printf("1. Push\n2. Pop\n3. Display\n4. Peep\n5. Exit\n");

while (1)
{
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1: printf("Enter the item to push: ");
scanf("%d", &item);
push(item); break;
case 2: pop(); break;
case 3: peep(); break;
case 4: display(); break;
case 5: return 0;
default: printf("Invalid choice.\n");
}
}
}

void push(int item)


{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

if (newNode == NULL) // Check if memory allocation fails


{
printf("\nMemory overflow\n");
return;
}
newNode->data = item;
newNode->next = top; // Point the new node's next to the current top
top = newNode; // Make the new node the new top
printf("Item %d pushed to stack.\n", item);
}

void pop()
{
if (top == NULL) // Check if the stack is empty
{
printf("\nStack underflow\n");
}
else
{
struct Node* temp = top;
printf("Popped item: %d\n", temp->data);
top = top->next; // Move the top pointer to the next node
free(temp); // Free the memory of the popped node
}
}

void peep()
{
if (top == NULL)
{
printf("\nStack is empty, nothing to peep.\n");
}
else
{
printf("Top item: %d\n", top->data);
}
}

void display()
{
if (top == NULL) // Check if the stack is empty
{
printf("\nStack is empty (underflow)\n");
}
else
{
struct Node* temp = top;
printf("\nElements present in the stack:\n");
while (temp != NULL)
{
printf("%d\t", temp->data);
temp = temp->next;
}
printf("\n");
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy