<span style="font-family:Microsoft YaHei;font-size:12px;">定义</span>
<span style="font-family:Microsoft YaHei;font-size:12px;">#define datatype int
typedef struct stacknode
{
int num;
datatype data;
struct stacknode *pNext;
}StackNode;
StackNode * init(StackNode * phead);//初始化
StackNode * push(StackNode * phead, int num, datatype data);//进栈
StackNode * pop(StackNode * phead, StackNode * poutdata);//出栈
StackNode * freeall(StackNode * phead);//清空
StackNode * printall(StackNode * phead);//打印</span>
<span style="font-family:Microsoft YaHei;font-size:12px;">--------------------------------------------------------------</span>
<span style="font-family:Microsoft YaHei;font-size:12px;">声明</span>
<span style="font-family:Microsoft YaHei;font-size:12px;">#include"stacklinknode.h"
#include<stdio.h>
#include<stdlib.h>
StackNode * init(StackNode * phead)//初始化
{
return NULL;
}
StackNode * push(StackNode * phead, int num, datatype data)//进栈
{
StackNode *pnewnode = (StackNode *)malloc(sizeof(StackNode));//创建节点 类型转换(StackNode *) 明确解析方式
pnewnode->num = num;
pnewnode->data = data;
pnewnode->pNext = NULL;//开辟节点并赋值
if (NULL == phead)//空链表,直接连接上
{
phead = pnewnode;//连接一个节点
}
else
{
StackNode *p = phead;
while (p->pNext!=NULL)
{
p = p->pNext;//一直向前
}
p->pNext = pnewnode;//插入
}
return phead;//返回头结点
}
//显示节点数据
StackNode * printall(StackNode * phead)
{
if (NULL==phead)
{
return NULL;
}
else
{
printf("%d %d %p %p\n", phead->num, phead->data,phead,phead->pNext);
printall(phead->pNext);//打印
}
}
//出栈
StackNode * pop(StackNode * phead, StackNode * poutdata)
{
if (NULL==phead)
{
return NULL;//已经没有元素
}
else if (NULL==phead->pNext)
{
poutdata->num = phead->num;
poutdata->data = phead->data;//取出数据
free(phead);//释放内存
phead = NULL;//只有一个节点
return phead;
}
else
{
StackNode *p = phead;
while (p->pNext->pNext!=NULL)
{
p = p->pNext;//循环到倒数第二个节点
}
poutdata->num = p->pNext->num;
poutdata->data = p->pNext->data;//取出数据
free(p->pNext);//释放
p->pNext = NULL;
return phead;
}
}
//删除所有节点
StackNode * freeall(StackNode * phead)
{
if (NULL == phead)
{
return NULL;
}
else
{
StackNode *p1=NULL, *p2=NULL;
p1 = phead;//头结点
while (p1->pNext != NULL)
{
p2 = p1->pNext;//保存下一个节点
p1->pNext = p2->pNext;
free(p2);
}
free(phead);
return NULL;
}
}</span>
<span style="font-family:Microsoft YaHei;font-size:12px;">-------------------------------------------------------</span>
<span style="font-family:Microsoft YaHei;font-size:12px;">测试</span>
<span style="font-family:Microsoft YaHei;font-size:12px;">#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include"stacklinknode.h"
//链式链表 实现十进制数转为二进制数
void main()
{
printf("请输入数据:");
int num;
scanf("%d", &num);
printf("num=%d\n", num);//打印数据
StackNode *phead = NULL;//创建一个链式栈的头结点
while (num)
{
printf("%d", num % 2);
phead = push(phead, num%2, 0);//压入数据
num /= 2;
}
printf("\n");
while (phead != NULL)
{
StackNode *pout = (StackNode *)malloc(sizeof(StackNode));
phead = pop(phead, pout);
printf("%d", pout->num);//出栈
}
system("pause");
}
//测试 链式栈 先进后出
void main1()
{
StackNode *phead=NULL;//创建一个链式栈的头结点
phead = init(phead);//设置栈为空
//插入节点
phead = push(phead, 1, 1);
phead = push(phead, 2, 11);
phead = push(phead, 3, 111);
phead = push(phead, 4, 1111);
phead = push(phead, 5, 11111);
printall(phead);
while (phead!=NULL)
{
//保存出栈的数据
printf("出栈\n");
StackNode *pout =(StackNode *) malloc(sizeof(StackNode));
phead = pop(phead, pout);
printf("出栈之后\n");
printall(phead);
printf("\n出栈之后的数据%d %d", pout->num, pout->data);
}
printall(phead);
phead = freeall(phead);
printf("\n释放以后\n");
system("pause");
}</span>