**queue.h:**

/*
* Code for basic C skills diagnostic.
* Developed for courses 15-213/18-213/15-513 by R. E. Bryant, 2017
*/ /*
* This program implements a queue supporting both FIFO and LIFO
* operations.
*
* It uses a singly-linked list to represent the set of queue elements
*/ #include <stdbool.h> /************** Data structure declarations ****************/ /* Linked list element (You shouldn't need to change this) */
typedef struct ELE {
int value;
struct ELE *next;
} list_ele_t; /* Queue structure */
typedef struct {
list_ele_t *head; /* Linked list of elements */
list_ele_t *tail;
unsigned size;
} queue_t; /************** Operations on queue ************************/ /*
Create empty queue.
Return NULL if could not allocate space.
*/
queue_t *q_new(); /*
Free all storage used by queue.
No effect if q is NULL
*/
void q_free(queue_t *q); /*
Attempt to insert element at head of queue.
Return true if successful.
Return false if q is NULL or could not allocate space.
*/
bool q_insert_head(queue_t *q, int v); /*
Attempt to insert element at tail of queue.
Return true if successful.
Return false if q is NULL or could not allocate space.
*/
bool q_insert_tail(queue_t *q, int v); /*
Attempt to remove element from head of queue.
Return true if successful.
Return false if queue is NULL or empty.
If vp non-NULL and element removed, store removed value at *vp.
Any unused storage should be freed
*/
bool q_remove_head(queue_t *q, int *vp); /*
Return number of elements in queue.
Return 0 if q is NULL or empty
*/
int q_size(queue_t *q); /*
Reverse elements in queue
No effect if q is NULL or empty
*/
void q_reverse(queue_t *q);

queue.c:

/*
* Code for basic C skills diagnostic.
* Developed for courses 15-213/18-213/15-513 by R. E. Bryant, 2017
*/ /*
* This program implements a queue supporting both FIFO and LIFO
* operations.
*
* It uses a singly-linked list to represent the set of queue elements
*/ #include <stdlib.h>
#include <stdio.h> #include "harness.h"
#include "queue.h" /*
Create empty queue.
Return NULL if could not allocate space.
*/
queue_t *q_new()
{
queue_t *q = NULL;
if((q = malloc(sizeof(queue_t))))
{
q->head = NULL;
q->tail = NULL;
q->size = 0;
return q;
}
else
{
return NULL;
}
} /* Free all storage used by queue */
void q_free(queue_t *q)
{
/* How about freeing the list elements? */
/* Free queue structure */
if (q)
{
list_ele_t *i = q->head;
while(i)
{
list_ele_t *tmp = i;
i = i->next;
free(tmp);
}
free(q);
}
} /*
Attempt to insert element at head of queue.
Return true if successful.
Return false if q is NULL or could not allocate space.
*/
bool q_insert_head(queue_t *q, int v)
{
list_ele_t *newh;
/* What should you do if the q is NULL? */
if (q)
{
if ((newh = malloc(sizeof(list_ele_t))))
{
newh->value = v;
newh->next = q->head;
q->head = newh;
if (!q->size)
{
q->tail = newh;
}
++q->size;
return true;
}
else
{
return false;
}
}
else
{
return false;
}
} /*
Attempt to insert element at tail of queue.
Return true if successful.
Return false if q is NULL or could not allocate space.
*/
bool q_insert_tail(queue_t *q, int v)
{
/* You need to write the complete code for this function */
/* Remember: It should operate in O(1) time */
list_ele_t *newh;
if (q)
{
if ((newh = malloc(sizeof(list_ele_t))))
{
newh->value = v;
newh->next = NULL;
if (q->tail)
{
q->tail->next = newh;
q->tail = newh;
++q->size;
}
else
{
q->head = q->tail = newh;
++q->size;
}
return true;
}
else
{
return false;
}
}
else
{
return false;
}
} /*
Attempt to remove element from head of queue.
Return true if successful.
Return false if queue is NULL or empty.
If vp non-NULL and element removed, store removed value at *vp.
Any unused storage should be freed
*/
bool q_remove_head(queue_t *q, int *vp)
{
/* You need to fix up this code. */
if (!q || !q->size)
{
return false;
}
else
{
if (vp)
{
*vp = q->head->value;
}
list_ele_t *tmp = q->head;
q->head = q->head->next;
free(tmp);
--q->size;
return true;
}
} int q_size(queue_t *q)
{
/* You need to write the code for this function */
/* Remember: It should operate in O(1) time */
if (!q || !q->size)
{
return 0;
}
else
{
return q->size;
}
} /*
Reverse elements in queue
*/
void q_reverse(queue_t *q)
{
if (q && q->size)
{
int cache[q->size];
list_ele_t *tmp = q->head;
for (int i = q->size - 1; (i >= 0) && (tmp != NULL); --i)
{
cache[i] = tmp -> value;
tmp = tmp->next;
}
tmp = q->head;
for (int i = 0; (i < q->size) && (tmp != NULL); ++i)
{
tmp->value = cache[i];
tmp = tmp->next;
}
}
}

测试:

frank@under:~/Desktop/cs:app/lab/cprogramminglab/cprogramminglab-handout$ ./qtest
cmd>help
cmd>help
Commands:
# ... | Display comment
free | Delete queue
help | Show documentation
ih v [n] | Insert v at head of queue n times (default: n == 1)
it v [n] | Insert v at tail of queue n times (default: n == 1)
log file | Copy output to file
new | Create new queue
option [name val] | Display or set options
quit | Exit program
reverse | Reverse queue
rh [v] | Remove from head of queue. Optionally compare to expected value v
rhq [v] | Remove from head of queue without reporting value
show | Show queue contents
size [n] | Compute queue size n times (default: n == 1)
source file | Read commands from source file
time cmd arg ... | Time command execution
Options:
echo 1 Do/don't echo commands
error 5 Number of errors until exit
fail 30 Number of times allow queue operations to return false
malloc 0 Malloc failure probability percent
verbose 4 Verbosity level
cmd>new
cmd>new
q = []
cmd>show
cmd>show
q = []
cmd>ih 1
cmd>ih 1
q = [1]
cmd>ih 2
cmd>ih 2
q = [2 1]
cmd>ih 3
cmd>ih 3
q = [3 2 1]
cmd>size
cmd>size
Queue size = 3
q = [3 2 1]
cmd>it 0
cmd>it 0
q = [3 2 1 0]
cmd>it -1
cmd>it -1
q = [3 2 1 0 -1]
cmd>size
cmd>size
Queue size = 5
q = [3 2 1 0 -1]
cmd>reverse
cmd>reverse
q = [-1 0 1 2 3]
cmd>size
cmd>size
Queue size = 5
q = [-1 0 1 2 3]
cmd>rh -1
cmd>rh -1
Removed -1 from queue
q = [0 1 2 3]
cmd>size

评分:

frank@under:~/Desktop/cs:app/lab/cprogramminglab/cprogramminglab-handout$ ./driver.py
--- Trace Points
+++ TESTING trace trace-01-ops:
# Test of insert_head and remove_head
--- trace-01-ops 7/7
+++ TESTING trace trace-02-ops:
# Test of insert_head, insert_tail, and remove_head
--- trace-02-ops 7/7
+++ TESTING trace trace-03-ops:
# Test of insert_head, insert_tail, reverse, and remove_head
--- trace-03-ops 7/7
+++ TESTING trace trace-04-ops:
# Test of insert_head, insert_tail, and size
--- trace-04-ops 7/7
+++ TESTING trace trace-05-ops:
# Test of insert_head, insert_tail, remove_head reverse, and size
--- trace-05-ops 7/7
+++ TESTING trace trace-06-robust:
# Test operations on NULL queue
--- trace-06-robust 7/7
+++ TESTING trace trace-07-robust:
# Test operations on empty queue
--- trace-07-robust 7/7
+++ TESTING trace trace-08-robust:
# Test remove_head with NULL argument
--- trace-08-robust 7/7
+++ TESTING trace trace-09-malloc:
# Test of malloc failure on new
--- trace-09-malloc 7/7
+++ TESTING trace trace-10-malloc:
# Test of malloc failure on insert_head
--- trace-10-malloc 7/7
+++ TESTING trace trace-11-malloc:
# Test of malloc failure on insert_tail
--- trace-11-malloc 7/7
+++ TESTING trace trace-12-perf:
# Test performance of insert_tail
--- trace-12-perf 7/7
+++ TESTING trace trace-13-perf:
# Test performance of size
--- trace-13-perf 8/8
+++ TESTING trace trace-14-perf:
# Test performance of insert_tail, size, and reverse
--- trace-14-perf 8/8
--- TOTAL 100/100

CS:APP3e 深入理解计算机系统_3e C Programming Lab实验的更多相关文章

  1. CS:APP3e 深入理解计算机系统_3e MallocLab实验

    详细的题目要求和资源可以到 http://csapp.cs.cmu.edu/3e/labs.html 或者 http://www.cs.cmu.edu/~./213/schedule.html 获取. ...

  2. CS:APP3e 深入理解计算机系统_3e Attacklab 实验

    详细的题目要求和资源可以到 http://csapp.cs.cmu.edu/3e/labs.html 或者 http://www.cs.cmu.edu/~./213/schedule.html 获取. ...

  3. CS:APP3e 深入理解计算机系统_3e bomblab实验

    bomb.c /*************************************************************************** * Dr. Evil's Ins ...

  4. CS:APP3e 深入理解计算机系统_3e ShellLab(tsh)实验

    详细的题目要求和资源可以到 http://csapp.cs.cmu.edu/3e/labs.html 或者 http://www.cs.cmu.edu/~./213/schedule.html 获取. ...

  5. CS:APP3e 深入理解计算机系统_3e Y86-64模拟器指南

    详细的题目要求和资源可以到 http://csapp.cs.cmu.edu/3e/labs.html 或者 http://www.cs.cmu.edu/~./213/schedule.html 获取. ...

  6. CS:APP3e 深入理解计算机系统_3e Datalab实验

    由于http://csapp.cs.cmu.edu/并未完全开放实验,很多附加实验做不了,一些环境也没办法搭建,更没有标准答案.做了这个实验的朋友可以和我对对答案:) 实验内容和要求可在http:// ...

  7. CS:APP3e 深入理解计算机系统_3e CacheLab实验

    详细的题目要求和资源可以到 http://csapp.cs.cmu.edu/3e/labs.html 或者 http://www.cs.cmu.edu/~./213/schedule.html 获取. ...

  8. 深入理解计算机系统_3e 第八章家庭作业 CS:APP3e chapter 8 homework

    8.9 关于并行的定义我之前写过一篇文章,参考: 并发与并行的区别 The differences between Concurrency and Parallel +---------------- ...

  9. 深入理解计算机系统_3e 第四章家庭作业(部分) CS:APP3e chapter 4 homework

    4.52以后的题目中的代码大多是书上的,如需使用请联系 randy.bryant@cs.cmu.edu 更新:关于编译Y86-64中遇到的问题,可以参考一下CS:APP3e 深入理解计算机系统_3e ...

随机推荐

  1. js的call() ,apply() 两种方法的区别和用法,最白话文的解释,让枯燥滚粗!

    百度了一圈calll()函数和apply()函数,感觉还是糊里糊涂 正好我前几天刚又重新翻了一遍 那本 600多页 的圣经书,我习惯时不时的去打下基础,只是为了用来装逼,给人讲解....(我是有多蛋疼 ...

  2. 深入理解CSS盒模型

    如果你在面试的时候面试官让你谈谈对盒模型的理解,你是不是不知从何谈起.这种看似简单的题其实是最不好答的. 下面本文章将会从以下几个方面谈谈盒模型. 基本概念:标准模型 和IE模型 CSS如何设置这两种 ...

  3. 记录一下通过分析Tomcat内部jar包找出request.getReader()所用的字符编码在哪里设置和起效的完整分析流程

    前言: 之前写Java服务端处理POST请求时遇到了请求体转换成字符流所用编码来源的疑惑,在doPost方法里通过request.getReader()获取的BufferedReader对象内部的 R ...

  4. Android drawText 做到文字绝对居中

    我们在android中经常会遇到自定义一些组件,因为现有的android组件是往往不能满足当下的需求的,今天就给大家介绍一下在自定义组建过程中用到的drawText不居中的问题的解决方案 首先大家看一 ...

  5. java队列——queue详细分析

    Queue: 基本上,一个队列就是一个先入先出(FIFO)的数据结构 Queue接口与List.Set同一级别,都是继承了Collection接口.LinkedList实现了Deque接 口.   Q ...

  6. 2017EIS CTFwriteup

    EIS2017也就是2017年高校网络信息安全管理 运维挑战赛,全国一百多所高校参赛,侥幸拿了个地区三等奖,事先不知道理论赛占分比,不然就会是二等奖(吐槽),生活没有如果,下次努力吧. 比赛已经结束大 ...

  7. 那些年我们写js烦的不疼不痒的错误

    1.Js 字符变量不加双/单引号. 列如:var strJsonInfo = '@Html.Raw(ViewBag.JsonInfo)'; 2.js 对象初始化器,最后一个属性值加逗号. 例如:var ...

  8. cocos2d-x安卓应用启动调用过程简析

    调用org.cocos2dx.cpp.AppActivity AppActivity是位于proj.android/src下的开发者类(即开发者自定义的类),它继承自org.cocos2dx.lib. ...

  9. Python测试开发之函数

    对于初学者而言,感觉函数还是不是很好理解,尤其是当写一个脚本,或者是写一个算法,认为可能for循环就已经可以解决的问题为什么还要用函数来实现呢? 今天就来说一下函数的优点,其实函数的最大优点就是可重用 ...

  10. 用Redis轻松实现秒杀系统

    秒杀系统的架构设计 秒杀系统,是典型的短时大量突发访问类问题.对这类问题,有三种优化性能的思路: 写入内存而不是写入硬盘 异步处理而不是同步处理 分布式处理 用上这三招,不论秒杀时负载多大,都能轻松应 ...