Learning to implement a Linked List Queue in C++ -



Learning to implement a Linked List Queue in C++ -

i implement linked list using queue in c++. here class:

class linkedlistqueue { public: linkedlistqueue(); void enqueue(int x); void dequeue(); int peekfront(); int peekback(); private: struct node { int data; node *next; }; node *head; node *tail; }; linkedlistqueue::linkedlistqueue() { tail = 0; head = tail; } void linkedlistqueue::enqueue(int x) { struct node* newnode = (struct node*) malloc(sizeof(struct node)); newnode->data = x; newnode->next = tail; tail = newnode; if(head != null) { head = tail->next; } } void linkedlistqueue::dequeue() { struct node* newtail = tail->next; delete m_tailptr; tail = newtail; } int linkedlistqueue::peekfront() { if(tail != null) { homecoming tail->data; } else { homecoming head->data; } } int linkedlistqueue::peekback() { if(head != null) { homecoming head->data; } else { homecoming tail->data; } }

i think functions working except peekfront. maintain implementation , style same, prepare error. please help.

you don't explain error if access peekfront empty linklistqueue instance tail pointer null. dereferencing null pointer (as peekfront does) cause undefined behavior.

there may other phone call sequences involve calling peekfront null tail pointer.

edit

you shouldn't mixing malloc delete. utilize new delete (or malloc free if insist).

c++ list hyperlink queue

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -