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

Stack Implementation

This document contains code for implementing a stack using both a linked list and an array. For the linked list implementation, it defines functions for push, pop, and display operations on the stack. For the array implementation, it defines push, pop, and display functions and uses them in a main function to demonstrate the stack operations.

Uploaded by

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

Stack Implementation

This document contains code for implementing a stack using both a linked list and an array. For the linked list implementation, it defines functions for push, pop, and display operations on the stack. For the array implementation, it defines push, pop, and display functions and uses them in a main function to demonstrate the stack operations.

Uploaded by

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

Stack implementation (linked list)

#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;

int main ()
{
int choice=0;
printf("\n*********Stack operations using linked
list*********\n");
printf("\n----------------------------------------------\n");
while(choice != 4)
{
printf("\n\nChose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid choice ");
}
}
}
}
void push ()
{
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element");
}
else
{
printf("Enter the value");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;
}
printf("Item pushed");

}
}

void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");

}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}

Stack implementation(Array)

#include <stdio.h>
#define SIZE 10

int stack[SIZE];
int top = -1;
void push(int value)
{
if(top<SIZE-1)
{
if (top < 0)
{
stack[0] = value;
top = 0;
}
else
{
stack[top+1] = value;
top++;
}
}
else
{
printf("Stackoverflow!!!!\n");
}
}

int pop()
{
if(top >= 0)
{
int n = stack[top];
top--;
return n;
}
}

void display()
{
int i;
for(i=0;i<=top;i++)
{
printf("%d\n",stack[i]);
}
}

int main()
{
push(4);
push(8);
display();
pop();
display();
return 0;
}

You might also like