Question

In: Computer Science

Create a C program that performs the following (please comment the codes): a) Create a Stack...

Create a C program that performs the following (please comment the codes):

a) Create a Stack ADT. Stack should be implemented using the linked list.

b) Enter 10 random integer numbers between 0 to 50 in the stack.

c) After pushing each element, print the content of the top of the stack.

c) Then pop out those 10 integer numbers and print those numbers.

d) Finally destroy the Stack.

Solutions

Expert Solution

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

struct node {

int info;

struct node* link;

} * top;

struct node *top = NULL;

struct node* push(struct node*, int);

struct node* pop(struct node*);

int Elements(struct node*);

int main()

{

srand(time(NULL));

for(int i = 0 ; i<10; ++i){

top = push(top, rand()%50);

printf("Top is: %d \n",top->info);

}

for(int i = 0 ; i<10; ++i)

top = pop(top);


struct node *temp = top;

while(temp != NULL){

struct node *curr = temp;

temp = temp->link;

free(curr);

}

return 0;

}

// Push

struct node* push(struct node* top, int item)

{

struct node* temp;

temp = (struct node *) malloc(sizeof(struct node));

temp->info = item;

temp->link = top;

top = temp;

return top;

}

// Pop

struct node* pop(struct node* top)

{

struct node* temp;

if (top == NULL)

printf("The stack is Empty\n");

else {

temp = top;

printf("Element has been Popped %d \n",temp->info);

top = top->link;

free(temp);

}

return top;

}

// Elements

int Elements(struct node* top)

{

struct node* pointer;

pointer = top;

int count = 0;

if (top == NULL)

return 0;

else {

while (pointer != NULL) {

printf("%d \n", pointer->info);

pointer = pointer->link;

++count;

}

}

return count;

}

=========================================

SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern.


Related Solutions

Create JAVA PROGRAM, and write comment for codes also. 3) You want to keep track of...
Create JAVA PROGRAM, and write comment for codes also. 3) You want to keep track of your progress towards running a 10K. There are two kinds of races - 5K and 10K. The program needs to ask what race was run and what the time was in seconds until the user quits. When they quit, display the average and best time for each type of race in minutes.
Create JAVA PROGRAM, and write comment for codes also. 4) Prompt the user for how much...
Create JAVA PROGRAM, and write comment for codes also. 4) Prompt the user for how much stuff a truck can carry, in pounds. Then ask them for the weight of each thing they add until they stop. Don't forget to be safe.
Create JAVA PROGRAM, and write comment for codes also. 1) Let the user enter in candidate...
Create JAVA PROGRAM, and write comment for codes also. 1) Let the user enter in candidate names for a ballot. Then imagine the program is moved to a voting booth. Each voter enters their unique name and who they vote for, and when there are no more voters display who won. (Imagine something outside your program is telling it there are no more voters.)
Write a program to implement the IntStack that stores a static stack of integers and performs...
Write a program to implement the IntStack that stores a static stack of integers and performs the pop, push, isFull, and isEmpty operations. Write the main class to create a static stack of numbers 10, 20, 30, 40, and 50 then try the member functions. C++
Write a program to implement the IntStack that stores a static stack of integers and performs...
Write a program to implement the IntStack that stores a static stack of integers and performs the pop, push, isFull, and isEmpty operations. Write the main class to create a static stack of numbers 10, 20, 30, 40, and 50 then try the member functions. C++
DO IN C++ Secret Codes! Create a program that will accept a message from the user...
DO IN C++ Secret Codes! Create a program that will accept a message from the user and either encrypt ordecrypt it with the following algorithms: To encrypt: - get the character you wish to encrypt - find the index of that character in the alphabet array - add the shift offset to the index - add the increment to the index - "wrap around" the new index so the result is between 0 and 29 - find the character at...
Using the Stack ADT: Create a program that uses a stack. Your program should ask the...
Using the Stack ADT: Create a program that uses a stack. Your program should ask the user to input a few lines of text and then outputs strings in reverse order of entry. (Optional) Create a similar program that uses a stack. Your new program should ask the user to input a line of text and then it should print out the line of text in reverse. To do this your application should use a stack of Character. In Java...
Create JAVA PROGRAM, and write comment for codes also. 2) Every extra 3500 calories means gaining...
Create JAVA PROGRAM, and write comment for codes also. 2) Every extra 3500 calories means gaining a pound. Falling short by 3500 means losing a pound. Get the base amount of calories the person needs in a day (BMR). Then have the user enter in calorie totals for each day and display their weight every week. They might eat more than once in a day.
2. Using the Stack ADT: Create a program that uses a stack. Your program should ask...
2. Using the Stack ADT: Create a program that uses a stack. Your program should ask the user to input a few lines of text and then outputs strings in reverse order of entry. In Java please.
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack List 2. Display the list 3. Create the function isEmply 4. Count the number of nodes 5. Insert a new node in the Stack List. 6. Delete the node in the Stack List. 7. Call all methods above in main method with the following data: Test Data : Input the number of nodes : 4 Input data for node 1 : 5 Input data...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT