Use this program
to run certain function with array and stack. Function: search, insert,
show,
push and pop. Use a menu to make choice for run operation. I give you a
code
based on my algorithm. Try to find another algorithm and then make
code. Before
start learn about Array and Stack and difference between them.
#include<stdio.h>
#include<stdlib.h>
#define max
20
int
ARRAY();
int
STACK();
int
SEARCH(int
LA[max],
int
N);
int
INSERT(int
LA[max],
int
N);
int
SHOW(int
LA[max],
int
N);
int
PUSH(int
stk[max],
int
TOP);
int
POP(int
stk[max],
int
TOP);
int
main()
{
char
ch;
for(;;)
{
printf("\nMake a choice of
your operation:") ;
printf("\nA: for ARRAY
operation");
printf("\nS: for STACK
operation");
printf("\nX: for EXIT\n"); |
A program for function: Search, Insert, Show, Push, Pop || Array and Stack using Function call
Program of Searching and inserting a item in a linear array - using function
#include<stdio.h>
#include<stdlib.h>
int
SEARCH(int
LA[100], int
N);
int
INSERT(int
LA[100], int
N);
int
SHOW(int
LA[100], int
N);
int
main()
{
int
LA[100],i, N;
char
ch;
printf("\nEnter how many
array element: ");
scanf("%d",
&N);
printf("\nEnter array
elements:\n");
for(i=0;i<N;i++)
{
printf("\nElement
no %d: ",
i+1);
scanf("%d",
&LA[i]);
}
menu:
{
printf("\nSelect your
operation:\n")
;
printf("\n1: SEARCH an item");
printf("\n2: INSERT an
element ");
printf("\n0: Exit\n ")
|
Code: Towers of hanoi - write a program to solve Towers of Hanoi -
#include<stdio.h>
int
TOWER(int
n, char
beg, char
aux, char
end);
int
TOWER(int
n, char
beg, char
aux, char
end)
{
if
(n==1)
printf("\nmove disk %d from %c to %c",
n, beg, end);
else
{
TOWER(n-1,beg,end,aux);
printf("\nmove disk %d from %c to %c",
n, beg, end);
TOWER(n-1,aux, beg,
end);
return
0;
}
}
int
main()
{
int
n;
printf("TOWER OF HANOI:\n");
printf("\nHow many disk:
");
scanf("%d",
&n);
if(n<1)
{
printf("INVALID DISK MOVEMENT");
return
0;
}
TOWER(n,'A', 'B', 'C');
return
0;
}
|
Subscribe to:
Posts (Atom)