链式队列 无表头
来源:互联网 发布:网络用语arp是什么意思 编辑:程序博客网 时间:2024/05/19 20:00
#ifndef QUEUE2_H_INCLUDED
#define QUEUE2_H_INCLUDED
//没有头结点 好处是出队列的时候单节点表不用单独考虑 但是插入时候要单独考虑空队列
typedef int elem;
struct node
{
elem data;
struct node*next;
};
typedef struct
{
struct node*front;
struct node*rear;
}queue;
queue Initial(queue Q)
{
Q.front=Q.rear=NULL;
return Q;
}
int length(queue Q)
{
int len;
for (len=0;Q.front!=NULL;Q.front=Q.front->next)
len++;
return len;
}
queue EnQueue(queue Q,elem x)//从队尾插入
{
struct node * p;
p=(struct node*)malloc(sizeof(struct node));
p->data=x;
p->next=NULL;
if (Q.front!=NULL)
{
Q.rear->next=p; //空队列不能引用next
Q.rear=Q.rear->next;
}
else
{
Q.rear = p;
Q.front=p;
}
return Q;
}
queue DeQueue(queue Q,elem *x)//从队首删除
{
if (Q.front==NULL)
{
printf("NULL queue\n");
exit(0);
}
*x=Q.front->data;
struct node*p=Q.front;
Q.front=p->next;
free(p);
return Q;
}
void Print(queue Q)
{
printf("queue has %d elements\n",length(Q));
while (Q.front)
{
printf("%d\t",Q.front->data);
Q.front=Q.front->next;
}
printf("\n");
}
#endif // QUEUE2_H_INCLUDED
#define QUEUE2_H_INCLUDED
//没有头结点 好处是出队列的时候单节点表不用单独考虑 但是插入时候要单独考虑空队列
typedef int elem;
struct node
{
elem data;
struct node*next;
};
typedef struct
{
struct node*front;
struct node*rear;
}queue;
queue Initial(queue Q)
{
Q.front=Q.rear=NULL;
return Q;
}
int length(queue Q)
{
int len;
for (len=0;Q.front!=NULL;Q.front=Q.front->next)
len++;
return len;
}
queue EnQueue(queue Q,elem x)//从队尾插入
{
struct node * p;
p=(struct node*)malloc(sizeof(struct node));
p->data=x;
p->next=NULL;
if (Q.front!=NULL)
{
Q.rear->next=p; //空队列不能引用next
Q.rear=Q.rear->next;
}
else
{
Q.rear = p;
Q.front=p;
}
return Q;
}
queue DeQueue(queue Q,elem *x)//从队首删除
{
if (Q.front==NULL)
{
printf("NULL queue\n");
exit(0);
}
*x=Q.front->data;
struct node*p=Q.front;
Q.front=p->next;
free(p);
return Q;
}
void Print(queue Q)
{
printf("queue has %d elements\n",length(Q));
while (Q.front)
{
printf("%d\t",Q.front->data);
Q.front=Q.front->next;
}
printf("\n");
}
#endif // QUEUE2_H_INCLUDED
- 链式队列 无表头
- 链式队列 有表头
- 线性表的链式实现(单链表)——无独立表头的实现
- 链式队列
- 链式队列
- 链式队列
- 链式队列
- 队列--链式
- 链式队列
- 链式队列
- 链式队列
- 链式队列
- 链式队列
- 队列-链式
- 链式队列
- 链式队列
- 链式队列
- 链式队列
- 浏览器缓存机制
- 编译libvirt源码,替换apt-get安装的libvirt包
- 单链表排序
- 计算卷积 (汇编)
- wireshark插件开发
- 链式队列 无表头
- Android Listview与Checkbox结合实现选择,删除,显示选择结果
- extends和implements的区别;接口和抽象类的区别
- 删除重复记录方法
- 单链表删除相同值
- BNU 29045 Party Games - from lanshui_Yang
- 平面分割问题
- 专业-房地产行业公司呼叫中心客户服务系统软件解决方案
- static_cast、dynamic_cast reinterpret_cast和const_