(ii) Develop the code for the following timing experiments. Report your results
ID: 665746 • Letter: #
Question
(ii) Develop the code for the following timing experiments. Report your results in the form of tables. The first column should indicate the size of the data structure. The remaining columns should report the measured times for the procedures that you apply. Derive conclusions out of your experiments and write them down.
4.(15 points) Perform time measurements of the following two operations on stacks: iterated insertion (PUSH operation), iterated deletion (POP operation). Run each of your procedures on three types of stacks: a selected stack type library data structure, your implementation of the stack ADT with arrays, your implementation of the stack ADT with pointers.
Explanation / Answer
#include <stdio.h>
#include <time.h>
#define MAXSIZE 5
struct stack
{
int stk[MAXSIZE];
int top;
};
typedef struct stack STACK;
STACK s;
void push(void);
int pop(void);
void display(void);
void main ()
{
int choice;
int option = 1;
s.top = -1;
time_t start_time, end_time;
double diff_time;
printf ("STACK DEMO ");
while (option)
{
printf ("******************************* ");
printf ("1. PUSH ");
printf ("2. POP ");
printf ("3. DISPLAY ");
printf ("4. EXIT ");
printf ("******************************* ");
printf ("Enter a choice ");
scanf ("%d", &choice);
switch (choice)
{
case 1:
time(&start_time);
push();
time(&end_time);
diff_time = difftime(end_time, start_time);
printf(" Sizeof s Time Taken for push");
printf(" %d %d ",sizeof(s),diff_time);
break;
case 2:
time(&start_time);
pop();
time(&end_time);
diff_time = difftime(end_time, start_time);
printf(" Sizeof s Time Taken for pop");
printf(" %d %d ",sizeof(s),diff_time);
break;
case 3:
display();
break;
case 4:
return;
}
fflush (stdin);
printf (" Do you want to continue(Type 0 or 1)? ");
scanf ("%d", &option);
}
}
/* Function to push an element to the stack */
void push ()
{
int num;
if (s.top == (MAXSIZE - 1))
{
printf ("The stack is full ");
return;
}
else
{
printf ("Please enter the element to be pushed ");
scanf ("%d", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
}
return;
}
/* Function to pop an element from the stack */
int pop ()
{
int num;
if (s.top == - 1)
{
printf ("The stack is Empty ");
return (s.top);
}
else
{
num = s.stk[s.top];
printf ("The popped element is %d", s.stk[s.top]);
s.top = s.top - 1;
}
return(num);
}
/* Function to display the stack */
void display ()
{
int i;
if (s.top == -1)
{
printf ("The stack is empty ");
return;
}
else
{
printf (" The present status of the stack is ");
for (i = s.top; i >= 0; i--)
{
printf ("%d ", s.stk[i]);
}
}
printf (" ");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.