|
楼主 |
发表于 2004-3-18 22:25:11
|
显示全部楼层
举例:
用顺序栈把ABCDEFGHIJKLM变为MLKJIHGFEDCBA
[code:1]
#define maxsize 40
typedef struct sqstack
{
char data[maxsize];
int top;
} SqStackTp;
int main()
{
SqStackTp sq;
int i;
char ch;
InitStack(&sq);
for (ch='A'; ch<='A'+12; ch++)
{
Push(&sq, ch);
printf("%c", ch);
}
printf("\n");
while(! EmptyStack(sq))
{
Pop(&sq, &ch);
printf("%c", ch);
}
printf("\n");
}
[/code:1]
用链接栈把一个带头节点的单链表倒置.
[code:1]
int reverse_list(LinkedListTp *head)
{
LStackTp ls, p;
int x;
InitStack(&ls);
p=head->next;
while(p!=NULL)
{
Push(&ls, p->data);
p=p->next;
}
p=head->next;
while(! EmptyStack(&ls))
{
Pop(&ls, &x);
p->data=x;
p=p->next;
}
}
[/code:1] |
|