链式栈
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");
}
?
?
?
?
?
?
?
?
總結
- 上一篇: 拍拍贷资金筹集中是不是稳了
- 下一篇: 东莞银行是国企还是私人 哪一年成立的