Question

hw5

Data Structure:

in C language:

2. Assuming that there is a series of int type values in stack, such as list -> 5 3 2 10 6 8 1 4 12 7 4, write a program to

0 0
Add a comment Improve this question Transcribed image text
Answer #1
 #include <limits.h> #include <stdio.h> #include <stdlib.h> struct Stack { int top; unsigned capacity; int* array; }; struct Stack* createStack(unsigned capacity) { struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack)); stack->capacity = capacity; stack->top = -1; stack->array = (int*)malloc(stack->capacity * sizeof(int)); return stack; } int isFull(struct Stack* stack) { return stack->top == stack->capacity - 1; } int isEmpty(struct Stack* stack) { return stack->top == -1; } void push(struct Stack* stack, int item) { if (isFull(stack)) return; stack->array[++stack->top] = item; // printf("%d pushed to stack\n", item); } int pop(struct Stack* stack) { if (isEmpty(stack)) return INT_MIN; return stack->array[stack->top--]; } int peek(struct Stack* stack) { if (isEmpty(stack)) return INT_MIN; return stack->array[stack->top]; } int main() {   int n;  printf("How many elements?");   scanf("%d",&n);     struct Stack* stack1 = createStack(n);  struct Stack* stack2 = createStack(n);  int arr[n];     printf("Enter the elements");   int i,j;        for( i=0;i<n;++i)            scanf("%d",&arr[i]);        for( i=0;i<n;++i)    {               int flag=0;             for( j=i+1;j<n;++j)          {                       if(arr[j]>arr[i])                    {                               push(stack1,arr[j]);                            flag=1;                                 break;                  }               }               if(flag==0)                     push(stack1,-1);        }       for( i=0;i<n;++i)            push(stack2,pop(stack1));       for( i=0;i<n;++i)    {               printf("%d ",arr[i]);           int k=pop(stack2);              if(k==-1)                       printf("none\n");               else                    printf("%d\n",k);       } } 
Add a comment
Know the answer?
Add Answer to:
hw5 Data Structure: in C language: 2. Assuming that there is a series of int type...
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
  • In the problems below, give the order of the element in the indicated factor group. (a)  in...

    In the problems below, give the order of the element in the indicated factor group. (a)  in (b)  in (c)  in (5)(20 points) In the problems below, give the order of the element in the indicated factor group. (a) (1, 2)+ < (1,1) > in Z3 x Z6/ < (1,1) >. (b) (3, 2)+ < (4,4) > in Z6 * Z8/ < (4,4) >. (c) 26+ < 12 > in Z60/ <12>.

  • In C language 1. Write a program to declare and initialize an array of size 5...

    In C language 1. Write a program to declare and initialize an array of size 5 and place odd numbers 3, 5, 7,9 and 11 in it. Declare and initialize another array of size 5 and place even numbers 4, 6, 8, 10 and 12 in it. Write a for loop to add each element and place it in a third array of the same dimensions. Display each array.

  • 1. What is the output of the following program? include <stdio.h> int wilma (int x) if...

    1. What is the output of the following program? include <stdio.h> int wilma (int x) if (x<5) x = 7; return (x) int main (void) int x-1 x=wilma (x) ; printf ("%d", x); return (0) b)3 c) 4 d) 7 a) 1 e) none of these

  • Write the parametric equations x=2siny=4cos0 in the given Cartesian form. y^2/16= with x0. Write the parametric...

    Write the parametric equations x=2siny=4cos0 in the given Cartesian form. y^2/16= with x0. Write the parametric equations x=2sin2y=5cos2 in the given Catesian form. y= with 0x2. Write the parametric equations x=4ety=8e−t as a function of x in Cartesian form. y= with x0. Write the parametric equations x = 2 sin 0, y = 4 cos 0, 0<O< in the given Cartesian form. = with x > 0. 16 Write the parametric equations x = 2 sin’e, y = 5 cos?...

  • Please Answer this question using the language of C++. I provide you with the picture of figure 1...

    Please Answer this question using the language of C++. I provide you with the picture of figure 18_02. Thank you. I 7/ Fig. 18.2: Stack.h 2 // Stack class template. #ifndef #de fine 3 STACK-H STACK-H 5 #include 7 template 8 class Stack ( 9 public: 10 I const T& top) 12 13 l/ return the top element of the Stack return stack.frontO; // push an element onto the Stack void push(const T& pushValue) 15 stack push front (pushValue); 17...

  • 1) 2) 3) integer A, B; input (A): while (A> 0) 5) A=2*A; 6) i (A...

    1) 2) 3) integer A, B; input (A): while (A> 0) 5) A=2*A; 6) i (A < 20 or A> 30) 7) 8) 9) 10 lse B-A 2, 12) B-A 2 13) 14) output (A, B): 15) input (A): 16) end;

  • Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top =...

    Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top = -1; //globally defining the value of top, as the stack is empty void push(int stack[], int x, int n) { if (top == -1) //if top position is the last of posiition of stack,means stack is full { cout << "Stack is full Overflow condition"; } else { top = top + 1; //incrementing top position stack[top] = x; //inserting element on incremented position...

  • *****In SML/NJ****** 1. Write a function count_list with type int list -> int that returns the...

    *****In SML/NJ****** 1. Write a function count_list with type int list -> int that returns the number of items in a list. An item that is repeated is counted each time it appears in the list. 2. Write an ML function sum_list with type int list -> int that returns the sum of all the elements within a list 3. Write a function countdown with the type int -> int list that returns a list of numbers from its argument...

  • In C programming Language Write a version of the infix-to-postfix conversion algorithm. Write a program that converts an...

    In C programming Language Write a version of the infix-to-postfix conversion algorithm. Write a program that converts an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers For Example: Infix expression (6 + 2) * 5 - 8 / 4 to a postfix expression is  62+5*84/- The program should read the expression into character array infix and use the stack functions implemented in this chapter to help create the postfix expression in character array postfix. The...

  • c language not c++ c language int mininum(int a[135) int i; int min. Question 2 (4...

    c language not c++ c language int mininum(int a[135) int i; int min. Question 2 (4 points) If ptr is an integer pointer and x in an integer variable then write one statement to set ptr to point to x then write another statement to increment x by 10 using ptr (not using x) (4 points) Question 3 (4 points) Saved

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