Demo entry 4722634

asd

   

Submitted by anonymous on May 12, 2016 at 07:17
Language: C. Code size: 1.3 kB.

#include<stdio.h>
#include<stdlib.h>
#define STACK_LENGTH 10000

void add(int n);
int pop(void);
int odd_array[STACK_LENGTH + 1]={}; int j=0;
int even_array[STACK_LENGTH + 1]={}; int k=0;
void odd_even(int n);
void build_and_print(int n);

int *b, *p, stack1[STACK_LENGTH], stack2[STACK_LENGTH];
int main()
{
  printf("N = ");
  int n; scanf("%d", &n);
  odd_even(n);
  b = stack1;
  p = stack1;
  build_and_print(0);
  printf("\n");
  b = stack2;
  p = stack2;
  build_and_print(1);
}

void add(int n)
{
  p++;
  if(p==(b+STACK_LENGTH))
  {
    printf("\n no more room in stack (max stack lenght = 10000)\n");
    exit(1);
  }
  *p=n;
}

int pop()
{
  if(p==b)
  {
    printf("\n stack is empty\n");
    exit(1);
  }
  p--;
  return *(p+1);
}

void odd_even(int n)
{
  for(int i=1; i<=n; i++)
  {
    if(i%2==0)
    {
      even_array[k] = i;
      k++;
    }
    else
    {
      odd_array[j] = i;
      j++;
    }
  }
}

void build_and_print(int n)
{
  if(n%2 == 0)
  {
    printf("Even stack:\n");
    for(int i=0; i<j; i++) add(even_array[i]);
    for(int i=0; i<j; i++) printf("%d\n",pop());
  }
  if(n%2 != 0)
  {
    printf("Odd stack:\n");
    for(int i=0; i<k; i++) add(odd_array[i]);
    for(int i=0; i<k; i++) printf("%d\n",pop());
  }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).