Skip to content

Instantly share code, notes, and snippets.

@kimkidong
Created October 11, 2012 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimkidong/3875155 to your computer and use it in GitHub Desktop.
Save kimkidong/3875155 to your computer and use it in GitHub Desktop.
Multiple Stack
#include <stdio.h>
#define FULL_STACK_SIZE 10
#define STACK_NUMBER 3
//
#define NEXT_POS 1
#define PREVIOUS_POS -1
#define INCREASE 1
#define DECREASE -1
// boundary[0] < Stack0 <= boundary[1]
int Container[FULL_STACK_SIZE];
int top[STACK_NUMBER];
int boundary[STACK_NUMBER+1];
// Set boundary & top position
void SetPosition();
// Add data to Stack
void push(int stack_no, int data);
// get data from stack
int pop(int stack_no);
// Check stack size functions.
bool isFull(int stack_no);
bool isEmpty(int stack_no);
// stack resize
void ShiftLeft(int target, int standard);
void ShiftRight(int target, int standard);
void main()
{
SetPosition();
printf("Full Stack Size :%d\n", FULL_STACK_SIZE);
printf("Created Stack : %d\n",STACK_NUMBER);
printf("\n");
for (int i = 0 ; i < STACK_NUMBER ; ++i)
{
printf("top[%d] = %d\n",i,top[i]);
printf("boundary[%d] = %d\n",i,top[i]);
printf("\n");
}
push(0,1);
push(0,2);
push(0,3);
push(1,4);
push(1,5);
push(1,6);
push(1,7);
push(2,2);
push(2,2);
push(0,1);
push(0,1);
for (int i = 0 ; i < FULL_STACK_SIZE; ++i)
{
printf("%2d ",Container[i]);
}
}
void SetPosition()
{
int position = -1;
top[0] = boundary[0] = position;
for(int i = 1 ; i < STACK_NUMBER ; ++i)
{
top [i] = boundary [i] = ( FULL_STACK_SIZE / STACK_NUMBER ) * i;
}
boundary[STACK_NUMBER] = FULL_STACK_SIZE-1;
}
void push(int stack_no, int data)
{
// 현재 stack이 풀 일경우.
if ( isFull(stack_no) )
{
// 다른 스택이 비었는지 검사.
bool bFull = true;
// 왼쪽 검사
for (int i = 0 ; i < stack_no ; ++i)
if ( top[i] < boundary[i+NEXT_POS])
{
ShiftLeft(i,stack_no);
bFull = false;
break;
}
// 오르쪽 검사
for (int i = stack_no+NEXT_POS ; i < STACK_NUMBER ; ++i)
if ( top[i] < boundary[i+NEXT_POS])
{
ShiftRight(i,stack_no);
bFull = false;
break;
}
if ( bFull)
{
printf("stack is full \n");
return;
}
}
Container[++top[stack_no]] = data;
}
int pop(int stack_no)
{
return Container[top[stack_no]--];
}
bool isFull(int stack_no)
{
return top[stack_no] == boundary[stack_no+NEXT_POS] ? true : false;
}
bool isEmpty(int stack_no)
{
return top[stack_no] == boundary[stack_no] ? true : false;
}
void ShiftLeft(int target, int standard)
{
boundary[target+NEXT_POS] += DECREASE;
top[standard] += DECREASE;
for (int i = boundary[target+NEXT_POS]+INCREASE; i <= boundary[standard+NEXT_POS] ; ++i)
{
Container[i] = Container[i+NEXT_POS];
}
}
void ShiftRight(int target, int standard)
{
boundary[target] += INCREASE;
top[target] += INCREASE;
for(int i = boundary[target+NEXT_POS] ; i > boundary[standard+NEXT_POS] ; --i)
{
Container[i] = Container[i+PREVIOUS_POS];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment