LINUX C 链表封装
来源:互联网 发布:广电网络上市公司 编辑:程序博客网 时间:2024/05/16 16:01
main.c
//初始化队列
void InitQueue(LiQueue *q)
{
q=(LiQueue*)malloc(sizeof(LiQueue));
q->front=q->rear=NULL;
}
//判断是否为空
int QueueEmpty(LiQueue *q)
{
if(q->rear==NULL)
{
return 1;
}
else
{
return 0;
}
}
//释放
void ClearQueue(LiQueue *q)
{
QNode *p=q->front,*r;
if(p!=NULL)
{
r=p->next;
while(r!=NULL)
{
free(p);
p=r;
r=p->next;
}
}
free(q);
}
//实现队列的入队
void enQueue(LiQueue *q,struct TCPMASSAGE stTcpSendBuff)
{
//封装结点
QNode *s;
s=(QNode*)malloc(sizeof(QNode));
memcpy(&s->data , &stTcpSendBuff , sizeof(stTcpSendBuff));
//s->data =e;
s->next=NULL;
if(q->rear==NULL)
{
q->front =s;
q->rear =s;
}
else
{
q->rear->next =s;
q->rear =s;
}
}
//出队函数
int deQueue(LiQueue *q,struct TCPMASSAGE stTcpSendBuff)
{
QNode *t;
if(q->rear ==NULL)
{
return 0;
}
if(q->front ==q->rear )//只有一个结点
{
t=q->front;
q->front =NULL;
q->rear =NULL;
}
else
{
t=q->front;
q->front=q->front->next;
}
memcpy(stTcpSendBuff.cTcpBuff,t->data.cTcpBuff,t->data.len);
stTcpSendBuff.len = t->data.len;
free(t);
return 1;
}
/*
//出队函数
int deQueue(LiQueue *q,char *pstbuff,int *lenth)
{
QNode *t;
if(q->rear ==NULL)
{
return 0;
}
if(q->front ==q->rear )//只有一个结点
{
t=q->front;
q->front =NULL;
q->rear =NULL;
}
else
{
t=q->front;
q->front=q->front->next;
}
memcpy(pstbuff,t->data.cTcpBuff,t->data.len);
*lenth = t->data.len;
free(t);
return 1;
}*/
//实现队列的测长
int length(LiQueue *q)
{
int n=0;
QNode *p;
p=q->front ;
while(p!=NULL)
{
printf("-第-%d-元素--%s----%d--/n",n,p->data.cTcpBuff,p->data.len);
p=p->next;
n++;
}
return n;
}
main()
{
LiQueue *q;
InitQueue(q);
printf("初始化队列q/n");
struct TCPMASSAGE stTcpBuff;
struct TCPMASSAGE stTcpquBuff;
int iTimes = 0;
for(iTimes = 0 ; iTimes <20; iTimes++)
{
strcat(stTcpBuff.cTcpBuff,"11");
stTcpBuff.len = strlen(stTcpBuff.cTcpBuff);
enQueue(q,stTcpBuff);
length(q);
sleep(1);
}
printf("队列为%s/n",(QueueEmpty(q)?"空":"非空"));
char cqbuff[300];
int len = 0;
while(1)
{
memset(&stTcpquBuff,0,sizeof(struct TCPMASSAGE));
//if(deQueue(q,cqbuff,&len)==0)
if(deQueue(q,stTcpquBuff)==0)
{
printf("队空不能出对/n");
break;
}
else
{
printf("出对一个元素%s-----%d/n",stTcpquBuff.cTcpBuff,stTcpquBuff.len);
//printf("出对一个元素%s-----%d/n",cqbuff,len);
//printf("队列的元素个数:%d/n",length(q));
length(q);
sleep(1);
}
}
printf("释放队列/n");
ClearQueue(q);
}
-----------------------------------------------------------
.h 文件
struct TCPMASSAGE
{
char cTcpBuff[256];
short int len;
};
typedef struct qnode
{
struct TCPMASSAGE data;
struct qnode *next;
}QNode;
typedef struct LiQueue
{
QNode *front;
QNode *rear;
}LiQueue;
//初始化队列
void InitQueue(LiQueue *q);
//判断是否为空
int QueueEmpty(LiQueue *q);
//释放
void ClearQueue(LiQueue *q);
//实现队列的入队
void enQueue(LiQueue *q,struct TCPMASSAGE stTcpSendBuff);
//出队函数
//int deQueue(LiQueue *q,char *pstbuff,int *lenth);
int deQueue(LiQueue *q,struct TCPMASSAGE stTcpSendBuff);
//实现队列的测长
int length(LiQueue *q);
- LINUX C 链表封装
- Linux mysql c API封装
- 用C语言封装的链表
- C语言之单向链表封装
- 使用C语言封装链表函数
- Linux C调用C++库(用C封装C++接口)
- Linux C调用C++库(用C封装C++接口)
- Linux C调用C++库(用C封装C++接口)
- Linux C调用C++库(用C封装C++接口)
- C#--封装
- 【C#】封装
- Linux下c实现域名转IP的方法封装
- 用C语言封装的线性表
- [C\C++]何谓封装
- 【C/C++学院】0802-链式栈/链表队列以及优先队列/封装链表库
- c#dll封装虚拟机
- C中的封装?
- OOP之封装 【C#】
- 键盘驱动不兼容的问题及解决方案
- E1知识点总结
- SELECT、INSERT、UPDATE 和 DELETE 语句指定表级锁定提示的范围
- 这些可以慢慢看,但一定要看
- WM待机下的定时程序处理
- LINUX C 链表封装
- java中的IO流总结
- Java:Struts 项目中web.xml文件配置
- Windows 7 RC版从3月1日开始2个小时关闭一次
- 检查点SCN深入研究 checkpoint说明
- 粗暴式去乱码
- linux学习笔记
- 界面设计的行业标准总结(上)
- 我的空间'