Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a computer program as a Win32 console application in C to maintain a stack

ID: 3692366 • Letter: W

Question

Write a computer program as a Win32 console application in C to maintain a stack Abstract Data Type (ADT) of single characters. You must create header and implementation files (“stack.h” and “stack.c”) for a data type stack_t and operations on the stack. Use the functions push and pop. Your program shall receive information from the user as a command-line argument.   For example, a typical command line to start your program (from a command prompt) might be L:>p6 testfile2.txt
Your program shall NOT seek information from the user such as by a call to scanf(). Your program shall NOT print information to the screen except as required herein. If your program is started without any command-line arguments, it shall print the following line to the screen and terminate execution: EE 233 Spring 2016 P6, <your Blazer id>, <your name>.

Explanation / Answer

Answer:

#include <stdio.h>
#include <stdlib.h>

struct node
{
int data;
struct node *ptr;
}*top,*secondtop,*support;

int stacktopelement();
void push(int data);
void pop();

void print();

void construct();

int count = 0;

void main()
{
int number, ch, e;

printf(" 1 - Push");
printf(" 2 - Pop");
printf(" 3 - Top");
printf(" 4 - Print");
printf(" 5 - Exit");
construct();

while (1)
{
printf(" Enter choice : ");
scanf("%d", &ch);

switch (ch)
{
case 1:
printf("Enter value : ");
scanf("%d", &number);
push(number);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("Number of elements in stack");
else
{
e = stacktopelement();
printf(" Top element : %d", e);
}
break;
   case 4:
print();
break;
case 5:
exit(0);
  
  
default :
printf(" Incorrrect choice Please enter correct choice ");
break;
}
}
}
void construct()

{
top = NULL;
}
void push(int data)

{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->data = data;
}
else
{
support =(struct node *)malloc(1*sizeof(struct node));
support->ptr = top;
support->data = data;
top = support;
}
count++;
}

void print()
{
secondtop = top;

if (secondtop == NULL)
{
printf("Stack is empty");
return;
}

while (secondtop != NULL)
{
printf("%d ", secondtop->data);
secondtop = secondtop->ptr;
}
}

void pop()
{
secondtop = top;

if (secondtop == NULL)
{
printf(" Error in pop up element from empty stack");
return;
}
else
secondtop = secondtop->ptr;
printf(" Removed value : %d", top->data);
free(top);
top = secondtop;
count--;
}
int stacktopelement()

{
return(top->data);
}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote