Question about queue's below:
I am trying to insert at the head and tail of a queue and im having some trouble with the tail portion. Heres what i have: My structure: ``` typedef struct queue { int key; struct queue *prev; struct queue *after; }DEQUEUE; ``` I am passing in my head and tail from main like this, and my create function works just fine as well ``` int main(void) { DEQUEUE *head,*tail; head = NULL; tail = NULL; int x; createQueue(&head,&tail); ``` i enter 1,2,3,4 and get the output ``` TAIL -> 4 <--> 3 <--> 2 <--> 1 <--> NULL ``` but when i try to enter a key to the end of the list using this function ``` void insertAtTail(DEQUEUE **T,int x) { DEQUEUE *new,*curr; curr = *T; new = malloc(sizeof(DEQUEUE)); if(!new) { printf("Malloc failure, out of memory.\n"); exit(1); } new->key = x; if(!curr) { printf("You must create a list first, it is currently empty.\n\n"); } else { curr->prev = new; new->after = curr; curr = new; } } ``` i get the output when i attempt to add a new number to the queue at the end of the list above as follows. Say i tried to add 55 ``` TAIL -> 4 <--> 55 <--> NULL ``` im trying to teach myself this stuff and im obviously missing something so any help would be awesome!
and im wanting the output ``` TAIL -> 55 <--> 4 <--> 3 <--> 2 <--> 1 <--> NULL ```
``` #include <stdlib.h> #include <stdio.h> #include "dequeue.h" int main(void) { DEQUEUE *head, *tail; head = NULL; tail = NULL; int x; createQueue(&head,&tail); printf("Head = %d\n",head->key); printf("Tail = %d\n",tail->key); /* printf("What number do you want to insert at the head: "); scanf("%d",&x); printf("\n"); insertAtHead(&head,x); */ printf("What number do you want to insert at the tail: "); scanf("%d",&x); printf("\n"); insertAtTail(&tail,x); printQueue(head,tail); return 0; } void createQueue(DEQUEUE **H, DEQUEUE **T) { { DEQUEUE *new,*curr; do{ new = malloc(sizeof(DEQUEUE)); if(new == NULL) // check to see if malloc worked { printf("Malloc failure, memory not allocated!!\n"); exit(1); } printf("Enter a number (-1 to quit): "); // lets user enter desired number or -1 to quit scanf("%d",&new->key); new->prev = NULL; new->after = NULL; printf("\n"); if(new->key == -1) { printf("List created\n\n"); // tell user list was created } else { if(*H == NULL && *T == NULL) // if its a new list, make the new value entered the head node { *H = new; *T = new; curr = new; } else { (*T)->after = new; new->prev = (*T); (*T) = new; } } }while(new->key >0); // while the number entered wasnt negative } } void printQueue(DEQUEUE *H,DEQUEUE *T) { DEQUEUE *curr,*temp; curr = H; temp = T; if(!curr) { printf("Empty List\n"); exit(0); } else { printf("BACK <--> "); while(temp!=NULL) { printf("%d <--> ",temp->key); temp = temp->prev; } printf("FRONT\n\n"); } } void insertAtHead(DEQUEUE **H,int x) { DEQUEUE *new,*curr; curr = *H; new = malloc(sizeof(DEQUEUE)); if(!new) { printf("Malloc failure, out of memory.\n"); exit(1); } new->key = x; new->prev = NULL; new->after = NULL; if(!curr) { printf("You must have an existing list first, create one.\n\n"); } else { new->after = curr; curr->prev = new; curr = new; } } void insertAtTail(DEQUEUE **T,int x) { DEQUEUE *new,*curr; curr = *T; new = malloc(sizeof(DEQUEUE)); if(!new) { printf("Malloc failure, out of memory.\n"); exit(1); } new->key = x; if(!curr) { printf("You must create a list first, it is currently empty.\n\n"); } else { curr->prev = new; new->after = curr; curr = new; } } ``` whole program if that helps any.
@e.mccormick
Join our real-time social learning platform and learn together with your friends!