Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void que_insert(QueueADT queue, void *data) {
- // create node w/ data
- Node* n;
- n = malloc(sizeof(n));
- n->next = NULL;
- n->previous = NULL;
- n->data = data;
- // if compare function doesn't exist, place in front of queue
- if(queue->cmp == NULL) {
- n->next = queue->front;
- queue->front->previous = n;
- queue->front = n;
- return;
- }
- // if empty queue
- if(queue->length == 0) {
- // assign front and back to given node
- printf(": first insert \n");
- queue->front = n;
- queue->back = n;
- } else {
- Node* curr = queue->front;
- int first = 1;
- while(1) {
- // if < first node in queue, reassign head
- if(first && queue->cmp(data, curr->data) < 0) {
- printf(": first %ld %ld\n", (long int)data, (long int)curr->data);
- queue->front->previous = n;
- n->next = queue->front;
- queue->front = n;
- printf("{\n next: %ld,\n prev: NULL\n}\n\n",
- (long int)n->next->data);
- break;
- } else if(curr->next == NULL){
- // if > end reassign tail
- printf(": end\n");
- queue->back->next = n;
- n->previous = queue->back;
- queue->back = n;
- printf("{\n next: NULL,\n prev: %ld\n}\n\n",
- (long int)n->previous->data);
- break;
- } else if(queue->cmp(data, curr->next->data) < 0){
- // if middle check if next is less
- printf(": middle \n");
- n->next = curr;
- n->previous = curr->previous;
- curr->previous = n;
- printf("{\n next: %ld,\n prev: %ld\n}\n\n",
- (long int)n->next->data, (long int)n->previous->data);
- break;
- }
- first = 0;
- curr = curr->next;
- }
- printf("insert success\n");
- }
- queue->length++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement