Implementation of Stack Using Arrays
Implementation of Stack Using Arrays
stack is an ordered collection of data items in which insertion and deletion operations are
performed at one end( top of the stack).Stack is represented by array S with MAX size, TOP is a
stack pointer, which specifies status of the stack.
Stack works under Last In First Out (LIFO) principle. Ex: A coin stacker.
FUNCTIONS USED:
pop() int s []
VARIABLES USED
Return
endif
TOP TOP + 1
S[TOP] ITEM
Return
Y Element deleted
if TOP = 0 then
Return
endif
2. //Retrieve the top element of stack//
Y (S[TOP])
TOP TOP-1
Return(Y)
PROGRAM:
#include<stdio.h>
#include<conio.h>
int top=-1;
int s[10];
void main()
int ch;
int item,y;
clrscr ( ) ;
printf("\n\t1. Push");
printf("\n\t 3.Display.");
printf("\n\t 4.Exit");
do
if (ch>4||ch<=0)
break;
switch(ch)
case 1 :
scanf("%d",& item );
push(s, item );
break;
case 2:
y=pop ( s ) ;
break;
case 3 :
display();
break;
case 4 :
exit(0);
break;
}while(ch<=4);
if(top>10)
else
top=top+ 1 ;
s[top]= item ;
} }
int x;
if(top==-1)
else
{ x=s[top];
top--;
return x;
void display()
{
int i;
for(i=top;i>=0;i--)
}
QUEUES:
Queue is an ordered list of data items such that new item inserts in to a queue at
rear end, and deletes an item from queue at front end. Queue is represented with an array Q of
MAX size ,FRONT and REAR are the front and rear pointers of the queue.
FUNCTIONS USED:
deletion () int q
display() void q
VARIABLES USED:
ALGORITHM :
Return
endif
RR+ 1
if F= 0 then
F 1
endif
return
Y Temporary variable
if F 0 then
Print ('Queue Underflow')
Return
endif
2. //Delete the queue element at front end and store it into item//
YQ[F]
3. //If queue is empty after deletion, set front and rear pointers to 0//
if F = R then
F0
R0
else F F+ 1
Return (Y)
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 6
int front=0,rear=0;
int q[size];
main()
int ch ;
void insert();
void delete();
void display();
clrscr();
while (1)
printf ("\n1.Insert\n2.Delete\n3.display\n4.exit\n");
scanf ("%d",&ch);
switch (ch)
case 1 : insert();
break;
case 2 : delete();
break;
case 3 : display();
break ;
case 4 : exit(0);
}}
void insert()
int m;
if(rear==size)
printf("queue overflow\n");
else
scanf("%d",&m);
q[rear]=m;
rear=rear+1;
}}
void delete()
int n;
if(front==rear)
printf("queue underflow\n");
else
n=q[front];
front=front +1;
}}
void display()
int i;
for(i=front;i<rear;i++)
{
printf("\n%d\n",q[i]);
}}
#include<stdio.h>
#include<conio.h>
int r=-1;
int f=-1;
void insert(int[],int,int);
int deletion(int[]);
void display(int[]);
void main ( )
char ch;
int y,item;
int n,q[10];
clrscr () ;
printf("\n\t MENU");
printf("\n\t1. Insert.");
printf("\n\t 2.Delete.");
printf("\n\t 3.Display.");
printf("\n\t 4. Exit.");
do
break;
switch(ch)
case 1 :
scanf("%d",& item ) ;
insert(q,10, item ) ;
break;
case 2 :
y=deletion(q) ;
break;
case 3 :
display (q) ;
break;
case 4:
exit (0);
}while(ch<=4) ;
getch () ;
if (r>=n)
return;
else
r=r+1;
q[r]= item;
if (f==-1)
f=0;
int y;
if (f = =-1)
y=q[f] ;
if (f==r)
f=r=-1;
else
f++;
return y;
int i;
for (i=f;i<=r;i++)
printf("\t%d",q[i]);
getch();
OUTPUT:
MENU
1. Insert.
2.Delete.
3.Display.
4. Exit.
Enter ur choice 1
Enter ur choice 1
Enter ur choice 1
23 12 45
The Fibonacci sequence of numbers is 1,1,2,3,5,8 based on the recurrence relation
F(n)=F(n-1 )+F(n-2) for n>2. Write c program using do-while to calculate and print the
first m Fibonacci number.ITERATIVE TECHNIQUE
VARIABLES USED:
ALGORITHM:
integer n,i,fib1,fib2,fib;
fib1=0,fib2=1, i = 1
Read (n)
Print( fib1,fib2)
loop
fib= fibl+fib2;
fib1=fib2 ;
fib2=fib;
Print(fib)
i=i+1
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main ( )
{
clrscr();
scanf("%d",&n);
printf("\n\t %d %d",fib1,fib2);
do
fib=fib1+fib2;
fib1=fib2 ;
fib2=fib;
printf("\t %d",fib);
i++;
} while (i<n) ;
getch () ;
OUTPUT:
#include<stdio.h>
#include<conio.h>
void main()
int n,f,i;
clrscr();
scanf("%d",&n);
f=fib(i);
printf("%d\t",f);
getch();
int fib(int n)
if(n==0)
return 0;
else if( n==1)
return 1;
else
return(fib(n-1) + fib(n-2));
Output :-
fibonacci series is
0 1 1 2 3 5 8 13
/* factorial of a number using recursion */
#include<stdio.h>
#include<conio.h>
void main()
long fact(int);
int n;
long f;
clrscr();
scanf("%d",&n);
f=fact(n);
getch();
long fact(int n)
if(n==0)
return 1;
else
return(n*fact(n-1));
Output :-
factorial = 120
/* sum of the series 1+2+3+...+n using recursion */
#include<stdio.h>
#include<conio.h>
void main()
int n,s;
int sum(int);
clrscr();
scanf("%d",&n);
s=sum(n);
getch();
int sum(int n)
if (n==1)
return 1;
else
return(n+sum(n-1));
Output :-
#include<stdio.h>
void main()
clrscr();
scanf("%d", &n);
scanf("%d",&a[i]);
ptr=a;
small=*ptr;
for(i=1;i<n;i++)
if(*(ptr+i)<small)
small = *(ptr+i);
getch();
}
Output :-
10 56 34 23 89
small element is 10