Need help ASAP. Correct answer will get a thumbs up. This is in C language. Writ
ID: 3826138 • Letter: N
Question
Need help ASAP. Correct answer will get a thumbs up. This is in C language.
Write pseudocode and C-program to solve the problem: ask users to input a sequence of numbers and print them on the screen in the reverse order.
You must use the stack ADT as discussed in the class in your main program to solve the problem. No standard library functions are allowed, except malloc, free and formatted printing, in all your program. You must use a linked list to represent a stack in your implementation of the stack ADT. Your C-program has to be organized into three files: the header file for stack ADT, the source file for ADT implementation, and the client (i.e., the main) file that use the ADT.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
}*top,*top1,*temp;
/* Push data into stack */
void push(int num)
{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->link = NULL;
top->data = num;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->link = top;
temp->data = num;
top = temp;
}
}
/* Display data in stack
void display()
{
top1 = top;
if (top1 == NULL)
{
printf("Stack is empty");
return;
}
while (top1 != NULL)
{
printf("%d ", top1->data);
top1 = top1->link;
}
}
int main()
{
int n, ch, e;
top = NULL;
while(1)
{
printf(" Enter the element ");
scanf("%d",&n);
if(n==0)
break;
else
push(n);
}
display();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.