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

Programs On STACK Operations

The document provides two C programs for stack operations using arrays and linked lists. Each program includes functions for pushing, popping, peeking, and displaying stack elements, along with a main menu for user interaction. The array-based implementation has a fixed maximum size, while the linked list implementation allows for dynamic memory allocation.
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)
4 views5 pages

Programs On STACK Operations

The document provides two C programs for stack operations using arrays and linked lists. Each program includes functions for pushing, popping, peeking, and displaying stack elements, along with a main menu for user interaction. The array-based implementation has a fixed maximum size, while the linked list implementation allows for dynamic memory allocation.
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

Program to perform following operations on STACK using arrays

[Link]
[Link]
[Link]
[Link]

#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int s[MAX],top=-1;

//Functions declaration
void pop();
void push(int );
void peek();
void display();

//main function
void main()
{
int choice,x;
while(1)
{
printf("----MAIN MENU----\n");
printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter number to be pushed:");
scanf("%d",&x);
push(x);
break;
case 2: pop();
break;
case 3: peek();
break;
case 4: display();
break;
case 5: exit(0);
break;
default:printf("Entered invalid choice\n");
break;
}
}
}

//Push operation
void push(int x)
{
if(top==MAX-1)
printf("Stack is overflow\n");
else
{
top=top+1;
s[top]=x;
}
}

//Pop operation
void pop()
{
if(top==-1)
printf("Stack is underflow/empty\n");
else
{
printf("The value deleted from the stack is : %d\n",s[top]);
top=top-1;
}
}

//Peek operation
void peek()
{
if(top==-1)
printf("Stack is underflow/empty\n");
else
printf("Top element is : %d\n",s[top]);
}

//Display operation
void display()
{
if(top==-1)
printf("Stack is underflow/empty\n");
else
{
for(int i=top;i>=0;i--)
printf("%d ",s[i]);
printf("\n");
}
}

Program to perform following operations on STACK using linked lists


[Link]
[Link]
[Link]
[Link]

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*top=NULL,*temp,*p;

//Push operation
struct node *push(struct node *top)
{
int n;
printf("Enter [Link] nodes you want in stack:");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
temp=(struct node*)malloc(sizeof(struct node));
int x;
printf("Enter data to push into stack:");
scanf("%d",&x);
temp->data=x;
temp->next=NULL;
if(top==NULL)
top=temp;
else
{
temp->next=top;
top=temp;
}
}
return top;
}

//Pop operation
struct node *pop(struct node *top)
{
if(top==NULL)
printf("Stack is empty\n");
else if(top->next==NULL)
{
temp=top;
top=temp->next;
free(temp);
}
else
{
temp=top;
top=temp->next;
free(temp);
}
return top;
}

//Peek operation
void peek(struct node *top)
{
if(top==NULL)
printf("Stack is empty\n");
else
printf("Top element : %d",top->data);
}

//Display operation
struct node *display(struct node *top)
{
if(top==NULL)
printf("Stack is empty\n");
else
{
p=top;
while(p!=NULL)
{
printf("%d",p->data);
if(p->next!=NULL)
printf(" -> ");
p=p->next;
}
}
return top;
}

//main function
void main()
{
int choice;
while(1)
{
printf("\n----MAIN MENU----\n");
printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: top=push(top);
break;
case 2: top=pop(top);
break;
case 3: peek(top);
break;
case 4: top=display(top);
break;
case 5: exit(0);
break;
default:printf("Entered invalid choice\n");
break;
}
}
}

You might also like