标签:style color io 文件 数据 ar 代码 div
1.编写头文件
#define datatype int
struct statcknode
{
int num; //编号
datatype data; //数据
struct statcknode *pNext;//指针域
};
typedef struct statcknode StackNode;
//初始化
StackNode * init(StackNode *pHead);
//进栈
StackNode * push(StackNode *pHead,int num,datatype data);
//出栈
StackNode * pop(StackNode * pHead,StackNode *poutdata);
//清空
StackNode * freeall(StackNode *pHead);
//打印
StackNode * printfAll(StackNode *pHead);
2.编写链表的实现代码
#include "stacklinknode.h"
#include "stdio.h" //NULL在stdio.h中
#include "stdlib.h" //malloc在stdlib.h中
//初始化
StackNode * init(StackNode *pHead)
{
return NULL;
}
//进栈
StackNode * push(StackNode *pHead, int num, datatype data)
{
StackNode *pNewNode = (StackNode *)malloc(sizeof(StackNode));
pNewNode->num = num;
pNewNode->data = data;
pNewNode->pNext = NULL;
//如果是空的链式栈
if (pHead == NULL)
{
pHead = pNewNode;
}
else
{
//备份一个头指针
StackNode *p = pHead;
while (p->pNext != NULL)
{
//一直向前
p = p->pNext;
}
//插入
p->pNext = pNewNode;
}
//返回头结点
return pHead;
}
//出栈
StackNode * pop(StackNode * pHead, StackNode *poutdata)
{
if (pHead == NULL)
{
//已经没有元素
return NULL;
}
//表示只有一个元素的时候
else if (pHead->pNext == NULL)
{
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;
p->pNext = NULL;
return pHead;
}
}
//清空
StackNode * freeall(StackNode *pHead)
{
if (pHead == NULL)
{
return NULL;
}
else
{
StackNode *p1 = NULL, *p2 = NULL;
//头结点
p1 = pHead;
while (p1->pNext != NULL)
{
//保存下一个节点
p2 = p1->pNext;
//跳过p2
p1->pNext = p2->pNext;
//释放节点
free(p2);
}
free(pHead);
return NULL;
}
}
//打印
StackNode * printfAll(StackNode *pHead)
{
if (pHead == NULL)
{
return NULL;
}
else
{
printf("%d,%d,%p,%p\n", pHead->num, pHead->data, pHead, pHead->pNext);
//通过递归的方式进行打印
printfAll(pHead->pNext);
}
}
3.实现代码main.c
#pragma warning(disable:4996)
//#define_CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include "stacklinknode.h"
void main1()
{
int num;
scanf("%d", &num);
//打印数据
printf("num = %d\n",num);
//创建一个链式栈的头节点
StackNode *pHead = NULL;
printf("\n\n");
//通过这种方式实现打印打印十进制的二进制的表现形式
while (num)
{
printf("%d\n", num % 2);
pHead = push(pHead, num % 2, 0);
num /= 2;
}
while (pHead != NULL)
{
StackNode *pOut = (StackNode *)malloc(sizeof(StackNode));
pHead = pop(pHead,pOut);
printf("%d",pOut->num);
}
system("pause");
}
void main()
{
StackNode *pHead = NULL; //创建一个链式栈的头节点
pHead = init(pHead); //设置栈为NULL
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);
printfAll(pHead);
/* pHead =freeall(pHead);
printf("\n释放以后");
printfAll(pHead);*/
while (pHead != NULL)
{
//保存出栈的数据
printf("出栈\n");
StackNode *pOut = (StackNode *)malloc(sizeof(StackNode));
pHead = pop(pHead, pOut);
printf("出栈之后\n");
printfAll(pHead);
printf("\n出栈之后的数据%d,%d", pOut->num, pOut->num);
}
system("pause");
}
标签:style color io 文件 数据 ar 代码 div
原文地址:http://blog.csdn.net/tototuzuoquan/article/details/38361199