队列的创建和使用

C语言高级进阶

队列是一种线性的数据结构,其行为类似于排队。队列通常支持两种主要的操作:入队和出队。 入队操作把数据元素添加到队列中,出队操作从队列中删除数据元素。一般来说,队列的操作遵循先进先出(FIFO)原则,即第一个添加到队列中的元素也是第一个离开队列的元素。

学习内容

此章我们学习如何创建和使用队列这种线性数据结构。 实现队列经常用到链表,入队操作就是将节点添加到链表头,出队操作就是从链表尾删除节点。

学习产出

队的创建和使用如下示例:
//Queue Illustrated
typedef struct _employee
{
    char* name;
    unsigned char age;
}Employee;

int compareEmployee(Employee* e1, Employee* e2)
{
    return strcmp(e1->name, e2->name);
}

void displayEmployee(Employee* employee)
{
    printf("%s\t%d\n", employee->name, employee->age);
}

typedef void(*DISPLAY)(void *);
typedef int(*COMPARE)(void*, void*);

typedef struct _node
{
    void* data;
    struct _node* next; 
}Node;

typedef struct _linkedList
{
    Node* head;
    Node* tail;
    Node* current;
}Linkedlist;

void initializeList(Linkedlist*);
void addHead(Linkedlist*, void*);
void addTail(Linkedlist*, void*);
void delete(Linkedlist*, Node*);
Node* getNode(Linkedlist*, COMPARE, void*);
void displayLinkedList(Linkedlist*, DISPLAY);

void initializeList(Linkedlist* list)
{
    list->head = NULL;
    list->tail = NULL;
    list->current = NULL;
}

void addHead(Linkedlist* list, void* data)
{
    Node* node = (Node*)malloc(sizeof(Node));
    node->data = data;
    if(list->head == NULL)
    {
        list->tail = node;
        node->next = NULL;
    }
    else
    {
        node->next = list->head;
    }

    list->head = node;
}

typedef Linkedlist Queue;

void initializeQueue(Queue* queue)
{
	initializeList(queue);
}

void enqueue(Queue* queue, void* node)
{
	addHead(queue, node);
}

void* dequeue(Queue* queue)
{
	Node* tmp = queue->head;
	void* data;
	if(queue->head == NULL)
	{
		data = NULL;
	}
	else if(queue->head == queue->tail)
	{
		queue->head = queue->tail = NULL;
		data = tmp->data;
		free(tmp);
		tmp = NULL;
	}
	else
	{
		while(tmp->next != queue->tail)
		{
			tmp = tmp->next;
		}
		queue->tail = tmp;
		tmp = tmp->next;
		queue->tail->next = NULL;
		data = tmp->data;
		free(tmp);
		tmp = NULL;
	}
	return data;
}

int main()
{
	Queue queue;
	initializeQueue(&queue);

	Employee* e1 = (Employee*)malloc(sizeof(Employee));
    e1->name = "TomCat";
    e1->age = 13;

    Employee* e2 = (Employee*)malloc(sizeof(Employee));
    e2->name = "KittyMouse";
    e2->age = 16;

    Employee* e3 = (Employee*)malloc(sizeof(Employee));
    e3->name = "BabyFish";
    e3->age = 10;

    enqueue(&queue, e1);
    enqueue(&queue, e2);
    enqueue(&queue, e3);

    void* data = dequeue(&queue);
    printf("Dequeued1: %s\n", ((Employee*)data)->name);
    data = dequeue(&queue);
    printf("Dequeued2: %s\n", ((Employee*)data)->name);
    data = dequeue(&queue);
    printf("Dequeued3: %s\n", ((Employee*)data)->name);

	return 0;
}

运行结果如下:
queue

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C语言中,可以使用系统提供的消息队列函数集来创建消息队列。以下是一个简单的示例程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #define MAX_MSG_SIZE 1024 struct message { long msg_type; char msg_data[MAX_MSG_SIZE]; }; int main(int argc, char* argv[]) { int msgid; key_t key; struct message msg; // 生成一个唯一的key if ((key = ftok(".", 'a')) == -1) { perror("ftok"); exit(EXIT_FAILURE); } // 创建消息队列 if ((msgid = msgget(key, 0666 | IPC_CREAT)) == -1) { perror("msgget"); exit(EXIT_FAILURE); } // 发送消息 msg.msg_type = 1; strcpy(msg.msg_data, "Hello, world!"); if (msgsnd(msgid, &msg, sizeof(msg), 0) == -1) { perror("msgsnd"); exit(EXIT_FAILURE); } // 接收消息 if (msgrcv(msgid, &msg, sizeof(msg), 0, 0) == -1) { perror("msgrcv"); exit(EXIT_FAILURE); } printf("Received message: %s\n", msg.msg_data); // 删除消息队列 if (msgctl(msgid, IPC_RMID, NULL) == -1) { perror("msgctl"); exit(EXIT_FAILURE); } return 0; } ``` 在上述代码中,首先使用 `ftok` 函数生成一个唯一的key,然后使用 `msgget` 函数创建消息队列。发送消息使用 `msgsnd` 函数,接收消息使用 `msgrcv` 函数。最后使用 `msgctl` 函数删除消息队列。 在使用消息队列时,需要注意以下几个问题: - 消息队列的key需要保证唯一性; - 发送和接收的消息结构体需要一致; - 发送和接收时需要指定消息类型; - 消息队列的大小是有限制的,需要根据实际需求设置合适的大小; - 需要注意消息队列的并发访问问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值