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");
ch=toupper(getche());
switch(ch)
{
case
'A':
ARRAY();
break;
case
'S':
STACK();
break;
case
'X':
exit (0);
default:
printf("\nWRONG option
selected \n\nPlease select Correct Key:
");
}
}
return
0;
}
int
ARRAY()
{
int
LA[max],
N, i;
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]);
}
printf("\nSelect your
operation:\n")
;
printf("\n1: SEARCH an item");
printf("\n2: INSERT an
element\n");
do{
ch=getche();
switch
(ch) {
case
'1':
SEARCH(LA, N);
break;
case
'2':
INSERT(LA, N);
//SHOW(LA, N);
break;
default:
printf("\nWRONG option
selected \n\nPlease select Correct Key:
");
}
}while(ch!='1'
&& ch!='2');
return
0;
}
int
STACK()
{
int
stk[max],
TOP, i;
char
ch;
printf("\nTOP:
");
scanf("%d",
&TOP);
printf("\nEnter element:\n");
for(i=0;
i<TOP; i++)
scanf("%d",
&stk[i]);
printf("\nPress a
number:\n1: for PUSH\n2: for POP\n");
do
{
ch=getche();
switch
(ch) {
case
'1':
PUSH(stk, TOP);
break;
case
'2':
POP(stk, TOP);
break;
default:
printf("\nWRONG
option
selected
\n\nPlease select Correct Key: ");
}
}while(ch!='1'
&& ch!='2');
return
0;
}
int
SEARCH(int
LA[max],
int
N)
{
int
item,i=0, LOC=0;
printf("\nSearch ITEM: ");
scanf("%d",
&item);
while(i!=N)
{
if(LA[i]==item)
{
LOC=i+1;
printf("\nITEM location: %d",
LOC);
break;
}
i++;
}
if(i==N
&& LOC==0)
printf("\nITEM is not
existed !!!!!");
return
0;
}
int
INSERT(int
LA[100],
int
N)
{
int
item, LOC, i;
printf("\nInserting ITEM: ");
scanf("%d",
&item);
printf("\nInsearting
Location: ");
scanf("%d",&LOC);
for(i=N;
i>=LOC; i--)
LA[i]=LA[i-1];
LA[LOC-1]=item;
printf("\nLINEAR ARRAY:\n");
SHOW(LA,
N+1);
return
0;
}
int
SHOW(int
LA[100],
int
N)
{
int
i;
//printf("\nLINEAR ARRAY:\n");
for(i=0;
i<N; i++)
printf("\t%d",
LA[i]);
return
0;
}
int
PUSH(int
stk[max],
int
TOP)
{
int
ITEM;
printf("\nEnter the ITEM:
");
scanf("%d",
&ITEM);
if(TOP==max)
{
printf("\nSTACK overflow");
return
0;
}
TOP=TOP+1;
stk[TOP-1]=ITEM;
printf("\nUpdated STACK\n");
SHOW(stk,
TOP);
return
0;
}
int
POP(int
stk[max],
int
TOP)
{
if(TOP==0)
{
printf("\nSTACK underflow");
return
0;
}
TOP=TOP-1;
printf("\nUpdated STACK\n");
SHOW(stk, TOP);
return
0;
}
|
A program for function: Search, Insert, Show, Push, Pop || Array and Stack using Function call
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment