提示:该blog仅为完成作业,大佬请绕路
老师代码
#include <stdio.h>
#include <malloc.h>
/**
* 链队列的节点.
*/
typedef struct LinkNode{
int data;
LinkNode* next;
}*LinkNodePtr;
/**
* 链队列.
*/
typedef struct LinkQueue{
LinkNodePtr front;
LinkNodePtr rear;
}*LinkQueuePtr;
/**
* Construct an empty queue.
*/
LinkQueuePtr initQueue(){
LinkQueuePtr resultPtr = (LinkQueuePtr)malloc(sizeof(struct LinkQueue));
//The header, the data is not useful.
LinkNodePtr headerPtr = (LinkNodePtr)malloc(sizeof(struct LinkNodePtr));
headerPtr->next = NULL;
resultPtr->front = headerPtr;
resultPtr->rear = headerPtr;
return resultPtr;
}//Of initQueue
/**
* Output the queue.
*/
void outputLinkQueue(LinkQueuePtr paraQueuePtr){
LinkNodePtr tempPtr = paraQueuePtr->front->next;
while (tempPtr != NULL) {
printf("%d ", tempPtr->data);
tempPtr = tempPtr->next;
}//Of while
printf("\r\n");
}//Of outputLinkQueue
/**
* Enqueue.
*/
void enqueue(LinkQueuePtr paraQueuePtr, int paraElement) {
//Step 1. Create a new node
LinkNodePtr tempNodePtr = (LinkNodePtr)malloc(sizeof(struct LinkNode));
tempNodePtr->data = paraElement;
tempNodePtr->next = NULL;
//Step 2. Link to the existing rear
paraQueuePtr->rear->next = tempNodePtr;
//Step 3. It is the new rear
paraQueuePtr->rear = tempNodePtr;
}//Of enqueue
/**
* Dequeue.
* @return The value of the header
*/
int dequeue(LinkQueuePtr paraQueuePtr) {
int resultValue;
LinkNodePtr tempNodePtr;
//Step 1. Is the queue empty?
if (paraQueuePtr->front == paraQueuePtr->rear) {
printf("The queue is empty.\r\n");
return -1;
}//Of if
//Step 2. Change the queue.
tempNodePtr = paraQueuePtr->front->next;
resultValue = tempNodePtr->data;
paraQueuePtr->front->next = paraQueuePtr->front->next->next;
if (paraQueuePtr->rear == tempNodePtr) {
paraQueuePtr->rear = paraQueuePtr->front;
}//Of if
//Step 3. Free space.
// free(tempNodePtr);
tempNodePtr = NULL;
//Step 4. Return.
return resultValue;
}//Of enqueue
/**
* Unit test.
*/
void testLinkQueue(){
LinkQueuePtr tempQueuePtr;
tempQueuePtr = initQueue();
enqueue(tempQueuePtr, 10);
enqueue(tempQueuePtr, 30);
enqueue(tempQueuePtr, 50);
outputLinkQueue(tempQueuePtr);
printf("dequeue gets %d\r\n", dequeue(tempQueuePtr));
printf("dequeue gets %d\r\n", dequeue(tempQueuePtr));
printf("dequeue gets %d\r\n", dequeue(tempQueuePtr));
printf("dequeue gets %d\r\n", dequeue(tempQueuePtr));
enqueue(tempQueuePtr, 8);
outputLinkQueue(tempQueuePtr);
}//Of testLinkQueue
/**
* The entrance.
*/
int main(){
testLinkQueue();
return 1;
}//Of main
我的代码
#include <stdio.h>
#include <stdlib.h>
//链表中的节点结构
typedef struct QNode{
int data;
struct QNode * next;
}QNode;
//创建链式队列的函数
QNode * initQueue(){
//创建一个头节点
QNode * queue=(QNode*)malloc(sizeof(QNode));
//对头节点进行初始化
queue->next=NULL;
return queue;
}
QNode* enQueue(QNode * rear,int data){
QNode * enElem=(QNode*)malloc(sizeof(QNode));
enElem->data=data;
enElem->next=NULL;
//使用尾插法向链队列中添加数据元素
rear->next=enElem;
rear=enElem;
return rear;
}
QNode* DeQueue(QNode * top,QNode * rear){
if (top->next==NULL) {
printf("\n队列为空");
return rear;
}
QNode * p=top->next;
printf("出队的元素是:%d \n",p->data);
top->next=p->next;
if (rear==p) {
rear=top;
}
free(p);
return rear;
}
//队列的长度
int QueueLength(QNode * top)
{
int length=0;
QNode * pMove = top;
if(pMove->next==NULL){//头指针指向空,长度为0
return length;
}
while (pMove->next !=NULL) {//头指针不为空,移动指针计算长度
pMove = pMove->next;
length++;
}
return length;
}
void printQueue(QNode * top)
{
QNode * pMove = top->next;
if(pMove->next==NULL){
printf("该队列为空!\n");
}
while (pMove!=NULL) {
printf("%d ",pMove->data);
pMove = pMove->next;
}
printf("\n");
}
int main() {
QNode * queue,*top,*rear;
queue=top=rear=initQueue();//创建头结点
//向链队列中添加结点,使用尾插法添加的同时,队尾指针需要指向链表的最后一个元素
for(int i=0;i<10;i++)
{
rear = enQueue(rear, i+1);
}
printQueue(top);
printf("队列的长度为:%d\n",QueueLength(top));
//入队完成,所有数据元素开始出队列
rear=DeQueue(top, rear);
rear=DeQueue(top, rear);
return 0;
}