Question

Data Structures and Algorithms. (C++ Language) 1. Write the definition code for a function that p...

Data Structures and Algorithms. (C++ Language)

1. Write the definition code for a function that passes in a stack and returns (using a return statement) the number of items in the stack (the stack size).

a. assume the this function is to be toolkit function in the implementation of the ADT stack.

b. assume that this function is not a toolkit function.

2. Given the declaration:

s = stack

i = item

struct STACK

{

INFO_RC i;

int top;

}

write the definition source code for the following functions in the implementation of the ADT stack:

a. create_stacks(s)

b. empty(s)

c. pop(s,i)

d. top(s,i)

e. push(s,i)

f. purge(s)

3. Indicate the Big-0 "run-time" of each of the following:

a. deletion from a stack that is implemented using an array with the top of the stack always maintained in component 0. Thus items are always inserted and/or deleted at component 0.

b. deletion from a stack that is implemented using an ordinary array with the top of the stack always maintained in the first available component of the array (the stack grows "upward" starting at component 0).

c. print out all of the elements in a stack.

d. determine the number of items currently in the stack. (stack size is not a toolkit function)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer for 1

//Stack definition assuming stack contains only integer numbers

struct STACK

{

int i;

int top;

}

int stack_size(s,top)

{

return top+1;

}

Answer for 2

//create_stacks

STACK create_stacks(s)

{ s.top=-1;

}

//empty(s)

bool empty(s)

{ if (s.top==-1)

return 1;

return 0;

}

//pop(s,i)

int pop(s,i)

{ int i=s[top--];

return i;

}

//top(s,i)

int tops(s,i)

{ return s[top];

}

//push(s,i)

STACK push(s,i)

{ s[++top]=i;

return s;

}

Answer for 3c

O(n)-where n is size of stack

Answer for 3d

O(n)-where n is stack size

Add a comment
Know the answer?
Add Answer to:
Data Structures and Algorithms. (C++ Language) 1. Write the definition code for a function that p...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Data structures and Algorithms Use a Queue to reverse a stack in place. Assume you...

    C++ Data structures and Algorithms Use a Queue to reverse a stack in place. Assume you are given a stack of string values like Stack s; using a Queue, cause all of the items in the stack to be reversed. For example, if you have the following contents on the stack s Top --- bird cat dog turtle ----- Bottom After you run your code, you stack contents should look like Top --- turtle dog cat bird ----- Bottom

  • i want similar for this code to solve two questions : 1- Write a program to...

    i want similar for this code to solve two questions : 1- Write a program to convert a postfix expression to infix expression 2-Write a program to convert an infix expression to prefix expression each question in separate code ( so will be two codes ) #include <iostream> #include <string> #define SIZE 50 using namespace std; // structure to represent a stack struct Stack {   char s[SIZE];   int top; }; void push(Stack *st, char c) {   st->top++;   st->s[st->top] = c;...

  • Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write...

    Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write the pseudocode for removing and returning only the second element from the Stack. The top element of the Stack will remain the top element after this operation. You may assume that the Stack contains at least two items before this operation. (a) Write the algorithm as a provider implementing a Stack using a contiguous memory implementation. You can refer to the implementation given in...

  • Implement these in Python 3 preferably. Task: Implement the following algorithms to solve the Stock Span...

    Implement these in Python 3 preferably. Task: Implement the following algorithms to solve the Stock Span Problem using the ADT for a Stack in ython. The implementation of the ADT of a Stack (10 points) Implementation of computeSpans1 (20 points) Implementation of computeSpan2 (30 points) Provide the derived computational complexity of both computeSpans1 and computeSpans2 (40 points) Algorithm computeSpans1 (P) Input: an n-element list P of numbers such that P[i] is the prices of the stock on day I Output:...

  • You will write the following files: mystack.h - contains the class definition for the mystack class....

    You will write the following files: mystack.h - contains the class definition for the mystack class. mystack.cpp - contains the definitions for member functions of the mystack class. inpost.cpp - contains your convert() function. inpost.h - contains the function prototype for convert() so that the main() can call it. Each of the files (with the exception of inpost.h) is described in more detail below. All header files should contain header guards to prevent them from being included multiple times in...

  • Can you help with this C programming question. I have provided the skeleton code below along...

    Can you help with this C programming question. I have provided the skeleton code below along with the Stack/Data/Process Class for you to see/reference. Along with the Stack/Data type definition.   **SKELTON CODE** #include #include #include Stack* concat_stack(Stack *s1, Stack *s2) { //your code here return NULL; } **STACK CLASS FOR YOU TO REFERENCE** #include #include #include #include Stack* create_stack(int stack_capacity) { Stack *s = (Stack*) malloc(sizeof(Stack)); if (stack_capacity < 1) { fprintf(stderr, "Error(create_stack): invalid capacity, set to 10\n"); s->capacity =...

  • C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise...

    C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise 1 for the class linkedStackType question one:        Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same, false otherwise. Also, write the definition...

  • in C language we need to find the following : EXERCISE 3: 1. Write the definition...

    in C language we need to find the following : EXERCISE 3: 1. Write the definition of a structure named employee that contains character array members for an employee's first and last names, an int member for the employee's age, a char member that contains 'M' or 'F' for the employee's gender, and a double member for the employee's hourly salary. 2. Using the above mentioned definition, write a C program that asks a user to enter the values of...

  • I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and...

    I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and size using below pseudo code. I need two classes with stack pseudo code implementation and the main method to demonstrate the correct working of each operation. pseudo code StackADT (using raw array) class StackADT { int top int items[] int max StackADT(int n) Initialize array to n capacity top = 0 max = n boolean isEmpty() if array has no elements return true else...

  • CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int heig...

    CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect; typedef struct node { Rect* r; struct node* next; }Node; typedef struct { Node* head; Node* tail; }Queue; Specification: In this lab six Queue-related operation functions need to be implemented by using the given function prototypes and data structures. 1. List* createQueue(void); This function initializes an empty “Queue” with the “Queue” data structure and returns an empty queue. 2. int enQueue(List*); This function receives...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT