0% found this document useful (0 votes)
59 views5 pages

Stack Implementation Guide

The document discusses implementing a stack using arrays and pointers in C. For arrays, it defines functions to push, pop, and display elements in an array representing a stack. For pointers, it implements a stack using a linked list with nodes containing data and next pointer fields. Functions to push, pop and display the linked list stack are defined. The main function provides a menu to call these stack operations and uses a switch statement to call the corresponding functions.

Uploaded by

nawaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views5 pages

Stack Implementation Guide

The document discusses implementing a stack using arrays and pointers in C. For arrays, it defines functions to push, pop, and display elements in an array representing a stack. For pointers, it implements a stack using a linked list with nodes containing data and next pointer fields. Functions to push, pop and display the linked list stack are defined. The main function provides a menu to call these stack operations and uses a switch statement to call the corresponding functions.

Uploaded by

nawaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

4.

Write a program that implement stack (its operations) using


i) Arrays ii)Pointers

I) Arrays
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
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 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
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)");
}

}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");

}
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");
}
}

ii)pointers (Linked list)

#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int data;
struct node *next;
}*head,*ptr;
void main ()
{
int choice=0;
clrscr();
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;
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->data = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->data = val;
ptr->next = head;
head=ptr;

}
printf("Item pushed");

}
}

void pop()
{
if (head == NULL)
{
printf("Underflow");
}
else
{
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");

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

You might also like