ÉèΪÊ×Ò³ ¼ÓÈëÊÕ²Ø

TOP

Õ»ºÍ¶ÓÁÐ(Èý)
2014-11-24 02:20:18 ¡¾´ó ÖРС¡¿ ä¯ÀÀ:1225´Î
Tags£º¶ÓÁÐ
´Ó×î×óÏòÓÒÔö³¤


µÚ¶þ¸öÕ»B£º´Ó×îÓÒÏò×óÔö³¤


´úÂëʵÏÖÈçÏ£º
public class NewStack
{
object[] arr;
int top1;
int top2;
public NewStack(int capticy)
{
arr = new object[capticy];
top1 = 0;
top2 = capticy;
}
public void Push(int type, object element)
{
if (top1 == top2)
throw new Exception(¡°The stack is full¡±);
if (type == 1)
{
arr[top1] = element;
top1++;
}
else //type==2
{
top2¨C;
arr[top2] = element;
}
}
public object Pop(int type)
{
object obj = null;
if (type == 1)
{
if (top1 == 0)
throw new Exception(¡°The stack is empty¡±);
else
{
top1¨C;
obj = arr[top1];
arr[top1] = null;
}
}
else //type == 2
{
if (top2 == arr.Length)
throw new Exception(¡°The stack is empty¡±);
else
{
obj = arr[top2];
arr[top2] = null;
top2++;
}
}
return obj;
}
public object Peek(int type)
{
if (type == 1)
{
if (top1 == 0)
throw new Exception(¡°The stack is empty¡±);
return arr[top1 - 1];
}
else //type == 2
{
if (top2 == arr.Length)
throw new Exception(¡°The stack is empty¡±);
return arr[top2];
}
}
}
×ۺϱȽÏÉÏÊöÁ½ÖÖËã·¨£¬ÎÒÃÇ·¢ÏÖ£¬Ëã·¨1ʵÏÖµÄÁ½¸öÕ»£¬Ã¿¸ö¶¼Ö»ÓÐn/2¸ö¿Õ¼ä´óС£»¶øËã·¨2ʵÏÖµÄÁ½¸öÕ»£¬Èç¹ûÆäÖÐÒ»¸öºÜС£¬ÁíÒ»¸öÔò¿ÉÒԺܴó£¬ËüÃǵĺÍΪ³£Êýn¡£


9..ÈçºÎÓÃÒ»¸öÊý×éʵÏÖÈý¸öÕ»
×îºó£¬ÈÃÎÒÃÇ°Ñ¿ÙÃŶù½øÐе½µ×£¬ÏàÐÅ¿´Íê±¾ÎÄ£¬ÄãÒѾ­´ÓÎïÖʺ;«ÉñÉ϶¼Éý¼¶ÎªÒ»¸ö¿ÙÃŶùÖ÷ÒåÕß¡£
Èç¹û»¹Ê¹Óý»²æË÷ÒýµÄ°ì·¨£¬Ã¿¸öÕ»¶¼Ö»ÓÐN/3¸ö¿Õ¼ä¡£
ÈÃÎÒÃÇÖ»ºÃʹÓÃÉϸöÌâÄ¿µÄµÚ2¸ö·½·¨£¬²»¹ýÕâÖ»ÄÜÈÝÄÉ2¸öÕ»£¬ÎÒÃÇ»¹ÐèÒªÒ»¸öλÖôæ·ÅµÚ3¸öÕ»£¬²»È翼ÂÇÊý×éÖмäµÄλÖ᪡ªµÚ3¸öÕ»µÄÔö³¤¹æÂÉ¿ÉÒÔÈçÏ£º
µÚ1¸öÈëÕ»CµÄÔªËؽømid´¦


µÚ2¸öÈëÕ»CµÄÔªËؽømid+1´¦


µÚ3¸öÈëÕ»CµÄÔªËؽømid-1´¦


µÚ4¸öÈëÕ»CµÄÔªËؽømid+2´¦
Õâ¸ö·½·¨µÄºÃ´¦ÊÇ£¬ ÿ¸öÕ»¶¼ÓнӽüN/3¸ö¿Õ¼ä¡£
public class NewStack
{
object[] arr;
int top1;
int top2;
int top3_left;
int top3_right;
bool isLeft;
public NewStack(int capticy)
{
arr = new object[capticy];
top1 = 0;
top2 = capticy;
isLeft = true;
top3_left = capticy / 2;
top3_right = top3_left + 1;
}
public void Push(int type, object element)
{
if (type == 1)
{
if (top1 == top3_left + 1)
throw new Exception(¡°The stack is full¡±);
arr[top1] = element;
top1++;
}
else if (type == 2)
{
if (top2 == top3_right)
throw new Exception(¡°The stack is full¡±);
top2¨C;
arr[top2] = element;
}
else //type==3
{
if (isLeft)
{
if (top1 == top3_left + 1)
throw new Exception(¡°The stack is full¡±);
arr[top3_left] = element;
top3_left¨C;
}
else
{
if (top2 == top3_right)
throw new Exception(¡°The stack is full¡±);
arr[top3_right] = element;
top3_right++;
}
isLeft = !isLeft;
}
}
public object Pop(int type)
{
object obj = null;
if (type == 1)
{
if (top1 == 0)
throw new Exception(¡°The stack is empty¡±);
else
{
top1¨C;
obj = arr[top1];
arr[top1] = null;
}
}
else if (type == 2)
{
if (top2 == arr.Length)
throw new Exception(¡°The stack is empty¡±);
else
{
obj = arr[top2];
arr[top2] = null;
top2++;
}
}
else //type==3
{
if (top3_right == top3_left + 1)
throw new Exception(¡°The stack is empty¡±);
if (isLeft)
{
top3_left++;
obj = arr[top3_left];
arr[top3_left] = null;
}
else
{
top3_right¨C;
obj = arr[top3_right];
arr[top3_right] = null;
}
isLeft = !isLeft;
}
return obj;
}
public object Peek(int type)
{
if (type == 1)
{
if (top1 == 0)
throw new Exception(¡°The stack is empty¡±);
return arr[top1 - 1];
}
else if (type == 2)
{
if (top2 == arr.Length)
throw new Exception(¡°The stack is empty¡±);
return arr[top2];
}
else //type==3
{
if (top3_right == top3_left + 1)
throw new Exception(¡°The stack is empty¡±);
if (isLeft)
return arr[top3_left + 1];
else
return arr[top3_right - 1];
}
}
}


Ê×Ò³ ÉÏÒ»Ò³ 1 2 3 ÏÂÒ»Ò³ βҳ 3/3/3
¡¾´ó ÖРС¡¿¡¾´òÓ¡¡¿ ¡¾·±Ìå¡¿¡¾Í¶¸å¡¿¡¾Êղء¿ ¡¾ÍƼö¡¿¡¾¾Ù±¨¡¿¡¾ÆÀÂÛ¡¿ ¡¾¹Ø±Õ¡¿ ¡¾·µ»Ø¶¥²¿¡¿
ÉÏһƪ£ºOralceÊý¾Ý¿â±ÊÊÔÌâÒ»Ì× ÏÂһƪ£ºJava³ÌÐòÔ±³£¼û±ÊÊÔÌâÖ®Öм¶¼ò´ðÌâ

×îÐÂÎÄÕÂ

ÈÈÃÅÎÄÕÂ

Hot ÎÄÕÂ

Python

C ÓïÑÔ

C++»ù´¡

´óÊý¾Ý»ù´¡

linux±à³Ì»ù´¡

C/C++ÃæÊÔÌâÄ¿