数据结构_链队列相关操作
2014-07-15 22:34
239 查看
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}Node;
typedef struct
{
Node * front;
Node * rear;
}Queue;
void InitQueue(Queue &Q)
{
Node *p=(Node*)malloc(sizeof(Node));
p->next = NULL;
Q.front = p;
Q.rear=p;
}
int QueueEmpty(Queue Q)
{
return Q.rear==Q.front ? 1 : 0;
}
void EnQueue(Queue &Q, int e)
{
Node *p=(Node*)malloc(sizeof(Node));
p->data = e;
p->next =NULL;
Q.rear->next = p;
Q.rear = p;
}
void Visit(Queue Q)
{
Node *p=Q.front->next;
while(NULL !=p)
{
printf("%d\n",p->data);
p=p->next;
}
}
int DeQueue(Queue &Q, int &e)
{
Node *p=Q.front->next;
if(Q.front == Q.rear)
{
return 0;
}
e = p->data;
Q.front->next = p->next;
if(Q.rear==p)
{
Q.rear = Q.front;
}
free(p);
return 1;
}
int main(void)
{
const int N = 10;
int i;
int e;
Queue Q;
InitQueue(Q);
for(i=0;i<N;++i)
{
EnQueue(Q,i);
}
for(i=0;i<N;++i)
{
DeQueue(Q,e);
printf("data[%d]=%d\n",i,e);
}
system("pause");
return 0;
}
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}Node;
typedef struct
{
Node * front;
Node * rear;
}Queue;
void InitQueue(Queue &Q)
{
Node *p=(Node*)malloc(sizeof(Node));
p->next = NULL;
Q.front = p;
Q.rear=p;
}
int QueueEmpty(Queue Q)
{
return Q.rear==Q.front ? 1 : 0;
}
void EnQueue(Queue &Q, int e)
{
Node *p=(Node*)malloc(sizeof(Node));
p->data = e;
p->next =NULL;
Q.rear->next = p;
Q.rear = p;
}
void Visit(Queue Q)
{
Node *p=Q.front->next;
while(NULL !=p)
{
printf("%d\n",p->data);
p=p->next;
}
}
int DeQueue(Queue &Q, int &e)
{
Node *p=Q.front->next;
if(Q.front == Q.rear)
{
return 0;
}
e = p->data;
Q.front->next = p->next;
if(Q.rear==p)
{
Q.rear = Q.front;
}
free(p);
return 1;
}
int main(void)
{
const int N = 10;
int i;
int e;
Queue Q;
InitQueue(Q);
for(i=0;i<N;++i)
{
EnQueue(Q,i);
}
for(i=0;i<N;++i)
{
DeQueue(Q,e);
printf("data[%d]=%d\n",i,e);
}
system("pause");
return 0;
}
相关文章推荐
- 数据结构_链队列相关操作
- C语言数据结构中队列的相关操作
- 数据结构 队列的相关操作
- 数据结构(9)--链队列的定义以及相关操作的实现
- 堆栈和队列的数据结构和相关操作总结
- 数据结构--二叉树的创建和相关操作
- 数据结构--二叉树的创建和相关操作
- 数据结构队列的各种操作
- 数据结构学习1:链表的相关操作
- 数据结构与算法学习之队列及队列的相关操作
- 静态循环队列的相关操作及详解
- 图的相关操作(数据结构)
- 链表的相关操作查找插入删除(c++ 数据结构)
- 静态循环队列的相关操作及详解
- 数据结构--顺序队列基本操作
- 对链表的相关操作及数据结构的再理解
- 【数据结构】——顺序循环队列的相关操作
- 数据结构队列的各种操作
- 图的相关操作【C++数据结构】
- 队列的相关操作