Queue chain storage

Time:2020-11-26

Linked list realizes queue


Create three files: queuelinked. H, queuelinked. C, queuelinkedtest. C


queueLinked.h
#ifndef QUEUE_LINKED_H_
#define QUEUE_LINKED_H_

#ifdef __GNUC__
	#define DEPRECATED __attribute__( (deprecated) )
#elif defined(_MSC_VER)
	#define DEPRECATED __declspec( deprecated )
#else
	#define DEPRECATED
#endif

#ifndef PTOI
	#define PTOI( p ) ((int32_t)(int64_t)(p))
#endif
#ifndef ITOP
	#define ITOP( i ) ((void *)(int64_t)(i))
#endif

#define ADT QueueLinked

//Function: the process of comparison between a and B
//Parameters: A, B
//Return: a > B returns a positive number, a

queueLinked.c
#include 
#include 
#include 
#include "queueLinked.h"

//Function: print the error message and exit the program
//Parameters: expression (error judgment expression), message (error message to be printed)
//Return: none
//Note: only triggered when expression is true
#define ERROR_EXIT( expression, message )                                    \
if( (expression) ) {                                                         \
	fprintf( stderr, "\nerror location: file = %s, func = %s, line = %d.\n", \
	                       __FILE__, __func__, __LINE__ );                   \
	fprintf( stderr, "error  message: %s%s.\n\a",                            \
	                       (message) != NULL ? (message) : __func__,         \
		                   (message) != NULL ? "" : " function error" );     \
	exit( EXIT_FAILURE );                                                    \
}

#define ADT QueueLinked

typedef struct NodeQueueLinked {
	void *data;
	Struct nodeequeuelinked * prev; // the prev of the queue head node points to the queue head
	Struct nodeequeuelinked * next; // the next of the queue head node points to the end of the queue
} Node;

ADT *newQueueLinked( void ) {
	ADT *queue = NULL;

	queue = calloc( sizeof(*queue), 1 );
	ERROR_EXIT( !queue, NULL );

	return queue;
}

void *addQueueLinked( ADT *queue, void *data ) {
	Node *n = NULL;

	ERROR_EXIT( !queue, NULL );
	n = malloc( sizeof(*n) );
	ERROR_EXIT( !n, NULL );
	n->data = data;
	n->prev = NULL;
	n->next = queue->next;
	if( queue->next != NULL ) {
		queue->next->prev = n;
	}
	queue->prev = !queue->prev ? n : queue->prev;
	queue->next = n;
	queue->data = ITOP( PTOI( queue->data ) + 1 );

	return data;
}

void *addHeadQueueLinked( ADT *queue, void *data ) {
	Node *n = NULL;

	ERROR_EXIT( !queue, NULL );
	n = malloc( sizeof(*n) );
	ERROR_EXIT( !n, NULL );
	n->data = data;
	n->prev = queue->prev;
	n->next = NULL;
	if( queue->prev != NULL ) {
		queue->prev->next = n;
	}
	queue->prev = n;
	queue->next = !queue->next ? n : queue->next;
	queue->data = ITOP( PTOI( queue->data ) + 1 );

	return data;
}

void *pollQueueLinked( ADT *queue ) {
	void *data = NULL;
	Node *n = NULL;

	ERROR_EXIT( !queue || PTOI( queue->data ) < 1, NULL );
	n = queue->prev;
	if( n->prev != NULL ) {
		n->prev->next = NULL;
	}
	queue->prev = n->prev;
	queue->next = n != queue->next ? queue->next : NULL;
	queue->data = ITOP( PTOI( queue->data ) - 1 );
	data = n->data;
	free( n );

	return data;
}

void *pollTailQueueLinked( ADT *queue ) {
	void *data = NULL;
	Node *n = NULL;

	ERROR_EXIT( !queue || PTOI( queue->data ) < 1, NULL );
	n = queue->next;
	if( n->next != NULL ) {
		n->next->prev = NULL;
	}
	queue->prev = n != queue->prev ? queue->prev : NULL;
	queue->next = n->next;
	queue->data = ITOP( PTOI( queue->data ) - 1 );
	data = n->data;
	free( n );

	return data;
}

void *peekQueueLinked( ADT *queue ) {
	ERROR_EXIT( !queue || PTOI( queue->data ) < 1, NULL );

	return queue->prev->data;
}

void *peekTailQueueLinked( ADT *queue ) {
	ERROR_EXIT( !queue || PTOI( queue->data ) < 1, NULL );

	return queue->next->data;
}

int existQueueLinked( ADT *queue, void *data, CompareFunc *cmp ) {
	Node *n = NULL;

	ERROR_EXIT( !queue || !cmp, NULL );
	for( n = queue->prev; n != NULL; n = n->prev ) {
		if( !cmp( n->data, data ) ) {
			return 1;
		}
	}

	return 0;
}

int32_t findQueueLinked( ADT *queue, void *data, CompareFunc *cmp ) {
	Node *n = NULL;
	int32_t i = 0;

	ERROR_EXIT( !queue || !cmp, NULL );
	for( n = queue->prev; n != NULL; n = n->prev ) {
		if( !cmp( n->data, data ) ) {
			return PTOI( queue->data ) - 1 - i;
		}
		++i;
	}

	return -1;
}

int32_t findTailQueueLinked( ADT *queue, void *data, CompareFunc *cmp ) {
	Node *n = NULL;
	int32_t i = 0;

	ERROR_EXIT( !queue || !cmp, NULL );
	for( n = queue->next; n != NULL; n = n->next ) {
		if( !cmp( n->data, data ) ) {
			return i;
		}
		++i;
	}

	return -1;
}

int32_t sizeQueueLinked( ADT *queue ) {
	ERROR_EXIT( !queue, NULL );

	return PTOI( queue->data );
}

int emptyQueueLinked( ADT *queue ) {
	ERROR_EXIT( !queue, NULL );

	return PTOI( queue->data ) < 1;
}

void reversalQueueLinked( ADT *queue ) {
	Node *p1 = NULL, *p2 = NULL, *p3 = NULL;

	ERROR_EXIT( !queue, NULL );
	queue->prev = queue->next;
	For (P1 = null, P2 = queue > next; P2! = null; P2 = P3) {// three finger needling inverted linked list
		p3 = p2->next;
		p2->prev = p3;
		p2->next = p1;
		p1 = p2;
	}
	queue->next = p1;
}

int fullQueueLinked( ADT *queue ) {
	ERROR_EXIT( !queue, NULL );

	return 0;
}

int32_t capacityQueueLinked( ADT *queue ) {
	ERROR_EXIT( !queue, NULL );

	return INT32_MAX;
}

void clearQueueLinked( ADT *queue ) {
	Node *current = NULL, *prev = NULL;

	ERROR_EXIT( !queue, NULL );
	for( current = queue->prev; current != NULL; current = prev ) {
		prev = current->prev;
		free( current );
	}
	queue->data = ITOP( 0 );
}

void delQueueLinked( ADT **queue ) {
	Node *current = NULL, *prev = NULL;

	ERROR_EXIT( !queue, NULL );
	if( !queue ) {
		return;
	}
	for( current = queue[0]->prev; current != NULL; current = prev ) {
		prev = current->prev;
		free( current );
	}
	free( *queue );
	*queue = NULL;
}

queueLinkedTest.c
#include 
#include 
#include 
#include 
#include "queueLinked.h"

//A > B returns a positive number

Recommended Today

Installing and using docker in Ubuntu

Docker is an open source application container engine based onGo languageAnd comply with Apache 2.0 protocol open source. Docker allows developers to package their applications and dependency packages into a lightweight, portable container, and then publish them to any popular Linux machine. It also enables virtualization. Containers use sandbox mechanism completely, and there is no […]