c - Linked List - how to tail insert without moving head node -


so running problem. know is. can't figure out way solve allowed do..

first here tail insert function

status append(my_queue queue, int item) {     node_ptr temp;     head_ptr head = (head_ptr) queue;      //create new node      temp = (node_ptr)malloc(sizeof(node));     if (temp == null) {         printf("malloc failed\n");         return failure;     }      temp->data = item;     temp->next = null;     if (head->head == null){         head->head = temp;     }     else{         while(head->head->next) {             head->head = head->head->next;         }         head->head->next = temp;     }     return success; } 

as see. simple. if head node null. adds new node head. if not. keeps moving head until reaches null , adds node. thats problem. moving head node pointer not supposed do. cant seem think of way it. since passing in my_queue. include header files , declarations understand these are.

struct node {     int data;     node_ptr next;  };  struct head_node; typedef struct head_node head_node; typedef head_node *head_ptr;  struct head_node {     struct my_queue_public methods;     node_ptr head; };  void destroy(my_queue queue); status append(my_queue queue, int item); status service(my_queue queue); int* front(my_queue queue); bool empty(my_queue stack);  void init_functions(my_queue queue) {     //queue->destroy = destroy;     queue->empty = empty;     queue->service = service ;     queue->append = append;     queue->front = front; } my_queue my_queue_init_default(void) {     head_ptr head;     head = malloc(sizeof(head_node));      if (head != null)     {         head->head = null;         init_functions((my_queue)head);     }     return (my_queue)head; } 

the insert tail function append function. can't change parameters or return. have change whats inside function.

my_queue public version of struct node.

here header file

#include "status.h" struct my_queue_public; typedef struct my_queue_public* my_queue;  struct my_queue_public {     void(*destroy)(my_queue* phmy_queue);     status(*service)(my_queue hmy_queue);     status(*append)(my_queue hmy_queue, int item);     bool(*empty)(my_queue hmy_queue);     int* (*front)(my_queue hmy_queue); };  my_queue my_queue_init_default(void); 

by way queue. add @ end. items front. sum head moving , lose nodes. know how avoid way know change pass in. instead of my_queue. i'd pass my_queue*. there way have

destroy function

    void destroy(my_queue queue) {      head_ptr head = (head_ptr)queue;      node_ptr tmp;      if (head->head == null) {          return;     }       while (head->head !=null){         tmp = head->head;         head->head = head->head->next;         free(tmp);     }      head->head = null; } 

thats problem. moving head node pointer not supposed do. cant seem think of way it.

you can use temporary variable tail track link, rather moving head node,

    node_ptr tail;     if (head->head == null){         head->head = temp;     }     else{         //while(head->head->next) {         //  head->head = head->head->next;         //  }         //head->head->next = temp;         tail = head->head;         while(tail->next) {             tail = tail->next;         }         tail->next = temp;     } 

for destroy func,

//void destroy(my_queue queue); void destroy(my_queque *p_queue);//change                                   //to keep consistent in struct my_queue_public   void destroy(my_queue *p_queue)  {     head_ptr head;     node_ptr tmp;     if(p_queue == null){      return;    }     if((head = (head_ptr)*p_queue) == null){      return;    }         if (head->head == null) {      return;    }     while (head->head !=null){       tmp = head->head;       head->head = head->head->next;       free(tmp);    }     free(head);   }  

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -