2012—07—11
来源:互联网 发布:怎么屏蔽网络基站定位 编辑:程序博客网 时间:2024/05/20 18:45
今天,today!考试了,阶段性测试。涉及了很多内容。比如宏定义,数组,指针,关键字(static,volatile,const),移位操作,参数传递。常用的C语言语句,如:switch,for,while,goto。字符串处理函数and so on。
做着做着,我想仰天大吼:有的题目我真的连见都没见过啊!唉!基础不牢,得再接再厉。
1.队列:
(1)概念
像栈一样,队列(queue)也是表。然而,使用队列时插入在一端进行而删除在另一端进行。
队列的基本操作是Enqueue(入队),它是在表的末端(叫做队尾(rear)插入一个元素,还有Dequeue(出队),它是删除(或返回)在表的开头(叫做队头(front)的元素。如图1所示显示一个队列的抽象模型。
(2)队列的实现
在别的博客中看到这样的一段程序:
typedef struct _Queue
{
DList *dlist;
}Queue;
下面定义基本操作,包括:
Queue *queue_create();
Ret queue_push(Queue *queue, void *data);
Ret queue_pop(Queue *queue);
Ret queue_destroy(Queue *queue, DES_FUNC des_func ,int index);
void *queue_foreach(Queue *queue, VISIT_FUNC visit_func, void *ctx);
int queue_len(Queue *queue);
DListNode *queue_get(Queue *queue);
完整头文件如下
#ifndef QUEUE_H
#define QUEUE_H
#include "dlist.h"
/*
* File: queue.h
* Author: ecorefeng
* Created on 2010年8月13日
*/
typedef struct _Queue
{
DList *dlist;
}Queue;
Queue *queue_create();
Ret queue_push(Queue *queue, void *data);
Ret queue_pop(Queue *queue);
Ret queue_destroy(Queue *queue, DES_FUNC des_func ,int index);
void *queue_foreach(Queue *queue, VISIT_FUNC visit_func, void *ctx);
int queue_len(Queue *queue);
DListNode *queue_get(Queue *queue);
#endif /*QUEUE_H*/
以下是函数实现部分(queue.c):
#include "queue.h"
#include <malloc.h>
/*
* File: queue.c
* Author: ecorefeng
* Created on 2010年8月13日
*/
/*
*功能:实现一个结构体的初始化
*参数:void
*返回:Queue结构体
*/
Queue *queue_create()
{
Queue *queue = (Queue*)malloc(sizeof(Queue));
if(queue !=NULL)
{
queue->dlist = dlist_create();
if(queue->dlist == NULL)
{
free(queue);
queue = NULL;
}
}
return queue;
}
Ret queue_push(Queue *queue, void *data)
{
return_val_if_fail(queue != NULL&&data != NULL, RET_FAULT);
return dlist_add(queue->dlist, -1, data);
}
Ret queue_pop(Queue *queue)
{
return_val_if_fail(queue !=NULL, RET_FAULT);
return dlist_delete(queue->dlist, 1);
}
Ret queue_destroy(Queue *queue, DES_FUNC des_func ,int index)
{
if(queue != NULL)
{
dlist_destroy(queue->dlist, des_func, index);
queue->dlist = NULL;
free(queue);
}
return RET_OK;
}
void *queue_foreach(Queue *queue, VISIT_FUNC visit_func, void *ctx)
{
return_val_if_fail(queue != NULL&&visit_func != NULL, NULL);
return dlist_foreach(queue->dlist, visit_func, ctx);
}
int queue_len(Queue *queue)
{
return_val_if_fail(queue != NULL, 0);
return dlist_len(queue->dlist);
}
DListNode *queue_get(Queue *queue)
{
return dlist_get(queue->dlist, 1);
}
最后把测试也写在下面,供学习者测试(queuetest.c)
#include <stdio.h>
#include <assert.h>
#include "queue.h"
/*
* File: queuetest.c:动态数组函数实现
* Author: ecorefeng
* Created on 2010年8月13日
*/
/*
int main(int argc, char * argv[])
{
int i = 0;
int a = 6666666;
int b = 7777777;
Queue *queue = NULL;
queue = (Queue *)queue_create();
queue_push(queue,&a );
queue_push(queue,&a );
queue_push(queue,&a );
for(i = 0; i < 10; i++)
{
queue_push(queue, &b);
}
assert(13 == queue_len(queue));
queue_pop(queue);
assert(12 == queue_len(queue));
void * print_int(void *ctx, void *data)
{
printf("%d/n",*(int *)data);
return NULL;
}
queue_foreach(queue, print_int,NULL);
queue_destroy(queue, NULL, 0);
}
2.单链表
#include <stdio.h>
#include <stdlib.h>
#define NN 12
#define MM 20
//链表结构体定义
typedef int ElemType;
struct sNode {
ElemType data; /*值域*/
struct sNode* next; /*链接指针域*/
};
//1、初始化线性表,即置单链表表头指针为空
void InitList(struct sNode** HL)
{
*HL=NULL;
}
//2、清除线性表中所有元素,即释放单链表中所有结点,使之成为一个空表
void ClearList(struct sNode** HL)
{
/*用cp和np分别作为指向两个相邻结点的指针*/
struct sNode *cp, *np;
/*单链表的表头指针赋给*cp*/
cp=*HL;
/*遍历单链表,依次释放每个结点*/
while(cp!=NULL)
{
np=cp->next;
free(cp);
cp=np;
}
/*置单链表的表头指针为空*/
*HL=NULL;
}
//3、返回线性表L的长度,即单链表的长度
int SizeList(struct sNode* HL)
{
int i; /*用来统计结点个数*/
while(HL!=NULL)
{
i++;
HL=HL->next;
}
return i;
}
//4、检查单链表是否为空
int EmptyList(struct sNode* HL)
{
if(HL==NULL) return 1;else return 0;
}
//5. 返回单链表中第pos个结点中的元素,若pos超出范围,则停止程序运行
ElemType GetElem(struct sNode* HL, int pos)
{
int i=0; /*统计已遍历的结点数*/
if(pos<1)
{
printf("pos值非法,退出运行!\n");
exit(1);
}
while(HL!=NULL)
{
i++;
if(i==pos) break;
HL=HL->next;
}
if(HL!=NULL)
return HL->data;
else
{
printf("pos值非法,退出运行!\n");
exit(1);
}
}
//6、遍历一个单链表
void TraverseList(struct sNode* HL)
{
while(HL!=NULL)
{
printf("5d%",HL->data);
HL=HL->next;
}
printf("\n");
}
//11. 向单链表中第pos个结点位置插入元素为x的结点,若插入成功返回1,否则返回
int InsertPosList(struct sNode** HL, int pos, ElemType x)
{
int i=0;
struct sNode *newp;
struct sNode* cp=*HL, *ap=NULL;
/*对pos值小于等于0的情况进行处理*/
if(pos<=0)
{
printf("pos值不正确,返回0表示插入失败!\n");
return 0;
}
/*查找第pos个结点*/
while(cp!=NULL)
{
i++;
if(pos==i) break;
else {ap=cp; cp=cp->next;}
}
/*产生新结点,若分配失败,则停止插入*/
newp=malloc(sizeof(struct sNode));
if(newp==NULL)
{
printf("内存动态空间用完,无法插入!\n");
return 0;
}
/*把x的值赋给新结点的data域*/
newp->data=x;
/*把新结点插入到表头*/
if(ap==NULL)
{
newp->next=cp; /*或改为newp->next=*HL*/
*HL=newp;
}
/*把新结点插入到ap和cp之间*/
else
{
newp->next=cp;
ap->next=newp;
}
/*插入成功返回1*/
return 1;
}
//12. 向有序单链表中插入元素x结点,使得插入后仍然有序
void InsertOrderList(struct sNode** HL, ElemType x)
{
/*把单链表的表头指针赋给cp,把空赋给ap*/
struct sNode* cp=*HL, *ap=NULL;
/*建立新结点*/
struct sNode *newp;
newp=malloc(sizeof(struct sNode));
if(newp==NULL)
{
printf("内存动态空间用完,退出运行!\n");
exit(1);
}
newp->data=x; /*把x的值赋给新结点的data域*/
/*把新结点插入到表头*/
if(cp==NULL || x<cp->data)
{
newp->next=cp;
*HL=newp;
return;
}
/*顺序查找出x结点的插入位置*/
while(cp!=NULL)
{
if(x<cp->data) break;
else {ap=cp; cp=cp->next;}
}
/*把x结点插入到ap和cp之间*/
newp->next=cp;
ap->next=newp;
}
//16. 从单链表中删除值为x的第一个结点,若删除成功则返回1否则返回0
int DeleteValueList(struct sNode** HL, ElemType x)
{
/*初始化cp和ap指针,使cp指向表头结点,使ap为空*/
struct sNode* cp=*HL;
struct sNode* ap=NULL;
/*从单链表中查找值为x的结点,找到后由cp指向该结点,由
ap指向其前驱结点*/
while(cp!=NULL)
{
if(cp->data==x) break;
ap=cp; cp=cp->next;
}
/*若查找失败,即单链表中不存在值为x的结点,则返回0*/
if(cp==NULL) return 0;
/*对删除的是表头或非表头分别进行处理*/
if(ap==NULL)
*HL=(*HL)->next; /*或改为*HL=cp->next*/
else
ap->next=cp->next;
/*回收被删除的结点*/
free(cp);
/*返回1表示删除成功*/
return 1;
}
void main()
{
int a[NN];
int i;
struct sNode *p;//*h,*s,
InitList(&p);
for(i=0;i<NN;i++) a[i]=rand()%MM;
printf("随机数序列:");
for(i=0;i<NN;i++) printf("%5d",a[i]);
printf("\n");
printf("随机数序列");
TraverseList(p);
printf("单链表长度:%5d\n",SizeList(p));
ClearList(&p);
}
- 2012—07—11
- 2012—07—02
- 2012—07—03
- 2012—07—09
- 2012—07—10
- 2012—07—13
- 2012—07—05 星期四
- 2012—07—04 星期三
- 2012—07—03 星期二
- 2012—07—12 单链表
- 2012-11-07记事
- 2012/11/5——2012/11/9 周记
- 2012/11/12——2012/11/16周记
- 2012/11/19——2012/11/23周记
- 2012/11/26——2012/11/30周记
- 2012/10/29——2012/11/2周记
- 2012-11-07 金山笔试
- 数独2012 07 11
- HQL
- Oracle存储过程返回记录集
- 简单的animation动画效果的实现过程
- 查看端口号是否被占用(情况 命令) 详解
- JAVA_POI处理Excel
- 2012—07—11
- linux—select详解
- 自定义UITableViewCell
- JS创建对象的几种方法
- PHP extract()函数
- x86,x64,Any CPU
- NY-232 How to eat more Banana
- 段错误与栈溢出
- ecmall 检查用户名存在的函数调用