STACK
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.
Push operation is not possible if stack is in overflow (i,e.TOP=MAX).
Pop operation is not possible if stack is in underflow (i.e. TOP=0)
Stacks are used for evaluation of simple expressions/postfix expressions, conversion of
infix, postfix,
prefix expressions and in recursion methods.
Implementation of stack using arrays:
FUNCTIONS USED:
Function name return type parameters
push () void s[],item
pop() int s []
display() void s[]
VARIABLES USED
Variable name data type purpose
s[] int to store stack elements
max int size of stack
top int stack pointer
item int to store element to be pushed
y int to store element to be popped
ch int to store choice
ALGORITHM :
Procedure PUSH (S ,ITEM)
S . An Array of MAX size representing stack.
TOP Stack pointer
ITEM Element to be push
1. //Check for stack overflow//
if TOP >= MAX then
Print ('Stack Overflow')
Return
endif
2. //Increment pointer Top to provide space for inserting element//
TOP TOP + 1
3. //Insert ITEM at top of the stack//
S[TOP] ITEM
Return
Function POP (S)
S An Array of MAX size representing stack.
TOP stack pointer
Y Element deleted
1. //Check for stack underflow//
if TOP = 0 then
Print ('Stack Underflow')
Return
endif
2. //Retrieve the top element of stack//
Y (S[TOP])
3 //Decrement pointer TOP to save the space//
TOP TOP-1
Return(Y)
PROGRAM:
#include<stdio.h>
#include<conio.h>
int top=-1;
void push(int [],int);
int pop (int[ ]);
void display ();
int s[10];
void main()
int ch;
int item,y;
clrscr ( ) ;
printf("\n\t MENU ");
printf("\n\t1. Push");
printf( "\n\t 2.Pop");
printf("\n\t 3.Display.");
printf("\n\t 4.Exit");
do
printf("\n Enter ur choice");
scanf ( "%d" , &ch) ;
if (ch>4||ch<=0)
printf ("\n Invalid choice ");
break;
switch(ch)
case 1 :
printf("\n\t Enter the element: ");
scanf("%d",& item );
push(s, item );
break;
case 2:
y=pop ( s ) ;
printf("\n\t the deleted break value %d" ,y);
break;
case 3 :
display();
break;
case 4 :
exit(0);
break;
}while(ch<=4);
void push(int s[],int item )
if(top>10)
printf("\t stack is overflow");
else
top=top+ 1 ;
s[top]= item ;
} }
int pop(int s[])
int x;
if(top==-1)
printf("\n\t the stack is underflow");
else
{ x=s[top];
top--;
return x;
void display()
{
int i;
for(i=top;i>=0;i--)
printf( "%d\t" ,s[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.
Queue works under First In First Out (FIFO) principle.
Ex: A 'Q' for a bus.
Insertion is not possible if queue is in overflow (REAR=MAX).
Deletion is not possible if queue is in underflow (FRONT=0)
Implementation of queue using arrays:
FUNCTIONS USED:
Function name return type parameters
insert ( ) void q,max,item
deletion () int q
display() void q
VARIABLES USED:
Variable name data type purpose
q[] int to store queue elements
max int size of the queue
f,r int to store queue front ,rear pointers
item int element to be insert
ch int to store choice
y int to store deletion element
ALGORITHM :
Procedure INSERT (Q, MAX, ITEM)
Q An Array of MAX size representing queue
F Front index of the queue
R Rear index of the queue
ITEM Information to be inserted at the rear of queue.
At the time of creation of Q, F R 0;
1. // Check for queue overflow//
if R >= MAX then
Print ('Queue overflow')
Return
endif
2. //Increment Rear pointer to provide space for inserting element//
RR+ 1
3. //Insert new element at rear end of queue//
Q[R] ITEM
4. //If initially, the queue is empty, adjust the front pointer//
if F= 0 then
F 1
endif
return
Function DELETION (Q)
Q Array of size MAX representing queue.
F Pointer to the front of queue
R Pointer to the rear of queue
Y Temporary variable
1. //Check for queue underflow//
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
4. //Otherwise increment front pointer//
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("enter your choice\n");
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
printf("enter the element to be inserted\n");
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;
printf("deleted element is %d\n",n);
}}
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
printf("\n\t Enter ur choice");
scanf ( "%d" , &ch) ;
if (ch>4||ch<=0)
printf ("\n\t Invalid choice ");
break;
switch(ch)
case 1 :
printf("\n\t Enter the element to be insert:");
scanf("%d",& item ) ;
insert(q,10, item ) ;
break;
case 2 :
y=deletion(q) ;
printf("\n\tThe deleted value:%d",y);
break;
case 3 :
display (q) ;
break;
case 4:
exit (0);
}while(ch<=4) ;
getch () ;
void insert(int q[] ,int n,int item)
{
if (r>=n)
printf("\n\t Queue is overflow");
return;
else
r=r+1;
q[r]= item;
printf("\n\t The value is inserted");
if (f==-1)
f=0;
int deletion(int q[])
int y;
if (f = =-1)
printf("\n\t the Queue is Empty");
y=q[f] ;
if (f==r)
f=r=-1;
else
f++;
return y;
void display(int q[])
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 the element to be insert : 23
The value is inserted
Enter ur choice 1
Enter the element to be insert : 12
The value is inserted
Enter ur choice 1
Enter the element to be insert: 45
The value is inserted
Enter ur choice 3
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:
Variable name datatype purpose
n int to store given terms
fib1,fib2 int to store first,second temporary numbers
fib int to store fibonacci number
i int to repeat while loop
ALGORITHM:
Procedure to GENARATE FIBONACCI SERIES
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
repeat until (i<n) ;
end GENARATE FIBONACCI SERIES
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main ( )
{
int fib1=0, fib2=1, fib, n,i=2;
clrscr();
printf("\n\t Enter number of terms - ") ;
scanf("%d",&n);
printf("\n\t The Fibonacci series for first %d terms are:", n);
printf("\n\t %d %d",fib1,fib2);
/* generation of Fibonacci series */
do
fib=fib1+fib2;
fib1=fib2 ;
fib2=fib;
printf("\t %d",fib);
i++;
} while (i<n) ;
getch () ;
OUTPUT:
Enter number of terms - 6
The Fibonacci series for first 6 terms are 0 1 1 2 3 5
/* fibinacci series using recursion */
#include<stdio.h>
#include<conio.h>
void main()
int n,f,i;
clrscr();
printf("\n enter the number of fibonacci numbers \t");
scanf("%d",&n);
printf("\n fibonacci series is \n\n");
for(i=0; i<n; i++)
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 :-
enter the number of fibonacci numbers 8
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();
printf("\n enter the number \t");
scanf("%d",&n);
f=fact(n);
printf("\n factorial = %ld",f);
getch();
long fact(int n)
if(n==0)
return 1;
else
return(n*fact(n-1));
Output :-
enter the number 5
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();
printf("\n enter the number \t");
scanf("%d",&n);
s=sum(n);
printf("\n sum of the series = %d",s);
getch();
int sum(int n)
if (n==1)
return 1;
else
return(n+sum(n-1));
Output :-
enter the number 5
sum of the series = 15
/* find the small element of list numbers using pointers */
#include<stdio.h>
void main()
int i,n,small, *ptr,a[50];
clrscr();
printf("\n how many elements ?");
scanf("%d", &n);
printf("\n enter the elements: \n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
ptr=a;
small=*ptr;
for(i=1;i<n;i++)
if(*(ptr+i)<small)
small = *(ptr+i);
printf("\nsmall element is %d",small);
getch();
}
Output :-
how many elements ?5
enter the elements:
10 56 34 23 89
small element is 10