| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2887 人关注过本帖
标题:双向栈用C语言怎样实现?
只看楼主 加入收藏
herbert_1987
Rank: 5Rank: 5
等 级:贵宾
威 望:15
帖 子:1314
专家分:0
注 册:2007-5-13
收藏
得分:0 

ypedef struct
{
Elemtype *base[2];
Elemtype *top[2];
}BDStacktype; //双向栈类型

Status Init_Stack(BDStacktype &tws,int m)//初始化一个大小为m的双向栈tws
{
tws.base[0]=(Elemtype*)malloc(sizeof(Elemtype)*m);
tws.base[1]=tws.base[0]+m;
tws.top[0]=tws.base[0];
tws.top[1]=tws.base[1];
return OK;
}//Init_Stack

Status push(BDStacktype &tws,int i,Elemtype x)//x入栈,i=0表示低端栈,i=1表示高端栈
{
if (tws.top[0] > tws.top[1])
{
return OVERFLOW; //注意此时的栈满条件
}
if (i == 0)
{
*tws.top[0]++ = x;
}
else if ( i==1)
{
*tws.top[1]-- = x;
}
else
{
return ERROR;
}

return OK;
}//push

Status pop(BDStacktype &tws,int i,Elemtype &x)//x出栈,i=0表示低端栈,i=1表示高端栈
{
if (i == 0)
{
if (tws.top[0] == tws.base[0])
{
return OVERFLOW;
}
x = *--tws.top[0];
}
else if ( i == 1)
{
if (tws.top[1] == tws.base[1])
{
return OVERFLOW;
}
x = *++tws.top[1];
}
else
{
return ERROR;
}
return OK;
}//pop


人生重要的不是所站的位置,而是所朝的方向
2007-06-17 13:00
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 

good algorithm of herbert_1987. Here is my C verion:

#include <stdio.h>
#include <stdlib.h>
#define N 30
#define UNDERFLOW (1<<31) //
#define OVERFLOW ~(1<<31)

int A[N];

typedef struct
{
int top;
int size;
} Stack;

void InitStack(Stack* s, int direction)
{
if(direction != 0 && direction != 1)
direction = 0;

if(direction ==0)
{
s->top = -1;
}
else
{
s->top = N;
}

s->size = 0;
}

int StackPop(Stack* s, int direction)
{
int x;

if(s->size==0)
return UNDERFLOW;

if(direction != 0 && direction != 1)
direction = 0;

x = A[s->top];

if(direction ==0)
{
--s->top;
}
else
{
++s->top;
}

--s->size;

return x;
}

void StackPush(Stack* s, int x, int direction)
{
if(direction != 0 && direction != 1)
direction = 0;

if(direction ==0)
{
++s->top;
}
else
{
--s->top;
}

A[s->top] = x;

++s->size;
}


int main(int argc, char** argv)
{
int a[20];
Stack s1;
Stack s2;
int i;

for( i = 0; i<20; ++i)
{
a[i] = i+1;
}

InitStack(&s1, 0); // from start
InitStack(&s2, 1); // from end

for( i = 0; i<20; ++i)
{
if(s1.size + s2.size == N)
{
printf("stack overflow. exit the loop.\n");
break;
}

if( (a[i] & 1) == 0 ) // even
StackPush(&s1, a[i], 0);
else
StackPush(&s2, a[i], 1);
}

while(s1.size>0)
{
printf("%d ", StackPop(&s1, 0));
}
printf("\n");

while(s2.size>0)
{
printf("%d ", StackPop(&s2, 1));
}
printf("\n");

printf("States of the shared storage:\n");
for( i = 0; i<N; ++i)
{
printf("%d ", A[i] );
}
printf("\n");


return 0;
}


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-06-17 13:26
killer_l
Rank: 2
等 级:新手上路
威 望:3
帖 子:1139
专家分:0
注 册:2007-5-25
收藏
得分:0 
谢谢了,先看看

2007-06-17 15:11
快速回复:双向栈用C语言怎样实现?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021604 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved