- 论坛徽章:
- 0
|
今天有时间找了些资料,把第二个问题解决了下:
#include <stdlib.h>
#include <stdio.h>
#define TRUE 1
#define FALSE 0
#define NULL 0
typedef struct node{
char *name;
void *data;
struct node *next;
}StackNode,*LinkStack;
/*初始化一个栈*/
void Init_Stack (LinkStack top)
{
top->next = NULL;
}
/*判断一个栈是否为空*/
int Empty_Stack (LinkStack top)
{
if(top->next == NULL)
return TRUE;
return FALSE;
}
/*把项压入栈顶*/
int Push_Stack(LinkStack top, char *trgcode,void *element)
{
StackNode *temp;
temp = (StackNode *)malloc(sizeof(StackNode));
if(temp == NULL) return FALSE;
temp->name = trgcode;
temp->data = element;
temp->next = top->next;
top->next = temp;
return TRUE;
}
/*移除栈顶并作为此函数的值返回该对象*/
int Pop_Stack_double (LinkStack top, char *trgcode,double *element)
{
StackNode *temp;
if(Empty_Stack(top)) return FALSE;
temp = top->next;
while((*(trgcode++)=*(temp->name++))!='\0');
*element = *(double *)temp->data;
top->next = temp->next;
free(temp);
return TRUE;
}
int Pop_Stack_int (LinkStack top, char *trgcode,int *element)
{
StackNode *temp;
if(Empty_Stack(top)) return FALSE;
temp = top->next;
while((*(trgcode++)=*(temp->name++))!='\0');
*element = *(int *)temp->data;
top->next = temp->next;
free(temp);
return TRUE;
}
/*查看栈顶并作为此函数的值返回该对象*/
int Peek_Stack_double (LinkStack top, char *trgcode,double *element)
{
if(Empty_Stack(top))
return FALSE;
else
{
while((*(trgcode++) = *(top->next->name++))!='\0');
*element = *(double *)top->next->data;
}
}
int Peek_Stack_int (LinkStack top, char *trgcode,int *element)
{
if(Empty_Stack(top))
return FALSE;
else
{
while((*(trgcode++) = *(top->next->name++))!='\0');
*element = *(int *)top->next->data;
}
}
/*销毁栈*/
void Destroy_Stack (LinkStack top)
{
StackNode *cp, *np;
cp = top->next; /* 使cp指向栈顶结点 */
/* 从栈顶依次删除每个结点 */
while(cp != NULL){
np = cp->next;
free(cp);
cp = np;
}
top->next = NULL; /* 置链栈为空 */
return;
}
/*测试main函数*/
void main()
{
LinkStack s;
double rslt,i,pushe;
int j,k;
char *ss,*sss;
char ssss[20];
ssss[19]='\0';
s = (LinkStack)malloc(sizeof(StackNode));
Init_Stack(s);
ss = "abh0000001";
i = 99993333333333333.24;
Push_Stack(s,ss,&i);
sss = "abh0000002";
j = 12456;
Push_Stack(s,sss,&j);
printf("you pushed data is:");
Pop_Stack_int(s,ssss,&k);
printf("%s %d\n ",ssss,k);
Peek_Stack_double(s,ssss,&pushe);
printf("%s %f\n ",ssss,pushe);
getch();
}
结果:
you pushed data is:abh0000002 12456
abh0000001 99993333333333328.000000 |
|