DATA STRUCTURE ; LESSON: 1 ; STACK WITH EXAMPLE UING C PROGRAM
Data Structure :: Lesson 1:- Stack
Question-1: What Is Data Structure:
Organaised Calulation of data in a particular format is called Data Structre.
It is a technique or method of a studey , how the data are interrelated to each other , logically or mathametically.
Classification of Data Structure
There are three types of Data Structure
- Linear And Non Linear Data Structure
- Homogenies and hetrogenies Data Structure
- Static and Dynemic Data Structure
What is Stack ?
Stack is a collection of data item where the insertion and delation takes place on one end called top of the stacks.
In Stack we can perform two operations that is push and pop . push means inserting a new item into a Stack and pop means Deleting an item from the stack.
Stack always perform the LIFO oparations,(LIFO means- Last In First Out)
//Programme Of Stack Using ARRAY In C
#include<stdio.h>
#define size 5
int stack[size];
int top=-1;
//push
int push(int value)
{
if(top==size-1)
{
printf("The Stack is Full");
}
else{
top++;
stack[top]=value;
}
}
//pop
int pop()
{
if(top==-1)
{
printf("The stack is Empty");
}
else{
top--;
}
}
//display
int display()
{
if(top==-1)
{
printf("It's Empty");
}
else{
for(int i=top;i>=0;i--)
{
printf("%d",stack[i]);
}
}
}
//main of the stack
{
int value,c;
while(1)
{
printf("1 for push\n");
printf("2 for pop\n");
printf("3 for display\n");
printf("4 for exit\n");
printf("Enter Your Choice : ");
scanf("%d",&c);
switch(c)
{
case 1:
scanf("%d",&value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return(0);
}
}
}
#include<stdio.h>
#define size 5
int stack[size];
int top=-1;
//push
int push(int value)
{
if(top==size-1)
{
printf("The Stack is Full");
}
else{
top++;
stack[top]=value;
}
}
//pop
int pop()
{
if(top==-1)
{
printf("The stack is Empty");
}
else{
top--;
}
}
//display
int display()
{
if(top==-1)
{
printf("It's Empty");
}
else{
for(int i=top;i>=0;i--)
{
printf("%d",stack[i]);
}
}
}
//main of the stack
{
int value,c;
while(1)
{
printf("1 for push\n");
printf("2 for pop\n");
printf("3 for display\n");
printf("4 for exit\n");
printf("Enter Your Choice : ");
scanf("%d",&c);
switch(c)
{
case 1:
scanf("%d",&value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return(0);
}
}
}
Comments
Post a Comment