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

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++
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...
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...
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...
Please code in C#-Visual Studio Tasks The program needs to contain the following A comment header...
Please code in C#-Visual Studio Tasks The program needs to contain the following A comment header containing your name and a brief description of the program Output prompting the user for the following inputs: Name as a string Length of a rectangle as a double Width of a rectangle as a double Length of a square as an int After accepting user input, the program outputs the following: User name Area of a rectangle with dimensions matching the inputs Area...
Design and implement a C++ program that performs the following steps:Ask the user to enter a...
Design and implement a C++ program that performs the following steps:Ask the user to enter a positive integer number N; Your program may need to prompt the user to enter many times until it reads in a positive number;Let user to enter N (obtained in the previous step) floating point numbers, and count how many positive ones there are in the sequence and sum up these positive numbers; (Hint: negative numbers or 0 are ignored).Display result.You can and should use...
In C: Write a complete program that performs the following task: Ask the user for the...
In C: Write a complete program that performs the following task: Ask the user for the number of sequences to display. For each sequence, Ask the user for a starting value Print out the value and double it (multiply by 2). Continue printing and doubling (all on the same line, separated by one space each) as long as the current number is less than 1000, or until 8 numbers have been printed on the line. You may assume that the...
USE C++ and please keep program as simple as possible and also comment so it is...
USE C++ and please keep program as simple as possible and also comment so it is easy to understad Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This should be in 12:59:59 format. This entire input should be assigned first to a string variable. Then the string should be tokenized thereby assigning the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT