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

Stack Implementation in C: Array & Linked List

The document provides two implementations of a stack: one using an array and the other using a linked list. The array implementation allows for basic stack operations such as push, pop, and display, while the linked list implementation focuses on the push operation. Both implementations include user interaction for entering values and displaying stack contents.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Stack Implementation in C: Array & Linked List

The document provides two implementations of a stack: one using an array and the other using a linked list. The array implementation allows for basic stack operations such as push, pop, and display, while the linked list implementation focuses on the push operation. Both implementations include user interaction for entering values and displaying stack contents.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

ARRAY IMPLEMENTATION
USING STACK
//stack using array
#include<stdio.h>
#include<conio.h>
int stack[100],choice,n,top,x,i;
void push();
void pop();
void display();
void main()
{
//clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t [Link]\n\t [Link]\n\t [Link]\n\t [Link]");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
getch();
}
}
while(choice!=4);
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
getch();
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
OUTPUT
C:\Users\shash\OneDrive\Desktop\ds lab\stack\output> & .\'a program to
implement stack using [Link]'

Enter the size of STACK[MAX=100]:10

STACK OPERATIONS USING ARRAY


--------------------------------
[Link]
[Link]
[Link]
[Link]
Enter the Choice:1
Enter a value to be pushed:12

Enter the Choice:1


Enter a value to be pushed:24

Enter the Choice:1


Enter a value to be pushed:98

Enter the Choice:3

The elements in STACK

98
24
12
Press Next Choice
Enter the Choice:2

The popped elements is 98


Enter the Choice:3

The elements in STACK

24
12
Press Next Choice
Enter the Choice:4

EXIT POINT
PS C:\Users\shash\OneDrive\Desktop\ds lab\stack\output>

[Link] OF STACK
USING LINKED LIST
#include <stdio.h>
#include <stdlib.h>

// Define the structure for a node


struct node {
int val;
struct node* next;
};

// Define the head pointer (top of the stack)


struct node* head = NULL;

// Function to push an element onto the stack


void push() {
int val;
// Allocate memory for the new node
struct node* ptr = (struct node*)malloc(sizeof(struct node));

// Check if memory allocation failed


if (ptr == NULL) {
printf("Unable to push the element. Memory allocation failed.\n");
return; // Exit the function if memory allocation fails
}

// Prompt user for input


printf("Enter the value to push: ");
scanf("%d", &val);

// Assign the input value to the new node


ptr->val = val;

// If the stack is empty, the new node becomes the top node
if (head == NULL) {
ptr->next = NULL; // Set next to NULL as there are no other nodes
head = ptr; // Set head to the new node
} else {
// Otherwise, insert the new node at the top of the stack
ptr->next = head; // Link the new node to the current top
head = ptr; // Update head to the new node
}

// Print confirmation
printf("Item %d pushed to stack\n", val);
}

// Main function to test the push operation


int main() {
push(); // Call push to add an element to the stack
push(); // Call push again to add another element

return 0;
}

OUTPUT
PS C:\Users\shash\OneDrive\Desktop\ds lab\stack\output> & .\'implementation of
stck using linked [Link]'
Enter the value to push: 23
Item 23 pushed to stack
Enter the value to push: 76
Item 76 pushed to stack
PS C:\Users\shash\OneDrive\Desktop\ds lab\stack\output>

You might also like