数据结构链式栈进栈出栈代码
时间: 2023-11-28 12:45:41 浏览: 93
以下是链式栈的进栈和出栈代码:
```c
#define datatype int
typedef struct stacknode {
datatype data;
struct stacknode *pNext;
} StackNode;
StackNode *init(StackNode *phead) {
phead = (StackNode *)malloc(sizeof(StackNode));
phead->pNext = NULL;
return phead;
}
int isEmpty(StackNode *phead) {
if (phead->pNext == NULL) {
return 1;
}
else {
return 0;
}
}
StackNode *push(StackNode *phead, datatype data) {
StackNode *pnew = (StackNode *)malloc(sizeof(StackNode));
pnew->data = data;
pnew->pNext = phead->pNext;
phead->pNext = pnew;
return phead;
}
StackNode *pop(StackNode *phead, datatype *data) {
if (isEmpty(phead)) {
printf("The stack is empty!\n");
return phead;
}
else {
StackNode *ptemp = phead->pNext;
*data = ptemp->data;
phead->pNext = ptemp->pNext;
free(ptemp);
return phead;
}
}
```
阅读全文