打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
链栈(c实现)
栈是一种特殊的线性表,只能对线性表的一端进行操作。
链栈所以可以使用链式线性表来实现栈
===============================================================================
linklist.h
=============================================================
#ifndef _MYLINKLIST_H_
#define _MYLINKLIST_H_
typedef void LinkList;
/*
typedef struct _tag_LinkListNode LinkListNode;
struct _tag_LinkListNode
{
LinkListNode* next;
};
*/
typedef struct _tag_LinkListNode
{
struct _tag_LinkListNode* next;
}LinkListNode;
LinkList* LinkList_Create();
void LinkList_Destroy(LinkList* list);
void LinkList_Clear(LinkList* list);
int LinkList_Length(LinkList* list);
int LinkList_Insert(LinkList* list, LinkListNode* node, int pos);
LinkListNode* LinkList_Get(LinkList* list, int pos);
LinkListNode* LinkList_Delete(LinkList* list, int pos);
#endif
==============================================================
linkstack.h
==============================================================
#ifndef _MY_LINKSTACK_H
#define _MY_LINKSTACK_H
typedef void LinkStack;
LinkStack* LinkStack_Create();
void LinkStack_Destroy(LinkStack* lstack);
void LinkStack_Clear(LinkStack* lstack);
int LinkStack_Push(LinkStack* lstack, void* item);
LinkStack* LinkStack_Top(LinkStack* lstack);
LinkStack* LinkStack_Pop(LinkStack* lstack);
int LinkStack_Size(LinkStack* lstack);
#endif // _MY_LINKSTACK_H
==============================================================
linklist.c
==============================================================
#include<stdio.h>
#include "stdlib.h"
#include "string.h"
#include "linklist.h"
typedef struct _tag_LinkList
{
//这个句柄里面,需要保存所有节点信息。需要有一个起始点
//就是带头节点的链表。。。
LinkListNode header;
int length;
}TLinkList;
LinkList* LinkList_Create()
{
TLinkList *ret = (TLinkList *)malloc(sizeof(TLinkList));
if (ret == NULL)
{
return NULL;
}
//memset(ret, 0, sizeof(TLinkList));
ret->header.next = NULL;
ret->length = 0;
return ret;
}
void LinkList_Destroy(LinkList* list)
{
if (list == NULL)
{
return ;
}
free(list);
return ;
}
void LinkList_Clear(LinkList* list)
{
TLinkList *tList =NULL;
if (list == NULL)
{
return ;
}
tList = (TLinkList *)list;
tList->length = 0;
tList->header.next = NULL;
return ;
}
int LinkList_Length(LinkList* list)
{
TLinkList *tList = (TLinkList *)list;
if (tList == NULL)
{
return -1;
}
return tList->length;
}
int LinkList_Insert(LinkList* list, LinkListNode* node, int pos)
{
int i = 0;
TLinkList *tList  = NULL;
LinkListNode *current = NULL;
tList = (TLinkList *)list;
//准备环境让辅助指针变量 指向链表头节点
current = &tList->header;
for (i=0; i<pos &&(current->next!=NULL); i++)
{
current = current->next;
}
//让node节点链接后续链表
node->next = current->next ;
//让前边的链表。链接node
current->next = node;
tList->length ++;
return 0;
}
LinkListNode* LinkList_Get(LinkList* list, int pos)
{
int i = 0;
TLinkList *tList  = NULL;
LinkListNode *current = NULL;
LinkListNode *ret = NULL;
tList = (TLinkList *)list;
if (list == NULL || pos <0 ||pos>=tList->length)
{
return NULL;
}
//准备环境让辅助指针变量 指向链表头节点
current = &tList->header;
for (i=0; i<pos &&(current->next!=NULL); i++)
{
current = current->next;
}
ret = current->next;
return ret;
}
LinkListNode* LinkList_Delete(LinkList* list, int pos)
{
int i = 0;
TLinkList *tList  = NULL;
LinkListNode *current = NULL;
LinkListNode *ret = NULL;
tList = (TLinkList *)list;
if (list == NULL || pos <0 ||pos>=tList->length)
{
return NULL;
}
//准备环境让辅助指针变量 指向链表头节点
current = &tList->header;
for (i=0; i<pos &&(current->next!=NULL); i++)
{
current = current->next;
}
ret = current->next;
//删除算法
current->next =ret->next;
tList->length--;
return ret;
}
==============================================================
linkstack.c
==============================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linkstack.h"
#include "linklist.h"
typedef struct _tag_LinkStackNode
{
LinkListNode node;  //占位结构。。。只要定义一个和node节大小一样的数据即可
void* item;
}TLinkStackNode;
//我要创建一个linkstack,准备用linklist去模拟实现
//相当于在 linkstack.c中写 linklist库的测试程序。。。。。。
LinkStack* LinkStack_Create()
{
//创建一个栈,通过线性表去模拟。。。(创建一个栈,相当于创建一个线性表)
return LinkList_Create();
}
void LinkStack_Destroy(LinkStack* lstack)
{
LinkStack_Clear(lstack); //注意 destory的时候,需要把栈中的所有元素都清空
LinkList_Destroy(lstack);
}
void LinkStack_Clear(LinkStack* lstack)
{
while(LinkStack_Size(lstack) > 0)
{
LinkStack_Pop(lstack); //在这个函数里面有内存释放函数
}
return;
}
//向栈中放元素,相当于 向线性表中插入一个元素
int LinkStack_Push(LinkStack* lstack, void* item)
{
int ret = 0;
//需要item数据,转换成 linklist的业务节点
TLinkStackNode *pTe = (TLinkStackNode *)malloc(sizeof(TLinkStackNode));
if(pTe == NULL)
{
return -1;
}
pTe->item = item;
//头插法 ,向线性表中插入元素,只不过是插入元素的时候,需要构造业务节点而已。。。。。。
//ret = LinkList_Insert(lstack, (LinkListNode*)(&pTe->node), 0);
ret = LinkList_Insert(lstack, (LinkListNode*)(pTe), 0);
if(ret != 0)
{
free(pTe);
}
return ret;
}
LinkStack* LinkStack_Top(LinkStack* lstack)
{
void* myItem = NULL;
TLinkStackNode* tmp = NULL;
tmp = (TLinkStackNode*)LinkList_Get(lstack, 0);
if(tmp == NULL)
{
return NULL;
}
myItem = tmp->item;
return myItem;
}
LinkStack* LinkStack_Pop(LinkStack* lstack)
{
void* myItem = NULL;
TLinkStackNode* tmp = NULL;
tmp = (TLinkStackNode*)LinkList_Delete(lstack, 0);
if(tmp == NULL)
{
return NULL;
}
myItem = tmp->item;
//注意向线性表中,插入元素的时,打造节点,分配内存;
//弹出元素时,需要释放节点内存,不要忘记
if(tmp != NULL)
{
free(tmp);
}
return myItem;
}
int LinkStack_Size(LinkStack* lstack)
{
return LinkList_Length(lstack);
}
==============================================================
linkstack_test.c
==============================================================
#include <stdio.h>
#include <stdlib.h>
#include "linkstack.h"
int main()
{
int a[10], i;
LinkStack* lstack = NULL;
lstack = LinkStack_Create();
for(i = 0; i < 10; i++)
{
a[i] = i + 1;
LinkStack_Push(lstack, &a[i]);
}
printf("top : %d \n", *((int *)(LinkStack_Top(lstack))));
printf("size : %d \n", LinkStack_Size(lstack));
while(LinkStack_Size(lstack) > 0)
{
printf("linkstack pop: %d \n", *((int *)LinkStack_Pop(lstack)));
}
LinkStack_Destroy(lstack);
printf("Hello world!\n");
return 0;
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
链栈的初始化,建立,插入,查找,删除
PHP数据结构-栈的相关逻辑操作
C#数据结构之单链表(LinkList)实例详解
树的实现与操作(C语言实现)
二叉树 非递归遍历 栈实现(前、中后序)
线形表a、b为两个有序升序的线形表,使两个有序线形表合并成一个有序升序线形表h:两路归并排序
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服