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 a C++ integer linked list program that performs the following methods below: Please create these...
Create a C++ integer linked list program that performs the following methods below: Please create these three source files: intList.h, intList.cpp, & intListTest.cpp. Implement recursive routines in the intList class to do the following: Print the list in reverse order Return the value in the middle node Return the average of all the odd values Remove every node containing an odd value Modify main (in IntListTest.cpp) so it does the following: Insert the numbers 1, 3, 4, 6, 7, 10,...
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.)
Instructions: program in C++, add pseudocode and comment throughout the program. Assignment: Create a program to...
Instructions: program in C++, add pseudocode and comment throughout the program. Assignment: Create a program to keep track of the statistics for a kid’s soccer team. The program will have a structure that defines what data the program will collect for each of the players. The structure will keep the following data: Players Name (string) Players Jersey Number (integer) Points scored by Player (integer) The program will have an array of 12 players (use less for testing and development, use...
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...
Create a C++ or Java program codes for Login Attendance applying the queue principle that will...
Create a C++ or Java program codes for Login Attendance applying the queue principle that will select students from the last attendance login to answer a question. NOTE: Program execution should include deletion and insertion of attendance information/element
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT