队列_链表实现
2011-06-17 09:54
225 查看
1: // RLC分割.cpp : 定义控制台应用程序的入口点。
2: //
3:
4: #include "stdafx.h"
5: #include
6:
7: typedef char ElemType;
8:
9: typedef struct QNode{
10: ElemType data;
11: QNode *next;
12: } QNode , *QueuePtr;
13:
14: typedef struct{
15: QueuePtr front; //队头指针
16: QueuePtr rear; //队尾指针
17: }LinkQueue;
18:
19: void initQueue(LinkQueue *q){
20: /*初始化一个空队列*/
21: q->front = q->rear = (QueuePtr)malloc(sizeof(QNode)); /*创建一个头结点,队头队尾指针 指向该结点*/
22: if( !q->front) exit(0); /*创建头结点失败*/
23: q->front->next = NULL; /*头结点指针域置NULL*/
24: }
25:
26: void EnQueue(LinkQueue *q, ElemType e){
27: QueuePtr p;
28: p = (QueuePtr)malloc(sizeof(QNode)); /*创建一个队列元素结点*/
29: if( !p) exit(0); /*创建结点失败*/
30: p->data = e;
31: p->next = NULL;
32: q->rear ->next = p;
33: q->rear = p;
34: }
35:
36: void DeQueue(LinkQueue *q, ElemType *e){
37: /*如果队列q不为空,删除q的队头元素,用e返回其值*/
38: QueuePtr p;
39: if(q->front == q->rear) return; /*队列为空,返回*/
40: p = q->front->next;
41: *e = p->data;
42: q->front->next = p->next;
43: if(q->rear == p) q->rear = q->front; /*如果队头就是队尾,则修改队尾指针*/
44: free(p);
45: }
46:
47: int _tmain(int argc, _TCHAR* argv[])
48: {
49: ElemType e;
50: LinkQueue q;
51: initQueue(&q); /*初始化一个队列q*/
52: printf("Please input a char into a queue/n");
53: scanf("%c",&e);
54: while(e!='@'){
55: EnQueue(&q,e); /*向队列中输入字符串,以@表示结束*/
56: scanf("%c",&e);
57: }
58: printf("The string into the queue is/n");
59: while(q.front != q.rear){ /*将队列中的元素出队列,并打印在屏幕上*/
60: DeQueue(&q,&e);
61: printf("%c",e);
62: }
63: printf("/n");
64: system("pause");
65: return 0;
66: }
67:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
相关文章推荐
- FIFO 队列的链表和数组实现
- 用链表实现的队列
- 一步一步学数据结构之1--1(队列--单链表实现--含队头尾指针)
- 不用指针链表实现先入先出队列
- 双端队列(deque)链表实现
- 栈和队列的基本实现(数组实现和链表实现)
- java数据结构之LinkedQueue(用链表实现的双端单向队列)
- 队列(queue)的链表(list)实现及循环数组(circular array)实现 C++实现
- 队列的链表实现
- 队列链表实现以及有序表的合并
- (第7讲)利用双向链表实现双端队列、优先级队列(有序数组实现、有序链表实现)
- 链表-E-实现队列
- 队列(链表实现)
- 练习 -- 使用双向链表来实现双向队列
- 基于数组和链表的队列实现
- 用链表实现队列的操作
- 数据结构:用单链表实现的队列
- CPP 链表实现队列
- Java 实现双向链表,基于双向链表实现双端队列
- 链表实现循环队列