[求助]栈在C语言中的实现
InitStack(&s),Push((&s,e),Pop(&s,&e),GetTop(s,&e)怎么用c语言实现?
搜索更多相关的解决方案:
C语言
----------------解决方案--------------------------------------------------------
用链表或数组实现~~~~~~~
----------------解决方案--------------------------------------------------------
挖。。。。楼上~~~~~~~~~~~
我可想死你了~~~~~~~~
by 雨中飞燕 QQ:78803110 QQ讨论群:5305909
[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url]
[url=http://bbs.bc-cn.net/viewthread.php?tid=162918]C++编写的Windows界面游戏[/url]
[url=http://yzfy.org/]C/C++算法习题(OnlineJudge):[/url] http://yzfy.org/
----------------解决方案--------------------------------------------------------
灌水 把楼上删了
----------------解决方案--------------------------------------------------------
以下是引用coachard在2007-9-18 18:49:08的发言:
用链表或数组实现~~~
能不能说具体点 用链表或数组实现~~~
----------------解决方案--------------------------------------------------------
以下是我有学数据结构的时侯编写的:
存储方式为链式存储结构:
typedef struct snode
{
DataType data;
struct snode *next;
}LSNode;
void StackInitiate(LSNode **head)
{
if((*head=(LSNode *)malloc(sizeof(LSNode)))==NULL) exit(1);
(*head)->next=NULL;
}
int StackNotEmpty(LSNode *head)
{
if(head->next==NULL) return 0;
else return 1;
}
int StackPush(LSNode *head,DataType x)
{
LSNode *p;
if((p=(LSNode*)malloc(sizeof(LSNode)))==NULL)
{
printf("There is not enough space for being inserted in Function StackPush\n");
printf("Press any key to end .\n");
getch();
return 0;
}
p->data=x;
p->next=head->next;
head->next=p;
return 1;
}
int StackPop(LSNode *head,DataType *d)
{
LSNode *p=head->next;
if(p==NULL)
{
printf("Stack is empty in Function StackPop !\n");
printf("Press any key to end.\n");
getch();
return 0;
}
head->next=p->next;
*d=p->data;
free(p);
return 1;
}
int StackTop(LSNode *head,DataType *d)
{
LSNode *p=head->next;
if(p==NULL)
{
printf("Stack is empty in StackTop.\n");
printf("Press any key to end .\n");
getch();
return 0;
}
*d=p->data;
return 1;
}
void StackDestroy(LSNode *head)
{
LSNode *p,*p1;
p=head;
while(p!=NULL)
{
p1=p;
p=p->next;
free(p1);
}
return ;
}
存储方式为顺序存储结构:
typedef struct
{
DataType stack[MaxStackSize];
int top;
}SeqStack;
void StackInitiate(SeqStack *S)
{
S->top=0;
}
int StackNotEmpty(SeqStack S)
{
if(S.top<=0) return 0;
else return 1;
}
int StackPush(SeqStack *S,DataType x)
{
if(S->top>=MaxStackSize)
{
printf("Stack is full now,not being inserted!\n");
return 0;
}
else
{
S->stack[S->top]=x;
S->top++;
return 1;
}
}
int StackPop(SeqStack *S,DataType *d)
{
if(S->top<=0)
{
printf("Stack is empty now, not having any data.\n");
return 0;
}
else
{
S->top--;
*d=S->stack[S->top];
return 1;
}
}
int StackTop(SeqStack S,DataType *d)
{
if(S.top<=0)
{
printf("Stack is empty now!\n");
return 0;
}
else
{
*d=S.stack[S.top-1];
return 1;
}
}
----------------解决方案--------------------------------------------------------