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

Array&LL Implementation of Stack

This document defines a stack data structure implemented using an array and includes functions to push and pop values onto/off the stack. It also defines a linked list implementation of a stack with functions to push, pop and display the stack. The main function demonstrates pushing values onto the linked list stack, popping values off and displaying the stack after each operation.

Uploaded by

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

Array&LL Implementation of Stack

This document defines a stack data structure implemented using an array and includes functions to push and pop values onto/off the stack. It also defines a linked list implementation of a stack with functions to push, pop and display the stack. The main function demonstrates pushing values onto the linked list stack, popping values off and displaying the stack after each operation.

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>

#define MAX 10

int stack[MAX];
int top = -1;

void push(int value) {


if (top == MAX - 1) {
printf("Error: Stack overflow!\n");
return;
}
stack[++top] = value;
}

int pop() {
if (top == -1) {
printf("Error: Stack underflow!\n");
return -1;
}
return stack[top--];
}

int main() {
int choice, value;
while (1) {
printf("1. Push\n2. Pop\n3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;

case 2:
value = pop();
if (value != -1) {
printf("Popped value: %d\n", value);
}
break;

case 3:
exit(0);

default:
printf("Invalid choice!\n");
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>

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

struct Node *head = NULL;

void push(int val)


{
//create new node
struct Node *newNode = malloc(sizeof(struct Node));
newNode->data = val;

//make the new node points to the head node


newNode->next = head;

//make the new node as head node


//so that head will always point the last inserted data
head = newNode;
}

void pop()
{
//temp is used to free the head node
struct Node *temp;

if(head == NULL)
printf("Stack is Empty\n");
else
{
printf("Poped element = %d\n", head->data);

//backup the head node


temp = head;

//make the head node points to the next node.


//logically removing the node
head = head->next;

//free the poped element's memory


free(temp);
}
}

//print the linked list


void display()
{
struct Node *temp = head;
//iterate the entire linked list and print the data
while(temp != NULL)
{
printf("%d->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main()
{
push(10);
push(20);
push(30);
printf("Linked List\n");
display();
pop();
printf("After the pop, the new linked list\n");
display();
pop();
printf("After the pop, the new linked list\n");
display();

return 0;
}

You might also like