insert - C: Enqueue() - Inserting at the end of a linked list, returning head of list -



insert - C: Enqueue() - Inserting at the end of a linked list, returning head of list -

i'm new programming. trying write function receives head of list + info inserted -- , passes new head of list. i've done lot adding element head of list, reason can't wrap head around slight difference.

#include <assert.h> #include <stdio.h> #include <stdlib.h> typedef struct node_{ int data; struct node_ *next; } queue; int main(void){ queue* queue = null; queue = enqueue(queue, 1); assert(queue->next == null); assert(queue->data == 1); queue = enqueue(queue, 2); assert(queue->data == 1); assert(queue->next != null); assert(queue->next->data == 2); free(queue->next); free(queue); homecoming 0; } queue *enqueue(queue *queue, int data){ queue *new_node, *p; new_node = malloc(sizeof(queue)); new_node->data = data; new_node->next = null; p = queue; while(p->next != null){ p = p->next; } p->next = new_node; homecoming ?????? }

i know insert head, can:

new_node->data = data; new_node->next = queue; homecoming new_node;

apologies if i've written above doesn't create much sense. i'm pretty tired , i've gone through quite few iterations. missing obvious.

just homecoming queue. have test if input queue null, in case can't access next pointer find end, in case homecoming new node.

queue *enqueue(queue *queue, int data){ queue *new_node, *p; new_node = malloc(sizeof(queue)); new_node->data = data; new_node->next = null; if (!queue) homecoming new_node; p = queue; while (p->next) p = p->next; p->next = new_node; homecoming queue; }

c insert linked-list queue

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -