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
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
ADVERTISEMENT